fix(gate): resolve a bead gate whose awaited bead no longer exists (#6395)

* fix(gate): resolve a bead gate whose awaited bead no longer exists (wy-dgfuiu)

A gate with await_type=bead whose awaited bead has been purged could never
resolve: GetIssue returns storage.ErrNotFound (or a wrapped sql.ErrNoRows)
against real storage, checkBeadGate returned (false, "bead gate %q: %v"), the
bead arm of evaluateGates never sets r.err, and applyGateCheckResults fell to
the "pending" arm with exit 0. The gate stayed pending forever.

Treat a missing bead as resolution, reusing the existing gateProxiedNotFound
predicate so a genuine backend or transport failure still stays pending.
close.go shares checkBeadGate and inherits the fix.

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

* fix(gate): resolve a deleted bead gate target only after a local sighting

Review fixes for #6395 (adopt-pr iterations 1 to 5).

The PR resolves a bead gate when its awaited bead no longer exists, but
the lookups it relies on turned a routing failure into not-found: the
direct getter (getIssueWithRouting) and the proxied getter both returned
the local miss when a prefix-routed rig could not be read, so an OPEN
bead in an unreachable rig resolved its gate and unblocked the step. An
await_id that never named a real bead (a typo, a short ID) also resolved
on the first check.

- A miss outside this rig's own store is errBeadGateTargetUnconfirmed: a
  matched routes.jsonl route whose rig fails or does not return the bead,
  an unreadable routes.jsonl, or the contributor auto-routed store. The
  gate stays pending and the reason names the cause. inspectBeadGate
  tests it before not-found, because the wrapped routing error can itself
  be a not-found. Only this rig's own absence counts as gone.
- bd gate check resolves a gone target only if an earlier check saw it in
  this rig. The first local sighting stamps await_seen=<await_id> in the
  gate's metadata (an OpSetMetadata merge, so other keys survive, and
  retargeting the gate invalidates it). A target that is gone and was
  never seen stays pending with "no earlier gate check saw it; check the
  await_id, or close the gate with bd gate resolve". A bead read through
  a route is never stamped, so a misdirected route cannot arm a later
  local miss. A failed stamp write is an error row, like a failed gh:run
  await_id persist.
- The proxied getter reads the target with workapi.GetIssueOrWisp (the
  issue use case alone never returns a wisp), stamps inside the apply
  transaction, and never auto-routes.
- bd close applies the same rule (evaluateBeadGate, recording no
  sighting): a gone bead satisfies the gate only if an earlier check saw
  it, so closing a gate on a typo'd await_id still needs --force.
