diff --git a/.github/scripts/proxied-cmd-test-shards.txt b/.github/scripts/proxied-cmd-test-shards.txt index 33611719b..7c60e35ad 100644 --- a/.github/scripts/proxied-cmd-test-shards.txt +++ b/.github/scripts/proxied-cmd-test-shards.txt @@ -259,6 +259,7 @@ 34 31 TestProxiedServerComment 34 31 TestProxiedServerConfigB 34 31 TestProxiedServerEventsJournal +34 31 TestProxiedServerGateAddWaiterConvoy 34 31 TestProxiedServerIfRevisionGuardClose 34 31 TestProxiedServerMolBond diff --git a/CHANGELOG.md b/CHANGELOG.md index 1808f549a..10a63b334 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -377,6 +377,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- **`bd gate discover` skips template gates, as `bd gate check` does.** A + template gh:run gate is a proto that work is poured from; on the direct, + proxied and embedded routes `bd gate discover` used to record a discovered + run ID on it, which every later pour copied. It is now left alone on every + route. `bd gate add-waiter` on a template gate is refused by the update + role's template guard, where the direct, proxied and embedded routes all + used to write the waiter. +- **`bd gate create` refuses a negative `--timeout` on every route** + (`timeout cannot be negative`); the direct, proxied and embedded routes + used to store it. A `--type` longer than 32 characters fails with the same + typed validation error instead of a raw Dolt column error. +- **`bd gate create` and `bd gate add-waiter` follow `dolt.auto-commit` + like every other role write.** Under `dolt.auto-commit=off` `bd gate + create` no longer makes its own Dolt commit on the direct and embedded + routes (the proxied route already did not). The Dolt commit messages are + the role's: `bd: create gate X blocking Y` becomes the create's default + message, and `bd gate add-waiter`'s becomes the update's (`bd: update X` + on the direct and embedded routes, `update issue` on the proxied route). + On the proxied route `bd gate create` no longer journals a + `dependency_added` event on the blocked issue, matching the direct route + and `bd create --deps`. - **Gate members are bounds-checked in the library, on every local backend.** `issueops.Lifecycle.Create`, `Lifecycle.Update` and `BatchApplier` (create and update items) now refuse, with a typed `*issueops.GateFieldError` @@ -595,6 +616,38 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- **`bd gate create`, `bd gate add-waiter` and `bd gate discover` work over a + connected HTTP workspace.** They wrote through the legacy `CreateIssue`, + `AddDependency` and map-based `UpdateIssue`, which the HTTP store does not + implement, so each failed before any gate member reached the wire. They now + run through the library roles on every route: + - `bd gate create` is ONE `Lifecycle.Create` carrying the gate members and + a reverse `blocks` edge from the blocked issue to the new gate, so the + gate and its edge land atomically (one `createIssue` over HTTP, behind + `issues.create.gates`). The direct route used to make two writes and an + explicit commit; the proxied-server route now uses the same role. + - `bd gate add-waiter` reads the gate through `Reader.Get`, deduplicates, + and writes the whole waiter list through `Lifecycle.Update` guarded by + the row version it read. On a version mismatch it re-reads and retries + after a jittered, doubling backoff (10 ms up to 400 ms per wait, at most + 20 writes), so a convoy of workers registering on one gate at once all + succeed. On the direct route concurrent adders used to lose + each other's waiters while every one exited 0; no waiter is lost now. + Over HTTP it needs `issues.update.gates`. + - `bd gate discover` lists through `issueops.ListGates` (as `bd gate check` + does) and records each discovered run ID through `Lifecycle.Update`, the + same `await_id` write `bd gate check` and `bd close` make. Over HTTP it + needs `issues.update.gates`. Its summary now counts only the gates whose + run ID was recorded, and it exits non-zero when any matched gate's write + failed (it used to report such a gate as updated and exit 0). + + Against a server that does not advertise the token it needs, `bd gate + create` and `bd gate add-waiter` are refused before any write, naming the + token; `bd gate discover` writes nothing and reports the token as that + gate's error. The served corpus gains `mc-gate-create-timer`, + `mc-gate-check-timer-resolves`, `mc-gate-add-waiter` and + `mc-update-await-id` (pending until the reference server advertises the + gate tokens), pinned passing by `TestE2E_GatesOverHTTP`. - **`bd import` no longer reports a failed dependency check as a skipped dependency.** Import skips an edge the dependency checks refuse (a self-dependency, a cycle, or a blocker that is the issue's own ancestor or diff --git a/backend/http/BUILD.bazel b/backend/http/BUILD.bazel index 133286b71..da2d2454d 100644 --- a/backend/http/BUILD.bazel +++ b/backend/http/BUILD.bazel @@ -33,6 +33,7 @@ go_test( "attach_test.go", "e2e_cli_test.go", "e2e_export_test.go", + "e2e_gates_test.go", "e2e_heartbeat_test.go", "e2e_list_filters_test.go", "e2e_list_page_edges_test.go", diff --git a/backend/http/corpus/gate_check_served_test.go b/backend/http/corpus/gate_check_served_test.go index ffcc44b98..60d9f82c0 100644 --- a/backend/http/corpus/gate_check_served_test.go +++ b/backend/http/corpus/gate_check_served_test.go @@ -8,6 +8,7 @@ import ( "net/http" "os" "path/filepath" + "slices" "strings" "sync" "testing" @@ -51,11 +52,12 @@ func gateCheckWrites(reqs []string) []string { } // TestServedGateCheckOverAwaitTypedGates runs `bd gate check` over HTTP, through -// the reference proxy, against REAL await-typed gates. The reference server -// advertises neither gate token (issues.create.gates, issues.update.gates), so -// no create through it carries await_*: the gates are seeded straight into the -// served store before it listens, and every outcome is read back from that -// store: +// the reference proxy, against REAL await-typed gates, on a server that +// advertises neither gate token (issues.create.gates, issues.update.gates; the +// profile is masked here, so this holds whatever reference_server.json +// advertises). No create through it carries await_*: the gates are seeded +// straight into the served store before it listens, and every outcome is read +// back from that store: // // - --dry-run writes nothing; // - an expired timer closes, a pending one stays open; @@ -141,7 +143,13 @@ func TestServedGateCheckOverAwaitTypedGates(t *testing.T) { } }) - proxy := newReferenceProxy(t, loadReferenceProfile(t), srv.Addr()) + // The gh:run case pins a server that cannot take an await_id patch, so the + // profile is held without issues.update.gates whatever + // reference_server.json advertises: upgrading the reference profile must + // not turn this case into a different test. + profile := loadReferenceProfile(t) + profile.Capabilities = slices.DeleteFunc(profile.Capabilities, func(c string) bool { return c == "issues.update.gates" }) + proxy := newReferenceProxy(t, profile, srv.Addr()) reqs := &gateCheckRequestLog{next: proxy} proxyAddr := startProxy(t, reqs) diff --git a/backend/http/corpus/testdata/corpus.yaml b/backend/http/corpus/testdata/corpus.yaml index ba69ac9ee..668b72491 100644 --- a/backend/http/corpus/testdata/corpus.yaml +++ b/backend/http/corpus/testdata/corpus.yaml @@ -103,7 +103,11 @@ files: {"key": "t-ro", "id": "e2e-t-ro", "title": "target reopen", "type": "task"}, {"key": "t-dr", "id": "e2e-t-dr", "title": "target dep remove", "type": "task", "deps": [{"target": "t-db"}]}, {"key": "t-dc", "id": "e2e-t-dc", "title": "cascade root", "type": "epic"}, - {"key": "t-dc.1", "id": "e2e-t-dc.1", "title": "cascade child", "type": "task", "parent_key": "t-dc"} + {"key": "t-dc.1", "id": "e2e-t-dc.1", "title": "cascade child", "type": "task", "parent_key": "t-dc"}, + {"key": "t-gcr", "id": "e2e-t-gcr", "title": "target gate create", "type": "task"}, + {"key": "t-gct", "id": "e2e-t-gct", "title": "target gate check", "type": "task"}, + {"key": "t-gw", "id": "e2e-t-gw", "title": "target gate waiter", "type": "gate"}, + {"key": "t-gaw", "id": "e2e-t-gaw", "title": "target gate await id", "type": "gate"} ]} - name: bulk-ready-a.json bulk: {id_prefix: e2e-bra, count: 60, priority: 3, metadata: {gc.routed_to: pool-a}} @@ -445,7 +449,8 @@ shapes: source: core pack orders (gate list --all) args: [gate, list, --all, --json] # gate check lists the open gates through the same ListGates entry. No - # seeded gate carries an await type (no wire create accepts await_*), so the + # seeded gate carries an await type (the seed graph carries no gate members; + # mc-gate-check-timer-resolves below creates a real timer gate), so the # typed sweeps find nothing of their type and the untyped one lists the open # gates and evaluates none: the listing is what these shapes prove served, # and nothing resolves, escalates (no gt exec) or writes. @@ -796,3 +801,33 @@ shapes: expect_exit: 1 expect_stderr: 'unknown command "meta"' compare: exit + # ---------------------------------------------------------------- gates + # The gate writes: create (one createIssue carrying the gate members and the + # blocks edge), a real timer gate resolving under gate check, add-waiter (a + # guarded waiters patch) and update --await-id. A --timeout=1ns timer is + # expired by the time gate check runs, so the resolution is deterministic. + - id: mc-gate-create-timer + source: core formulas / gate-sweep (bd gate create --type=timer --timeout --blocks) + args: [gate, create, --type=timer, --timeout=1ns, --blocks, e2e-t-gcr, --json] + ignore_fields: [id] # each side mints its own gate id + pending: {slice: "SRV-1", reason: "bd gate create sends the gate members on createIssue behind issues.create.gates, which the reference server does not advertise, so the client refuses before dialing (pinned passing against the current server by backend/http TestE2E_GatesOverHTTP); remove the mark when reference_server.json is upgraded"} + - id: mc-gate-check-timer-resolves + source: core gate-sweep.sh (gate check --type=timer --escalate) over a real timer gate + setup: [[gate, create, --type=timer, --timeout=1ns, --blocks, e2e-t-gct]] + args: [gate, check, --type=timer, --escalate] + compare: exit + verify: [[ready, --json, --limit, "0"]] # e2e-t-gct is ready on both sides + ignore_fields: [depends_on_id] # its (closed) gate's id is minted per workspace + pending: {slice: "SRV-1", reason: "the setup bd gate create sends the gate members on createIssue behind issues.create.gates, which the reference server does not advertise, so the client refuses before dialing (pinned passing against the current server by backend/http TestE2E_GatesOverHTTP); remove the mark when reference_server.json is upgraded"} + - id: mc-gate-add-waiter + source: bd done --phase-complete (gate add-waiter) + args: [gate, add-waiter, e2e-t-gw, "rig/workers/a"] + compare: text + verify: [[show, --json, e2e-t-gw]] + pending: {slice: "SRV-1", reason: "bd gate add-waiter sends waiters on updateIssue behind issues.update.gates, which the reference server does not advertise, so the client refuses before dialing (pinned passing against the current server by backend/http TestE2E_GatesOverHTTP); remove the mark when reference_server.json is upgraded"} + - id: mc-update-await-id + source: gate discover / gate check gh:run persist (update --await-id) + args: [update, e2e-t-gaw, --await-id, "12345", --json] + compare: exit + verify: [[show, --json, e2e-t-gaw]] + pending: {slice: "SRV-1", reason: "bd update --await-id sends await_id on updateIssue behind issues.update.gates, which the reference server does not advertise, so the client refuses before dialing (pinned passing against the current server by backend/http TestE2E_GatesOverHTTP); remove the mark when reference_server.json is upgraded"} diff --git a/backend/http/e2e_gates_test.go b/backend/http/e2e_gates_test.go new file mode 100644 index 000000000..d1ca36fe8 --- /dev/null +++ b/backend/http/e2e_gates_test.go @@ -0,0 +1,315 @@ +//go:build cgo + +package bdhttp_test + +import ( + "encoding/json" + "fmt" + "net/http" + "os" + "path/filepath" + "slices" + "strings" + "sync" + "testing" + "time" +) + +// gateTokens are the two behavior tokens the gate commands depend on: +// issues.create.gates for a create carrying gate members (bd gate create), +// issues.update.gates for an await_id or waiters patch (bd gate add-waiter, +// bd gate discover, gate check's gh:run persist, bd update --await-id). +var gateTokens = []string{"issues.create.gates", "issues.update.gates"} + +// withoutGateTokens rewrites the handshake into a server that predates the +// gate members: the live capability list minus the two gate tokens. +func withoutGateTokens(body map[string]any) { + caps, _ := body["capabilities"].([]any) + kept := make([]any, 0, len(caps)) + for _, c := range caps { + if s, _ := c.(string); slices.Contains(gateTokens, s) { + continue + } + kept = append(kept, c) + } + body["capabilities"] = kept +} + +// gateShow is the subset of `bd show --json` the gate cases read. +type gateShow struct { + ID string `json:"id"` + Status string `json:"status"` + IssueType string `json:"issue_type"` + AwaitType string `json:"await_type"` + AwaitID string `json:"await_id"` + Timeout int64 `json:"timeout"` + Waiters []string `json:"waiters"` +} + +func showGate(t *testing.T, f *remoteRolesFixture, id string) gateShow { + t.Helper() + out := strings.TrimSpace(f.mustRun(nil, "show", id, "--json").stdout) + var rows []gateShow + if err := json.Unmarshal([]byte(out), &rows); err != nil { + var one gateShow + if err1 := json.Unmarshal([]byte(out), &one); err1 != nil { + t.Fatalf("parse bd show --json: %v\n%s", err, out) + } + return one + } + if len(rows) == 0 { + t.Fatalf("bd show %s --json returned no row", id) + } + return rows[0] +} + +func readyIDs(t *testing.T, f *remoteRolesFixture) map[string]bool { + t.Helper() + ids := map[string]bool{} + for _, row := range jsonRows(t, f.mustRun(nil, "ready", "--json", "--limit", "0").stdout) { + if id, ok := row["id"].(string); ok { + ids[id] = true + } + } + return ids +} + +func writesOf(paths []string) []string { + var writes []string + for _, p := range paths { + if !strings.HasPrefix(p, http.MethodGet+" ") { + writes = append(writes, p) + } + } + return writes +} + +// TestE2E_GatesOverHTTP runs the gate commands through the real `bd` binary +// against an in-process `bd serve`. +// +// Against a server that advertises the gate tokens: +// - `bd gate create` is ONE createIssue carrying the gate members and the +// blocks edge (timer, gh:run and bead gates), and the blocked issue +// leaves the ready set; +// - `bd gate check --type=timer` closes an expired timer gate and the +// blocked issue becomes ready; +// - `bd gate add-waiter` registers a waiter once (the repeat writes nothing); +// - `bd update --await-id` sets await_id. +// +// Against a server that does not advertise them, each command is refused +// before any write, naming the token it needs, and nothing changes. +func TestE2E_GatesOverHTTP(t *testing.T) { + skipUnlessEmbeddedDolt(t) + bin := buildBD(t) + addr := startE2EServer(t) + + proxyAddr, counter := startCountingProxy(t, addr) + f := newHTTPFixture(t, bin, proxyAddr) + + var waiterGate string + + t.Run("advertised", func(t *testing.T) { + timerTarget := f.create("timer target") + counter.reset() + created := f.mustRun(nil, "gate", "create", "--type=timer", "--timeout=1ns", "--blocks", timerTarget, "--json") + if w := writesOf(counter.snapshot()); !slices.Equal(w, []string{"POST /v0/beads/issues"}) { + t.Errorf("gate create wrote %v, want exactly one createIssue", w) + } + timerGate := firstJSONField(t, created.stdout, "id") + if g := showGate(t, f, timerGate); g.IssueType != "gate" || g.AwaitType != "timer" || g.Timeout != 1 || g.Status != "open" { + t.Errorf("timer gate reads back %+v, want an open timer gate with a 1ns timeout", g) + } + if readyIDs(t, f)[timerTarget] { + t.Errorf("%s is ready while its gate is open", timerTarget) + } + + time.Sleep(10 * time.Millisecond) + r := f.mustRun(nil, "gate", "check", "--type=timer", "--escalate") + if !strings.Contains(r.stdout, "1 resolved") { + t.Errorf("gate check --type=timer = %q, want one resolved gate", r.stdout) + } + if g := showGate(t, f, timerGate); g.Status != "closed" { + t.Errorf("expired timer gate %s is %s after gate check, want closed", timerGate, g.Status) + } + if !readyIDs(t, f)[timerTarget] { + t.Errorf("%s is not ready after its timer gate resolved", timerTarget) + } + + runTarget := f.create("run target") + runGate := firstJSONField(t, f.mustRun(nil, "gate", "create", "--type=gh:run", "--await-id=ci-wf", "--repo=owner/repo", "--blocks", runTarget, "--json").stdout, "id") + if g := showGate(t, f, runGate); g.AwaitType != "gh:run" || g.AwaitID != "ci-wf" { + t.Errorf("gh:run gate reads back %+v, want await gh:run ci-wf", g) + } + + awaited := f.create("awaited bead") + beadTarget := f.create("bead target") + beadGate := firstJSONField(t, f.mustRun(nil, "gate", "create", "--type=bead", "--await-id", awaited, "--blocks", beadTarget, "--json").stdout, "id") + if g := showGate(t, f, beadGate); g.AwaitType != "bead" || g.AwaitID != awaited { + t.Errorf("bead gate reads back %+v, want await bead %s", g, awaited) + } + waiterGate = beadGate + + counter.reset() + r = f.mustRun(nil, "gate", "add-waiter", beadGate, "rig/workers/a") + if !strings.Contains(r.stdout, "Added waiter to gate "+beadGate+": rig/workers/a") { + t.Errorf("add-waiter output = %q", r.stdout) + } + if w := writesOf(counter.snapshot()); len(w) != 1 || !strings.HasPrefix(w[0], "PATCH /v0/beads/issues/") { + t.Errorf("add-waiter wrote %v, want one updateIssue", w) + } + counter.reset() + r = f.mustRun(nil, "gate", "add-waiter", beadGate, "rig/workers/a") + if !strings.Contains(r.stdout, "Waiter already registered on gate "+beadGate) { + t.Errorf("repeat add-waiter output = %q", r.stdout) + } + if w := writesOf(counter.snapshot()); len(w) != 0 { + t.Errorf("a repeat add-waiter wrote %v", w) + } + f.mustRun(nil, "gate", "add-waiter", beadGate, "rig/workers/b") + if g := showGate(t, f, beadGate); !slices.Equal(g.Waiters, []string{"rig/workers/a", "rig/workers/b"}) { + t.Errorf("waiters = %v, want [rig/workers/a rig/workers/b]", g.Waiters) + } + + f.mustRun(nil, "update", runGate, "--await-id", "12345", "--json") + if g := showGate(t, f, runGate); g.AwaitID != "12345" { + t.Errorf("await_id after update --await-id = %q, want 12345", g.AwaitID) + } + }) + + // A convoy registering on one gate at once: every adder must exit 0 and + // every waiter must be on the gate. Each round of simultaneous guarded + // writes has one winner, so this pins the retry backoff, not just the + // compare-and-set. + t.Run("concurrent add-waiter", func(t *testing.T) { + const adders = 10 + target := f.create("convoy target") + gate := firstJSONField(t, f.mustRun(nil, "gate", "create", "--blocks", target, "--json").stdout, "id") + results := make([]bdResult, adders+1) + var wg sync.WaitGroup + start := make(chan struct{}) + for i := 0; i <= adders; i++ { + waiter := fmt.Sprintf("rig/workers/c%d", i%adders) // the last one repeats c0 + wg.Add(1) + go func(i int) { + defer wg.Done() + <-start + results[i] = f.run(nil, "gate", "add-waiter", gate, waiter) + }(i) + } + close(start) + wg.Wait() + for i, r := range results { + if r.code != 0 { + t.Errorf("adder %d exited %d: %s", i, r.code, r.stderr) + } + } + got := showGate(t, f, gate).Waiters + slices.Sort(got) + var want []string + for i := 0; i < adders; i++ { + want = append(want, fmt.Sprintf("rig/workers/c%d", i)) + } + slices.Sort(want) + if !slices.Equal(got, want) { + t.Errorf("waiters after %d concurrent adders = %v, want each exactly once: %v", adders+1, got, want) + } + }) + + // gate discover over HTTP: a fake gh answers the run listing, and the + // discovered run ID is recorded through updateIssue (issues.update.gates). + binDir := t.TempDir() + gh := `#!/bin/sh +case "$1 $2" in +"run list") echo '[{"databaseId":4242,"name":"ci-wf","status":"in_progress","conclusion":"","createdAt":"'"$(date -u +%Y-%m-%dT%H:%M:%SZ)"'","workflowName":"ci-wf","headBranch":"main"}]' ;; +*) echo "unexpected gh $*" >&2; exit 3 ;; +esac +` + if err := os.WriteFile(filepath.Join(binDir, "gh"), []byte(gh), 0o755); err != nil { + t.Fatalf("write fake gh: %v", err) + } + ghEnv := []string{"PATH=" + binDir + string(os.PathListSeparator) + os.Getenv("PATH")} + + t.Run("discover", func(t *testing.T) { + target := f.create("discover target") + gate := firstJSONField(t, f.mustRun(nil, "gate", "create", "--type=gh:run", "--await-id=ci-wf", "--blocks", target, "--json").stdout, "id") + counter.reset() + r := f.run(ghEnv, "gate", "discover") + if r.code != 0 { + t.Fatalf("gate discover exited %d: %s %s", r.code, r.stdout, r.stderr) + } + if !strings.Contains(r.stdout, "Updated 1 gate(s)") { + t.Errorf("gate discover output = %q, want one updated gate", r.stdout) + } + if w := writesOf(counter.snapshot()); len(w) != 1 || !strings.HasPrefix(w[0], "PATCH /v0/beads/issues/") { + t.Errorf("gate discover wrote %v, want one updateIssue", w) + } + if g := showGate(t, f, gate); g.AwaitID != "4242" { + t.Errorf("await_id after discover = %q, want 4242", g.AwaitID) + } + }) + + t.Run("refused without the tokens", func(t *testing.T) { + if waiterGate == "" { + t.Skip("the advertised case created no gate") + } + maskedAddr, maskedCounter := startMaskingProxy(t, addr, withoutGateTokens) + m := newHTTPFixture(t, bin, maskedAddr) + target := m.create("masked target") + before := showGate(t, f, waiterGate) + + cases := []struct { + name string + args []string + token string + }{ + {"gate create", []string{"gate", "create", "--type=timer", "--timeout=1h", "--blocks", target}, "issues.create.gates"}, + {"gate create human", []string{"gate", "create", "--blocks", target}, "issues.create.gates"}, + {"gate add-waiter", []string{"gate", "add-waiter", waiterGate, "rig/workers/c"}, "issues.update.gates"}, + {"update --await-id", []string{"update", waiterGate, "--await-id", "999"}, "issues.update.gates"}, + } + for _, tc := range cases { + maskedCounter.reset() + r := m.run(nil, tc.args...) + if r.code == 0 { + t.Errorf("%s against a server without %s exited 0: %s", tc.name, tc.token, r.stdout) + } + if !strings.Contains(r.stderr, tc.token) { + t.Errorf("%s refusal does not name %s: %s", tc.name, tc.token, r.stderr) + } + if w := writesOf(maskedCounter.snapshot()); len(w) != 0 { + t.Errorf("a refused %s wrote %v; the refusal must come before any write", tc.name, w) + } + } + + // gate discover finds the run but cannot record it: the gate's error + // names the token, nothing is written, and the command fails rather + // than reporting the gate as updated. + runTarget := f.create("masked discover target") + runGate := firstJSONField(t, f.mustRun(nil, "gate", "create", "--type=gh:run", "--await-id=ci-wf", "--blocks", runTarget, "--json").stdout, "id") + maskedCounter.reset() + r := m.run(ghEnv, "gate", "discover") + if r.code == 0 { + t.Errorf("gate discover against a server without issues.update.gates exited 0: %s", r.stdout) + } + if !strings.Contains(r.stderr, "issues.update.gates") || !strings.Contains(r.stderr, runGate) { + t.Errorf("gate discover refusal does not name the gate and issues.update.gates: %s", r.stderr) + } + if strings.Contains(r.stdout, "Updated 1") { + t.Errorf("gate discover reported the refused gate as updated: %s", r.stdout) + } + if w := writesOf(maskedCounter.snapshot()); len(w) != 0 { + t.Errorf("a refused gate discover wrote %v", w) + } + if g := showGate(t, f, runGate); g.AwaitID != "ci-wf" { + t.Errorf("await_id after a refused discover = %q, want ci-wf untouched", g.AwaitID) + } + + if !readyIDs(t, m)[target] { + t.Errorf("%s left the ready set though every gate create was refused", target) + } + after := showGate(t, f, waiterGate) + if after.AwaitID != before.AwaitID || !slices.Equal(after.Waiters, before.Waiters) { + t.Errorf("refused writes changed the gate: before %+v after %+v", before, after) + } + }) +} diff --git a/cmd/bd/gate.go b/cmd/bd/gate.go index 47c069ed9..25fe25e0a 100644 --- a/cmd/bd/gate.go +++ b/cmd/bd/gate.go @@ -6,8 +6,10 @@ import ( "encoding/json" "errors" "fmt" + "math/rand/v2" "os" "os/exec" + "slices" "strings" "time" @@ -237,9 +239,6 @@ This is used by 'bd done --phase-complete' to register for gate wake notificatio SilenceUsage: true, SilenceErrors: true, RunE: func(cmd *cobra.Command, args []string) error { - if usesProxiedServer() { - return runGateAddWaiterProxiedServer(cmd, rootCtx, args) - } CheckReadonly("gate add-waiter") evt := metrics.NewCommandEvent("gate-add-waiter") @@ -251,34 +250,27 @@ This is used by 'bd done --phase-complete' to register for gate wake notificatio gateID := args[0] waiter := args[1] - ctx := rootCtx - var issue *types.Issue - var err error - - issue, err = store.GetIssue(ctx, gateID) + reader, err := openIssueReader() if err != nil { - return HandleError("gate not found: %s", gateID) + return HandleError("%v", err) + } + lifecycle, err := openIssueLifecycle() + if err != nil { + return HandleError("%v", err) + } + ctx, err := issueOpsContext(rootCtx) + if err != nil { + return HandleError("%v", err) } - if issue.IssueType != "gate" { - return HandleError("%s is not a gate issue (type=%s)", gateID, issue.IssueType) + added, err := addGateWaiter(ctx, reader, lifecycle, gateID, waiter) + if err != nil { + return HandleError("%v", err) } - - for _, w := range issue.Waiters { - if w == waiter { - renderGateWaiterAlready(gateID) - return nil - } - } - - newWaiters := append(issue.Waiters, waiter) - - updates := map[string]interface{}{ - "waiters": newWaiters, - } - if err := store.UpdateIssue(ctx, gateID, updates, currentActor()); err != nil { - return HandleError("updating gate: %v", err) + if !added { + renderGateWaiterAlready(gateID) + return nil } commandDidWrite.Store(true) @@ -288,6 +280,110 @@ This is used by 'bd done --phase-complete' to register for gate wake notificatio }, } +// addGateWaiter's compare-and-set budget. A version miss means another writer +// changed the gate between this command's read and its write; under a convoy +// (a phase finishing together, every worker registering on the same gate) each +// round of simultaneous writes has exactly one winner, so retrying at once +// would fail all but a handful of adders. Each retry therefore waits a +// jittered, doubling backoff first: gateAddWaiterBackoffBase, doubled per miss +// up to gateAddWaiterBackoffCap, each wait drawn uniformly from [d/2, d) so +// the losers of one round do not collide again in the next. +// +// The loop is bounded by gateAddWaiterMaxAttempts writes, not by wall time: +// one round's latency is the server's (a loaded remote server can take +// seconds per read and write), and a wall-clock budget would fail a convoy on +// a slow server after only two or three rounds. Each round has a winner, so +// the bound carries at least that many simultaneous adders; the jitter +// spreads them further. The waits themselves add up to at most about 6s, and +// a gate rewritten without pause still fails after the last attempt. +const ( + gateAddWaiterMaxAttempts = 20 + gateAddWaiterBackoffBase = 10 * time.Millisecond + gateAddWaiterBackoffCap = 400 * time.Millisecond +) + +// gateAddWaiterBackoff is the wait before retry number retry (1-based): +// jittered in [d/2, d) around d = base * 2^(retry-1), capped. +func gateAddWaiterBackoff(retry int) time.Duration { + d := gateAddWaiterBackoffBase + for i := 1; i < retry && d < gateAddWaiterBackoffCap; i++ { + d *= 2 + } + if d > gateAddWaiterBackoffCap { + d = gateAddWaiterBackoffCap + } + half := d / 2 + return half + time.Duration(rand.Int64N(int64(d-half))) // #nosec G404 -- retry jitter, not a secret +} + +// gateAddWaiterSleep waits d or until ctx is done. It is a variable so tests +// can record the backoff without sleeping. +var gateAddWaiterSleep = func(ctx context.Context, d time.Duration) error { + timer := time.NewTimer(d) + defer timer.Stop() + select { + case <-ctx.Done(): + return ctx.Err() + case <-timer.C: + return nil + } +} + +// addGateWaiter registers waiter on the gate gateID through the Reader and +// Lifecycle roles, so every route (local, proxied server, and a connected HTTP +// workspace, which sends getIssue then updateIssue behind issues.update.gates) +// runs the same steps. It reports false, writing nothing, when the waiter is +// already registered. +// +// The write is a whole-value waiter list guarded by the row version the read +// returned, so two concurrent adders cannot lose each other's waiter: the +// second write misses its version, re-reads the list with the first waiter in +// it, and writes again. The deduplication is this command's, not the role's; +// the role stores the list it is given. +func addGateWaiter(ctx context.Context, reader issueops.Reader, lifecycle issueops.Lifecycle, gateID, waiter string) (bool, error) { + var lastErr error + attempts := 0 + for attempts < gateAddWaiterMaxAttempts { + if attempts > 0 { + if err := gateAddWaiterSleep(ctx, gateAddWaiterBackoff(attempts)); err != nil { + return false, fmt.Errorf("updating gate: %w", err) + } + } + attempts++ + details, err := reader.Get(ctx, issueops.GetRequest{ID: gateID}) + if errors.Is(err, storage.ErrNotFound) { + return false, fmt.Errorf("gate not found: %s", gateID) + } + if err != nil { + return false, fmt.Errorf("reading gate %s: %w", gateID, err) + } + gate := &details.Issue + if gate.IssueType != "gate" { + return false, fmt.Errorf("%s is not a gate issue (type=%s)", gateID, gate.IssueType) + } + if slices.Contains(gate.Waiters, waiter) { + return false, nil + } + + waiters := append(slices.Clone(gate.Waiters), waiter) + version := gate.RowVersion + _, err = lifecycle.Update(ctx, issueops.UpdateRequest{ + Actor: currentActor(), + IssueID: gate.ID, + Patch: issueops.IssuePatch{Waiters: issueops.Field[[]string]{Set: true, Value: waiters}}, + ExpectedVersion: &version, + }) + if err == nil { + return true, nil + } + if !errors.Is(err, issueops.ErrVersionMismatch) { + return false, fmt.Errorf("updating gate: %w", err) + } + lastErr = err + } + return false, fmt.Errorf("updating gate: %s changed on every one of %d attempts: %w", gateID, attempts, lastErr) +} + // renderGateWaiterAlready and renderGateWaiterAdded are shared by the direct // and proxied-server routes so `bd gate add-waiter` prints identically on both. func renderGateWaiterAlready(gateID string) { @@ -326,9 +422,6 @@ Examples: SilenceUsage: true, SilenceErrors: true, RunE: func(cmd *cobra.Command, args []string) error { - if usesProxiedServer() { - return runGateCreateProxiedServer(cmd, rootCtx) - } CheckReadonly("gate create") evt := metrics.NewCommandEvent("gate-create") @@ -343,37 +436,25 @@ Examples: return HandleErrorRespectJSON("%v", err) } - ctx := rootCtx - - targetIssue, err := store.GetIssue(ctx, in.blocksID) + reader, err := openIssueReader() if err != nil { - return HandleErrorRespectJSON("issue not found: %s", in.blocksID) + return HandleErrorRespectJSON("%v", err) + } + lifecycle, err := openIssueLifecycle() + if err != nil { + return HandleErrorRespectJSON("%v", err) + } + ctx, err := issueOpsContext(rootCtx) + if err != nil { + return HandleErrorRespectJSON("%v", err) } - gate := buildGateIssue(in, targetIssue.ID) - metadata, metaErr := gateMetadataForCreate(in, targetIssue) - if metaErr != nil { - return HandleErrorRespectJSON("%v", metaErr) - } - gate.Metadata = metadata - - if err := store.CreateIssue(ctx, gate, currentActor()); err != nil { - return HandleErrorRespectJSON("creating gate: %v", err) + gate, targetIssue, err := createGateThroughRoles(ctx, reader, lifecycle, in) + if err != nil { + return HandleErrorRespectJSON("%v", err) } - dep := &types.Dependency{ - IssueID: targetIssue.ID, - DependsOnID: gate.ID, - Type: types.DepBlocks, - } - if err := store.AddDependency(ctx, dep, currentActor()); err != nil { - return HandleErrorRespectJSON("adding blocking dependency: %v", err) - } - - commitMsg := fmt.Sprintf("bd: create gate %s blocking %s", gate.ID, targetIssue.ID) - if err := store.Commit(ctx, commitMsg); err != nil && !isDoltNothingToCommit(err) { - return HandleErrorRespectJSON("failed to commit: %v", err) - } + commandDidWrite.Store(true) if jsonOutput { return outputJSON(gate) @@ -384,6 +465,48 @@ Examples: }, } +// createGateThroughRoles creates the gate and its blocking edge as ONE Lifecycle.Create: +// the gate row plus a reverse blocks dependency from the blocked issue to the +// new gate, in one transaction and one version commit on every route. A +// connected HTTP workspace sends it as one createIssue carrying the gate +// members, behind issues.create.gates; a server that does not advertise that +// token is refused before any request is sent, and nothing is written. +// +// It returns the created gate (without its dependency records, which +// `bd gate create` has never printed) and the blocked issue. +func createGateThroughRoles(ctx context.Context, reader issueops.Reader, lifecycle issueops.Lifecycle, in gateCreateInput) (*types.Issue, *types.Issue, error) { + details, err := reader.Get(ctx, issueops.GetRequest{ID: in.blocksID}) + if err != nil { + // Every target-lookup failure has always been reported as not-found. + return nil, nil, fmt.Errorf("issue not found: %s", in.blocksID) + } + target := &details.Issue + + gate := buildGateIssue(in, target.ID) + metadata, err := gateMetadataForCreate(in, target) + if err != nil { + return nil, nil, err + } + gate.Metadata = metadata + + result, err := lifecycle.Create(ctx, issueops.CreateRequest{ + Actor: currentActor(), + Issue: gate, + Dependencies: []issueops.CreateDependency{{ + TargetID: target.ID, + Type: types.DepBlocks, + Reverse: true, + }}, + }) + if err != nil { + return nil, nil, fmt.Errorf("creating gate: %w", err) + } + created := result.Issue + created.Dependencies = nil + created.Comments = nil + return created, target, nil +} + // gateCreateInput carries `bd gate create`'s parsed flags. Both routes gather // it through gatherGateCreateInput so they cannot drift on flag semantics. type gateCreateInput struct { diff --git a/cmd/bd/gate_discover.go b/cmd/bd/gate_discover.go index b1842e1de..1344107cb 100644 --- a/cmd/bd/gate_discover.go +++ b/cmd/bd/gate_discover.go @@ -17,6 +17,7 @@ import ( "github.com/steveyegge/beads/internal/metrics" "github.com/steveyegge/beads/internal/types" "github.com/steveyegge/beads/internal/ui" + "github.com/steveyegge/beads/issueops" ) // GHWorkflowRun represents a GitHub workflow run from `gh run list --json` @@ -91,7 +92,7 @@ func runGateDiscover(cmd *cobra.Command, args []string) error { ctx := rootCtx - gates, err := findPendingGates() + gates, err := findPendingGates(ctx) if err != nil { return HandleError("finding gates: %v", err) } @@ -124,7 +125,11 @@ func runGateDiscover(cmd *cobra.Command, args []string) error { }) // Step 3/4: report matches (and, outside dry-run, persist them) + // matchCount counts the gates whose run ID was recorded (or, under + // --dry-run, would be); updateFailures the matched gates whose write + // failed, which fail the command after the summary. matchCount := 0 + updateFailures := 0 for _, m := range matches { if m.err != nil { fmt.Fprintf(os.Stderr, " %s %s - %v\n", @@ -140,20 +145,22 @@ func runGateDiscover(cmd *cobra.Command, args []string) error { continue } - matchCount++ runIDStr := strconv.FormatInt(m.run.DatabaseID, 10) if dryRun { + matchCount++ fmt.Printf(" %s %s → run %s (%s) [dry-run]\n", ui.RenderPass("✓"), ui.RenderID(m.gate.ID), runIDStr, m.run.Status) continue } if err := updateGateAwaitID(ctx, m.gate.ID, runIDStr); err != nil { + updateFailures++ fmt.Fprintf(os.Stderr, " %s %s - update failed: %v\n", ui.RenderFail("✗"), ui.RenderID(m.gate.ID), err) continue } + matchCount++ fmt.Printf(" %s %s → run %s (%s)\n", ui.RenderPass("✓"), ui.RenderID(m.gate.ID), runIDStr, m.run.Status) @@ -187,6 +194,9 @@ func runGateDiscover(cmd *cobra.Command, args []string) error { } return HandleError("querying GitHub runs failed for %d repo(s): %s", len(failures), strings.Join(details, "; ")) } + if updateFailures > 0 { + return HandleError("recording the discovered run ID failed for %d gate(s)", updateFailures) + } return nil } @@ -374,21 +384,32 @@ func workflowNameMatches(hint, workflowName, runName string) bool { // findPendingGates returns open gh:run gates that need run ID discovery. // This includes gates with empty AwaitID OR non-numeric AwaitID (workflow name hint). -func findPendingGates() ([]*types.Issue, error) { - var gates []*types.Issue - - gateType := types.IssueType("gate") - filter := types.IssueFilter{ - IssueType: &gateType, - ExcludeStatus: []types.Status{types.StatusClosed}, +// +// The open gates come from issueops.ListGates over the route's Querier, the +// entry `bd gate list` and `bd gate check` use, so a connected HTTP workspace +// answers it with queryIssues rather than refusing the raw storage filter. +func findPendingGates(ctx context.Context) ([]*types.Issue, error) { + querier, err := openQuerier() + if err != nil { + return nil, err } + return findPendingGatesWith(ctx, querier) +} - allGates, err := store.SearchIssues(rootCtx, "", filter) +// findPendingGatesWith is findPendingGates over a given Querier. Template +// gates are skipped: a template is read-only (work is poured from it), so its +// await_id is never a run to discover. +func findPendingGatesWith(ctx context.Context, querier issueops.Querier) ([]*types.Issue, error) { + allGates, err := issueops.ListGates(ctx, querier, issueops.GateListRequest{}) if err != nil { return nil, fmt.Errorf("search gates: %w", err) } + var gates []*types.Issue for _, g := range allGates { + if g.IsTemplate { + continue + } if needsDiscovery(g) { gates = append(gates, g) } @@ -576,13 +597,11 @@ func matchGateToRun(gate *types.Issue, runs []GHWorkflowRun, maxAge time.Duratio return nil } -// updateGateAwaitID updates a gate's await_id field +// updateGateAwaitID records a discovered run ID as a gate's await_id through +// the Lifecycle role (persistGateCheckAwaitID), the write `bd gate check` and +// `bd close` make for the same discovery. A connected HTTP workspace sends it +// as updateIssue behind issues.update.gates, and a server that does not +// advertise that token refuses it before any request is sent. func updateGateAwaitID(_ interface{}, gateID, runID string) error { - updates := map[string]interface{}{ - "await_id": runID, - } - if err := store.UpdateIssue(rootCtx, gateID, updates, currentActor()); err != nil { - return err - } - return nil + return persistGateCheckAwaitID(rootCtx, gateID, runID) } diff --git a/cmd/bd/gate_frontdoor_parity_integration_test.go b/cmd/bd/gate_frontdoor_parity_integration_test.go index 1f251755f..96317fc66 100644 --- a/cmd/bd/gate_frontdoor_parity_integration_test.go +++ b/cmd/bd/gate_frontdoor_parity_integration_test.go @@ -4,8 +4,11 @@ package main import ( "encoding/json" + "fmt" "os/exec" + "slices" "strings" + "sync" "testing" ) @@ -124,3 +127,75 @@ func TestProxiedServerGateFrontDoorDirectServerProxyParity(t *testing.T) { } } } + +// TestProxiedServerGateAddWaiterConvoy registers ten waiters (plus one +// duplicate) on one gate at once, on the direct SQL route and the proxied +// route against a real Dolt server: every adder must exit 0 and every waiter +// must be on the gate exactly once. add-waiter is a guarded whole-list write, +// so each round of simultaneous writes has one winner; this pins the retry's +// backoff and budget, which must carry a convoy finishing a phase together. +func TestProxiedServerGateAddWaiterConvoy(t *testing.T) { + requireSharedProxiedServer(t) + bd := buildEmbeddedBD(t) + directProject := newServerModeProject(t, bd, "cv") + proxiedProject := newSharedProxiedProject(t, bd, "cv") + modes := []gateFrontDoorRunner{ + {name: "direct-sql", dir: directProject.dir, env: func(string) []string { return directProject.env }}, + {name: "proxied", dir: proxiedProject.dir, env: bdProxiedEnv}, + } + const adders = 10 + for _, r := range modes { + r.mustRun(t, bd, "create", "Convoy target", "--id", "cv-target") + gateID := parseCreatedGateID(t, r.mustRun(t, bd, "gate", "create", "--blocks", "cv-target")) + + type result struct { + stdout, stderr string + err error + } + results := make([]result, adders+1) + var wg sync.WaitGroup + start := make(chan struct{}) + for i := 0; i <= adders; i++ { + waiter := fmt.Sprintf("rig/workers/c%d", i%adders) // the last one repeats c0 + wg.Add(1) + go func(i int) { + defer wg.Done() + <-start + cmd := exec.Command(bd, "gate", "add-waiter", gateID, waiter) + cmd.Dir = r.dir + cmd.Env = r.env(r.dir) + var stdout, stderr strings.Builder + cmd.Stdout, cmd.Stderr = &stdout, &stderr + err := cmd.Run() + results[i] = result{stdout.String(), stderr.String(), err} + }(i) + } + close(start) + wg.Wait() + for i, res := range results { + if res.err != nil { + t.Errorf("[%s] adder %d failed: %v\nstderr:\n%s", r.name, i, res.err, res.stderr) + } + } + + var shown map[string]any + out := r.mustRun(t, bd, "gate", "show", gateID, "--json") + if err := json.Unmarshal([]byte(strings.TrimSpace(out)), &shown); err != nil { + t.Fatalf("[%s] parse gate show --json: %v\n%s", r.name, err, out) + } + raw, _ := shown["waiters"].([]any) + got := make([]string, 0, len(raw)) + for _, w := range raw { + got = append(got, fmt.Sprint(w)) + } + want := make([]string, 0, adders) + for i := 0; i < adders; i++ { + want = append(want, fmt.Sprintf("rig/workers/c%d", i)) + } + slices.Sort(got) + slices.Sort(want) + if !slices.Equal(got, want) { + t.Errorf("[%s] waiters after %d concurrent adders = %v, want each exactly once: %v", r.name, adders+1, got, want) + } + } +} diff --git a/cmd/bd/gate_proxied_server.go b/cmd/bd/gate_proxied_server.go index 15d5a133b..0821180c6 100644 --- a/cmd/bd/gate_proxied_server.go +++ b/cmd/bd/gate_proxied_server.go @@ -268,152 +268,6 @@ func runGateShowProxiedServer(_ *cobra.Command, ctx context.Context, args []stri return nil } -type gateAddWaiterApply struct { - already bool - after *types.Issue -} - -func runGateAddWaiterProxiedServer(_ *cobra.Command, ctx context.Context, args []string) error { - CheckReadonly("gate add-waiter") - - evt := metrics.NewCommandEvent("gate-add-waiter") - defer func() { - if c := metrics.Global(); c != nil { - c.CloseEventAndAdd(evt) - } - }() - - gateID := args[0] - waiter := args[1] - - if uowProvider == nil { - return HandleError("proxied-server UOW provider not initialized") - } - - applied, err := uow.RunTxResult(ctx, uowProvider, func(ctx context.Context, uw uow.UnitOfWork) (gateAddWaiterApply, string, error) { - var out gateAddWaiterApply - - issue, err := uw.IssueUseCase().GetIssue(ctx, gateID) - if gateProxiedNotFound(err) { - return out, "", fmt.Errorf("gate not found: %s", gateID) - } - if err != nil { - return out, "", fmt.Errorf("reading gate %s: %w", gateID, err) - } - if issue.IssueType != "gate" { - return out, "", fmt.Errorf("%s is not a gate issue (type=%s)", gateID, issue.IssueType) - } - - for _, w := range issue.Waiters { - if w == waiter { - out.already = true - // Empty commit message: a registered waiter is a no-op, and a - // no-op writes no Dolt commit. - return out, "", nil - } - } - - newWaiters := append(issue.Waiters, waiter) - if err := uw.IssueUseCase().UpdateIssue(ctx, gateID, map[string]any{"waiters": newWaiters}, currentActor()); err != nil { - return out, "", fmt.Errorf("updating gate: %w", err) - } - if after, getErr := uw.IssueUseCase().GetIssue(ctx, gateID); getErr == nil { - out.after = after - } - return out, fmt.Sprintf("bd: gate add-waiter %s", gateID), nil - }) - if err != nil { - return HandleError("%v", err) - } - - if applied.already { - renderGateWaiterAlready(gateID) - return nil - } - - commandDidWrite.Store(true) - - renderGateWaiterAdded(gateID, waiter) - return nil -} - -type gateCreateApply struct { - gate *types.Issue - target *types.Issue -} - -func runGateCreateProxiedServer(cmd *cobra.Command, ctx context.Context) error { - CheckReadonly("gate create") - - evt := metrics.NewCommandEvent("gate-create") - defer func() { - if c := metrics.Global(); c != nil { - c.CloseEventAndAdd(evt) - } - }() - - in, err := gatherGateCreateInput(cmd) - if err != nil { - return HandleErrorRespectJSON("%v", err) - } - - if uowProvider == nil { - return HandleError("proxied-server UOW provider not initialized") - } - - // One transaction, one Dolt commit for the whole invocation: the direct - // route's create + add-dependency + explicit store.Commit collapse into a - // single unit of work carrying the same commit message. Semantically - // equivalent, minus the window where the gate exists without its edge. - applied, err := uow.RunTxResult(ctx, uowProvider, func(ctx context.Context, uw uow.UnitOfWork) (gateCreateApply, string, error) { - var out gateCreateApply - - target, err := uw.IssueUseCase().GetIssue(ctx, in.blocksID) - if err != nil { - // The direct route reports every target-lookup failure as - // not-found; keep that message for parity. - return out, "", fmt.Errorf("issue not found: %s", in.blocksID) - } - - gate := buildGateIssue(in, target.ID) - metadata, metaErr := gateMetadataForCreate(in, target) - if metaErr != nil { - return out, "", metaErr - } - gate.Metadata = metadata - - res, err := uw.IssueUseCase().CreateIssue(ctx, domain.CreateIssueParams{Issue: gate}, currentActor()) - if err != nil { - return out, "", fmt.Errorf("creating gate: %w", err) - } - - dep := &types.Dependency{ - IssueID: target.ID, - DependsOnID: res.Issue.ID, - Type: types.DepBlocks, - } - if err := uw.DependencyUseCase().AddDependency(ctx, dep, currentActor()); err != nil { - return out, "", fmt.Errorf("adding blocking dependency: %w", err) - } - - out.gate = res.Issue - out.target = target - return out, fmt.Sprintf("bd: create gate %s blocking %s", res.Issue.ID, target.ID), nil - }) - if err != nil { - return HandleErrorRespectJSON("%v", err) - } - - commandDidWrite.Store(true) - - if jsonOutput { - return outputJSON(applied.gate) - } - - renderGateCreated(applied.gate, applied.target, in) - return nil -} - type gateResolveApply struct { before *types.Issue after *types.Issue diff --git a/cmd/bd/gate_test.go b/cmd/bd/gate_test.go index 980906b9f..06bcee406 100644 --- a/cmd/bd/gate_test.go +++ b/cmd/bd/gate_test.go @@ -2505,3 +2505,356 @@ func TestGateCheckAwaitIDPassesOtherRefusalsThrough(t *testing.T) { }) } } + +// fakeGateRoles is the Reader and Lifecycle `bd gate create` and +// `bd gate add-waiter` run over. Its Update honors ExpectedVersion the way +// every backend does, and can simulate concurrent adders: each of the first +// `concurrent` updates finds that another writer appended its waiter (and +// reminted the row version) after the command's read. +type fakeGateRoles struct { + issueops.Lifecycle + issues map[string]*types.Issue + creates []issueops.CreateRequest + updates []issueops.UpdateRequest + createErr error + updateErr error + concurrent []string +} + +func newFakeGateRoles(issues ...*types.Issue) *fakeGateRoles { + f := &fakeGateRoles{issues: map[string]*types.Issue{}} + for _, issue := range issues { + f.issues[issue.ID] = issue + } + return f +} + +func (f *fakeGateRoles) Ready(context.Context, issueops.ReadyRequest) (issueops.IssuePage, error) { + panic("unused") +} + +func (f *fakeGateRoles) List(context.Context, issueops.ListRequest) (issueops.IssuePage, error) { + panic("unused") +} + +func (f *fakeGateRoles) Get(_ context.Context, req issueops.GetRequest) (*issueops.IssueDetails, error) { + issue, ok := f.issues[req.ID] + if !ok { + return nil, storage.ErrNotFound + } + copied := *issue + copied.Waiters = slices.Clone(issue.Waiters) + return &issueops.IssueDetails{Issue: copied}, nil +} + +func (f *fakeGateRoles) Create(_ context.Context, req issueops.CreateRequest) (issueops.CreateResult, error) { + f.creates = append(f.creates, req) + if f.createErr != nil { + return issueops.CreateResult{}, f.createErr + } + created := *req.Issue + created.ID = "bd-gate1" + created.Dependencies = []*types.Dependency{{IssueID: req.Dependencies[0].TargetID, DependsOnID: created.ID, Type: types.DepBlocks}} + f.issues[created.ID] = &created + return issueops.CreateResult{Issue: &created}, nil +} + +func (f *fakeGateRoles) Update(_ context.Context, req issueops.UpdateRequest) (issueops.UpdateResult, error) { + f.updates = append(f.updates, req) + if f.updateErr != nil { + return issueops.UpdateResult{}, f.updateErr + } + issue := f.issues[req.IssueID] + if len(f.concurrent) > 0 { + issue.Waiters = append(issue.Waiters, f.concurrent[0]) + issue.RowVersion++ + f.concurrent = f.concurrent[1:] + } + if req.ExpectedVersion != nil && *req.ExpectedVersion != issue.RowVersion { + return issueops.UpdateResult{}, issueops.ErrVersionMismatch + } + if req.Patch.Waiters.Set { + issue.Waiters = slices.Clone(req.Patch.Waiters.Value) + } + issue.RowVersion++ + return issueops.UpdateResult{Issue: issue}, nil +} + +func setGateTestActor(t *testing.T) { + t.Helper() + origActor := actor + t.Cleanup(func() { actor = origActor }) + actor = "test-actor" +} + +// TestCreateGateIsOneCreateWithAReverseBlocksEdge: `bd gate create` is ONE +// Lifecycle.Create carrying the gate members and the blocks edge from the +// blocked issue to the new gate, so the gate and its edge land atomically on +// every route (a connected HTTP workspace sends one createIssue). +func TestCreateGateIsOneCreateWithAReverseBlocksEdge(t *testing.T) { + setGateTestActor(t) + roles := newFakeGateRoles(&types.Issue{ID: "bd-target", Title: "blocked", IssueType: types.TypeTask}) + + in := gateCreateInput{blocksID: "bd-target", gateType: "timer", timeout: 90 * time.Minute, reason: "cool down"} + gate, target, err := createGateThroughRoles(context.Background(), roles, roles, in) + if err != nil { + t.Fatalf("createGateThroughRoles: %v", err) + } + if len(roles.creates) != 1 || len(roles.updates) != 0 { + t.Fatalf("createGateThroughRoles made %d creates and %d updates, want exactly one create", len(roles.creates), len(roles.updates)) + } + req := roles.creates[0] + wantDeps := []issueops.CreateDependency{{TargetID: "bd-target", Type: types.DepBlocks, Reverse: true}} + if !reflect.DeepEqual(req.Dependencies, wantDeps) { + t.Fatalf("create dependencies = %+v, want %+v", req.Dependencies, wantDeps) + } + if req.Actor != "test-actor" { + t.Errorf("create actor = %q, want test-actor", req.Actor) + } + if req.Issue.IssueType != "gate" || req.Issue.AwaitType != "timer" || req.Issue.Timeout != 90*time.Minute { + t.Errorf("created gate = type %q await_type %q timeout %s, want a 90m timer gate", req.Issue.IssueType, req.Issue.AwaitType, req.Issue.Timeout) + } + if !strings.Contains(req.Issue.Description, "cool down") { + t.Errorf("gate description %q does not carry the reason", req.Issue.Description) + } + if gate.ID != "bd-gate1" || target.ID != "bd-target" { + t.Errorf("createGateThroughRoles returned gate %q target %q", gate.ID, target.ID) + } + if gate.Dependencies != nil { + t.Errorf("createGateThroughRoles returned the gate with dependency records %v; bd gate create never printed them", gate.Dependencies) + } +} + +// TestCreateGateRefusalWritesNothing: a refused create (a server that does not +// advertise issues.create.gates) surfaces the refusal naming the token, and a +// missing blocked issue never reaches the create. +func TestCreateGateRefusalWritesNothing(t *testing.T) { + setGateTestActor(t) + + t.Run("token refused", func(t *testing.T) { + roles := newFakeGateRoles(&types.Issue{ID: "bd-target", IssueType: types.TypeTask}) + roles.createErr = &storage.ErrUnsupported{Op: "createIssue", Backend: "http", Capability: "issues.create.gates"} + _, _, err := createGateThroughRoles(context.Background(), roles, roles, gateCreateInput{blocksID: "bd-target", gateType: "timer"}) + var unsupported *storage.ErrUnsupported + if !errors.As(err, &unsupported) || !strings.Contains(err.Error(), "issues.create.gates") { + t.Fatalf("createGateThroughRoles error = %v, want the refusal naming issues.create.gates", err) + } + if _, ok := roles.issues["bd-gate1"]; ok || len(roles.updates) != 0 { + t.Fatalf("a refused create wrote something") + } + }) + + t.Run("missing target", func(t *testing.T) { + roles := newFakeGateRoles() + _, _, err := createGateThroughRoles(context.Background(), roles, roles, gateCreateInput{blocksID: "bd-absent", gateType: "human"}) + if err == nil || err.Error() != "issue not found: bd-absent" { + t.Fatalf("createGateThroughRoles error = %v, want issue not found: bd-absent", err) + } + if len(roles.creates) != 0 { + t.Fatalf("createGateThroughRoles created a gate for a missing target") + } + }) +} + +// TestAddGateWaiterIsAGuardedWholeListUpdate: the waiter list is written whole, +// guarded by the version the read returned. A concurrent adder makes the write +// miss; the retry re-reads (keeping the other waiter) and re-deduplicates. +func TestAddGateWaiterIsAGuardedWholeListUpdate(t *testing.T) { + setGateTestActor(t) + origSleep := gateAddWaiterSleep + t.Cleanup(func() { gateAddWaiterSleep = origSleep }) + var sleeps []time.Duration + gateAddWaiterSleep = func(_ context.Context, d time.Duration) error { + sleeps = append(sleeps, d) + return nil + } + gate := func() *types.Issue { + return &types.Issue{ID: "bd-g", IssueType: "gate", Waiters: []string{"a"}, RowVersion: 7} + } + + t.Run("first write", func(t *testing.T) { + roles := newFakeGateRoles(gate()) + added, err := addGateWaiter(context.Background(), roles, roles, "bd-g", "b") + if err != nil || !added { + t.Fatalf("addGateWaiter = %v, %v; want added", added, err) + } + if len(roles.updates) != 1 { + t.Fatalf("updates = %d, want 1", len(roles.updates)) + } + req := roles.updates[0] + if req.ExpectedVersion == nil || *req.ExpectedVersion != 7 { + t.Errorf("update not guarded by the read's version 7: %v", req.ExpectedVersion) + } + want := issueops.IssuePatch{Waiters: issueops.Field[[]string]{Set: true, Value: []string{"a", "b"}}} + if !reflect.DeepEqual(req.Patch, want) { + t.Errorf("patch = %+v, want only the whole waiter list [a b]", req.Patch) + } + }) + + t.Run("concurrent adder retries", func(t *testing.T) { + roles := newFakeGateRoles(gate()) + roles.concurrent = []string{"x"} + added, err := addGateWaiter(context.Background(), roles, roles, "bd-g", "b") + if err != nil || !added { + t.Fatalf("addGateWaiter = %v, %v; want added after one retry", added, err) + } + if len(roles.updates) != 2 { + t.Fatalf("updates = %d, want the missed write and its retry", len(roles.updates)) + } + if got := roles.issues["bd-g"].Waiters; !reflect.DeepEqual(got, []string{"a", "x", "b"}) { + t.Errorf("waiters = %v, want [a x b]: the concurrent waiter must survive", got) + } + }) + + t.Run("concurrent adder of the same waiter", func(t *testing.T) { + roles := newFakeGateRoles(gate()) + roles.concurrent = []string{"b"} + added, err := addGateWaiter(context.Background(), roles, roles, "bd-g", "b") + if err != nil || added { + t.Fatalf("addGateWaiter = %v, %v; want already registered after the re-read", added, err) + } + if got := roles.issues["bd-g"].Waiters; !reflect.DeepEqual(got, []string{"a", "b"}) { + t.Errorf("waiters = %v, want [a b]", got) + } + }) + + t.Run("already registered writes nothing", func(t *testing.T) { + roles := newFakeGateRoles(gate()) + added, err := addGateWaiter(context.Background(), roles, roles, "bd-g", "a") + if err != nil || added || len(roles.updates) != 0 { + t.Fatalf("addGateWaiter = %v, %v with %d updates; want a no-op", added, err, len(roles.updates)) + } + }) + + t.Run("backs off between attempts", func(t *testing.T) { + sleeps = nil + roles := newFakeGateRoles(gate()) + roles.concurrent = []string{"x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9"} + added, err := addGateWaiter(context.Background(), roles, roles, "bd-g", "b") + if err != nil || !added { + t.Fatalf("addGateWaiter = %v, %v; want added once the nine concurrent writers are done", added, err) + } + if len(roles.updates) != 10 || len(sleeps) != 9 { + t.Fatalf("updates = %d, sleeps = %d; want 10 writes with a backoff before each retry", len(roles.updates), len(sleeps)) + } + want := gateAddWaiterBackoffBase + for i, d := range sleeps { + if d < want/2 || d >= want { + t.Errorf("backoff %d = %s, want in [%s, %s)", i+1, d, want/2, want) + } + if want*2 <= gateAddWaiterBackoffCap { + want *= 2 + } else { + want = gateAddWaiterBackoffCap + } + } + }) + + t.Run("gives up after the attempt bound", func(t *testing.T) { + sleeps = nil + roles := newFakeGateRoles(gate()) + for i := 0; i <= gateAddWaiterMaxAttempts; i++ { + roles.concurrent = append(roles.concurrent, fmt.Sprintf("x%d", i)) + } + _, err := addGateWaiter(context.Background(), roles, roles, "bd-g", "b") + if !errors.Is(err, issueops.ErrVersionMismatch) { + t.Fatalf("addGateWaiter error = %v, want the version mismatch", err) + } + if len(roles.updates) != gateAddWaiterMaxAttempts { + t.Fatalf("updates = %d, want %d attempts", len(roles.updates), gateAddWaiterMaxAttempts) + } + }) + + t.Run("a cancelled wait stops the loop", func(t *testing.T) { + roles := newFakeGateRoles(gate()) + roles.concurrent = []string{"x1", "x2"} + gateAddWaiterSleep = func(context.Context, time.Duration) error { return context.Canceled } + t.Cleanup(func() { + gateAddWaiterSleep = func(_ context.Context, d time.Duration) error { + sleeps = append(sleeps, d) + return nil + } + }) + _, err := addGateWaiter(context.Background(), roles, roles, "bd-g", "b") + if !errors.Is(err, context.Canceled) || len(roles.updates) != 1 { + t.Fatalf("addGateWaiter = %v after %d updates, want the cancellation after one write", err, len(roles.updates)) + } + }) + + t.Run("refusal is not retried", func(t *testing.T) { + roles := newFakeGateRoles(gate()) + roles.updateErr = &storage.ErrUnsupported{Op: "updateIssue", Backend: "http", Capability: "issues.update.gates"} + _, err := addGateWaiter(context.Background(), roles, roles, "bd-g", "b") + if err == nil || !strings.Contains(err.Error(), "issues.update.gates") || len(roles.updates) != 1 { + t.Fatalf("addGateWaiter error = %v after %d updates, want one refused update naming issues.update.gates", err, len(roles.updates)) + } + }) + + t.Run("not a gate", func(t *testing.T) { + roles := newFakeGateRoles(&types.Issue{ID: "bd-t", IssueType: types.TypeTask}) + _, err := addGateWaiter(context.Background(), roles, roles, "bd-t", "b") + if err == nil || !strings.Contains(err.Error(), "is not a gate issue") { + t.Fatalf("addGateWaiter error = %v, want the not-a-gate refusal", err) + } + }) + + t.Run("missing gate", func(t *testing.T) { + roles := newFakeGateRoles() + _, err := addGateWaiter(context.Background(), roles, roles, "bd-none", "b") + if err == nil || err.Error() != "gate not found: bd-none" { + t.Fatalf("addGateWaiter error = %v, want gate not found: bd-none", err) + } + }) +} + +// TestFindPendingGatesListsThroughListGates: gate discover lists the open gates +// through issueops.ListGates (queryIssues over HTTP), never the raw storage +// filter, and skips templates and gates that already hold a run ID. +func TestFindPendingGatesListsThroughListGates(t *testing.T) { + fake := &fakeGateCheckStore{issues: []*types.Issue{ + {ID: "bd-hint", IssueType: "gate", AwaitType: "gh:run", AwaitID: "release.yml"}, + {ID: "bd-empty", IssueType: "gate", AwaitType: "gh:run"}, + {ID: "bd-numeric", IssueType: "gate", AwaitType: "gh:run", AwaitID: "12345"}, + {ID: "bd-template", IssueType: "gate", AwaitType: "gh:run", IsTemplate: true}, + {ID: "bd-timer", IssueType: "gate", AwaitType: "timer"}, + }} + gates, err := findPendingGatesWith(context.Background(), fake) + if err != nil { + t.Fatalf("findPendingGatesWith: %v", err) + } + var ids []string + for _, g := range gates { + ids = append(ids, g.ID) + } + if !reflect.DeepEqual(ids, []string{"bd-hint", "bd-empty"}) { + t.Fatalf("pending gates = %v, want [bd-hint bd-empty]", ids) + } + if fake.searchCalled { + t.Fatal("gate discover used the raw SearchIssues filter") + } + if len(fake.queries) != 1 || fake.queries[0].Expression != issueops.GateListExpression { + t.Fatalf("queries = %+v, want one ListGates query", fake.queries) + } + if fake.queries[0].IncludeClosed { + t.Fatal("gate discover listed closed gates; only open gates await a run") + } +} + +// TestUpdateGateAwaitIDGoesThroughTheLifecycleRole: gate discover's and +// bd close's await_id persist is the Lifecycle patch gate check makes, never +// the legacy map-based UpdateIssue the HTTP store does not serve. +func TestUpdateGateAwaitIDGoesThroughTheLifecycleRole(t *testing.T) { + origStore := store + t.Cleanup(func() { store = origStore }) + setGateTestActor(t) + fake := &fakeGateCheckStore{} + store = fake + + if err := updateGateAwaitID(nil, "bd-run", "4242"); err != nil { + t.Fatalf("updateGateAwaitID: %v", err) + } + want := issueops.IssuePatch{AwaitID: issueops.Field[string]{Set: true, Value: "4242"}} + if len(fake.updates) != 1 || fake.updates[0].IssueID != "bd-run" || !reflect.DeepEqual(fake.updates[0].Patch, want) { + t.Fatalf("updates = %+v, want one AwaitID=4242 patch on bd-run", fake.updates) + } +}