- bd close reads the awaited bead from the store that owns the gate
  (RoutedResult.Store), as bd gate check in that rig would. Read from the
  launcher's store, a routed gate whose awaited bead only its own rig can
  reach (no route for it from the launcher) saw that open bead as missing
  and, its sighting recorded, closed without --force. The proxied-server
  route keeps the fresh-read getter (#5861).
- bd rename and bd rename-prefix (both the rename loop and --repair) point
  a bead gate at its bead's new ID, keeping a <rig>: prefix and carrying
  the sighting. UpdateIssueID rewrites text references but never
  await_id, so a seen bead's rename read as a deletion and resolved the
  gate on its next check.
- UpdateIssueID also commits on its own, so a retarget written after it
  left a window, or after a failure a lasting state, where the gate held
  a sighting of an ID that no longer existed and resolved while its bead
  was open under the new ID. All three paths now rename through
  renameIssueKeepingBeadGates, which orders the writes so no state in
  between resolves a gate: it moves await_id to the new ID while the
  sighting still names the old one (dropping, in the same write, a
  sighting that already names the new ID), renames the bead, and then
  records the sighting under the new ID. A check in between finds a
  never-seen ID and stays pending. If moving a gate or renaming the bead
  fails, the gates are pointed back at the old ID without a sighting,
  since a failed write may still have landed, and the rename fails:
  bd rename exits non-zero instead of warning from its text-reference
  loop. Losing only the final sighting write is a warning; the next
  check records the sighting again.
- A check lists the gate before it looks the bead up, so a rename landing
  in between left it holding a copy that still waited on the old ID with
  its sighting: the old ID was gone, and the check closed the gate while
  the bead was open under its new ID. Once the bead is found gone,
  evaluateBeadGate reads the gate back from its own store
  (rereadBeadGate) and resolves only if the stored gate still waits on
  the same await_id and still records the sighting. The rename moves the
  gate before the bead takes the new ID, so a read made after the miss
  sees the move. This covers bd gate check, its proxied route and
  bd close; a gate that cannot be read back, or is found only through a
  route, stays pending.
- checkBeadGate, the target lookup without the sighting rule, had no
  production caller left and read like the bead-gate rule, which is how
  bd close came to skip the sighting. It moves into the tests;
  inspectBeadGate carries the lookup contract and points callers at
  evaluateBeadGate.
- The gate check help, docs/workflows/gates.md and the CHANGELOG describe
  the rule, including that a rename does not read as a deletion to a
  check or bd close that read the gate before it, and that a rename that
  fails partway leaves the gate pending without a sighting; the #6404
  entry no longer says a routed read failure reads as not-found. The
  CHANGELOG also warns that a rename by an older bd does not move the
  gate, so once a check has seen the bead, the next check or bd close
  after that rename closes the gate while the bead is still open:
  upgrade the clients that rename beads together with the ones that
  check gates. docs/cli-reference is pinned to v1.2.2, so it is
  unchanged (generate-cli-docs.sh --check passes).

Tests: TestCheckBeadGate_UnconfirmedMissStaysPending,
TestEvaluateGates_BeadGateSightingRule, TestEvaluateBeadGate_*,
TestBeadGateSeenUpdate_KeepsOtherMetadata, TestBeadGateTargetSeen,
TestProxiedFreshReadGetter_LocalMissFollowsRoutes,
TestRoutedBeadGateGetter_ReportsWhereTheAnswerCameFrom,
TestCheckGateSatisfaction_BeadGateNeedsASighting,
TestCloseDirectPreflight_BeadGateReadsTheGatesOwnStore,
TestCloseBeadGateGetter_RouteSelection, TestBeadGateRetargetUpdate,
TestPlanBeadGateMoves, TestRenameIssueKeepingBeadGates and
TestRenameIssueKeepingBeadGates_FailedWritesResolveNoGate, which fails
each write of a rename in turn, landed or not, and evaluates every gate
after each write, both as stored and as a concurrent check listed it
before the rename (unit); the unreadable-rig step of
TestCheckBeadGateCrossRigPrefixRoute,
TestRepairPrefixesRetargetsBeadGates (gates renamed before and after
their bead) and TestRenameFailureLeavesBeadGatesPending (bd rename,
--repair and rename-prefix with a failing UpdateIssueID, landed or not)
(cgo);
TestEmbeddedGateCreate/check_bead_gate_resolves_only_a_missing_target_it_saw
and /rename_keeps_a_bead_gate_on_its_target (embedded);
TestProxiedServerGateCheck/bead_gate_resolves_when_seen_target_is_deleted
and /bead_gate_never_seen_target_stays_pending (proxied). Reordering the
unconfirmed case after not-found, dropping the sighting condition,
swallowing the routed error, stamping routed sightings, or matching any
stamp regardless of await_id each fails these tests, as do closing with
the sighting-blind lookup, reading the launcher's store on close,
dropping the carried sighting or the rig prefix on a retarget, dropping
the retarget from bd rename, the rename-prefix loop or --repair,
renaming the bead before moving its gates, keeping the sighting when
moving a gate back, keeping a stale sighting of the new ID, skipping
the final sighting write, resolving a gone bead without reading the gate
back or from a gate read back that moved, lost its sighting or was found
only through a route, or reporting an unconfirmed read-back as an error
or an unreadable gate as pending.

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 Fable 5.1 <noreply@anthropic.com>
Co-authored-by: Eddie the Engineer <adopt-pr@gascity.com>
This commit is contained in:
Bee 2026-10-08 17:10:33 +00:00 • committed by GitHub
commit a6fcf915c0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 1779 additions and 108 deletions

View file

@ -1491,9 +1491,35 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
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 missing bead still stays pending, and for now so does a bead in
a prefix-routed rig whose store cannot be read: routing still reports that
failure as not-found.
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 <id> 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

View file

@ -676,8 +676,10 @@ func isMachineCheckableGate(issue *types.Issue) bool {
}
// checkGateSatisfaction checks whether a gate issue's condition is satisfied.
// gateStore is the store that owns the gate (nil on the proxied-server route);
// see closeBeadGateGetter.
// Returns nil if the gate is satisfied (or not a machine-checkable gate), or an error describing why it cannot be closed.
func checkGateSatisfaction(issue *types.Issue) error {
func checkGateSatisfaction(issue *types.Issue, gateStore storage.DoltStorage) error {
if !isMachineCheckableGate(issue) {
return nil
}
@ -695,7 +697,9 @@ func checkGateSatisfaction(issue *types.Issue) error {
case issue.AwaitType == "timer":
resolved, escalated, reason, err = checkTimer(issue, time.Now())
case issue.AwaitType == "bead":
resolved, reason, err = checkBeadGate(rootCtx, closeBeadGateGetter(), issue.AwaitID)
// bd gate check's rule, without recording a sighting: a missing
// bead no check ever saw does not satisfy the gate.
resolved, reason, err = evaluateBeadGate(rootCtx, issue, closeBeadGateGetter(gateStore), nil)
if err != nil {
// Unlike the gh:* and timer arms above, a bead gate whose store
// cannot be read stays closed: the close would need that same
@ -726,17 +730,16 @@ func checkGateSatisfaction(issue *types.Issue) error {
}
// closeBeadGateGetter picks the bead-gate lookup the current route can serve.
// The proxied-server route never opens the local store (the root pre-run
// short-circuits to uowProvider), so a store-backed getter there refused every
// bead gate with "no local store available" even when the awaited bead was
// closed (#5861); that route now reads through the same fresh-read getter
// `bd gate check` uses. The direct and embedded routes keep the store-backed
// local -> prefix-route lookup.
func closeBeadGateGetter() issueGetter {
// The direct and embedded routes look the awaited bead up from gateStore, the
// store that owns the gate, as bd gate check in that rig would: for a gate
// reached through a route, the launcher's store can report an open bead as
// missing. The proxied-server route never opens a local store, so it reads
// through the same fresh-read getter bd gate check uses there (#5861).
func closeBeadGateGetter(gateStore storage.DoltStorage) issueGetter {
if usesProxiedServer() {
return proxiedFreshReadGetter{}
}
return routedBeadGateGetter{localStore: store}
return routedBeadGateGetter{localStore: gateStore}
}
// autoCloseCompletedMolecule checks if closing a step completed an auto-closing

View file

@ -50,7 +50,7 @@ type closeDirectPlan struct {
func closeDirectPreflight(results []*RoutedResult, resolvedIDs, reasons []string, force bool) closeDirectPlan {
plan := closeDirectPlan{refusals: make([]string, len(resolvedIDs))}
for i, id := range resolvedIDs {
if refusal := closeDirectCheckOne(id, results[i].Issue, force); refusal != "" {
if refusal := closeDirectCheckOne(id, results[i].Issue, results[i].Store, force); refusal != "" {
plan.refusals[i] = refusal
continue
}
@ -65,8 +65,8 @@ func closeDirectPreflight(results []*RoutedResult, resolvedIDs, reasons []string
}
// closeDirectCheckOne returns one argument's refusal, or "" when it may go to
// the batch.
func closeDirectCheckOne(id string, issue *types.Issue, force bool) string {
// the batch. st is the store that owns the issue.
func closeDirectCheckOne(id string, issue *types.Issue, st storage.DoltStorage, force bool) string {
// Close validation guards a state change; a row already at literal
// StatusClosed has none to guard, so skip it and let the re-close reach the
// engine as the idempotent no-op it has always been (ga-ktn9pe.4.8).
@ -88,7 +88,7 @@ func closeDirectCheckOne(id string, issue *types.Issue, force bool) string {
// Gate satisfaction for machine-checkable gates (GH#1467).
if !force {
if err := checkGateSatisfaction(issue); err != nil {
if err := checkGateSatisfaction(issue, st); err != nil {
return fmt.Sprintf("cannot close %s: %s", id, err)
}
}

View file

@ -2,6 +2,7 @@ package main
import (
"context"
"encoding/json"
"errors"
"strings"
"testing"
@ -124,7 +125,7 @@ func TestCheckGateSatisfaction_NonGateIssues(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := checkGateSatisfaction(tt.issue)
err := checkGateSatisfaction(tt.issue, nil)
if err != nil {
t.Errorf("checkGateSatisfaction() returned error for non-machine-checkable issue: %v", err)
}
@ -141,7 +142,7 @@ func TestCheckGateSatisfaction_GHPRWithoutAwaitID(t *testing.T) {
Title: "PR gate without ID",
}
err := checkGateSatisfaction(issue)
err := checkGateSatisfaction(issue, nil)
if err == nil {
t.Error("checkGateSatisfaction() should return error for gh:pr gate without await_id")
}
@ -159,7 +160,7 @@ func TestCheckGateSatisfaction_GHRunWithoutAwaitID(t *testing.T) {
Title: "Run gate without ID",
}
err := checkGateSatisfaction(issue)
err := checkGateSatisfaction(issue, nil)
if err == nil {
t.Error("checkGateSatisfaction() should return error for gh:run gate without await_id")
}
@ -177,7 +178,7 @@ func TestCheckGateSatisfaction_BeadGateInvalidFormat(t *testing.T) {
Title: "Bead gate with bad format",
}
err := checkGateSatisfaction(issue)
err := checkGateSatisfaction(issue, nil)
if err == nil {
t.Error("checkGateSatisfaction() should return error for bead gate with invalid await_id format")
}
@ -192,7 +193,7 @@ func TestCheckGateSatisfaction_ErrorMessageFormat(t *testing.T) {
Title: "Test gate",
}
err := checkGateSatisfaction(issue)
err := checkGateSatisfaction(issue, nil)
if err == nil {
t.Fatal("expected error")
}
@ -221,9 +222,6 @@ func TestCheckGateSatisfaction_BeadGateUnreadableStoreRefuses(t *testing.T) {
// A bead gate whose store read fails (anything but not-found) must refuse
// the close with the read error, not fall through to the fail-open
// warning the gh:* and timer arms use.
saveAndRestoreGlobals(t)
store = &unreadableGateStore{err: errors.New("dolt exploded")}
issue := &types.Issue{
IssueType: "gate",
AwaitType: "bead",
@ -231,7 +229,7 @@ func TestCheckGateSatisfaction_BeadGateUnreadableStoreRefuses(t *testing.T) {
Title: "Bead gate on an unreadable store",
}
err := checkGateSatisfaction(issue)
err := checkGateSatisfaction(issue, &unreadableGateStore{err: errors.New("dolt exploded")})
if err == nil {
t.Fatal("checkGateSatisfaction() let a bead gate close although its store could not be read")
}
@ -242,20 +240,113 @@ func TestCheckGateSatisfaction_BeadGateUnreadableStoreRefuses(t *testing.T) {
}
}
func TestCheckGateSatisfaction_BeadGateNeedsASighting(t *testing.T) {
// The close pre-check applies bd gate check's rule: an awaited bead the
// gate's store does not have satisfies the gate only if an earlier check
// saw it there, so a gate on a typo'd await_id cannot be closed past. Nor
// can a gate that a rename moved on after bd close read it.
withBeadGateTown(t)
gateStore := &beadGateLocalStore{issues: map[string]*types.Issue{}}
for _, awaitID := range []string{"bd-typo123", "other:ot-typo123"} {
gate := &types.Issue{ID: "bd-gate", IssueType: "gate", AwaitType: "bead", AwaitID: awaitID}
gateStore.issues[gate.ID] = gate
err := checkGateSatisfaction(gate, gateStore)
if err == nil {
t.Fatalf("%s: checkGateSatisfaction() let a gate close on a bead no check ever saw", awaitID)
}
for _, want := range []string{"no earlier gate check saw it", "--force"} {
if !strings.Contains(err.Error(), want) {
t.Errorf("%s: error should mention %q, got: %v", awaitID, want, err)
}
}
gate.Metadata = json.RawMessage(`{"await_seen":"` + awaitID + `"}`)
if err := checkGateSatisfaction(gate, gateStore); err != nil {
t.Errorf("%s: a seen bead that is gone should satisfy the gate, got: %v", awaitID, err)
}
moved := *gate
moved.AwaitID = awaitID + "-renamed"
gateStore.issues[gate.ID] = &moved
err = checkGateSatisfaction(gate, gateStore)
if err == nil || !strings.Contains(err.Error(), "the gate changed") {
t.Errorf("%s: a gate moved on since it was read should not be closed past, got: %v", awaitID, err)
}
}
}
func TestCloseDirectPreflight_BeadGateReadsTheGatesOwnStore(t *testing.T) {
// A gate reached through a route is checked against the store that owns
// it, as bd gate check in its rig would. The launcher's store does not
// have the awaited bead: checking there would read the open bead as
// deleted and, the gate's sighting being recorded, let the close through.
saveAndRestoreGlobals(t)
withBeadGateTown(t)
store = &beadGateLocalStore{issues: map[string]*types.Issue{}}
for _, tt := range []struct {
name string
target types.Status
wantRefusal string
}{
{name: "awaited bead open in the gate's rig", target: types.StatusOpen, wantRefusal: "bead rg-target is open"},
{name: "awaited bead closed in the gate's rig", target: types.StatusClosed},
} {
t.Run(tt.name, func(t *testing.T) {
gate := &types.Issue{
ID: "rg-gate",
Status: types.StatusOpen,
IssueType: "gate",
AwaitType: "bead",
AwaitID: "rg-target",
Metadata: json.RawMessage(`{"await_seen":"rg-target"}`),
}
gateStore := &beadGateLocalStore{issues: map[string]*types.Issue{
"rg-target": {ID: "rg-target", Status: tt.target},
}}
results := []*RoutedResult{{Issue: gate, Store: gateStore, Routed: true, ResolvedID: gate.ID}}
plan := closeDirectPreflight(results, []string{gate.ID}, []string{"done"}, false)
if tt.wantRefusal == "" {
if plan.refusals[0] != "" || len(plan.items) != 1 {
t.Fatalf("close refused (%q), want it to go to the batch", plan.refusals[0])
}
if plan.items[0].store != gateStore {
t.Error("the close does not go to the gate's own store")
}
return
}
if len(plan.items) != 0 {
t.Fatalf("the gate went to the batch, want a refusal mentioning %q", tt.wantRefusal)
}
if !strings.Contains(plan.refusals[0], tt.wantRefusal) {
t.Errorf("refusal %q does not mention %q", plan.refusals[0], tt.wantRefusal)
}
})
}
}
// The close pre-check reads a bead gate's target through the route that can
// serve it: the proxied route has no local store, so it must not build a
// store-backed getter there (#5861).
// store-backed getter there (#5861), and the direct route reads from the
// store that owns the gate.
func TestCloseBeadGateGetter_RouteSelection(t *testing.T) {
oldMode := proxiedServerMode
t.Cleanup(func() { proxiedServerMode = oldMode })
gateStore := &beadGateLocalStore{}
proxiedServerMode = true
if _, ok := closeBeadGateGetter().(proxiedFreshReadGetter); !ok {
t.Errorf("proxied-server mode: got %T, want proxiedFreshReadGetter", closeBeadGateGetter())
if _, ok := closeBeadGateGetter(gateStore).(proxiedFreshReadGetter); !ok {
t.Errorf("proxied-server mode: got %T, want proxiedFreshReadGetter", closeBeadGateGetter(gateStore))
}
proxiedServerMode = false
if _, ok := closeBeadGateGetter().(routedBeadGateGetter); !ok {
t.Errorf("direct mode: got %T, want routedBeadGateGetter", closeBeadGateGetter())
getter, ok := closeBeadGateGetter(gateStore).(routedBeadGateGetter)
if !ok {
t.Fatalf("direct mode: got %T, want routedBeadGateGetter", closeBeadGateGetter(gateStore))
}
if getter.localStore != gateStore {
t.Error("direct mode: the getter does not read the gate's own store")
}
}

View file

@ -314,7 +314,7 @@ func closeProxiedCheckOne(ctx context.Context, uw uow.UnitOfWork, id string, in
// unprefixed, so both routes now spell one refusal one way.
if !in.force {
if err := checkGateSatisfaction(current); err != nil {
if err := checkGateSatisfaction(current, nil); err != nil {
return fmt.Sprintf("cannot close %s: %s", id, err), nil
}
}

View file

@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
@ -13,6 +14,7 @@ import (
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/metrics"
"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/ui"
)
@ -604,7 +606,7 @@ Gate types:
gh:run - Check GitHub Actions workflow runs
gh:pr - Check pull request merge status
timer - Check timer gates (auto-expire based on timeout)
bead - Check cross-rig bead gates
bead - Check bead gates
all - Check all gate types
GitHub gates use the 'gh' CLI to query status:
@ -615,7 +617,8 @@ A gate is resolved when:
- gh:run: status=completed AND conclusion=success
- gh:pr: state=MERGED
- timer: current time > created_at + timeout
- bead: target bead status=closed
- bead: target bead status=closed, or a bead an earlier check saw in
this rig no longer exists
A gate is escalated when:
- gh:run: status=completed AND conclusion in (failure, canceled)
@ -626,7 +629,7 @@ Examples:
bd gate check --type=gh # Check only GitHub gates
bd gate check --type=gh:run # Check only workflow run gates
bd gate check --type=timer # Check only timer gates
bd gate check --type=bead # Check only cross-rig bead gates
bd gate check --type=bead # Check only bead gates
bd gate check --dry-run # Show what would happen without changes
bd gate check --escalate # Escalate expired/failed gates`,
SilenceUsage: true,
@ -670,13 +673,21 @@ Examples:
}
var persistAwaitID func(gateID, runID string) error
var recordSeen func(gate *types.Issue) error
if !dryRun {
persistAwaitID = func(gateID, runID string) error {
return updateGateAwaitIDFunc(nil, gateID, runID)
}
recordSeen = func(gate *types.Issue) error {
if err := store.UpdateIssue(ctx, gate.ID, beadGateSeenUpdate(gate.AwaitID), actor); err != nil {
return err
}
commandDidWrite.Store(true)
return nil
}
}
results := evaluateGates(ctx, filteredGates, time.Now(), routedBeadGateGetter{localStore: store}, persistAwaitID)
results := evaluateGates(ctx, filteredGates, time.Now(), routedBeadGateGetter{localStore: store}, persistAwaitID, recordSeen)
resolvedCount, escalatedCount, errorCount := applyGateCheckResults(
results, dryRun, escalateFlag,
@ -715,7 +726,7 @@ func printNoOpenGates(typeFilter string) {
}
}
func evaluateGates(ctx context.Context, gates []*types.Issue, now time.Time, getter issueGetter, persistAwaitID func(gateID, runID string) error) []gateCheckResult {
func evaluateGates(ctx context.Context, gates []*types.Issue, now time.Time, getter issueGetter, persistAwaitID func(gateID, runID string) error, recordSeen func(gate *types.Issue) error) []gateCheckResult {
results := make([]gateCheckResult, 0, len(gates))
for _, gate := range gates {
r := gateCheckResult{gate: gate}
@ -727,7 +738,7 @@ func evaluateGates(ctx context.Context, gates []*types.Issue, now time.Time, get
case gate.AwaitType == "timer":
r.resolved, r.escalated, r.reason, r.err = checkTimer(gate, now)
case gate.AwaitType == "bead":
r.resolved, r.reason, r.err = checkBeadGate(ctx, getter, gate.AwaitID)
r.resolved, r.reason, r.err = evaluateBeadGate(ctx, gate, getter, recordSeen)
default:
continue
}
@ -1248,71 +1259,318 @@ func checkTimer(gate *types.Issue, now time.Time) (resolved, escalated bool, rea
return false, false, fmt.Sprintf("expires in %s", remaining), nil
}
// issueGetter is the one storage method checkBeadGate needs, split out so
// issueGetter is the one storage method inspectBeadGate needs, split out so
// tests can fake the lookup without standing up a Dolt store.
type issueGetter interface {
GetIssue(ctx context.Context, id string) (*types.Issue, error)
}
// beadGateTargetGetter is implemented by getters that also report where the
// awaited bead was looked up. local is true when the answer came from this
// rig's own store; a bead read through a prefix or contributor route is not
// local. Only a local sighting is recorded on the gate (see
// evaluateBeadGate), so a misdirected route can never let a later local miss
// resolve the gate. A getter without this method is treated as local.
type beadGateTargetGetter interface {
getBeadGateTarget(ctx context.Context, id string) (issue *types.Issue, local bool, err error)
}
// errBeadGateTargetUnconfirmed marks a miss that happened somewhere other
// than this rig's own store: a route matched but its store could not be read,
// or the routed store did not return the bead. Neither proves the bead is
// gone, so the gate stays pending instead of resolving.
var errBeadGateTargetUnconfirmed = errors.New("cannot confirm the awaited bead is gone")
// beadGateNotFound reports whether a lookup failed because the bead does not
// exist, as opposed to the read itself failing.
func beadGateNotFound(err error) bool {
return gateProxiedNotFound(err) || isNotFoundErr(err)
}
// routedBeadGateGetter gives direct-mode gate checks the same local -> prefix
// route -> contributor fallback used by other read commands. Routed stores are
// opened read-only and closed before the result is returned.
// opened read-only and closed before the result is returned. Unlike
// getIssueWithRouting, a route that fails or misses is reported as
// errBeadGateTargetUnconfirmed instead of as the local not-found.
type routedBeadGateGetter struct {
localStore storage.DoltStorage
}
func (g routedBeadGateGetter) GetIssue(ctx context.Context, id string) (*types.Issue, error) {
issue, _, err := g.getBeadGateTarget(ctx, id)
return issue, err
}
func (g routedBeadGateGetter) getBeadGateTarget(ctx context.Context, id string) (*types.Issue, bool, error) {
if g.localStore == nil {
return nil, fmt.Errorf("no local store available")
return nil, false, fmt.Errorf("no local store available")
}
issue, err := g.localStore.GetIssue(ctx, id)
if err == nil && issue != nil {
return issue, true, nil
}
if err != nil && !beadGateNotFound(err) {
return nil, false, err
}
routed, routeErr := prefixRoutedBeadGateTarget(ctx, id)
if routed == nil {
var autoErr error
routed, autoErr = autoRoutedBeadGateTarget(ctx, g.localStore, id)
if routeErr == nil {
routeErr = autoErr
}
}
if routed != nil {
return routed, false, nil
}
if routeErr != nil {
return nil, false, routeErr
}
return issue, true, err
}
// prefixRoutedBeadGateTarget looks id up through routes.jsonl. It returns
// (nil, nil) when no route sends id to another rig, so this rig's own answer
// stands, and errBeadGateTargetUnconfirmed when routes.jsonl cannot be read or
// a matched route's rig fails or does not return the bead.
func prefixRoutedBeadGateTarget(ctx context.Context, id string) (*types.Issue, error) {
beadsDir := resolveCommandBeadsDir(dbPath)
if beadsDir == "" {
return nil, nil
}
routes, err := loadPrefixRoutes(beadsDir)
if errors.Is(err, os.ErrNotExist) {
return nil, nil
}
result, err := getIssueWithRouting(ctx, g.localStore, id)
if err != nil {
return nil, err
return nil, fmt.Errorf("%w: reading routes.jsonl: %w", errBeadGateTargetUnconfirmed, err)
}
route := matchPrefixRoute(routes, id)
if route == nil || route.Path == "." {
return nil, nil
}
result, err := resolveViaPrefixRouting(ctx, id)
if err != nil {
return nil, fmt.Errorf("%w: bead %s routes to %s: %w", errBeadGateTargetUnconfirmed, id, route.Path, err)
}
defer result.Close()
if result.Issue == nil {
return nil, fmt.Errorf("%w: bead %s routes to %s, which did not return it", errBeadGateTargetUnconfirmed, id, route.Path)
}
return result.Issue, nil
}
// checkBeadGate checks if a bead gate is satisfied.
// Returns (satisfied, reason, err). A non-nil err means the awaited bead could
// not be read at all (backend or transport failure): the gate is neither
// satisfied nor pending, and the caller reports it as an error rather than
// letting a dead store read as "still waiting". A missing bead is not an
// error; it stays pending with a not-found reason.
//
// A plain await_id names a bead in this rig. The historical
// <rig>:<bead-id> form uses the bead ID as the routed lookup key; the rig
// component is retained for compatibility while routes.jsonl remains keyed by
// bead prefix. The supplied getter owns local-versus-routed lookup policy.
func checkBeadGate(ctx context.Context, st issueGetter, awaitID string) (bool, string, error) {
if awaitID == "" {
return false, "bead gate has no await_id", nil
}
targetID := awaitID
if strings.Contains(awaitID, ":") {
parts := strings.SplitN(awaitID, ":", 2)
if parts[0] == "" || parts[1] == "" {
return false, fmt.Sprintf("invalid cross-rig bead gate %q: expected <rig>:<bead-id>", awaitID), nil
}
targetID = parts[1]
}
if st == nil {
return false, fmt.Sprintf("bead gate %q: no local store available", awaitID), nil
}
issue, err := st.GetIssue(ctx, targetID)
// autoRoutedBeadGateTarget looks id up in the contributor auto-routed store.
// It returns (nil, nil) when no auto-route is configured; like a prefix
// route, a store that cannot be opened or does not return the bead leaves its
// absence unconfirmed.
func autoRoutedBeadGateTarget(ctx context.Context, localStore storage.DoltStorage, id string) (*types.Issue, error) {
routedStore, routed, _, err := openRoutedReadStore(ctx, localStore)
if err != nil {
if gateProxiedNotFound(err) || isNotFoundErr(err) {
return false, fmt.Sprintf("bead gate %q: bead not found", awaitID), nil
return nil, fmt.Errorf("%w: %w", errBeadGateTargetUnconfirmed, err)
}
if !routed {
return nil, nil
}
defer func() { _ = routedStore.Close() }()
result, err := resolveAndGetFromStore(ctx, routedStore, id, true)
if err != nil {
return nil, fmt.Errorf("%w: looking up bead %s in the auto-routed store: %w", errBeadGateTargetUnconfirmed, id, err)
}
if result.Issue == nil {
return nil, fmt.Errorf("%w: the auto-routed store did not return bead %s", errBeadGateTargetUnconfirmed, id)
}
return result.Issue, nil
}
// lookupBeadGateTarget asks st for id and reports whether the answer came
// from this rig's own store.
func lookupBeadGateTarget(ctx context.Context, st issueGetter, id string) (*types.Issue, bool, error) {
if g, ok := st.(beadGateTargetGetter); ok {
return g.getBeadGateTarget(ctx, id)
}
issue, err := st.GetIssue(ctx, id)
return issue, true, err
}
// beadGateCheck is the outcome of one bead gate lookup. gone means this rig's
// own store answered that the awaited bead does not exist; seenHere means it
// returned the bead, not yet closed.
type beadGateCheck struct {
resolved bool
reason string
targetID string
gone bool
seenHere bool
}
// beadGateTargetID returns the bead ID named by a bead gate's await_id. A
// plain await_id names a bead in this rig. The historical <rig>:<bead-id>
// form uses the bead ID as the routed lookup key; the rig component is
// retained for compatibility while routes.jsonl remains keyed by bead prefix.
// When there is nothing to look up, it returns the reason the gate stays
// pending instead.
func beadGateTargetID(awaitID string) (targetID, pendingReason string) {
if awaitID == "" {
return "", "bead gate has no await_id"
}
if !strings.Contains(awaitID, ":") {
return awaitID, ""
}
parts := strings.SplitN(awaitID, ":", 2)
if parts[0] == "" || parts[1] == "" {
return "", fmt.Sprintf("invalid cross-rig bead gate %q: expected <rig>:<bead-id>", awaitID)
}
return parts[1], ""
}
// inspectBeadGate looks up a bead gate's target. A non-nil err means the
// awaited bead could not be read at all (backend or transport failure): the
// gate is neither resolved nor pending, and the caller reports it as an error
// rather than letting a dead store read as "still waiting". A bead that this
// rig's own store reports missing is not an error either: it is reported gone,
// and resolved with a "no longer exists" reason. A miss anywhere else (see
// errBeadGateTargetUnconfirmed) keeps the gate pending.
//
// The supplied getter owns local-versus-routed lookup policy. The lookup alone
// is not the bead-gate rule: a gone bead resolves the gate only after an
// earlier sighting, so decide through evaluateBeadGate.
func inspectBeadGate(ctx context.Context, st issueGetter, awaitID string) (beadGateCheck, error) {
targetID, pendingReason := beadGateTargetID(awaitID)
if pendingReason != "" {
return beadGateCheck{reason: pendingReason}, nil
}
c := beadGateCheck{targetID: targetID}
if st == nil {
c.reason = fmt.Sprintf("bead gate %q: no local store available", awaitID)
return c, nil
}
issue, local, err := lookupBeadGateTarget(ctx, st, targetID)
switch {
case errors.Is(err, errBeadGateTargetUnconfirmed):
// Checked before the not-found test below, because the routing error
// it wraps can itself be a not-found from the routed store.
c.reason = fmt.Sprintf("bead gate %q: %v", awaitID, err)
case err != nil && !beadGateNotFound(err):
return c, fmt.Errorf("bead gate %q: %w", awaitID, err)
case err != nil || issue == nil:
// A bead that no longer exists can never close, so a gate awaiting it
// would stay pending forever. Resolve it.
c.resolved, c.gone = true, true
c.reason = fmt.Sprintf("awaited bead %s no longer exists (treated as resolved)", targetID)
case issue.Status == types.StatusClosed:
c.resolved = true
c.reason = fmt.Sprintf("bead %s closed", targetID)
default:
c.seenHere = local
c.reason = fmt.Sprintf("bead %s is %s", targetID, issue.Status)
}
return c, nil
}
// beadGateSeenKey is the gate metadata key that records a bd gate check
// seeing the awaited bead in this rig's own store. Its value is the await_id
// that was seen, so retargeting the gate invalidates the record.
const beadGateSeenKey = "await_seen"
// evaluateBeadGate is the bd gate check rule for a bead gate. It adds one
// condition to inspectBeadGate: an awaited bead that does not exist resolves
// the gate only if an earlier check saw it here and the stored gate still
// records that sighting (see rereadBeadGate). An await_id that never named a
// real bead (a typo, a short ID, a rig without a route) keeps the gate pending
// with a diagnostic instead of unblocking its step. getter reads the gate's own
// store. recordSeen, when non-nil, records the first sighting of the bead on
// the gate.
func evaluateBeadGate(ctx context.Context, gate *types.Issue, getter issueGetter, recordSeen func(gate *types.Issue) error) (bool, string, error) {
c, err := inspectBeadGate(ctx, getter, gate.AwaitID)
if err != nil {
return false, "", err
}
seen := beadGateTargetSeen(gate)
if c.gone && !seen {
return false, fmt.Sprintf("awaited bead %s not found, and no earlier gate check saw it; check the await_id, or close the gate with bd gate resolve", c.targetID), nil
}
if c.gone {
if pendingReason, err := rereadBeadGate(ctx, getter, gate, c.targetID); err != nil || pendingReason != "" {
return false, pendingReason, err
}
return false, "", fmt.Errorf("bead gate %q: %w", awaitID, err)
}
if issue == nil {
return false, fmt.Sprintf("bead gate %q: bead not found", awaitID), nil
if c.seenHere && !seen && recordSeen != nil {
if err := recordSeen(gate); err != nil {
return false, "", fmt.Errorf("recording that bead %s exists: %w", c.targetID, err)
}
}
if issue.Status == types.StatusClosed {
return true, fmt.Sprintf("bead %s closed", targetID), nil
return c.resolved, c.reason, nil
}
// rereadBeadGate reads gate back from getter once its awaited bead, targetID,
// was found gone, and returns a pending reason unless the stored gate still
// waits on the same await_id and records its sighting. The caller's copy can
// predate a rename of the bead: bd rename points the gate at the new ID
// before the bead takes it (see renameIssueKeepingBeadGates), so a check that
// listed the gate before the rename and looks the bead up after it finds the
// old ID gone on a copy that still records the sighting. This read comes after
// that miss, so it sees the gate already moved. An error means the gate could
// not be read at all.
func rereadBeadGate(ctx context.Context, getter issueGetter, gate *types.Issue, targetID string) (string, error) {
stored, local, err := lookupBeadGateTarget(ctx, getter, gate.ID)
switch {
case err != nil && !errors.Is(err, errBeadGateTargetUnconfirmed) && !beadGateNotFound(err):
return "", fmt.Errorf("reading gate %s back: %w", gate.ID, err)
case err != nil || stored == nil || !local:
return fmt.Sprintf("awaited bead %s not found, and the gate could not be read back to confirm it still waits on it; check it again", targetID), nil
case stored.AwaitID != gate.AwaitID || !beadGateTargetSeen(stored):
return fmt.Sprintf("awaited bead %s not found, but the gate changed while it was being checked; check it again", targetID), nil
}
return false, fmt.Sprintf("bead %s is %s", targetID, issue.Status), nil
return "", nil
}
// beadGateTargetSeen reports whether gate metadata records a sighting of the
// gate's current await_id. Unreadable metadata counts as no sighting.
func beadGateTargetSeen(gate *types.Issue) bool {
return gate != nil && gate.AwaitID != "" && beadGateSeenID(gate) == gate.AwaitID
}
// beadGateSeenID returns the await_id that gate metadata records a sighting
// of, or "" when it records none. Unreadable metadata counts as none.
func beadGateSeenID(gate *types.Issue) string {
if len(gate.Metadata) == 0 {
return ""
}
var meta map[string]json.RawMessage
if err := json.Unmarshal(gate.Metadata, &meta); err != nil {
return ""
}
var seen string
if err := json.Unmarshal(meta[beadGateSeenKey], &seen); err != nil {
return ""
}
return seen
}
// beadGateSeenUpdate is the update that records a sighting of awaitID. It is
// a metadata merge operation, resolved against the row inside the write
// transaction, so other metadata keys on the gate are preserved.
func beadGateSeenUpdate(awaitID string) map[string]interface{} {
value, _ := json.Marshal(awaitID)
return map[string]interface{}{
storageissueops.OpSetMetadata: map[string]json.RawMessage{beadGateSeenKey: value},
}
}
// beadGateRetargetUpdate is the update that points a bead gate at awaitID.
// With dropSeen it also removes the gate's sighting in the same write, for a
// rename step that cannot keep it (see renameIssueKeepingBeadGates).
func beadGateRetargetUpdate(awaitID string, dropSeen bool) map[string]interface{} {
update := map[string]interface{}{"await_id": awaitID}
if dropSeen {
update[storageissueops.OpUnsetMetadata] = []string{beadGateSeenKey}
}
return update
}
// closeGate closes a gate issue with the given reason

View file

@ -127,6 +127,64 @@ func TestProxiedServerGateCheck(t *testing.T) {
}
})
t.Run("bead_gate_resolves_when_seen_target_is_deleted", func(t *testing.T) {
t.Parallel()
p := newSharedProxiedProject(t, bd, "gcg")
target := bdProxiedCreate(t, bd, p.dir, "Deleted bead gate target")
gate := bdProxiedCreate(t, bd, p.dir, "Bead gate", "--type", "gate")
db := openProxiedDB(t, p)
seedGateAwait(t, db, gate.ID, "bead", target.ID, time.Now().UTC(), 0)
// The first check sees the open target and records that on the gate.
if _, stderr, err := bdProxiedRunBuffers(t, bd, p.dir, "gate", "check", "--type", "bead"); err != nil {
t.Fatalf("gate check (target open) failed: %v\nstderr:\n%s", err, stderr)
}
if got := readStatus(t, db, gate.ID); got == types.StatusClosed {
t.Fatal("bead gate should stay pending while target is open")
}
var metadata string
if err := db.QueryRowContext(context.Background(),
"SELECT metadata FROM issues WHERE id = ?", gate.ID).Scan(&metadata); err != nil {
t.Fatalf("read gate metadata: %v", err)
}
if !strings.Contains(metadata, beadGateSeenKey) {
t.Fatalf("gate metadata %s does not record that the target was seen", metadata)
}
if _, stderr, err := bdProxiedRunBuffers(t, bd, p.dir, "delete", target.ID, "--force"); err != nil {
t.Fatalf("delete target failed: %v\nstderr:\n%s", err, stderr)
}
out, stderr, err := bdProxiedRunBuffers(t, bd, p.dir, "gate", "check", "--type", "bead")
if err != nil {
t.Fatalf("gate check (target deleted) failed: %v\nstderr:\n%s", err, stderr)
}
if got := readStatus(t, db, gate.ID); got != types.StatusClosed {
t.Errorf("bead gate should resolve after its seen target is deleted, got %q", got)
}
if !strings.Contains(out, "1 resolved") || !strings.Contains(out, "no longer exists") {
t.Errorf("expected 1 resolved with a 'no longer exists' reason, got:\n%s", out)
}
})
t.Run("bead_gate_never_seen_target_stays_pending", func(t *testing.T) {
t.Parallel()
p := newSharedProxiedProject(t, bd, "gcu")
gate := bdProxiedCreate(t, bd, p.dir, "Bead gate on a mistyped target", "--type", "gate")
db := openProxiedDB(t, p)
seedGateAwait(t, db, gate.ID, "bead", "gcu-nosuchbead", time.Now().UTC(), 0)
out, stderr, err := bdProxiedRunBuffers(t, bd, p.dir, "gate", "check", "--type", "bead")
if err != nil {
t.Fatalf("gate check failed: %v\nstderr:\n%s", err, stderr)
}
if got := readStatus(t, db, gate.ID); got == types.StatusClosed {
t.Error("a bead gate whose target no check ever saw must stay pending")
}
if !strings.Contains(out, "no earlier gate check saw it") {
t.Errorf("expected the never-seen diagnostic, got:\n%s", out)
}
})
t.Run("type_filter_scopes_evaluation", func(t *testing.T) {
t.Parallel()
p := newSharedProxiedProject(t, bd, "gcf")

View file

@ -72,6 +72,31 @@ func createGate(t *testing.T, bd, dir, title string, extraArgs ...string) *types
return bdCreate(t, bd, dir, args...)
}
// createBeadGate creates a bead gate awaiting awaitID on a new task, and
// returns the gate.
func createBeadGate(t *testing.T, bd, dir, awaitID string) *types.Issue {
t.Helper()
task := bdCreate(t, bd, dir, "Task waiting on "+awaitID, "--type", "task")
cmd := exec.Command(bd, "gate", "create", "--blocks", task.ID,
"--type", "bead", "--await-id", awaitID, "--json")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd gate create --type=bead failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
var gate types.Issue
s := strings.TrimSpace(stdout.String())
start := strings.Index(s, "{")
if start < 0 {
t.Fatalf("no JSON in output: %s", s)
}
if err := json.Unmarshal([]byte(s[start:]), &gate); err != nil {
t.Fatalf("parse gate JSON: %v\n%s", err, s)
}
return &gate
}
func TestEmbeddedGate(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")
@ -628,6 +653,113 @@ func TestEmbeddedGateCreate(t *testing.T) {
t.Errorf("expected gate blocking %s in gate list", task.ID)
}
})
t.Run("check_bead_gate_resolves_only_a_missing_target_it_saw", func(t *testing.T) {
// Use a fresh db so the check sees only these bead gates
freshDir, freshBeads, _ := bdInit(t, bd, "--prefix", "gb")
fs := openStore(t, freshBeads, "gb")
if err := fs.SetConfig(t.Context(), "types.custom", `["gate"]`); err != nil {
t.Fatalf("SetConfig: %v", err)
}
fs.Close()
target := bdCreate(t, bd, freshDir, "Awaited bead", "--type", "task")
seenGate := createBeadGate(t, bd, freshDir, target.ID)
neverSeenGate := createBeadGate(t, bd, freshDir, "gb-nosuchbead")
// The first check finds the target open and records that on its gate.
out := bdGate(t, bd, freshDir, "check", "--type", "bead")
if !strings.Contains(out, "no earlier gate check saw it") {
t.Errorf("expected a diagnostic for the never-seen await_id: %s", out)
}
if got := bdShow(t, bd, freshDir, seenGate.ID); got.Status != types.StatusOpen || !strings.Contains(string(got.Metadata), beadGateSeenKey) {
t.Fatalf("gate on an open target: status=%s metadata=%s, want open with %s", got.Status, got.Metadata, beadGateSeenKey)
}
// A later check resolves the gate whose target it saw and that is now
// deleted; the await_id no check ever saw stays pending.
bdDelete(t, bd, freshDir, target.ID, "--force")
out = bdGate(t, bd, freshDir, "check", "--type", "bead")
if !strings.Contains(out, "1 resolved") || !strings.Contains(out, "no longer exists") {
t.Errorf("expected the deleted target's gate to resolve: %s", out)
}
if got := bdShow(t, bd, freshDir, seenGate.ID); got.Status != types.StatusClosed {
t.Errorf("gate on a deleted target: status=%s, want closed", got.Status)
}
if got := bdShow(t, bd, freshDir, neverSeenGate.ID); got.Status != types.StatusOpen {
t.Errorf("gate on a never-seen await_id: status=%s, want open", got.Status)
}
})
t.Run("rename_keeps_a_bead_gate_on_its_target", func(t *testing.T) {
// Renaming the awaited bead points its gate at the new ID and moves
// the sighting along, so the next check finds the bead instead of
// resolving the gate as though the bead had been deleted.
for _, tt := range []struct {
name string
prefix string
rename func(t *testing.T, dir, targetID string) (newID func(id string) string)
}{
{
name: "bd rename",
prefix: "gn",
rename: func(t *testing.T, dir, targetID string) func(string) string {
bdCommand(t, bd, dir, "rename", targetID, "gn-renamed")
return func(id string) string {
if id == targetID {
return "gn-renamed"
}
return id
}
},
},
{
name: "bd rename-prefix",
prefix: "gp",
rename: func(t *testing.T, dir, _ string) func(string) string {
bdRenamePrefix(t, bd, dir, "gq")
return func(id string) string { return strings.Replace(id, "gp-", "gq-", 1) }
},
},
} {
t.Run(tt.name, func(t *testing.T) {
freshDir, freshBeads, _ := bdInit(t, bd, "--prefix", tt.prefix)
fs := openStore(t, freshBeads, tt.prefix)
if err := fs.SetConfig(t.Context(), "types.custom", `["gate"]`); err != nil {
t.Fatalf("SetConfig: %v", err)
}
fs.Close()
target := bdCreate(t, bd, freshDir, "Awaited bead", "--type", "task")
gate := createBeadGate(t, bd, freshDir, target.ID)
bdGate(t, bd, freshDir, "check", "--type", "bead")
if got := bdShow(t, bd, freshDir, gate.ID); !beadGateTargetSeen(got) {
t.Fatalf("the first check did not record the open target: metadata=%s", got.Metadata)
}
newID := tt.rename(t, freshDir, target.ID)
gateID, targetID := newID(gate.ID), newID(target.ID)
got := bdShow(t, bd, freshDir, gateID)
if got.AwaitID != targetID || !beadGateTargetSeen(got) {
t.Fatalf("gate after the rename: await_id=%q metadata=%s, want %q recorded as seen", got.AwaitID, got.Metadata, targetID)
}
out := bdGate(t, bd, freshDir, "check", "--type", "bead")
if got := bdShow(t, bd, freshDir, gateID); got.Status != types.StatusOpen {
t.Fatalf("gate on a renamed open bead: status=%s, want open\n%s", got.Status, out)
}
bdClose(t, bd, freshDir, targetID)
out = bdGate(t, bd, freshDir, "check", "--type", "bead")
if got := bdShow(t, bd, freshDir, gateID); got.Status != types.StatusClosed {
t.Errorf("gate on a renamed bead that closed: status=%s, want closed\n%s", got.Status, out)
}
if !strings.Contains(out, "bead "+targetID+" closed") {
t.Errorf("expected the gate to resolve on the closed bead: %s", out)
}
})
}
})
}
// TestEmbeddedGateConcurrent exercises gate operations concurrently.

View file

@ -30,30 +30,42 @@ type gateCheckApply struct {
updated []*types.Issue
closeErrs map[string]error
awaitErrs map[string]error
seenErrs map[string]error
}
// proxiedFreshReadGetter reads bead gate targets in proxied-server mode, each
// lookup in a fresh read transaction. A target can be an issue or a wisp. A
// local miss falls back to prefix routing like the direct getter; proxied
// mode has no contributor auto-routing.
type proxiedFreshReadGetter struct{}
func (proxiedFreshReadGetter) GetIssue(ctx context.Context, id string) (*types.Issue, error) {
func (g proxiedFreshReadGetter) GetIssue(ctx context.Context, id string) (*types.Issue, error) {
issue, _, err := g.getBeadGateTarget(ctx, id)
return issue, err
}
func (proxiedFreshReadGetter) getBeadGateTarget(ctx context.Context, id string) (*types.Issue, bool, error) {
uw, err := proxiedOpenReadUOW(ctx)
if err != nil {
return nil, err
return nil, false, err
}
issue, localErr := uw.IssueUseCase().GetIssue(ctx, id)
issue, _, localErr := workapi.GetIssueOrWisp(ctx, workapi.NewUOWDetailSource(uw), id)
uw.Close(ctx)
if localErr == nil {
return issue, nil
return issue, true, nil
}
if !gateProxiedNotFound(localErr) {
return nil, localErr
return nil, false, localErr
}
result, routeErr := resolveViaPrefixRouting(ctx, id)
routed, routeErr := prefixRoutedBeadGateTarget(ctx, id)
if routeErr != nil {
return nil, localErr
return nil, false, routeErr
}
defer result.Close()
return result.Issue, nil
if routed != nil {
return routed, false, nil
}
return nil, true, localErr
}
func runGateCheckProxiedServer(cmd *cobra.Command, ctx context.Context) error {
@ -83,12 +95,18 @@ func runGateCheckProxiedServer(cmd *cobra.Command, ctx context.Context) error {
}
discovered := map[string]string{}
var seen []*types.Issue
var persistAwaitID func(gateID, runID string) error
var recordSeen func(gate *types.Issue) error
if !dryRun {
persistAwaitID = func(gateID, runID string) error {
discovered[gateID] = runID
return nil
}
recordSeen = func(gate *types.Issue) error {
seen = append(seen, gate)
return nil
}
}
readUW, err := proxiedOpenReadUOW(ctx)
@ -107,7 +125,7 @@ func runGateCheckProxiedServer(cmd *cobra.Command, ctx context.Context) error {
printNoOpenGates(gateTypeFilter)
return nil
}
results := evaluateGates(ctx, filteredGates, time.Now(), proxiedFreshReadGetter{}, persistAwaitID)
results := evaluateGates(ctx, filteredGates, time.Now(), proxiedFreshReadGetter{}, persistAwaitID, recordSeen)
if dryRun {
resolved, escalated, errCount := applyGateCheckResults(results, true, escalateFlag, nil)
@ -118,6 +136,7 @@ func runGateCheckProxiedServer(cmd *cobra.Command, ctx context.Context) error {
out := gateCheckApply{
closeErrs: map[string]error{},
awaitErrs: map[string]error{},
seenErrs: map[string]error{},
}
for gateID, runID := range discovered {
@ -130,6 +149,14 @@ func runGateCheckProxiedServer(cmd *cobra.Command, ctx context.Context) error {
}
}
for _, gate := range seen {
if err := recordBeadGateSeenProxied(ctx, uw, gate); err != nil {
out.seenErrs[gate.ID] = err
continue
}
out.updated = append(out.updated, gate)
}
for _, r := range results {
if r.err != nil || !r.resolved {
continue
@ -177,6 +204,11 @@ func runGateCheckProxiedServer(cmd *cobra.Command, ctx context.Context) error {
results[i].escalated = false
results[i].err = awaitErr
}
if seenErr, failed := applied.seenErrs[results[i].gate.ID]; failed {
results[i].resolved = false
results[i].escalated = false
results[i].err = seenErr
}
}
resolved, escalated, errCount := applyGateCheckResults(results, false, escalateFlag,
@ -186,6 +218,21 @@ func runGateCheckProxiedServer(cmd *cobra.Command, ctx context.Context) error {
return printGateCheckSummary(len(results), resolved, escalated, errCount, dryRun)
}
// recordBeadGateSeenProxied is the proxied-server form of the direct route's
// recordSeen write: it stamps beadGateSeenKey on a bead gate, which may be an
// issue or a wisp.
func recordBeadGateSeenProxied(ctx context.Context, uw uow.UnitOfWork, gate *types.Issue) error {
_, isWisp, err := workapi.GetIssueOrWisp(ctx, workapi.NewUOWDetailSource(uw), gate.ID)
if err == nil {
err = proxiedUpdateByID(ctx, uw, gate.ID, isWisp, beadGateSeenUpdate(gate.AwaitID))
}
if err != nil {
targetID, _ := beadGateTargetID(gate.AwaitID)
return fmt.Errorf("recording that bead %s exists: %w", targetID, err)
}
return nil
}
// gateProxiedNotFound reports whether an issue lookup failed because the row
// does not exist, as opposed to the read itself failing. The distinction is
// what keeps `bd gate show` fail-closed: "no gate" and "could not read" must

View file

@ -14,7 +14,8 @@ import (
// TestCheckBeadGateCrossRigPrefixRoute proves the full evaluator seam: the
// historical rig:id value is reduced to the target bead ID, the current
// routes.jsonl prefix router opens the foreign store read-only, and target
// lifecycle state determines whether the gate resolves.
// lifecycle state determines whether the gate resolves. A routed rig that
// cannot be read leaves the gate pending: its bead is not proven gone.
//
// NOTE: This test uses os.Chdir and cannot run in parallel with other tests.
func TestCheckBeadGateCrossRigPrefixRoute(t *testing.T) {
@ -74,4 +75,19 @@ func TestCheckBeadGateCrossRigPrefixRoute(t *testing.T) {
if !gateTestContainsIgnoreCase(reason, "open") {
t.Fatalf("pending reason %q does not report target status", reason)
}
// Point the rig at a database that does not exist: the route still
// matches, but the rig's store cannot be opened, so its open bead must not
// read as deleted.
writeTestMetadata(t, filepath.Join(rigBeadsDir, "dolt"), "gate_routing_missing_db")
resolved, reason, err = checkBeadGate(ctx, getter, "rig:gt-open")
if err != nil {
t.Fatalf("an unreadable routed rig is a pending gate, not an error: %v", err)
}
if resolved {
t.Fatalf("open target in an unreadable routed rig resolved the gate: %s", reason)
}
if !gateTestContainsIgnoreCase(reason, "cannot confirm") {
t.Fatalf("pending reason %q does not say the absence is unconfirmed", reason)
}
}

View file

@ -17,6 +17,7 @@ import (
"time"
"github.com/steveyegge/beads/internal/storage"
storageissueops "github.com/steveyegge/beads/internal/storage/issueops"
"github.com/steveyegge/beads/internal/types"
)
@ -170,6 +171,13 @@ func (f *fakeBeadGateGetter) GetIssue(_ context.Context, id string) (*types.Issu
return f.issues[id], nil
}
// checkBeadGate runs only the target lookup of a bead gate check. It skips
// the sighting rule evaluateBeadGate adds, so a gone bead reads as resolved.
func checkBeadGate(ctx context.Context, st issueGetter, awaitID string) (bool, string, error) {
c, err := inspectBeadGate(ctx, st, awaitID)
return c.resolved, c.reason, err
}
func TestCheckBeadGate_CrossRigUsesBeadIDForRoutedLookup(t *testing.T) {
ctx := context.Background()
st := &fakeBeadGateGetter{issues: map[string]*types.Issue{
@ -192,10 +200,17 @@ func TestProxiedFreshReadGetterPreservesLocalNotFoundWhenRoutingUnavailable(t *t
dbPath = filepath.Join(t.TempDir(), ".beads", "dolt")
t.Cleanup(func() { dbPath = oldDBPath })
_, err := (proxiedFreshReadGetter{}).GetIssue(context.Background(), "bd-missing")
if !errors.Is(err, sql.ErrNoRows) {
_, err := (proxiedFreshReadGetter{}).GetIssue(context.Background(), stubMissingID)
if !errors.Is(err, storage.ErrNotFound) {
t.Fatalf("GetIssue error = %v, want original local not-found", err)
}
if errors.Is(err, errBeadGateTargetUnconfirmed) {
t.Fatalf("GetIssue error = %v: with no route the local miss is authoritative, not unconfirmed", err)
}
issue, local, _ := (proxiedFreshReadGetter{}).getBeadGateTarget(context.Background(), stubMissingID)
if issue != nil || !local {
t.Fatalf("getBeadGateTarget = (%v, local %v), want (nil, local true)", issue, local)
}
}
func TestCheckBeadGate_InvalidCrossRigFormat(t *testing.T) {
@ -260,17 +275,25 @@ func TestCheckBeadGate_LocalBead(t *testing.T) {
}
}
// A gate awaiting a bead that no longer exists can never resolve on its own:
// the bead will never close. Treat the absence as resolution rather than
// leaving the gate pending forever.
func TestCheckBeadGate_LocalBeadNotFound(t *testing.T) {
st := &fakeBeadGateGetter{issues: map[string]*types.Issue{}}
satisfied, reason, _ := checkBeadGate(context.Background(), st, "bd-missing")
if satisfied {
t.Error("expected not satisfied for missing local bead")
satisfied, reason, err := checkBeadGate(context.Background(), st, "bd-missing")
if err != nil {
t.Fatalf("a missing bead is not an error, got %v", err)
}
if !gateTestContainsIgnoreCase(reason, "not found") {
t.Errorf("reason %q does not mention not found", reason)
if !satisfied {
t.Errorf("expected satisfied for missing local bead, got reason %q", reason)
}
if !gateTestContainsIgnoreCase(reason, "no longer exists") {
t.Errorf("reason %q does not mention that the bead no longer exists", reason)
}
}
// Negative control: a genuine backend failure is not a missing bead, so it
// is an error, neither pending nor resolved.
func TestCheckBeadGate_LocalBeadLookupError(t *testing.T) {
// A store that cannot be read is an error, not a pending gate: the
// caller must be able to tell "dolt is down" from "still waiting".
@ -294,10 +317,11 @@ func TestCheckBeadGate_LocalBeadLookupError(t *testing.T) {
}
}
func TestCheckBeadGate_LocalBeadNotFoundErrorStaysPending(t *testing.T) {
// A getter that reports absence through an error (the routed getter
// surfaces storage.ErrNotFound; the proxied fresh-read getter surfaces
// sql.ErrNoRows) is a missing bead, not a broken store.
func TestCheckBeadGate_LocalBeadNotFoundErrorResolves(t *testing.T) {
// A getter that reports absence through an error (storage.ErrNotFound
// from a store, sql.ErrNoRows from the proxied domain seam, or the
// partial-ID resolver's text) is a missing bead, not a broken store: the
// gate resolves rather than pending forever on a bead that can never close.
for _, tt := range []struct {
name string
err error
@ -305,19 +329,20 @@ func TestCheckBeadGate_LocalBeadNotFoundErrorStaysPending(t *testing.T) {
{name: "storage sentinel", err: storage.ErrNotFound},
{name: "wrapped storage sentinel", err: fmt.Errorf("lookup bd-missing: %w", storage.ErrNotFound)},
{name: "sql no rows", err: sql.ErrNoRows},
{name: "wrapped sql no rows", err: fmt.Errorf("query bead: %w", sql.ErrNoRows)},
{name: "partial-id resolver text", err: errors.New("no issue found matching bd-missing")},
} {
t.Run(tt.name, func(t *testing.T) {
st := &fakeBeadGateGetter{err: tt.err}
satisfied, reason, err := checkBeadGate(context.Background(), st, "bd-missing")
if err != nil {
t.Fatalf("not-found must stay pending, got error %v", err)
t.Fatalf("not-found is a missing bead, not an error, got %v", err)
}
if satisfied {
t.Error("expected not satisfied for a missing bead")
if !satisfied {
t.Errorf("expected satisfied for a missing bead, got reason %q", reason)
}
if !gateTestContainsIgnoreCase(reason, "not found") {
t.Errorf("reason %q does not mention not found", reason)
if !gateTestContainsIgnoreCase(reason, "no longer exists") {
t.Errorf("reason %q does not mention that the bead no longer exists", reason)
}
})
}
@ -329,7 +354,7 @@ func TestEvaluateGates_BeadStoreErrorCountsAsError(t *testing.T) {
gate := &types.Issue{ID: "bd-gate", IssueType: "gate", AwaitType: "bead", AwaitID: "bd-abc"}
st := &fakeBeadGateGetter{err: errors.New("dolt exploded")}
results := evaluateGates(context.Background(), []*types.Issue{gate}, time.Now(), st, nil)
results := evaluateGates(context.Background(), []*types.Issue{gate}, time.Now(), st, nil, nil)
if len(results) != 1 {
t.Fatalf("results = %d, want 1", len(results))
}
@ -356,6 +381,716 @@ func TestEvaluateGates_BeadStoreErrorCountsAsError(t *testing.T) {
}
}
func TestCheckBeadGate_UnconfirmedMissStaysPending(t *testing.T) {
// A miss outside this rig's own store (a route whose rig cannot be read,
// or a routed store that did not return the bead) does not prove the bead
// is gone. The gate stays pending with the cause, even when the wrapped
// error is itself a not-found, and it is not an error row either.
for _, tt := range []struct {
name string
err error
}{
{name: "routed store not-found", err: fmt.Errorf("%w: bead gt-open routes to gastown: %w", errBeadGateTargetUnconfirmed, fmt.Errorf("get gt-open: %w", storage.ErrNotFound))},
{name: "routed partial-id miss", err: fmt.Errorf("%w: bead gt-open routes to gastown: %w", errBeadGateTargetUnconfirmed, errors.New("no issue found matching gt-open"))},
{name: "routed store cannot be opened", err: fmt.Errorf("%w: bead gt-open routes to gastown: %w", errBeadGateTargetUnconfirmed, errors.New("target rig has no dolt_database configured"))},
} {
t.Run(tt.name, func(t *testing.T) {
st := &fakeBeadGateGetter{err: tt.err}
satisfied, reason, err := checkBeadGate(context.Background(), st, "gastown:gt-open")
if err != nil {
t.Fatalf("an unconfirmed miss is a pending gate, not an error, got %v", err)
}
if satisfied {
t.Fatalf("an unconfirmed miss resolved the gate: %s", reason)
}
if !strings.Contains(reason, "cannot confirm") {
t.Errorf("reason %q does not say the absence is unconfirmed", reason)
}
})
}
}
// fakeRoutedBeadGateGetter answers like a getter that reached the bead
// through a route: the answer did not come from this rig's own store.
type fakeRoutedBeadGateGetter struct{ fakeBeadGateGetter }
func (f *fakeRoutedBeadGateGetter) getBeadGateTarget(ctx context.Context, id string) (*types.Issue, bool, error) {
issue, err := f.GetIssue(ctx, id)
return issue, false, err
}
func TestEvaluateGates_BeadGateSightingRule(t *testing.T) {
// bd gate check resolves a gate whose awaited bead is gone only when an
// earlier check saw that bead in this rig, so an await_id that never
// named a real bead stays pending instead of unblocking its step.
const seen = `{"await_seen":"bd-target"}`
open := map[string]*types.Issue{"bd-target": {ID: "bd-target", Status: types.StatusOpen}}
closed := map[string]*types.Issue{"bd-target": {ID: "bd-target", Status: types.StatusClosed}}
// The getter is the gate's own store, which a check reads the gate back
// from once the bead is gone: there the gate records its sighting.
missing := map[string]*types.Issue{"bd-gate": {ID: "bd-gate", IssueType: "gate", AwaitType: "bead", AwaitID: "bd-target", Metadata: json.RawMessage(seen)}}
unconfirmed := fmt.Errorf("%w: bead bd-target routes to rig: %w", errBeadGateTargetUnconfirmed, storage.ErrNotFound)
for _, tt := range []struct {
name string
getter issueGetter
metadata string
wantResolved bool
wantReason string
wantRecords int
}{
{name: "missing and never seen", getter: &fakeBeadGateGetter{issues: missing}, wantReason: "no earlier gate check saw it"},
{name: "missing after a sighting", getter: &fakeBeadGateGetter{issues: missing}, metadata: seen, wantResolved: true, wantReason: "no longer exists"},
{name: "missing, sighting of another await_id", getter: &fakeBeadGateGetter{issues: missing}, metadata: `{"await_seen":"bd-other"}`, wantReason: "no earlier gate check saw it"},
{name: "open, first sighting", getter: &fakeBeadGateGetter{issues: open}, wantReason: "is open", wantRecords: 1},
{name: "open, already seen", getter: &fakeBeadGateGetter{issues: open}, metadata: seen, wantReason: "is open"},
{name: "open in another rig", getter: &fakeRoutedBeadGateGetter{fakeBeadGateGetter{issues: open}}, wantReason: "is open"},
{name: "closed", getter: &fakeBeadGateGetter{issues: closed}, wantResolved: true, wantReason: "closed"},
{name: "unconfirmed miss after a sighting", getter: &fakeBeadGateGetter{err: unconfirmed}, metadata: seen, wantReason: "cannot confirm"},
} {
t.Run(tt.name, func(t *testing.T) {
gate := &types.Issue{ID: "bd-gate", IssueType: "gate", AwaitType: "bead", AwaitID: "bd-target"}
if tt.metadata != "" {
gate.Metadata = json.RawMessage(tt.metadata)
}
var recorded []*types.Issue
recordSeen := func(g *types.Issue) error {
recorded = append(recorded, g)
return nil
}
results := evaluateGates(context.Background(), []*types.Issue{gate}, time.Now(), tt.getter, nil, recordSeen)
if len(results) != 1 {
t.Fatalf("results = %d, want 1", len(results))
}
r := results[0]
if r.err != nil {
t.Fatalf("err = %v, want a pending or resolved gate", r.err)
}
if r.resolved != tt.wantResolved {
t.Errorf("resolved = %v, want %v (reason %q)", r.resolved, tt.wantResolved, r.reason)
}
if !strings.Contains(r.reason, tt.wantReason) {
t.Errorf("reason %q does not contain %q", r.reason, tt.wantReason)
}
if len(recorded) != tt.wantRecords {
t.Fatalf("recordSeen called %d times, want %d", len(recorded), tt.wantRecords)
}
if tt.wantRecords == 1 && recorded[0] != gate {
t.Errorf("recordSeen got %v, want the checked gate", recorded[0])
}
})
}
}
// gateReadBackGetter is a gate's own store in which the awaited bead is gone.
// Reading the gate back returns gate, as found through a route when routed,
// or fails with err.
type gateReadBackGetter struct {
gate *types.Issue
routed bool
err error
}
func (g gateReadBackGetter) GetIssue(ctx context.Context, id string) (*types.Issue, error) {
issue, _, err := g.getBeadGateTarget(ctx, id)
return issue, err
}
func (g gateReadBackGetter) getBeadGateTarget(_ context.Context, id string) (*types.Issue, bool, error) {
switch {
case id != "bd-gate":
return nil, true, fmt.Errorf("get %s: %w", id, storage.ErrNotFound)
case g.err != nil:
return nil, false, g.err
case g.gate == nil:
return nil, true, fmt.Errorf("get %s: %w", id, storage.ErrNotFound)
}
return g.gate, !g.routed, nil
}
func TestEvaluateBeadGate_GoneBeadReadsTheGateBack(t *testing.T) {
// The gate a check holds can be older than a rename of its bead, which
// moves the stored gate to the new ID before the old one goes. Once the
// bead is gone, the gate resolves only if the stored gate still waits on
// it with its sighting.
gateWith := func(awaitID, metadata string) *types.Issue {
g := &types.Issue{ID: "bd-gate", IssueType: "gate", AwaitType: "bead", AwaitID: awaitID}
if metadata != "" {
g.Metadata = json.RawMessage(metadata)
}
return g
}
listed := gateWith("bd-target", `{"await_seen":"bd-target"}`)
boom := errors.New("dolt exploded")
for _, tt := range []struct {
name string
getter gateReadBackGetter
wantResolved bool
wantReason string
wantErr error
}{
{name: "stored as listed", getter: gateReadBackGetter{gate: gateWith("bd-target", `{"await_seen":"bd-target"}`)}, wantResolved: true, wantReason: "no longer exists"},
{name: "moved by a rename", getter: gateReadBackGetter{gate: gateWith("bd-renamed", `{"await_seen":"bd-renamed"}`)}, wantReason: "the gate changed while it was being checked"},
{name: "moved back without its sighting", getter: gateReadBackGetter{gate: gateWith("bd-target", "")}, wantReason: "the gate changed while it was being checked"},
{name: "gate gone", getter: gateReadBackGetter{}, wantReason: "the gate could not be read back"},
{name: "gate found only through a route", getter: gateReadBackGetter{gate: listed, routed: true}, wantReason: "the gate could not be read back"},
{name: "gate behind an unconfirmed route", getter: gateReadBackGetter{err: fmt.Errorf("%w: bead bd-gate routes to rig: %w", errBeadGateTargetUnconfirmed, boom)}, wantReason: "the gate could not be read back"},
{name: "gate cannot be read", getter: gateReadBackGetter{err: boom}, wantErr: boom},
} {
t.Run(tt.name, func(t *testing.T) {
gate := *listed
resolved, reason, err := evaluateBeadGate(context.Background(), &gate, tt.getter, nil)
if tt.wantErr != nil {
if !errors.Is(err, tt.wantErr) || resolved || reason != "" {
t.Fatalf("evaluateBeadGate = (%v, %q, %v), want only an error wrapping %v", resolved, reason, err, tt.wantErr)
}
return
}
if err != nil {
t.Fatalf("err = %v, want a pending or resolved gate", err)
}
if resolved != tt.wantResolved {
t.Errorf("resolved = %v, want %v (reason %q)", resolved, tt.wantResolved, reason)
}
if !strings.Contains(reason, tt.wantReason) {
t.Errorf("reason %q does not contain %q", reason, tt.wantReason)
}
})
}
}
func TestEvaluateBeadGate_WithoutRecorder(t *testing.T) {
// --dry-run passes no recorder: the open bead is reported and nothing is
// written.
gate := &types.Issue{ID: "bd-gate", AwaitType: "bead", AwaitID: "bd-target"}
st := &fakeBeadGateGetter{issues: map[string]*types.Issue{"bd-target": {ID: "bd-target", Status: types.StatusOpen}}}
resolved, reason, err := evaluateBeadGate(context.Background(), gate, st, nil)
if err != nil || resolved || !strings.Contains(reason, "is open") {
t.Fatalf("evaluateBeadGate = (%v, %q, %v), want a pending gate", resolved, reason, err)
}
}
func TestEvaluateBeadGate_RecordFailureIsAnError(t *testing.T) {
// A sighting that cannot be recorded would let the gate pend forever once
// the bead is deleted, so the failure is reported rather than dropped.
gate := &types.Issue{ID: "bd-gate", AwaitType: "bead", AwaitID: "bd-target"}
st := &fakeBeadGateGetter{issues: map[string]*types.Issue{"bd-target": {ID: "bd-target", Status: types.StatusOpen}}}
boom := errors.New("write refused")
resolved, reason, err := evaluateBeadGate(context.Background(), gate, st, func(*types.Issue) error { return boom })
if !errors.Is(err, boom) {
t.Fatalf("err = %v, want it to wrap the write error", err)
}
if !strings.Contains(err.Error(), "recording that bead bd-target exists") {
t.Errorf("err %q does not say what failed", err)
}
if resolved || reason != "" {
t.Errorf("evaluateBeadGate = (%v, %q), want neither resolved nor pending on an error", resolved, reason)
}
}
func TestBeadGateSeenUpdate_KeepsOtherMetadata(t *testing.T) {
old := &types.Issue{Metadata: json.RawMessage(`{"repo":"o/r"}`)}
resolved, err := storageissueops.ResolveMergeOps(old, beadGateSeenUpdate("rig:bd-x"))
if err != nil {
t.Fatalf("ResolveMergeOps: %v", err)
}
meta, ok := resolved["metadata"].(json.RawMessage)
if !ok {
t.Fatalf("resolved metadata is %T, want json.RawMessage", resolved["metadata"])
}
var got map[string]string
if err := json.Unmarshal(meta, &got); err != nil {
t.Fatalf("resolved metadata %s: %v", meta, err)
}
if got["repo"] != "o/r" || got[beadGateSeenKey] != "rig:bd-x" {
t.Errorf("metadata = %s, want repo kept and %s set to the await_id", meta, beadGateSeenKey)
}
gate := &types.Issue{AwaitID: "rig:bd-x", Metadata: meta}
if !beadGateTargetSeen(gate) {
t.Error("the recorded sighting is not read back")
}
gate.AwaitID = "rig:bd-y"
if beadGateTargetSeen(gate) {
t.Error("a sighting of the old await_id counts after the gate was retargeted")
}
}
func TestBeadGateTargetSeen(t *testing.T) {
if beadGateTargetSeen(nil) {
t.Error("a nil gate has no sighting")
}
for _, tt := range []struct {
name string
awaitID string
metadata string
want bool
}{
{name: "no metadata", awaitID: "bd-x"},
{name: "null metadata", awaitID: "bd-x", metadata: "null"},
{name: "metadata not an object", awaitID: "bd-x", metadata: `["bd-x"]`},
{name: "no sighting", awaitID: "bd-x", metadata: `{"repo":"o/r"}`},
{name: "sighting not a string", awaitID: "bd-x", metadata: `{"await_seen":true}`},
{name: "null sighting", awaitID: "bd-x", metadata: `{"await_seen":null}`},
{name: "no await_id", metadata: `{"await_seen":""}`},
{name: "sighting of this await_id", awaitID: "bd-x", metadata: `{"await_seen":"bd-x"}`, want: true},
} {
t.Run(tt.name, func(t *testing.T) {
gate := &types.Issue{AwaitID: tt.awaitID}
if tt.metadata != "" {
gate.Metadata = json.RawMessage(tt.metadata)
}
if got := beadGateTargetSeen(gate); got != tt.want {
t.Errorf("beadGateTargetSeen = %v, want %v", got, tt.want)
}
})
}
}
func TestBeadGateRetargetUpdate(t *testing.T) {
// The update points a gate at a new await_id and drops its sighting only
// when asked; the rest of the metadata is kept either way.
gate := &types.Issue{AwaitID: "bd-old", Metadata: json.RawMessage(`{"repo":"o/r","await_seen":"bd-old"}`)}
for _, tt := range []struct {
name string
dropSeen bool
wantMeta string // "" means the metadata is not written
}{
{name: "sighting kept"},
{name: "sighting dropped", dropSeen: true, wantMeta: `{"repo":"o/r"}`},
} {
t.Run(tt.name, func(t *testing.T) {
resolved, err := storageissueops.ResolveMergeOps(gate, beadGateRetargetUpdate("rig:bd-new", tt.dropSeen))
if err != nil {
t.Fatalf("ResolveMergeOps: %v", err)
}
if resolved["await_id"] != "rig:bd-new" {
t.Errorf("await_id = %v, want %q", resolved["await_id"], "rig:bd-new")
}
meta, written := resolved["metadata"]
switch {
case tt.wantMeta == "" && written:
t.Errorf("metadata = %s, want it left alone", meta)
case tt.wantMeta != "" && !written:
t.Errorf("metadata not written, want %s", tt.wantMeta)
case tt.wantMeta != "":
if got, ok := meta.(json.RawMessage); !ok || string(got) != tt.wantMeta {
t.Errorf("metadata = %s, want %s", meta, tt.wantMeta)
}
}
})
}
}
func TestPlanBeadGateMoves(t *testing.T) {
// A rename moves the bead gates that wait on the renamed bead, and only
// those. A <rig>: prefix on await_id is kept.
gate := func(id, awaitType, awaitID, metadata string) *types.Issue {
g := &types.Issue{ID: id, IssueType: types.TypeGate, AwaitType: awaitType, AwaitID: awaitID}
if metadata != "" {
g.Metadata = json.RawMessage(metadata)
}
return g
}
issues := []*types.Issue{
gate("bd-g1", "bead", "bd-old", ""),
gate("bd-g2", "bead", "rig:bd-old", `{"await_seen":"rig:bd-old"}`),
gate("bd-g3", "bead", "bd-old", `{"await_seen":"bd-new"}`),
gate("bd-g4", "bead", "bd-old.1", `{"await_seen":"bd-old.1"}`),
gate("bd-g5", "gh:run", "bd-old", ""),
gate("bd-g6", "bead", ":bd-old", ""),
{ID: "bd-task", IssueType: types.TypeTask, AwaitType: "bead", AwaitID: "bd-old"},
}
got := planBeadGateMoves(beadGatesByTarget(issues)["bd-old"], "bd-old", "bd-new")
want := []beadGateMove{
{gateID: "bd-g1", fromID: "bd-old", toID: "bd-new"},
{gateID: "bd-g2", fromID: "rig:bd-old", toID: "rig:bd-new", seen: true},
{gateID: "bd-g3", fromID: "bd-old", toID: "bd-new", staleSeen: true},
}
if !slices.Equal(got, want) {
t.Errorf("moves = %+v\nwant %+v", got, want)
}
}
// renameGateStore is an in-memory store for renameIssueKeepingBeadGates whose
// writes can fail. Writes are numbered from 1: failAt fails one write, or with
// failRest every write from it on, and with landed a failing write is applied
// before it returns its error, like a commit whose response was lost. After
// each applied write it checks that bd gate check would resolve no gate:
// every issue it holds is open, so none may resolve, whether the check reads
// the gates now or listed them before the rename began.
type renameGateStore struct {
t *testing.T
issues map[string]*types.Issue
listed []*types.Issue // a concurrent check's copies, listed before the rename
writes int
failAt int
failRest bool
landed bool
}
func newRenameGateStore(t *testing.T, issues []*types.Issue) *renameGateStore {
s := &renameGateStore{t: t, issues: make(map[string]*types.Issue, len(issues))}
for _, issue := range issues {
c := *issue
s.issues[issue.ID] = &c
}
return s
}
func (s *renameGateStore) GetIssue(_ context.Context, id string) (*types.Issue, error) {
issue, ok := s.issues[id]
if !ok {
return nil, fmt.Errorf("issue %s: %w", id, storage.ErrNotFound)
}
c := *issue
return &c, nil
}
func (s *renameGateStore) UpdateIssue(_ context.Context, id string, updates map[string]interface{}, _ string) error {
return s.write(func() error {
issue, ok := s.issues[id]
if !ok {
return fmt.Errorf("issue %s: %w", id, storage.ErrNotFound)
}
resolved, err := storageissueops.ResolveMergeOps(issue, updates)
if err != nil {
return err
}
updated := *issue
for field, value := range resolved {
switch field {
case "await_id":
updated.AwaitID, ok = value.(string)
case "metadata":
updated.Metadata, ok = value.(json.RawMessage)
default:
ok = false
}
if !ok {
return fmt.Errorf("unexpected update %s=%v", field, value)
}
}
*issue = updated
return nil
})
}
func (s *renameGateStore) UpdateIssueID(_ context.Context, oldID, newID string, issue *types.Issue, _ string) error {
return s.write(func() error {
stored, ok := s.issues[oldID]
if !ok {
return fmt.Errorf("issue %s: %w", oldID, storage.ErrNotFound)
}
// Like UpdateIssueIDInTx, write the ID and the text fields: await_id
// and metadata keep their stored values.
delete(s.issues, oldID)
stored.ID, stored.Title = newID, issue.Title
s.issues[newID] = stored
return nil
})
}
func (s *renameGateStore) write(apply func() error) error {
s.writes++
failing := s.failAt > 0 && (s.writes == s.failAt || (s.failRest && s.writes > s.failAt))
if failing && !s.landed {
return errors.New("injected write failure")
}
if err := apply(); err != nil {
return err
}
s.requireNoGateResolves()
if failing {
return errors.New("injected lost commit response")
}
return nil
}
func (s *renameGateStore) requireNoGateResolves() {
s.t.Helper()
for _, issue := range s.issues {
s.requireGateDoesNotResolve(issue, "")
}
for _, issue := range s.listed {
s.requireGateDoesNotResolve(issue, " listed before the rename")
}
}
func (s *renameGateStore) requireGateDoesNotResolve(issue *types.Issue, note string) {
s.t.Helper()
if issue.IssueType != types.TypeGate {
return
}
gate := *issue
resolved, reason, err := evaluateBeadGate(context.Background(), &gate, s, nil)
if resolved || err != nil {
s.t.Errorf("after write %d, gate %s%s (await_id %q, metadata %s) resolves: %q, err %v", s.writes, gate.ID, note, gate.AwaitID, gate.Metadata, reason, err)
}
}
// rename renames bd-old to bd-new the way bd rename does, with the gates
// listed before the first write. A concurrent bd gate check that listed them
// at the same moment holds its own copies, kept in s.listed.
func (s *renameGateStore) rename() error {
listing := make([]*types.Issue, 0, len(s.issues))
s.listed = make([]*types.Issue, 0, len(s.issues))
for _, issue := range s.issues {
renames, checks := *issue, *issue
listing = append(listing, &renames)
s.listed = append(s.listed, &checks)
}
slices.SortFunc(listing, func(a, b *types.Issue) int { return strings.Compare(a.ID, b.ID) })
issue, err := s.GetIssue(context.Background(), "bd-old")
if err != nil {
return err
}
return renameIssueKeepingBeadGates(context.Background(), s, issue, "bd-new", beadGatesByTarget(listing)["bd-old"], "test")
}
// renameGateFixtures each hold bd-old, the issue renamed to bd-new, with
// the bead gates on it, and the gates expected after the rename by ID.
var renameGateFixtures = []struct {
name string
issues []*types.Issue
want map[string]types.Issue // only AwaitID and Metadata are compared
}{
{
name: "gates on a bead",
issues: []*types.Issue{
{ID: "bd-old", Status: types.StatusOpen, IssueType: types.TypeTask},
{ID: "bd-g1", Status: types.StatusOpen, IssueType: types.TypeGate, AwaitType: "bead", AwaitID: "bd-old",
Metadata: json.RawMessage(`{"repo":"o/r","await_seen":"bd-old"}`)},
{ID: "bd-g2", Status: types.StatusOpen, IssueType: types.TypeGate, AwaitType: "bead", AwaitID: "rig:bd-old"},
{ID: "bd-g3", Status: types.StatusOpen, IssueType: types.TypeGate, AwaitType: "bead", AwaitID: "bd-old",
Metadata: json.RawMessage(`{"await_seen":"bd-new"}`)},
},
want: map[string]types.Issue{
"bd-g1": {AwaitID: "bd-new", Metadata: json.RawMessage(`{"await_seen":"bd-new","repo":"o/r"}`)},
"bd-g2": {AwaitID: "rig:bd-new"},
"bd-g3": {AwaitID: "bd-new", Metadata: json.RawMessage(`{}`)},
},
},
{
name: "a gate on itself",
issues: []*types.Issue{
{ID: "bd-old", Status: types.StatusOpen, IssueType: types.TypeGate, AwaitType: "bead", AwaitID: "bd-old",
Metadata: json.RawMessage(`{"await_seen":"bd-old"}`)},
},
want: map[string]types.Issue{
"bd-new": {AwaitID: "bd-new", Metadata: json.RawMessage(`{"await_seen":"bd-new"}`)},
},
},
}
func TestRenameIssueKeepingBeadGates(t *testing.T) {
// A rename points each bead gate on the bead at the new ID. A gate that
// had seen the bead keeps its sighting under the new ID; one that had not,
// or whose sighting named the new ID before the bead had it, is unseen.
for _, fx := range renameGateFixtures {
t.Run(fx.name, func(t *testing.T) {
st := newRenameGateStore(t, fx.issues)
if err := st.rename(); err != nil {
t.Fatalf("rename: %v", err)
}
for id, want := range fx.want {
got, ok := st.issues[id]
if !ok {
t.Errorf("gate %s is missing after the rename", id)
continue
}
if got.AwaitID != want.AwaitID || string(got.Metadata) != string(want.Metadata) {
t.Errorf("gate %s: await_id=%q metadata=%s, want %q and %s", id, got.AwaitID, got.Metadata, want.AwaitID, want.Metadata)
}
}
})
}
}
func TestRenameIssueKeepingBeadGates_FailedWritesResolveNoGate(t *testing.T) {
// UpdateIssueID commits on its own, so a rename takes several writes, and
// any of them can fail, or land and still return an error. No state they
// leave may let bd gate check resolve a gate whose bead exists; the store
// checks after every write. A rename that fails before anything lands
// leaves each gate on bd-old without a sighting, for a check to record
// again.
for _, fx := range renameGateFixtures {
probe := newRenameGateStore(t, fx.issues)
if err := probe.rename(); err != nil {
t.Fatalf("%s: rename without failures: %v", fx.name, err)
}
for n := 1; n <= probe.writes; n++ {
for _, mode := range []struct {
name string
failRest, landed bool
}{
{name: "fails"},
{name: "lands and fails", landed: true},
{name: "fails with every later write", failRest: true},
{name: "lands and fails with every later write", failRest: true, landed: true},
} {
t.Run(fmt.Sprintf("%s/write %d of %d %s", fx.name, n, probe.writes, mode.name), func(t *testing.T) {
st := newRenameGateStore(t, fx.issues)
st.failAt, st.failRest, st.landed = n, mode.failRest, mode.landed
err := st.rename()
st.requireNoGateResolves()
if err == nil {
// Only a sighting that could not be moved is not an error.
if _, ok := st.issues["bd-new"]; !ok {
t.Fatal("rename returned no error, but bd-old was not renamed")
}
return
}
if mode.failRest || mode.landed {
return
}
for _, issue := range fx.issues {
got := st.issues[issue.ID]
if issue.IssueType == types.TypeGate && (got.AwaitID != issue.AwaitID || beadGateTargetSeen(got)) {
t.Errorf("gate %s after a failed rename: await_id=%q metadata=%s, want %q without a sighting", issue.ID, got.AwaitID, got.Metadata, issue.AwaitID)
}
}
})
}
}
}
}
// withBeadGateTown points dbPath at a fresh town and returns its .beads
// directory, where a test writes the routes.jsonl a missed lookup follows.
func withBeadGateTown(t *testing.T) string {
t.Helper()
beadsDir := filepath.Join(t.TempDir(), ".beads")
if err := os.MkdirAll(beadsDir, 0o755); err != nil {
t.Fatalf("create town beads dir: %v", err)
}
oldDBPath := dbPath
dbPath = filepath.Join(beadsDir, "dolt")
t.Cleanup(func() { dbPath = oldDBPath })
return beadsDir
}
func TestProxiedFreshReadGetter_LocalMissFollowsRoutes(t *testing.T) {
// The proxied getter must not report a route it could not follow as the
// local not-found: an OPEN bead in a rig that cannot be read would
// resolve its gate.
for _, tt := range []struct {
name string
routes string // routes.jsonl content; empty makes it a directory, which cannot be read
awaitID string
wantResolved bool
wantReason []string
}{
{
name: "route to a rig that cannot be read",
routes: `{"prefix":"gt-","path":"gastown"}`,
awaitID: "gastown:gt-open",
wantReason: []string{"cannot confirm", "routes to gastown", "no dolt_database"},
},
{
name: "routes.jsonl cannot be read",
awaitID: stubMissingID,
wantReason: []string{"cannot confirm", "reading routes.jsonl"},
},
{
name: "no route for the prefix",
routes: `{"prefix":"gt-","path":"gastown"}`,
awaitID: stubMissingID,
wantResolved: true,
wantReason: []string{"no longer exists"},
},
{
name: "route back to this rig",
routes: `{"prefix":"bd-","path":"."}`,
awaitID: stubMissingID,
wantResolved: true,
wantReason: []string{"no longer exists"},
},
} {
t.Run(tt.name, func(t *testing.T) {
withStubbedProxiedLookup(t, nil)
routesPath := filepath.Join(withBeadGateTown(t), "routes.jsonl")
if tt.routes == "" {
if err := os.Mkdir(routesPath, 0o755); err != nil {
t.Fatalf("create unreadable routes.jsonl: %v", err)
}
} else if err := os.WriteFile(routesPath, []byte(tt.routes), 0o644); err != nil {
t.Fatalf("write routes.jsonl: %v", err)
}
resolved, reason, err := checkBeadGate(context.Background(), proxiedFreshReadGetter{}, tt.awaitID)
if err != nil {
t.Fatalf("checkBeadGate error = %v, want a pending or resolved gate", err)
}
if resolved != tt.wantResolved {
t.Errorf("resolved = %v, want %v (reason %q)", resolved, tt.wantResolved, reason)
}
for _, want := range tt.wantReason {
if !strings.Contains(reason, want) {
t.Errorf("reason %q does not contain %q", reason, want)
}
}
})
}
}
// beadGateLocalStore stands in for this rig's own store: GetIssue answers
// from issues (or fails with err), and no contributor routing is configured.
// Anything else is a nil call.
type beadGateLocalStore struct {
storage.DoltStorage
issues map[string]*types.Issue
err error
}
func (s *beadGateLocalStore) GetIssue(_ context.Context, id string) (*types.Issue, error) {
if s.err != nil {
return nil, s.err
}
if issue, ok := s.issues[id]; ok {
return issue, nil
}
return nil, fmt.Errorf("get %s: %w", id, storage.ErrNotFound)
}
func (s *beadGateLocalStore) GetAllConfig(context.Context) (map[string]string, error) {
return map[string]string{}, nil
}
func TestRoutedBeadGateGetter_ReportsWhereTheAnswerCameFrom(t *testing.T) {
ctx := context.Background()
beadsDir := withBeadGateTown(t)
if err := os.WriteFile(filepath.Join(beadsDir, "routes.jsonl"), []byte(`{"prefix":"gt-","path":"gastown"}`), 0o644); err != nil {
t.Fatalf("write routes.jsonl: %v", err)
}
getter := routedBeadGateGetter{localStore: &beadGateLocalStore{issues: map[string]*types.Issue{
"bd-open": {ID: "bd-open", Status: types.StatusOpen},
}}}
issue, local, err := getter.getBeadGateTarget(ctx, "bd-open")
if err != nil || issue == nil || !local {
t.Errorf("local bead: getBeadGateTarget = (%v, local %v, %v), want the bead from this rig", issue, local, err)
}
issue, local, err = getter.getBeadGateTarget(ctx, "bd-missing")
if !errors.Is(err, storage.ErrNotFound) || errors.Is(err, errBeadGateTargetUnconfirmed) || issue != nil || !local {
t.Errorf("unrouted miss: getBeadGateTarget = (%v, local %v, %v), want this rig's not-found", issue, local, err)
}
issue, local, err = getter.getBeadGateTarget(ctx, "gt-open")
if !errors.Is(err, errBeadGateTargetUnconfirmed) || issue != nil || local {
t.Errorf("unreadable route: getBeadGateTarget = (%v, local %v, %v), want an unconfirmed miss", issue, local, err)
}
boom := errors.New("dolt exploded")
_, _, err = routedBeadGateGetter{localStore: &beadGateLocalStore{err: boom}}.getBeadGateTarget(ctx, "gt-open")
if !errors.Is(err, boom) || errors.Is(err, errBeadGateTargetUnconfirmed) {
t.Errorf("local read failure: err = %v, want the read error, not a routed miss", err)
}
}
func TestPrintGateCheckSummary_ErrorsFailTheCommand(t *testing.T) {
origJSON := jsonOutput
t.Cleanup(func() { jsonOutput = origJSON })

View file

@ -4,7 +4,9 @@ import (
"context"
"errors"
"fmt"
"os"
"regexp"
"strings"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/metrics"
@ -83,9 +85,14 @@ func runRename(cmd *cobra.Command, args []string) error {
return HandleError("failed to check for existing issue: %v", err)
}
oldIssue.ID = newID
gateType := types.TypeGate
gates, err := store.SearchIssues(ctx, "", types.IssueFilter{IssueType: &gateType})
if err != nil {
return HandleError("failed to list gates: %v", err)
}
actor := getActorWithGit()
if err := store.UpdateIssueID(ctx, oldID, newID, oldIssue, actor); err != nil {
if err := renameIssueKeepingBeadGates(ctx, store, oldIssue, newID, beadGatesByTarget(gates)[oldID], actor); err != nil {
return HandleError("failed to rename issue: %v", err)
}
@ -150,3 +157,114 @@ func updateReferencesInAllIssues(ctx context.Context, store storage.DoltStorage,
return nil
}
// beadGateRenameStore is the part of the store a rename writes through.
type beadGateRenameStore interface {
UpdateIssue(ctx context.Context, id string, updates map[string]interface{}, actor string) error
UpdateIssueID(ctx context.Context, oldID, newID string, issue *types.Issue, actor string) error
}
// beadGatesByTarget indexes the bead gates in issues by the ID of the bead
// each one waits on (see beadGateTargetID).
func beadGatesByTarget(issues []*types.Issue) map[string][]*types.Issue {
gates := make(map[string][]*types.Issue)
for _, issue := range issues {
if issue.IssueType != types.TypeGate || issue.AwaitType != "bead" {
continue
}
if targetID, pendingReason := beadGateTargetID(issue.AwaitID); pendingReason == "" {
gates[targetID] = append(gates[targetID], issue)
}
}
return gates
}
// beadGateMove is one bead gate following its bead to a new ID.
type beadGateMove struct {
gateID string
fromID string // await_id before the rename
toID string // await_id after it; a <rig>: prefix is kept
seen bool // the gate records a sighting of fromID
staleSeen bool // the gate records a sighting of toID, left from an earlier await_id
}
// planBeadGateMoves lists the moves of gates, which wait on oldID, for its
// rename to newID.
func planBeadGateMoves(gates []*types.Issue, oldID, newID string) []beadGateMove {
moves := make([]beadGateMove, 0, len(gates))
for _, gate := range gates {
toID := strings.TrimSuffix(gate.AwaitID, oldID) + newID
moves = append(moves, beadGateMove{
gateID: gate.ID,
fromID: gate.AwaitID,
toID: toID,
seen: beadGateTargetSeen(gate),
staleSeen: beadGateSeenID(gate) == toID,
})
}
return moves
}
// renameIssueKeepingBeadGates renames issue to newID and points gates, the
// bead gates waiting on it, at the new ID. bd gate check resolves a gate whose
// awaited bead is missing once await_seen records a sighting of that same
// await_id, and UpdateIssueID commits on its own, so the writes are ordered
// to leave no state in between that resolves a gate:
//
// 1. Each gate's await_id moves to the new ID while its sighting still names
// the old one, so until the bead has the new ID the gate reads as never
// seen and stays pending. A sighting that already names the new ID would
// match early, so it is dropped in the same write.
// 2. The issue is renamed. UpdateIssueID writes neither await_id nor
// metadata, so the moves survive it, a gate's own rename included.
// 3. Each sighting is recorded again under the new ID. Losing one costs only
// the sighting, which the next check records again while the bead exists,
// so a failure here is a warning.
//
// If step 1 or 2 fails, the gates are moved back and the error is returned. A
// failed write may still have landed (a lost commit response), so a gate
// moved back loses its sighting too: it stays pending until a check sees the
// bead again. A gate that cannot be moved back is left on the new ID without a
// sighting of it, which also stays pending.
func renameIssueKeepingBeadGates(ctx context.Context, st beadGateRenameStore, issue *types.Issue, newID string, gates []*types.Issue, actorName string) error {
oldID := issue.ID
moves := planBeadGateMoves(gates, oldID, newID)
for i, m := range moves {
if err := st.UpdateIssue(ctx, m.gateID, beadGateRetargetUpdate(m.toID, m.staleSeen), actorName); err != nil {
err = fmt.Errorf("failed to point gate %s at %s: %w", m.gateID, m.toID, err)
return errors.Join(err, moveBeadGatesBack(ctx, st, moves[:i+1], actorName))
}
}
issue.ID = newID
if err := st.UpdateIssueID(ctx, oldID, newID, issue, actorName); err != nil {
issue.ID = oldID
return errors.Join(err, moveBeadGatesBack(ctx, st, moves, actorName))
}
for _, m := range moves {
if !m.seen {
continue
}
gateID := m.gateID
if gateID == oldID {
gateID = newID // the gate waits on itself
}
if err := st.UpdateIssue(ctx, gateID, beadGateSeenUpdate(m.toID), actorName); err != nil {
fmt.Fprintf(os.Stderr, "Warning: gate %s: could not move its sighting of %s to %s: %v\n", gateID, m.fromID, m.toID, err)
}
}
return nil
}
// moveBeadGatesBack points each moved gate at its old await_id again and
// drops its sighting (see renameIssueKeepingBeadGates).
func moveBeadGatesBack(ctx context.Context, st beadGateRenameStore, moves []beadGateMove, actorName string) error {
var errs []error
for _, m := range moves {
if err := st.UpdateIssue(ctx, m.gateID, beadGateRetargetUpdate(m.fromID, true), actorName); err != nil {
errs = append(errs, fmt.Errorf("failed to point gate %s back at %s: %w", m.gateID, m.fromID, err))
}
}
return errors.Join(errs...)
}

View file

@ -335,6 +335,7 @@ func repairPrefixes(ctx context.Context, st storage.DoltStorage, actorName strin
// Pattern to match any issue ID reference in text (both hash and sequential IDs)
oldPrefixPattern := regexp.MustCompile(`\b[a-z][a-z0-9-]*-[a-z0-9]+\b`)
gates := beadGatesByTarget(issues)
// Rename each issue
for _, is := range incorrectIssues {
@ -343,7 +344,6 @@ func repairPrefixes(ctx context.Context, st storage.DoltStorage, actorName strin
// Apply text replacements in all issue fields
issue := is.issue
issue.ID = newID
// Replace all issue IDs in text fields using the rename map
replaceFunc := func(match string) string {
@ -366,7 +366,7 @@ func repairPrefixes(ctx context.Context, st storage.DoltStorage, actorName strin
}
// Update the issue in the database
if err := st.UpdateIssueID(ctx, oldID, newID, issue, actorName); err != nil {
if err := renameIssueKeepingBeadGates(ctx, st, issue, newID, gates[oldID], actorName); err != nil {
return fmt.Errorf("failed to update issue %s -> %s: %w", oldID, newID, err)
}
@ -433,6 +433,7 @@ func renamePrefixInDB(ctx context.Context, oldPrefix, newPrefix string, issues [
return strings.Replace(match, oldP+"-", newP+"-", 1)
}
gates := beadGatesByTarget(issues)
for _, issue := range issues {
oldID := issue.ID
newID := rewriteIssueID(oldP, newP, oldID)
@ -455,8 +456,7 @@ func renamePrefixInDB(ctx context.Context, oldPrefix, newPrefix string, issues [
continue
}
issue.ID = newID
if err := store.UpdateIssueID(ctx, oldID, newID, issue, actor); err != nil {
if err := renameIssueKeepingBeadGates(ctx, store, issue, newID, gates[oldID], actor); err != nil {
return fmt.Errorf("failed to update issue %s: %w", oldID, err)
}
}

View file

@ -4,9 +4,13 @@ package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"path/filepath"
"testing"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/types"
)
@ -120,3 +124,175 @@ func TestRepairMultiplePrefixes(t *testing.T) {
}
}
}
func TestRepairPrefixesRetargetsBeadGates(t *testing.T) {
// The repair gives every off-prefix bead a new ID. A bead gate waiting on
// one must follow it, sighting included, or its next check would read the
// rename as a deletion and resolve. That holds whether the gate itself is
// renamed before its bead, after it, or not at all.
ctx := context.Background()
testStore := newTestStore(t, filepath.Join(t.TempDir(), "test.db"))
for _, issue := range []*types.Issue{
{ID: "test-1", Title: "Kept bead", Status: types.StatusOpen, Priority: 2, IssueType: types.TypeTask},
{ID: "old-1", Title: "Renamed bead", Status: types.StatusOpen, Priority: 2, IssueType: types.TypeTask},
{ID: "test-gate", Title: "Gate on the renamed bead", Status: types.StatusOpen, Priority: 2, IssueType: "gate",
AwaitType: "bead", AwaitID: "old-1", Metadata: json.RawMessage(`{"await_seen":"old-1"}`)},
{ID: "old-gate", Title: "Renamed gate on the kept bead", Status: types.StatusOpen, Priority: 2, IssueType: "gate",
AwaitType: "bead", AwaitID: "test-1", Metadata: json.RawMessage(`{"await_seen":"test-1"}`)},
// The repair renames in ID-number order: old-0, old-1, old-2.
{ID: "old-0", Title: "Gate renamed before its bead", Status: types.StatusOpen, Priority: 2, IssueType: "gate",
AwaitType: "bead", AwaitID: "old-1", Metadata: json.RawMessage(`{"await_seen":"old-1"}`)},
{ID: "old-2", Title: "Gate renamed after its bead", Status: types.StatusOpen, Priority: 2, IssueType: "gate",
AwaitType: "bead", AwaitID: "old-1", Metadata: json.RawMessage(`{"await_seen":"old-1"}`)},
} {
if err := testStore.CreateIssue(ctx, issue, "test"); err != nil {
t.Fatalf("failed to create issue %s: %v", issue.ID, err)
}
}
allIssues, err := testStore.SearchIssues(ctx, "", types.IssueFilter{})
if err != nil {
t.Fatalf("failed to search issues: %v", err)
}
if err := repairPrefixes(ctx, testStore, "test", "test", allIssues, detectPrefixes(allIssues), false); err != nil {
t.Fatalf("repair failed: %v", err)
}
allIssues, err = testStore.SearchIssues(ctx, "", types.IssueFilter{})
if err != nil {
t.Fatalf("failed to search issues after repair: %v", err)
}
idByTitle := make(map[string]string, len(allIssues))
for _, issue := range allIssues {
idByTitle[issue.Title] = issue.ID
}
renamedBead := idByTitle["Renamed bead"]
if renamedBead == "" || renamedBead == "old-1" {
t.Fatalf("old-1 was not renamed: %v", idByTitle)
}
for _, tt := range []struct {
title string
wantAwaitID string
}{
{title: "Gate on the renamed bead", wantAwaitID: renamedBead},
{title: "Renamed gate on the kept bead", wantAwaitID: "test-1"},
{title: "Gate renamed before its bead", wantAwaitID: renamedBead},
{title: "Gate renamed after its bead", wantAwaitID: renamedBead},
} {
gate, err := testStore.GetIssue(ctx, idByTitle[tt.title])
if err != nil {
t.Fatalf("%s: %v", tt.title, err)
}
if gate.AwaitID != tt.wantAwaitID || !beadGateTargetSeen(gate) {
t.Errorf("%s: await_id=%q metadata=%s, want %q recorded as seen", tt.title, gate.AwaitID, gate.Metadata, tt.wantAwaitID)
}
}
}
// failingRenameStore fails UpdateIssueID for failID. With landed, the rename
// is applied before the error is returned, like a commit whose response was
// lost.
type failingRenameStore struct {
storage.DoltStorage
failID string
landed bool
injected bool
}
func (s *failingRenameStore) UpdateIssueID(ctx context.Context, oldID, newID string, issue *types.Issue, actor string) error {
if oldID != s.failID {
return s.DoltStorage.UpdateIssueID(ctx, oldID, newID, issue, actor)
}
if s.landed {
if err := s.DoltStorage.UpdateIssueID(ctx, oldID, newID, issue, actor); err != nil {
return err
}
}
s.injected = true
return errors.New("injected UpdateIssueID failure")
}
// withRenameStore points the store and actor globals at st while run runs.
func withRenameStore(st storage.DoltStorage, run func() error) error {
oldStore, oldActor := store, actor
store, actor = st, "test"
storeMutex.Lock()
oldActive := storeActive
storeActive = true
storeMutex.Unlock()
defer func() {
store, actor = oldStore, oldActor
storeMutex.Lock()
storeActive = oldActive
storeMutex.Unlock()
}()
return run()
}
func TestRenameFailureLeavesBeadGatesPending(t *testing.T) {
// When renaming the awaited bead fails, whether or not that rename
// landed, its gate is pointed back at the bead's old ID without a
// sighting: bd gate check must find it pending, never resolve it as
// though the bead had been deleted. The prefix renames rename the gate
// first, so they move it back under its new ID.
for _, tt := range []struct {
name string
gateRenamed bool
run func(ctx context.Context, st storage.DoltStorage, issues []*types.Issue) error
}{
{name: "rename", run: func(_ context.Context, st storage.DoltStorage, _ []*types.Issue) error {
return withRenameStore(st, func() error { return runRename(renameCmd, []string{"old-1", "old-renamed"}) })
}},
{name: "repair", gateRenamed: true, run: func(ctx context.Context, st storage.DoltStorage, issues []*types.Issue) error {
return repairPrefixes(ctx, st, "test", "test", issues, detectPrefixes(issues), false)
}},
{name: "rename-prefix", gateRenamed: true, run: func(ctx context.Context, st storage.DoltStorage, issues []*types.Issue) error {
return withRenameStore(st, func() error { return renamePrefixInDB(ctx, "old", "test", issues) })
}},
} {
for _, landed := range []bool{false, true} {
t.Run(fmt.Sprintf("%s/landed=%v", tt.name, landed), func(t *testing.T) {
ctx := context.Background()
testStore := newTestStore(t, filepath.Join(t.TempDir(), "test.db"))
issues := []*types.Issue{
{ID: "old-gate", Title: "Gate", Status: types.StatusOpen, Priority: 2, IssueType: "gate",
AwaitType: "bead", AwaitID: "old-1", Metadata: json.RawMessage(`{"await_seen":"old-1"}`)},
{ID: "old-1", Title: "Awaited bead", Status: types.StatusOpen, Priority: 2, IssueType: types.TypeTask},
}
for _, issue := range issues {
if err := testStore.CreateIssue(ctx, issue, "test"); err != nil {
t.Fatalf("failed to create issue %s: %v", issue.ID, err)
}
}
st := &failingRenameStore{DoltStorage: testStore, failID: "old-1", landed: landed}
if err := tt.run(ctx, st, issues); err == nil || !st.injected {
t.Fatalf("rename error = %v (failure injected: %v), want the injected failure", err, st.injected)
}
all, err := testStore.SearchIssues(ctx, "", types.IssueFilter{})
if err != nil {
t.Fatalf("failed to search issues: %v", err)
}
var gate *types.Issue
for _, issue := range all {
if issue.Title == "Gate" {
gate = issue
}
}
if gate == nil || (gate.ID != "old-gate") != tt.gateRenamed {
t.Fatalf("gate after the failed rename: %+v, want it renamed: %v", gate, tt.gateRenamed)
}
if gate.AwaitID != "old-1" || beadGateTargetSeen(gate) {
t.Errorf("gate after the failed rename: await_id=%q metadata=%s, want %q without a sighting", gate.AwaitID, gate.Metadata, "old-1")
}
resolved, reason, err := evaluateBeadGate(ctx, gate, testStore, nil)
if err != nil || resolved {
t.Errorf("bd gate check on the gate: resolved=%v reason=%q err=%v, want it pending", resolved, reason, err)
}
})
}
}
}

View file

@ -37,7 +37,18 @@ bd gate resolve <gate-id> # close a gate manually
| `timer` | a duration after gate creation | `bd gate check` once the timeout elapses |
| `gh:run` | a GitHub Actions workflow to complete successfully | `bd gate check` (uses `gh run view`) |
| `gh:pr` | a pull request to merge | `bd gate check` (uses `gh pr view`) |
| `bead` | a bead to close — a plain ID names a bead in this rig, the cross-rig form is `<rig>:<bead-id>` | `bd gate check` for plain local IDs; cross-rig values cannot be checked — resolve those manually |
| `bead` | a bead to close, or to be deleted once a check has seen it — a plain ID names a bead in this rig, the cross-rig form is `<rig>:<bead-id>` | `bd gate check` for plain local IDs; cross-rig values cannot be checked — resolve those manually |
A `bead` gate also closes when its awaited bead is deleted, as long as an
earlier `bd gate check` saw that bead in this rig; the check records the
sighting in the gate's `await_seen` metadata. An await ID that no check has
seen, such as a typo or a bead deleted before the first check, keeps the gate
pending with a diagnostic: correct the await ID or close the gate with
`bd gate resolve`. Renaming the awaited bead with `bd rename` or
`bd rename-prefix` moves the gate's await ID and sighting to the new ID, so a
rename does not count as a deletion, even to a `bd gate 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.
Timeouts use Go duration syntax: `30m`, `1h`, `24h` (there is no `d` unit —
write `24h`, not `1d`).