fix(http): gate check records a bead gate's sighting through Lifecycle.Update (#7604)

* fix(http): gate check's bead sighting goes through Lifecycle.Update (issues.update)

The bead-gate sighting was written through the legacy map-based
UpdateIssue, which the http store does not implement, so the refusal
happened client-side and no issues.update was ever sent: every open bead
gate with an open target failed on every sweep over HTTP.

- recordSeen and the gh:run await_id persist now go through the Lifecycle
  role (updateGateThroughLifecycle): a single-key metadata set for the
  sighting, an AwaitID patch for the run. The http client still refuses the
  await_id patch before the dial (W-IssuePatch.AwaitID); the gate's error now
  names the await_id update instead of a Go method.
- backend/http/corpus TestServedGateCheckOverAwaitTypedGates seeds
  await-typed gates in the served store and proves, through the reference
  proxy: --dry-run writes nothing, timers close, the bead sighting persists,
  merged gh:pr gates close, and the gh:run persist refuses by name with
  await_id untouched.
- CHANGELOG corrected.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(cmd/bd): gate check skips template gates; narrow the await_id reword

Review fixes for the gate-check sighting change.

- A persisted proto's gates (bd cook --persist) are templates, and
  issueops.ListGates lists them with the open gates. bd gate check now
  leaves them out of its listing's query: GateListRequest gains
  ExcludeTemplates, which only listOpenGatesWith sets, and it adds
  `template=false` to the expression, so the cut at --limit counts only
  the gates that remain. bd gate list leaves it unset and still lists
  templates. Dropping them from the page after the cut would let a page
  of a proto's gates (newer than the live ones at the same priority)
  fill --limit: the sweep would print "No open gates found." and exit 0
  with the live gates unchecked. filterCheckableGates, which both the
  direct and the proxied-server routes select through, keeps the skip
  as a backstop. Before this, each route mishandled templates in its
  own way:
  - Direct route: the Lifecycle role refused the sighting and await_id
    writes, so the sweep failed every time, after a gh:run template had
    already queried GitHub. In a local workspace, an expired template
    timer gate was closed in the proto.
  - Over HTTP: bd serve refused the close with 409 template_read_only.
  - Proxied-server route: the template timer gate was closed and a
    sighting was stamped on the template bead gate, silently.
- persistGateCheckAwaitID rewords only the HTTP wire's
  W-IssuePatch.AwaitID refusal. The new message names the run, await_id
  and the bd serve URL, and advises checking the gate in a local
  workspace: giving it a numeric run ID would be the same await_id edit
  the wire refuses. The error chain is kept, so *storage.ErrUnsupported
  and the ledger row stay reachable. Every other error comes back as the
  role raised it, including a capability the server does not advertise
  (#7589's issues.update.gates) and a local backend's ErrUnsupported.
  Before, every ErrUnsupported got the same sentence.
- Tests:
  - Unit: the template skip for every type filter; a sweep over template
    gates beside a live one; ListGates putting `template=false` in the
    query under ExcludeTemplates, and gate check's listing setting it;
    the reword and its chain; the pass-through cases; the sighting patch
    matching its map form.
  - Embedded: a cooked proto's gh:run, bead and timer gates are
    untouched, and gh is never run. With --limit 1 behind that proto's
    gates, a live expired timer gate is checked and closed.
  - Proxied-server: template timer and bead gates stay open and
    unstamped. With --limit 1 behind two newer template timer gates, the
    live one is checked and closed.
  - Served: the template gates are untouched, and the gh:run error names
    the server. --dry-run --limit 3 behind three template gates checks
    three live gates.
  - Each --limit case first shows, through bd gate list, that a template
    leads the listing.
  - Against the PR-head bd, the embedded, proxied and served template
    cases fail. Against a bd that skips templates after the cut, the
    three --limit cases fail, printing "No open gates found." and
    exiting 0.
- bd_test gains the //internal/httpclient{,/encode} deps (gazelle-checked).
  The embedded shard manifest lists both TestEmbeddedGateCheck* tests.
  The CHANGELOG notes the template skip, that templates take no row of
  --limit, and the new wording.

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

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: Eddie the Engineer <adopt-pr@gascity.com>
This commit is contained in:
Julian Knutsen 2026-10-16 15:14:53 +00:00 • committed by GitHub
commit dff9492c61
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 1090 additions and 37 deletions

View file

@ -146,9 +146,11 @@
100 61 TestEmbeddedUpdateRoutedStoreCommitsTargetHead
100 62 TestEmbeddedGateCheckSkipsTemplateGates
100 62 TestEmbeddedReadyBlockersAndLabels
100 63 TestEmbeddedDepAndLinkBatchAutoCommitDoesNotAdvanceHead
100 63 TestEmbeddedGateCheckLimitCountsOnlyLiveGates
100 63 TestEmbeddedIfRevisionDeletePreflightGoneParentIsPreconditionFailed
100 64 TestEmbeddedGateWaiterCheckLifecycle

View file

@ -530,14 +530,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
core `gate-sweep` order (`bd gate check --type=timer --escalate`) failed on
every remote scope. It now lists through `issueops.ListGates` over the
route's Querier (direct, proxied and remote alike). Closing a resolved gate
was already served. Two writes the check can make still refuse over HTTP,
because the HTTP store has no raw `UpdateIssue` and the client refuses it
before sending a request: a bead gate's first sighting of its awaited bead
was already served. A bead gate's first sighting of its awaited bead
(`await_seen`, recorded when the check finds that bead, not yet closed, in
the gate's own store) and the gh:run discovery's `await_id` persist. Each
counts as that gate's error. A negative `--limit` is now an error; the
direct and proxied routes used to read it as no limit. The served corpus
gains `mc-gate-check-timer`, `mc-gate-check-gh` and `gate-check-all`.
the gate's own store) was written through the legacy map-based
`UpdateIssue`, which the HTTP store does not implement, so every open bead
gate with an open target was an error on every sweep; it now goes through
the Lifecycle role's `Update` (a single-key metadata set, `issues.update` on
the wire), as does the gh:run discovery's `await_id` persist. The HTTP
client still refuses the `await_id` patch before the dial, so a gh:run gate
with a workflow-name hint stays that gate's error, now naming the run and
the server that does not accept the `await_id` update rather than a Go
method. The sweep also skips the gates of a persisted proto
(`bd cook --persist`): they are templates that every pour copies, so a
write to one was refused on every sweep and, in a local workspace, a
resolved one (an expired timer, say) was closed in the proto itself. The
listing's query leaves them out (`template=false`), so they take no row of
`--limit` and cannot crowd a live gate out of the sweep; `bd gate list`
still lists them. A negative `--limit` is now an error; the direct and
proxied routes used to read it as no limit. The served corpus gains
`mc-gate-check-timer`, `mc-gate-check-gh`, `gate-check-all` and a served
test over await-typed and template gates.
- **`bd gate list` works over a connected HTTP workspace** (S20). The
workspace-wide listing built a raw `SearchIssues` filter, which the HTTP

View file

@ -11,6 +11,7 @@ go_test(
srcs = [
"corpus_parts_test.go",
"corpus_test.go",
"gate_check_served_test.go",
"harness_test.go",
"reference_proxy_test.go",
"required_corpus_tier_test.go",
@ -24,6 +25,7 @@ go_test(
"//internal/storage/domain",
"//internal/storage/embeddeddolt",
"//internal/testutil/bazeltest",
"//internal/types",
"@in_gopkg_yaml_v3//:yaml_v3",
],
)
@ -41,8 +43,10 @@ go_test(
# serial, ~242s together). The shapes are now dealt round robin across
# corpusParts = 4 parts (corpus_test.go), each a top-level test with its own
# server and seed, and served_pinned_shards.txt pins part k to shard k; the
# 5th shard runs the package's engine-free tests through the binary's
# round-robin. Each part pays the setup again, though it seeds the served
# 5th shard runs the package's other tests through the binary's
# round-robin (the engine-free ones, plus
# TestServedGateCheckOverAwaitTypedGates, which seeds its own small served
# store). Each part pays the setup again, though it seeds the served
# and embedded sides concurrently (~62-76s on the lane's executors), so a
# part's shard is that plus ~60s of shapes: 114-146s measured. Three parts
# ran 170-180s each; they save one setup (~75 test-seconds a run) but

View file

@ -0,0 +1,341 @@
//go:build cgo
package corpus_test
import (
"context"
"encoding/json"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"github.com/steveyegge/beads/internal/httpapi"
"github.com/steveyegge/beads/internal/storage/embeddeddolt"
"github.com/steveyegge/beads/internal/types"
)
// gateCheckRequestLog records every request the reference proxy forwards.
type gateCheckRequestLog struct {
mu sync.Mutex
seen []string
next http.Handler
}
func (l *gateCheckRequestLog) ServeHTTP(w http.ResponseWriter, r *http.Request) {
l.mu.Lock()
l.seen = append(l.seen, r.Method+" "+r.URL.Path)
l.mu.Unlock()
l.next.ServeHTTP(w, r)
}
func (l *gateCheckRequestLog) take() []string {
l.mu.Lock()
defer l.mu.Unlock()
out := l.seen
l.seen = nil
return out
}
func gateCheckWrites(reqs []string) []string {
var writes []string
for _, r := range reqs {
if !strings.HasPrefix(r, http.MethodGet+" ") {
writes = append(writes, r)
}
}
return writes
}
// TestServedGateCheckOverAwaitTypedGates runs `bd gate check` over HTTP, through
// the reference proxy, against REAL await-typed gates. No wire create accepts
// await_*, so 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;
// - a bead gate whose target is open records its sighting (issues.update),
// and one whose target is closed closes;
// - a merged gh:pr gate closes, a closed one escalates and stays open;
// - a gh:run gate with a workflow-name hint discovers its run but cannot
// persist await_id over the wire: that gate's error names the await_id
// update and the server, not a Go method, and await_id is untouched;
// - a persisted proto's gates are templates: listed, but no sweep evaluates
// or writes them, and they take no row of --limit (they sort first here).
//
// The corpus.yaml shapes prove the listing only (their gates have no
// await_type); this is the test that proves what the sweep does to the gates.
func TestServedGateCheckOverAwaitTypedGates(t *testing.T) {
skipUnlessEmbeddedDolt(t)
bin := buildBD(t)
ctx := context.Background()
store, err := embeddeddolt.Open(ctx, t.TempDir(), corpusDatabase, "main")
if err != nil {
t.Fatalf("open the served store: %v", err)
}
t.Cleanup(func() { _ = store.Close() })
if err := store.SetConfig(ctx, "issue_prefix", corpusPrefix); err != nil {
t.Fatalf("set the issue prefix: %v", err)
}
// The templates keep P0 and every other row gets P2, so the store's default
// order puts the template gates ahead of every live one.
seed := func(i *types.Issue) {
t.Helper()
i.Status = types.StatusOpen
i.Title = i.ID
if !i.IsTemplate {
i.Priority = 2
}
if err := store.CreateIssue(ctx, i, "seed"); err != nil {
t.Fatalf("seed %s: %v", i.ID, err)
}
}
seed(&types.Issue{ID: "e2e-tgt-open", IssueType: "task"})
seed(&types.Issue{ID: "e2e-tgt-closed", IssueType: "task"})
if err := store.CloseIssue(ctx, "e2e-tgt-closed", "done", "seed", ""); err != nil {
t.Fatalf("close e2e-tgt-closed: %v", err)
}
seed(&types.Issue{ID: "e2e-g-timer", IssueType: "gate", AwaitType: "timer", Timeout: time.Millisecond})
seed(&types.Issue{ID: "e2e-g-timer-pending", IssueType: "gate", AwaitType: "timer", Timeout: 100 * time.Hour})
seed(&types.Issue{ID: "e2e-g-bead-open", IssueType: "gate", AwaitType: "bead", AwaitID: "e2e-tgt-open"})
seed(&types.Issue{ID: "e2e-g-bead-closed", IssueType: "gate", AwaitType: "bead", AwaitID: "e2e-tgt-closed"})
seed(&types.Issue{ID: "e2e-g-pr-merged", IssueType: "gate", AwaitType: "gh:pr", AwaitID: "11"})
seed(&types.Issue{ID: "e2e-g-pr-closed", IssueType: "gate", AwaitType: "gh:pr", AwaitID: "12"})
seed(&types.Issue{ID: "e2e-g-run-hint", IssueType: "gate", AwaitType: "gh:run", AwaitID: "ci-wf"})
seed(&types.Issue{ID: "e2e-tpl-timer", IssueType: "gate", AwaitType: "timer", Timeout: time.Millisecond, IsTemplate: true})
seed(&types.Issue{ID: "e2e-tpl-bead", IssueType: "gate", AwaitType: "bead", AwaitID: "e2e-tgt-open", IsTemplate: true})
seed(&types.Issue{ID: "e2e-tpl-run", IssueType: "gate", AwaitType: "gh:run", AwaitID: "ci-wf", IsTemplate: true})
tokenFile := filepath.Join(t.TempDir(), "tokens")
if err := os.WriteFile(tokenFile, []byte(corpusToken+"\n"), 0o600); err != nil {
t.Fatalf("write the token file: %v", err)
}
auth, err := httpapi.NewTokenFileAuth(tokenFile)
if err != nil {
t.Fatalf("load the token file: %v", err)
}
cfg := corpusServeConfig(t, store)
cfg.AllowNonLoopback = true
cfg.Auth = auth
srv, err := httpapi.Listen(cfg)
if err != nil {
t.Fatalf("bind the in-process server: %v", err)
}
serveCtx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() { defer close(done); _ = srv.Serve(serveCtx) }()
t.Cleanup(func() {
cancel()
select {
case <-done:
case <-time.After(30 * time.Second):
t.Error("the in-process server did not shut down")
}
})
proxy := newReferenceProxy(t, loadReferenceProfile(t), srv.Addr())
reqs := &gateCheckRequestLog{next: proxy}
proxyAddr := startProxy(t, reqs)
// gh and gt are fakes on PATH: gh answers the run discovery and the two PR
// states, gt records each escalation.
binDir := t.TempDir()
gh := `#!/bin/sh
case "$1 $2" in
"run list") echo '[{"databaseId":4242,"name":"ci-wf","status":"completed","conclusion":"success","createdAt":"2026-10-15T00:00:00Z","workflowName":"ci-wf"}]' ;;
"run view") echo '{"status":"completed","conclusion":"success","name":"ci-wf"}' ;;
"pr view") if [ "$3" = "11" ]; then echo '{"state":"MERGED","title":"m"}'; else echo '{"state":"CLOSED","title":"c"}'; fi ;;
*) 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)
}
if err := os.WriteFile(filepath.Join(binDir, "gt"), []byte("#!/bin/sh\nexit 0\n"), 0o755); err != nil {
t.Fatalf("write fake gt: %v", err)
}
dir := t.TempDir()
env := []string{
"BEADS_HTTP_TOKEN=" + proxyAddr + "=" + corpusToken,
"BEADS_ACTOR=worker",
"BD_BACKUP_ENABLED=false",
"BD_EXPORT_AUTO=false",
"PATH=" + binDir + string(os.PathListSeparator) + os.Getenv("PATH"),
}
if r := runBD(t, bin, dir, env, "", "connect", "http://"+proxyAddr, "--expect-project-id", corpusProjectID, "--json"); r.code != 0 {
t.Fatalf("bd connect: %s", r)
}
reqs.take()
time.Sleep(50 * time.Millisecond) // the 1ms timer is expired by now
get := func(id string) *types.Issue {
t.Helper()
issue, err := store.GetIssue(ctx, id)
if err != nil {
t.Fatalf("read %s back: %v", id, err)
}
return issue
}
wantStatus := func(id string, want types.Status) {
t.Helper()
if got := get(id).Status; got != want {
t.Errorf("%s status = %s, want %s", id, got, want)
}
}
seenOf := func(id string) string {
t.Helper()
var meta map[string]json.RawMessage
raw := get(id).Metadata
if len(raw) == 0 {
return ""
}
if err := json.Unmarshal(raw, &meta); err != nil {
t.Fatalf("%s metadata %s: %v", id, raw, err)
}
var seen string
_ = json.Unmarshal(meta["await_seen"], &seen)
return seen
}
allGates := []string{"e2e-g-timer", "e2e-g-timer-pending", "e2e-g-bead-open", "e2e-g-bead-closed", "e2e-g-pr-merged", "e2e-g-pr-closed", "e2e-g-run-hint"}
t.Run("dry-run writes nothing", func(t *testing.T) {
r := runBD(t, bin, dir, env, "", "gate", "check", "--dry-run")
if r.code != 0 {
t.Fatalf("bd gate check --dry-run: %s", r)
}
if w := gateCheckWrites(reqs.take()); len(w) != 0 {
t.Fatalf("--dry-run sent writes %v\n%s", w, r)
}
for _, id := range allGates {
wantStatus(id, types.StatusOpen)
}
if seen := seenOf("e2e-g-bead-open"); seen != "" {
t.Errorf("--dry-run recorded a sighting %q", seen)
}
if got := get("e2e-g-run-hint").AwaitID; got != "ci-wf" {
t.Errorf("--dry-run persisted await_id %q", got)
}
})
t.Run("limit counts only live gates", func(t *testing.T) {
// bd gate list still lists templates, so its first three rows show the
// order the sweep's own listing would be cut from.
r := runBD(t, bin, dir, env, "", "gate", "list", "--limit", "3", "--json")
var listed []types.Issue
if start := strings.Index(r.stdout, "["); r.code != 0 || start < 0 || json.Unmarshal([]byte(r.stdout[start:]), &listed) != nil {
t.Fatalf("bd gate list --limit 3 --json: %s", r)
}
var ids []string
for _, g := range listed {
if strings.HasPrefix(g.ID, "e2e-tpl-") {
ids = append(ids, g.ID)
}
}
if len(listed) != 3 || len(ids) != 3 {
t.Fatalf("bd gate list --limit 3 listed %d gates, %d of them templates (%v), want the three template gates first", len(listed), len(ids), ids)
}
reqs.take()
r = runBD(t, bin, dir, env, "", "gate", "check", "--dry-run", "--limit", "3")
if r.code != 0 {
t.Fatalf("bd gate check --dry-run --limit 3: %s", r)
}
if w := gateCheckWrites(reqs.take()); len(w) != 0 {
t.Fatalf("--dry-run sent writes %v\n%s", w, r)
}
if !strings.Contains(r.stdout, "Checked 3 gates") {
t.Errorf("--limit 3 behind three template gates must still check three live gates:\n%s", r)
}
if strings.Contains(r.stdout+r.stderr, "e2e-tpl-") {
t.Errorf("a template gate was checked:\n%s", r)
}
})
t.Run("timer sweep closes the expired timer", func(t *testing.T) {
r := runBD(t, bin, dir, env, "", "gate", "check", "--type=timer", "--escalate")
if r.code != 0 {
t.Fatalf("bd gate check --type=timer --escalate: %s", r)
}
reqs.take()
wantStatus("e2e-g-timer", types.StatusClosed)
wantStatus("e2e-g-timer-pending", types.StatusOpen)
wantStatus("e2e-tpl-timer", types.StatusOpen)
})
t.Run("bead sweep records the sighting and closes the done gate", func(t *testing.T) {
r := runBD(t, bin, dir, env, "", "gate", "check", "--type=bead")
if r.code != 0 {
t.Fatalf("bd gate check --type=bead: %s", r)
}
writes := gateCheckWrites(reqs.take())
sighted := false
for _, w := range writes {
if strings.Contains(w, "e2e-g-bead-open") {
sighted = true
}
if strings.Contains(w, "e2e-tpl-bead") {
t.Errorf("the sweep wrote to the template gate: %s", w)
}
}
if !sighted {
t.Errorf("no write to e2e-g-bead-open reached the server; writes: %v\n%s", writes, r)
}
if seen := seenOf("e2e-g-bead-open"); seen != "e2e-tgt-open" {
t.Errorf("e2e-g-bead-open await_seen = %q, want e2e-tgt-open\n%s", seen, r)
}
if seen := seenOf("e2e-tpl-bead"); seen != "" {
t.Errorf("e2e-tpl-bead recorded a sighting %q; a template gate is never checked", seen)
}
wantStatus("e2e-g-bead-open", types.StatusOpen)
wantStatus("e2e-g-bead-closed", types.StatusClosed)
})
t.Run("gh:pr sweep closes the merged PR gate", func(t *testing.T) {
r := runBD(t, bin, dir, env, "", "gate", "check", "--type=gh:pr", "--escalate")
if r.code != 0 {
t.Fatalf("bd gate check --type=gh:pr --escalate: %s", r)
}
reqs.take()
wantStatus("e2e-g-pr-merged", types.StatusClosed)
wantStatus("e2e-g-pr-closed", types.StatusOpen)
if !strings.Contains(r.stdout, "e2e-g-pr-closed: ESCALATE") {
t.Errorf("closed PR gate was not escalated:\n%s", r)
}
})
t.Run("gh:run await_id persist refuses by name", func(t *testing.T) {
r := runBD(t, bin, dir, env, "", "gate", "check", "--type=gh:run")
if r.code == 0 {
t.Fatalf("bd gate check --type=gh:run succeeded; the wire cannot carry await_id: %s", r)
}
if w := gateCheckWrites(reqs.take()); len(w) != 0 {
t.Errorf("the refused await_id persist sent writes %v", w)
}
if !strings.Contains(r.stderr, "e2e-g-run-hint") || !strings.Contains(r.stderr, "await_id") {
t.Errorf("gh:run error does not name the gate and the await_id update:\n%s", r)
}
if !strings.Contains(r.stderr, "bd serve at http://"+proxyAddr) {
t.Errorf("gh:run error does not name the server it reached:\n%s", r)
}
if strings.Contains(r.stderr, "e2e-tpl-run") {
t.Errorf("the template gh:run gate was checked:\n%s", r)
}
for _, leak := range []string{"UpdateIssue", "Lifecycle.Update", "updateIssue"} {
if strings.Contains(r.stderr, leak) {
t.Errorf("gh:run error leaks %q:\n%s", leak, r)
}
}
if got := get("e2e-g-run-hint").AwaitID; got != "ci-wf" {
t.Errorf("e2e-g-run-hint await_id = %q, want ci-wf untouched", got)
}
wantStatus("e2e-g-run-hint", types.StatusOpen)
if got := get("e2e-tpl-run").AwaitID; got != "ci-wf" {
t.Errorf("e2e-tpl-run await_id = %q, want ci-wf untouched", got)
}
})
}

View file

@ -1339,6 +1339,8 @@ go_test(
"//internal/githooksenv",
"//internal/hooks",
"//internal/httpapi",
"//internal/httpclient",
"//internal/httpclient/encode",
"//internal/linear",
"//internal/metrics",
"//internal/migration",

View file

@ -12,6 +12,7 @@ import (
"time"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/httpclient"
"github.com/steveyegge/beads/internal/metrics"
"github.com/steveyegge/beads/internal/storage"
storageissueops "github.com/steveyegge/beads/internal/storage/issueops"
@ -670,14 +671,10 @@ Examples:
var recordSeen func(gate *types.Issue) error
if !dryRun {
persistAwaitID = func(gateID, runID string) error {
return updateGateAwaitIDFunc(nil, gateID, runID)
return gateCheckPersistAwaitIDFunc(ctx, gateID, runID)
}
recordSeen = func(gate *types.Issue) error {
if err := store.UpdateIssue(ctx, gate.ID, beadGateSeenUpdate(gate.AwaitID), currentActor()); err != nil {
return err
}
commandDidWrite.Store(true)
return nil
return updateGateThroughLifecycle(ctx, gate.ID, beadGateSeenPatch(gate.AwaitID))
}
}
@ -700,8 +697,9 @@ Examples:
// of work, or the remote http backend's queryIssues). It is the question the
// raw SearchIssues filter used to ask — the gate type, both planes, closed
// rows excluded, cut at limit — but a remote backend serves it, where the raw
// filter was refused before any gate was read. The per-type selection stays
// with filterCheckableGates.
// filter was refused before any gate was read. Templates are left out of the
// query (see listOpenGatesWith); the per-type selection stays with
// filterCheckableGates.
func listOpenGatesForCheck(ctx context.Context, limit int) ([]*types.Issue, error) {
querier, err := openQuerier()
if err != nil {
@ -711,9 +709,12 @@ func listOpenGatesForCheck(ctx context.Context, limit int) ([]*types.Issue, erro
}
// listOpenGatesWith is listOpenGatesForCheck over a given Querier: the open
// gates only (gate check never evaluates a closed one), cut at limit.
// gates only (gate check never evaluates a closed one), minus templates, cut at
// limit. The templates go in the query, not after it: a page of a persisted
// proto's gates, newer than the live ones, would otherwise fill --limit and
// leave the sweep nothing to check.
func listOpenGatesWith(ctx context.Context, querier issueops.Querier, limit int) ([]*types.Issue, error) {
return issueops.ListGates(ctx, querier, issueops.GateListRequest{Limit: limit})
return issueops.ListGates(ctx, querier, issueops.GateListRequest{ExcludeTemplates: true, Limit: limit})
}
type gateCheckResult struct {
@ -724,9 +725,21 @@ type gateCheckResult struct {
err error
}
// filterCheckableGates selects the listed gates a sweep evaluates: those that
// match typeFilter, minus templates. A template's gate is a blueprint that every
// pour copies, await_id included. Checking one fails every sweep where its
// write is refused (the Lifecycle role's template guard, a served close) and
// rewrites the proto where it is not (a local close, the proxied-server route's
// writes): a closed timer gate, a recorded sighting, or a run ID every later
// pour would await. listOpenGatesWith already leaves templates out of the
// query; the skip here is the backstop for a listing that returns one anyway.
// The direct and the proxied-server routes both select here.
func filterCheckableGates(gates []*types.Issue, typeFilter string) []*types.Issue {
var out []*types.Issue
for _, gate := range gates {
if gate.IsTemplate {
continue
}
if shouldCheckGate(gate, typeFilter) {
out = append(out, gate)
}
@ -742,6 +755,72 @@ func printNoOpenGates(typeFilter string) {
}
}
// updateGateThroughLifecycle is gate check's one write besides the close: a
// patch to a gate through the Lifecycle role, so every route the direct path
// serves (Dolt, embedded, Postgres, and a connected HTTP workspace, whose role
// sends issues.update) applies it the same way. The legacy map-based
// UpdateIssue is not on the HTTP store at all, so the sighting written through
// it was refused before any request was sent.
func updateGateThroughLifecycle(ctx context.Context, gateID string, patch issueops.IssuePatch) error {
lifecycle, err := openIssueLifecycle()
if err != nil {
return err
}
ctx, err = issueOpsContext(ctx)
if err != nil {
return err
}
if _, err := lifecycle.Update(ctx, issueops.UpdateRequest{Actor: currentActor(), IssueID: gateID, Patch: patch}); err != nil {
return err
}
commandDidWrite.Store(true)
return nil
}
// awaitIDPatchRefusal is the divergence-ledger row a connected HTTP
// workspace's Lifecycle role refuses an await_id edit by, before any request:
// the wire's issue patch has no await_id member.
const awaitIDPatchRefusal = "W-IssuePatch.AwaitID"
// persistGateCheckAwaitID records a gh:run gate's discovered run ID through
// the Lifecycle role. The one refusal it rewords is the HTTP wire's
// (awaitIDPatchRefusal), whose own text names the role method and the wire
// operation rather than what the user can do about it. That refusal stays
// this gate's error, so the rest of the sweep runs. Every other error,
// another unsupported capability included, is returned as the role raised it.
func persistGateCheckAwaitID(ctx context.Context, gateID, runID string) error {
err := updateGateThroughLifecycle(ctx, gateID, issueops.IssuePatch{AwaitID: issueops.Field[string]{Set: true, Value: runID}})
var refused *httpclient.InexpressibleError
if errors.As(err, &refused) && refused.Refused != nil && refused.Refused.Row.ID == awaitIDPatchRefusal {
serverURL := ""
if refused.Unsup != nil {
serverURL = refused.Unsup.ServerURL
}
return &awaitIDRefusedError{runID: runID, serverURL: serverURL, err: err}
}
return err
}
// awaitIDRefusedError is the HTTP wire's await_id refusal, reworded for the
// gate check row that prints it: it names the run, await_id and the server.
// Unwrap keeps the refusal, so errors.As still reaches *storage.ErrUnsupported
// and the ledger row.
type awaitIDRefusedError struct {
runID string
serverURL string
err error
}
func (e *awaitIDRefusedError) Error() string {
server := "this backend"
if e.serverURL != "" {
server = "bd serve at " + e.serverURL
}
return fmt.Sprintf("run %s cannot be recorded as await_id: %s does not accept an await_id update; check the gate in a local workspace", e.runID, server)
}
func (e *awaitIDRefusedError) Unwrap() error { return e.err }
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 {
@ -872,6 +951,7 @@ func runGHCommand(args ...string) (stdout, stderr []byte, err error) {
var (
discoverRunIDByWorkflowNameFunc = discoverRunIDByWorkflowName
updateGateAwaitIDFunc = updateGateAwaitID
gateCheckPersistAwaitIDFunc = persistGateCheckAwaitID
checkGHRunStatusFunc = checkGHRunStatus
)
@ -1578,6 +1658,14 @@ func beadGateSeenUpdate(awaitID string) map[string]interface{} {
}
}
// beadGateSeenPatch is beadGateSeenUpdate as a Lifecycle patch: the same
// single-key metadata set, which a connected HTTP workspace sends as
// issues.update.
func beadGateSeenPatch(awaitID string) issueops.IssuePatch {
value, _ := json.Marshal(awaitID)
return issueops.IssuePatch{Metadata: issueops.MetadataPatch{Set: 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).

View file

@ -29,7 +29,9 @@ func (q *recordingGateQuerier) Query(_ context.Context, req issueops.QueryReques
// TestGateCheckListsOpenGatesThroughListGates pins the gate check listing to
// the Querier role (queryIssues on a remote backend) instead of the raw
// SearchIssues filter the http store refused before reading a gate: the gate
// type, closed excluded, the --limit carried as given (0 = unlimited).
// type, closed excluded, templates excluded in the query itself (so a
// persisted proto's gates take no row of --limit), the --limit carried as
// given (0 = unlimited).
func TestGateCheckListsOpenGatesThroughListGates(t *testing.T) {
for _, limit := range []int{0, 5, 100} {
q := &recordingGateQuerier{items: []*types.IssueWithCounts{
@ -41,7 +43,7 @@ func TestGateCheckListsOpenGatesThroughListGates(t *testing.T) {
t.Fatalf("limit %d: listOpenGatesWith: %v", limit, err)
}
l := limit
want := issueops.QueryRequest{Expression: "type=gate", IncludeClosed: false, Limit: &l}
want := issueops.QueryRequest{Expression: "type=gate AND template=false", IncludeClosed: false, Limit: &l}
if len(q.calls) != 1 || !reflect.DeepEqual(q.calls[0], want) {
t.Fatalf("limit %d: Query calls = %#v, want one %#v", limit, q.calls, want)
}

View file

@ -5,6 +5,7 @@ package main
import (
"context"
"database/sql"
"encoding/json"
"strings"
"testing"
"time"
@ -259,4 +260,98 @@ func TestProxiedServerGateCheckB(t *testing.T) {
t.Errorf("timer gates never escalate; --escalate should be a no-op, got:\n%s", out)
}
})
// A persisted proto's gates are templates. The listing returns them, but no
// sweep evaluates them: neither the expired template timer nor the template
// bead gate, whose target is open, is closed or written.
t.Run("template_gates_are_not_checked", func(t *testing.T) {
t.Parallel()
p := newSharedProxiedProject(t, bd, "gck")
target := bdProxiedCreate(t, bd, p.dir, "Template bead gate target")
live := bdProxiedCreate(t, bd, p.dir, "Live timer gate", "--type", "gate")
tplTimer := bdProxiedCreate(t, bd, p.dir, "Template timer gate", "--type", "gate")
tplBead := bdProxiedCreate(t, bd, p.dir, "Template bead gate", "--type", "gate")
db := openProxiedDB(t, p)
seedGateAwait(t, db, live.ID, "timer", "", longAgo(), expiredTimeout)
seedGateAwait(t, db, tplTimer.ID, "timer", "", longAgo(), expiredTimeout)
seedGateAwait(t, db, tplBead.ID, "bead", target.ID, time.Now().UTC(), 0)
for _, id := range []string{tplTimer.ID, tplBead.ID} {
if _, err := db.ExecContext(context.Background(),
"UPDATE issues SET is_template = 1 WHERE id = ?", id); err != nil {
t.Fatalf("mark %s a template: %v", id, err)
}
}
out, stderr, err := bdProxiedRunBuffers(t, bd, p.dir, "gate", "check")
if err != nil {
t.Fatalf("gate check failed: %v\nstderr:\n%s", err, stderr)
}
if got := readStatus(t, db, live.ID); got != types.StatusClosed {
t.Errorf("the live expired timer gate should be closed, got %q", got)
}
if !strings.Contains(out, "Checked 1 gates: 1 resolved") {
t.Errorf("expected only the live gate checked, got:\n%s", out)
}
for _, id := range []string{tplTimer.ID, tplBead.ID} {
if got := readStatus(t, db, id); got != types.StatusOpen {
t.Errorf("template gate %s status = %q, want open", id, got)
}
}
var metadata string
if err := db.QueryRowContext(context.Background(),
"SELECT metadata FROM issues WHERE id = ?", tplBead.ID).Scan(&metadata); err != nil {
t.Fatalf("read template gate metadata: %v", err)
}
if strings.Contains(metadata, beadGateSeenKey) {
t.Errorf("template gate metadata %s records a sighting", metadata)
}
})
// The template gates, expired timers too, are newer than the live one, so
// they lead the listing. The query leaves them out before --limit cuts it:
// they used to fill the one row, and the sweep reported no open gates and
// exited 0 with the live gate unchecked.
t.Run("limit_counts_only_live_gates", func(t *testing.T) {
t.Parallel()
p := newSharedProxiedProject(t, bd, "gcv")
live := bdProxiedCreate(t, bd, p.dir, "Live timer gate", "--type", "gate")
tplA := bdProxiedCreate(t, bd, p.dir, "Template timer gate A", "--type", "gate")
tplB := bdProxiedCreate(t, bd, p.dir, "Template timer gate B", "--type", "gate")
db := openProxiedDB(t, p)
seedGateAwait(t, db, live.ID, "timer", "", longAgo(), expiredTimeout)
for _, id := range []string{tplA.ID, tplB.ID} {
seedGateAwait(t, db, id, "timer", "", time.Now().UTC().Add(-2*expiredTimeout), expiredTimeout)
if _, err := db.ExecContext(context.Background(),
"UPDATE issues SET is_template = 1 WHERE id = ?", id); err != nil {
t.Fatalf("mark %s a template: %v", id, err)
}
}
// bd gate list still lists templates: its first row is one, so a cut at
// one row taken before the template skip leaves the sweep nothing.
listed, stderr, err := bdProxiedRunBuffers(t, bd, p.dir, "gate", "list", "--limit", "1", "--json")
if err != nil {
t.Fatalf("gate list --limit 1 --json failed: %v\nstderr:\n%s", err, stderr)
}
var first []map[string]any
if start := strings.Index(listed, "["); start < 0 || json.Unmarshal([]byte(listed[start:]), &first) != nil ||
len(first) != 1 || (first[0]["id"] != tplA.ID && first[0]["id"] != tplB.ID) {
t.Fatalf("gate list --limit 1 --json = %s, want one of the template gates first", listed)
}
out, stderr, err := bdProxiedRunBuffers(t, bd, p.dir, "gate", "check", "--limit", "1")
if err != nil {
t.Fatalf("gate check --limit 1 failed: %v\nstderr:\n%s", err, stderr)
}
if got := readStatus(t, db, live.ID); got != types.StatusClosed {
t.Errorf("the live expired timer gate should be closed, got %q\noutput:\n%s", got, out)
}
if !strings.Contains(out, "Checked 1 gates: 1 resolved") {
t.Errorf("expected the live gate checked past the templates, got:\n%s", out)
}
for _, id := range []string{tplA.ID, tplB.ID} {
if got := readStatus(t, db, id); got != types.StatusOpen {
t.Errorf("template gate %s status = %q, want open", id, got)
}
}
})
}

View file

@ -7,6 +7,7 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"testing"
@ -755,6 +756,169 @@ func TestEmbeddedGateCreateBeadCheck(t *testing.T) {
})
}
// templateGateFormula is a proto with a gh:run, a bead and a timer gate step;
// await-ci is beads-release's own shape. bd cook --persist writes each step's
// gate as a template gate, <proto>.gate-<step>, which ListGates lists with the
// open gates. %[1]s is the formula name and %[2]s the bead gate's target.
const templateGateFormula = `formula = "%[1]s"
version = 1
type = "workflow"
[[steps]]
id = "await-ci"
title = "Await CI"
type = "task"
[steps.gate]
type = "gh:run"
id = "release.yml"
[[steps]]
id = "await-bead"
title = "Await bead"
type = "task"
[steps.gate]
type = "bead"
id = "%[2]s"
[[steps]]
id = "await-timer"
title = "Await timer"
type = "task"
[steps.gate]
type = "timer"
timeout = "1ms"
`
// TestEmbeddedGateCheckSkipsTemplateGates sweeps a workspace holding a
// persisted proto. Its gates are blueprints that every pour copies: checking
// one failed the sweep on the template guard (after asking GitHub for the
// gh:run gate's run), and the expired timer gate was closed in the proto. The
// sweep must leave all three untouched and still check the ordinary gate.
func TestEmbeddedGateCheckSkipsTemplateGates(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, beadsDir, _ := bdInit(t, bd, "--prefix", "gk")
fs := openStore(t, beadsDir, "gk")
if err := fs.SetConfig(t.Context(), "types.custom", `["gate"]`); err != nil {
t.Fatalf("SetConfig: %v", err)
}
fs.Close()
target := bdCreate(t, bd, dir, "Awaited bead", "--type", "task")
formulaPath := filepath.Join(dir, "gk-proto.formula.toml")
if err := os.WriteFile(formulaPath, []byte(fmt.Sprintf(templateGateFormula, "gk-proto", target.ID)), 0o600); err != nil {
t.Fatalf("write formula: %v", err)
}
bdCommand(t, bd, dir, "cook", formulaPath, "--persist")
live := createBeadGate(t, bd, dir, target.ID)
// The fake gh records every call, so a template gh:run gate that reaches
// GitHub shows up in ghLog.
binDir := t.TempDir()
ghLog := filepath.Join(binDir, "gh.log")
gh := fmt.Sprintf("#!/bin/sh\necho \"$*\" >> '%s'\nexit 3\n", ghLog)
if err := os.WriteFile(filepath.Join(binDir, "gh"), []byte(gh), 0o755); err != nil {
t.Fatalf("write fake gh: %v", err)
}
cmd := exec.Command(bd, "gate", "check")
cmd.Dir = dir
cmd.Env = append(bdEnv(dir), "PATH="+binDir+string(os.PathListSeparator)+os.Getenv("PATH"))
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd gate check over a persisted proto: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
if out := stdout.String(); !strings.Contains(out, "Checked 1 gates") {
t.Errorf("bd gate check output:\n%s\nwant only %s checked", out, live.ID)
}
if calls, err := os.ReadFile(ghLog); err == nil {
t.Errorf("the template gh:run gate ran gh: %s", calls)
}
for _, tpl := range []struct{ step, awaitID string }{
{"await-ci", "release.yml"},
{"await-bead", target.ID},
{"await-timer", ""},
} {
id := "gk-proto.gate-" + tpl.step
got := bdShow(t, bd, dir, id)
if !got.IsTemplate {
t.Fatalf("%s is not a template: the cook did not persist the proto's gates", id)
}
if got.Status != types.StatusOpen || got.AwaitID != tpl.awaitID || strings.Contains(string(got.Metadata), beadGateSeenKey) {
t.Errorf("template gate %s: status=%s await_id=%q metadata=%s, want it untouched (open, await_id %q, no sighting)",
id, got.Status, got.AwaitID, got.Metadata, tpl.awaitID)
}
}
if got := bdShow(t, bd, dir, live.ID); !strings.Contains(string(got.Metadata), beadGateSeenKey) {
t.Errorf("gate %s on an open target: metadata=%s, want the sighting recorded", live.ID, got.Metadata)
}
}
// TestEmbeddedGateCheckLimitCountsOnlyLiveGates puts a persisted proto's gates
// ahead of a live one in the listing order and checks with --limit 1. The
// template skip used to run after the limit cut the page, so the one row was a
// template, the sweep printed "No open gates found." and exited 0, and the live
// gate was never checked. The query now leaves templates out before the cut.
func TestEmbeddedGateCheckLimitCountsOnlyLiveGates(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, beadsDir, _ := bdInit(t, bd, "--prefix", "gl")
fs := openStore(t, beadsDir, "gl")
if err := fs.SetConfig(t.Context(), "types.custom", `["gate"]`); err != nil {
t.Fatalf("SetConfig: %v", err)
}
fs.Close()
task := bdCreate(t, bd, dir, "Task behind an expired timer", "--type", "task")
out := bdGate(t, bd, dir, "create", "--blocks", task.ID, "--type", "timer", "--timeout", "1ms", "--json")
var live types.Issue
if start := strings.Index(out, "{"); start < 0 || json.Unmarshal([]byte(out[start:]), &live) != nil || live.ID == "" {
t.Fatalf("bd gate create --type=timer --json: no gate in output:\n%s", out)
}
// The listing orders by priority first, so at P4 the live gate sorts after
// every P2 template gate the cook writes, whatever their created_at.
bdCommand(t, bd, dir, "update", live.ID, "--priority", "4")
target := bdCreate(t, bd, dir, "Awaited bead", "--type", "task")
formulaPath := filepath.Join(dir, "gl-proto.formula.toml")
if err := os.WriteFile(formulaPath, []byte(fmt.Sprintf(templateGateFormula, "gl-proto", target.ID)), 0o600); err != nil {
t.Fatalf("write formula: %v", err)
}
bdCommand(t, bd, dir, "cook", formulaPath, "--persist")
// bd gate list still lists templates: its first row is one, so a cut at
// one row taken before the template skip leaves the sweep nothing.
if first := bdGateListJSON(t, bd, dir, "--limit", "1"); len(first) != 1 || !strings.HasPrefix(fmt.Sprint(first[0]["id"]), "gl-proto.gate-") {
t.Fatalf("bd gate list --limit 1 = %v, want one of gl-proto's template gates first", first)
}
out = bdGate(t, bd, dir, "check", "--limit", "1")
if !strings.Contains(out, "Checked 1 gates: 1 resolved") {
t.Errorf("bd gate check --limit 1 output:\n%s\nwant %s checked and resolved", out, live.ID)
}
if got := bdShow(t, bd, dir, live.ID); got.Status != types.StatusClosed {
t.Errorf("live expired timer gate %s status = %s, want closed", live.ID, got.Status)
}
for _, step := range []string{"await-ci", "await-bead", "await-timer"} {
id := "gl-proto.gate-" + step
if got := bdShow(t, bd, dir, id); !got.IsTemplate || got.Status != types.StatusOpen {
t.Errorf("template gate %s: is_template=%v status=%s, want an open template", id, got.IsTemplate, got.Status)
}
}
}
func TestEmbeddedGateCreateBeadRename(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")

View file

@ -10,12 +10,15 @@ import (
"io"
"os"
"path/filepath"
"reflect"
"slices"
"strings"
"sync"
"testing"
"time"
"github.com/steveyegge/beads/internal/httpclient"
"github.com/steveyegge/beads/internal/httpclient/encode"
"github.com/steveyegge/beads/internal/storage"
storageissueops "github.com/steveyegge/beads/internal/storage/issueops"
"github.com/steveyegge/beads/internal/types"
@ -37,6 +40,8 @@ type fakeGateCheckStore struct {
queries []issueops.QueryRequest
searchCalled bool
closeCalls []gateCloseCall
updates []issueops.UpdateRequest
updateErr error
}
// SearchIssues records that the raw filter was used: gate check lists through
@ -57,6 +62,26 @@ func (f *fakeGateCheckStore) Query(_ context.Context, req issueops.QueryRequest)
return issueops.IssuePage{Items: items}, nil
}
// IssueLifecycle hands back a Lifecycle that records Update: gate check's
// sighting and await_id writes go through the role (issues.update over HTTP),
// never through the legacy map-based UpdateIssue the HTTP store does not serve.
func (f *fakeGateCheckStore) IssueLifecycle() (issueops.Lifecycle, error) {
return &fakeGateLifecycle{store: f}, nil
}
type fakeGateLifecycle struct {
issueops.Lifecycle
store *fakeGateCheckStore
}
func (l *fakeGateLifecycle) Update(_ context.Context, req issueops.UpdateRequest) (issueops.UpdateResult, error) {
l.store.updates = append(l.store.updates, req)
if l.store.updateErr != nil {
return issueops.UpdateResult{}, l.store.updateErr
}
return issueops.UpdateResult{}, nil
}
func (f *fakeGateCheckStore) CloseIssue(_ context.Context, id, reason, actor, session string) error {
f.closeCalls = append(f.closeCalls, gateCloseCall{
id: id,
@ -171,6 +196,41 @@ func TestShouldCheckGate(t *testing.T) {
}
}
// TestFilterCheckableGatesSkipsTemplates: no type filter selects a persisted
// proto's gates, even from a listing that returned them beside the open ones
// (gate check's own query leaves them out). A template gate is the blueprint
// every pour copies, so a sweep neither evaluates nor writes it.
func TestFilterCheckableGatesSkipsTemplates(t *testing.T) {
var gates []*types.Issue
for _, awaitType := range []string{"gh:run", "gh:pr", "timer", "human", "bead"} {
gates = append(gates,
&types.Issue{ID: "live-" + awaitType, IssueType: "gate", AwaitType: awaitType},
&types.Issue{ID: "tpl-" + awaitType, IssueType: "gate", AwaitType: awaitType, IsTemplate: true},
)
}
every := []string{"live-gh:run", "live-gh:pr", "live-timer", "live-human", "live-bead"}
for _, tt := range []struct {
filter string
want []string
}{
{"", every},
{"all", every},
{"gh", []string{"live-gh:run", "live-gh:pr"}},
{"gh:run", []string{"live-gh:run"}},
{"gh:pr", []string{"live-gh:pr"}},
{"timer", []string{"live-timer"}},
{"bead", []string{"live-bead"}},
} {
var got []string
for _, gate := range filterCheckableGates(gates, tt.filter) {
got = append(got, gate.ID)
}
if !slices.Equal(got, tt.want) {
t.Errorf("filterCheckableGates(%q) = %v, want %v", tt.filter, got, tt.want)
}
}
}
// fakeBeadGateGetter fakes the one lookup checkBeadGate performs.
type fakeBeadGateGetter struct {
issues map[string]*types.Issue
@ -633,6 +693,25 @@ func TestBeadGateSeenUpdate_KeepsOtherMetadata(t *testing.T) {
}
}
// TestBeadGateSeenPatchMatchesTheMapUpdate ties the sighting's two encodings
// together: the direct route records it as beadGateSeenPatch through the
// Lifecycle role, while the proxied route and rename still write
// beadGateSeenUpdate's map. A gate must read back the same sighting whichever
// route recorded it.
func TestBeadGateSeenPatchMatchesTheMapUpdate(t *testing.T) {
for _, awaitID := range []string{"bd-x", "rig:bd-x", `bd-"quoted"`} {
update := beadGateSeenUpdate(awaitID)
set, ok := update[storageissueops.OpSetMetadata].(map[string]json.RawMessage)
if len(update) != 1 || !ok {
t.Fatalf("beadGateSeenUpdate(%q) = %v, want a single metadata set", awaitID, update)
}
want := issueops.IssuePatch{Metadata: issueops.MetadataPatch{Set: set}}
if got := beadGateSeenPatch(awaitID); !reflect.DeepEqual(got, want) {
t.Errorf("beadGateSeenPatch(%q) = %+v, want the map update's set %+v", awaitID, got, want)
}
}
}
func TestBeadGateTargetSeen(t *testing.T) {
if beadGateTargetSeen(nil) {
t.Error("a nil gate has no sighting")
@ -1741,21 +1820,14 @@ func TestGateCheck_GHRunWorkflowDiscoveryPersistence(t *testing.T) {
t.Fatalf("set limit flag: %v", err)
}
updateCalls := 0
discoverRunIDByWorkflowNameFunc = func(workflowHint string) (string, error) {
if workflowHint != "release.yml" {
t.Fatalf("unexpected workflow hint %q", workflowHint)
}
return "12345", nil
}
updateGateAwaitIDFunc = func(_ interface{}, gateID, runID string) error {
updateCalls++
if gateID != "bd-gate" {
t.Fatalf("expected gate ID bd-gate, got %q", gateID)
}
if runID != "12345" {
t.Fatalf("expected discovered run ID 12345, got %q", runID)
}
updateGateAwaitIDFunc = func(_ interface{}, _, _ string) error {
t.Fatal("gate check persisted await_id through the legacy UpdateIssue path; want the Lifecycle role")
return nil
}
checkGHRunStatusFunc = func(runID string) (bool, bool, string, error) {
@ -1771,8 +1843,18 @@ func TestGateCheck_GHRunWorkflowDiscoveryPersistence(t *testing.T) {
}
})
if updateCalls != tt.wantUpdateCalls {
t.Fatalf("updateGateAwaitIDFunc call count = %d, want %d", updateCalls, tt.wantUpdateCalls)
if len(fakeStore.updates) != tt.wantUpdateCalls {
t.Fatalf("Lifecycle.Update call count = %d, want %d", len(fakeStore.updates), tt.wantUpdateCalls)
}
if tt.wantUpdateCalls == 1 {
req := fakeStore.updates[0]
if req.IssueID != "bd-gate" || req.Actor != "test-actor" {
t.Fatalf("Lifecycle.Update = %+v, want bd-gate by test-actor", req)
}
want := issueops.IssuePatch{AwaitID: issueops.Field[string]{Set: true, Value: "12345"}}
if !reflect.DeepEqual(req.Patch, want) {
t.Fatalf("Lifecycle.Update patch = %+v, want only AwaitID=12345", req.Patch)
}
}
if len(fakeStore.closeCalls) != tt.wantCloseCalls {
t.Fatalf("CloseIssue call count = %d, want %d", len(fakeStore.closeCalls), tt.wantCloseCalls)
@ -1790,8 +1872,8 @@ func TestGateCheck_GHRunWorkflowDiscoveryPersistence(t *testing.T) {
t.Fatalf("Querier.Query calls = %d, want 1", len(fakeStore.queries))
}
q := fakeStore.queries[0]
if q.Expression != "type=gate" || q.IncludeClosed || q.Limit == nil || *q.Limit != 100 {
t.Fatalf("gate check query = %+v, want type=gate, closed excluded, limit 100", q)
if q.Expression != "type=gate AND template=false" || q.IncludeClosed || q.Limit == nil || *q.Limit != 100 {
t.Fatalf("gate check query = %+v, want type=gate AND template=false, closed excluded, limit 100", q)
}
if tt.wantCloseCalls == 1 {
call := fakeStore.closeCalls[0]
@ -1809,6 +1891,118 @@ func TestGateCheck_GHRunWorkflowDiscoveryPersistence(t *testing.T) {
}
}
// templateGateCheckStore answers a bead gate's target lookup, so a sweep that
// evaluates a bead gate shows up in gets.
type templateGateCheckStore struct {
*fakeGateCheckStore
gets []string
}
func (s *templateGateCheckStore) GetIssue(_ context.Context, id string) (*types.Issue, error) {
s.gets = append(s.gets, id)
return &types.Issue{ID: id, Status: types.StatusOpen}, nil
}
// TestGateCheckSkipsTemplateGates sweeps a persisted proto's gates beside the
// open ones. The listing's query leaves templates out, but this fake answers
// every expression alike, so the rows reach filterCheckableGates and the test
// pins that backstop. The Lifecycle role refuses a write to a template, so
// evaluating one failed every sweep, after a gh:run gate had already asked
// GitHub for its run. A template gate is neither evaluated nor written, and an
// ordinary gate beside it still is.
func TestGateCheckSkipsTemplateGates(t *testing.T) {
expired := time.Now().Add(-2 * time.Hour)
templates := []*types.Issue{
{ID: "bd-proto.gate-await-ci", IssueType: "gate", AwaitType: "gh:run", AwaitID: "release.yml", IsTemplate: true},
{ID: "bd-proto.gate-await-bead", IssueType: "gate", AwaitType: "bead", AwaitID: "bd-target", IsTemplate: true},
{ID: "bd-proto.gate-await-timer", IssueType: "gate", AwaitType: "timer", CreatedAt: expired, Timeout: time.Hour, IsTemplate: true},
}
live := &types.Issue{ID: "bd-timer", IssueType: "gate", AwaitType: "timer", CreatedAt: expired, Timeout: time.Hour}
for _, tt := range []struct {
name string
gates []*types.Issue
wantClosed []string
wantOutput string
}{
{"only templates", templates, nil, "No open gates found."},
{"templates beside a live gate", append(slices.Clone(templates), live), []string{"bd-timer"}, "Checked 1 gates: 1 resolved, 0 escalated, 0 errors"},
} {
t.Run(tt.name, func(t *testing.T) {
origStore := store
origRootCtx := rootCtx
origJSONOutput := jsonOutput
origReadonlyMode := readonlyMode
origActor := actor
origDiscover := discoverRunIDByWorkflowNameFunc
origUpdate := updateGateAwaitIDFunc
origStatus := checkGHRunStatusFunc
t.Cleanup(func() {
store = origStore
rootCtx = origRootCtx
jsonOutput = origJSONOutput
readonlyMode = origReadonlyMode
actor = origActor
discoverRunIDByWorkflowNameFunc = origDiscover
updateGateAwaitIDFunc = origUpdate
checkGHRunStatusFunc = origStatus
resetGateCheckFlags(t)
})
resetGateCheckFlags(t)
fake := &templateGateCheckStore{fakeGateCheckStore: &fakeGateCheckStore{issues: tt.gates}}
store = fake
rootCtx = context.Background()
jsonOutput = false
readonlyMode = false
actor = "test-actor"
var ghCalls []string
discoverRunIDByWorkflowNameFunc = func(workflowHint string) (string, error) {
ghCalls = append(ghCalls, "discover "+workflowHint)
return "12345", nil
}
checkGHRunStatusFunc = func(runID string) (bool, bool, string, error) {
ghCalls = append(ghCalls, "status "+runID)
return false, false, "in progress", nil
}
updateGateAwaitIDFunc = func(_ interface{}, gateID, _ string) error {
ghCalls = append(ghCalls, "legacy await_id write "+gateID)
return nil
}
var runErr error
output := captureGateStdout(t, func() { runErr = gateCheckCmd.RunE(gateCheckCmd, nil) })
if runErr != nil {
t.Fatalf("gateCheckCmd.RunE: %v\noutput:\n%s", runErr, output)
}
if !gateTestContains(output, tt.wantOutput) {
t.Errorf("output %q does not contain %q", output, tt.wantOutput)
}
if len(ghCalls) != 0 {
t.Errorf("the template gh:run gate was evaluated: %v", ghCalls)
}
if len(fake.gets) != 0 {
t.Errorf("the template bead gate's target was looked up: %v", fake.gets)
}
var written []string
for _, req := range fake.updates {
written = append(written, req.IssueID)
}
if len(written) != 0 {
t.Errorf("Lifecycle.Update wrote %v, want no write to a template gate", written)
}
var closed []string
for _, call := range fake.closeCalls {
closed = append(closed, call.id)
}
if !slices.Equal(closed, tt.wantClosed) {
t.Errorf("closed gates = %v, want %v", closed, tt.wantClosed)
}
})
}
}
func TestWorkflowNameMatches(t *testing.T) {
tests := []struct {
name string
@ -2206,3 +2400,108 @@ func TestCheckGHRunNotFoundNamesTheRepository(t *testing.T) {
}
})
}
// TestGateCheckWritesGoThroughTheLifecycleRole pins gate check's two non-close
// writes to the Lifecycle role: the bead-gate sighting is a single-key metadata
// set and the gh:run discovery is an await_id patch, which a connected HTTP
// workspace sends as issues.update (the sighting) or refuses before the dial
// (await_id). The legacy map-based UpdateIssue is not on the HTTP store, so a
// sighting written through it never reached the server.
func TestGateCheckWritesGoThroughTheLifecycleRole(t *testing.T) {
origStore, origActor := store, actor
t.Cleanup(func() { store, actor = origStore, origActor })
fake := &fakeGateCheckStore{}
store, actor = fake, "test-actor"
if err := updateGateThroughLifecycle(context.Background(), "bd-gate", beadGateSeenPatch("rig:bd-x")); err != nil {
t.Fatalf("sighting: %v", err)
}
if err := persistGateCheckAwaitID(context.Background(), "bd-run", "4242"); err != nil {
t.Fatalf("await_id: %v", err)
}
if len(fake.updates) != 2 {
t.Fatalf("Lifecycle.Update calls = %d, want 2", len(fake.updates))
}
seen := fake.updates[0]
wantSeen := issueops.IssuePatch{Metadata: issueops.MetadataPatch{Set: map[string]json.RawMessage{beadGateSeenKey: json.RawMessage(`"rig:bd-x"`)}}}
if seen.IssueID != "bd-gate" || seen.Actor != "test-actor" || !reflect.DeepEqual(seen.Patch, wantSeen) {
t.Fatalf("sighting update = %+v, want a %s metadata set on bd-gate", seen, beadGateSeenKey)
}
run := fake.updates[1]
wantRun := issueops.IssuePatch{AwaitID: issueops.Field[string]{Set: true, Value: "4242"}}
if run.IssueID != "bd-run" || !reflect.DeepEqual(run.Patch, wantRun) {
t.Fatalf("await_id update = %+v, want AwaitID=4242 on bd-run", run)
}
}
// gateCheckWireRefusal is what a connected HTTP workspace's Lifecycle role
// returns for a patch member the v0 wire has no member for, before any
// request: the encoder's divergence-ledger row joined to the store's
// unsupported sentinel, as (*httpclient.Store).inexpressible builds it.
func gateCheckWireRefusal(row string) error {
return &httpclient.InexpressibleError{
Unsup: &httpclient.ErrHTTPUnsupported{
Unsup: &storage.ErrUnsupported{Op: "Lifecycle.Update", Backend: "http"},
ServerURL: "http://127.0.0.1:9",
},
Refused: &encode.RefusedError{Op: encode.OpUpdateIssue, Row: encode.RowByID(row)},
}
}
// TestGateCheckAwaitIDRefusalNamesTheRunAndTheServer: the HTTP wire refuses an
// await_id edit by its ledger row, and that refusal alone is reworded, so the
// gate's row names the run, await_id and the server instead of the role
// method and the wire operation. The typed chain survives the reword.
func TestGateCheckAwaitIDRefusalNamesTheRunAndTheServer(t *testing.T) {
origStore, origActor := store, actor
t.Cleanup(func() { store, actor = origStore, origActor })
refusal := gateCheckWireRefusal(awaitIDPatchRefusal)
store, actor = &fakeGateCheckStore{updateErr: refusal}, "test-actor"
err := persistGateCheckAwaitID(context.Background(), "bd-run", "4242")
want := "run 4242 cannot be recorded as await_id: bd serve at http://127.0.0.1:9 does not accept an await_id update; check the gate in a local workspace"
if err == nil || err.Error() != want {
t.Fatalf("await_id refusal = %v, want %q", err, want)
}
if !errors.Is(err, refusal) || !errors.Is(err, encode.ErrRefused) {
t.Errorf("reworded refusal %v no longer wraps the role's refusal", err)
}
var unsupported *storage.ErrUnsupported
if !errors.As(err, &unsupported) || unsupported.Op != "Lifecycle.Update" || unsupported.Backend != "http" {
t.Errorf("reworded refusal %v does not reach *storage.ErrUnsupported", err)
}
var refused *encode.RefusedError
if !errors.As(err, &refused) || refused.Row.ID != awaitIDPatchRefusal {
t.Errorf("reworded refusal %v does not reach the %s ledger row", err, awaitIDPatchRefusal)
}
}
// TestGateCheckAwaitIDPassesOtherRefusalsThrough: every other error from the
// await_id write is returned exactly as the role raised it. Another patch
// member's refusal, or a server that does not advertise the capability the
// member needs, says something the await_id sentence would hide.
func TestGateCheckAwaitIDPassesOtherRefusalsThrough(t *testing.T) {
origStore, origActor := store, actor
t.Cleanup(func() { store, actor = origStore, origActor })
actor = "test-actor"
for _, tt := range []struct {
name string
err error
}{
{"another patch member's refusal", gateCheckWireRefusal("W-IssuePatch.SpecID")},
{"a capability the server does not advertise", &httpclient.ErrHTTPUnsupported{
Unsup: &storage.ErrUnsupported{Op: "Lifecycle.Update", Backend: "http", Capability: "issues.update.gates"},
ServerURL: "http://127.0.0.1:9",
}},
{"a local backend's refusal", &storage.ErrUnsupported{Op: "Lifecycle.Update", Backend: "dolt"}},
{"a failed write", errors.New("connection reset")},
} {
t.Run(tt.name, func(t *testing.T) {
store = &fakeGateCheckStore{updateErr: tt.err}
if err := persistGateCheckAwaitID(context.Background(), "bd-run", "4242"); err != tt.err {
t.Fatalf("persistGateCheckAwaitID = %v, want the role's own error %v", err, tt.err)
}
})
}
}

View file

@ -13,10 +13,16 @@ import (
// it (QueryRequest.IncludeClosed).
const GateListExpression = "type=gate"
// GateListRequest describes `bd gate list` over the whole workspace.
// GateListRequest describes `bd gate list` over the whole workspace, and the
// listing `bd gate check` evaluates.
type GateListRequest struct {
// All admits closed gates, which the listing otherwise hides.
All bool
// ExcludeTemplates leaves template gates (a persisted proto's, which every
// pour copies) out of the query itself, so none of them takes a row of
// Limit. `bd gate list` leaves it unset and lists them; `bd gate check`,
// which never evaluates a template, sets it.
ExcludeTemplates bool
// Limit bounds the rows returned; 0 means unlimited. A negative limit is
// the Querier's ErrValidation.
Limit int
@ -31,7 +37,9 @@ type GateListRequest struct {
// The question is exactly the one the raw filter used to ask: the gate type,
// both planes, closed rows excluded unless All is set, no other default
// suppression (templates, pinned rows and ephemeral gates are all listed),
// in the store's default order, cut at Limit.
// in the store's default order, cut at Limit. ExcludeTemplates adds
// `template=false` to the expression rather than dropping templates from the
// page, so the cut at Limit counts only the gates that remain.
//
// A failed read is an error, never an empty list. Rows come back in the
// Querier's order; a row carrying no issue is dropped.
@ -39,9 +47,13 @@ func ListGates(ctx context.Context, querier Querier, req GateListRequest) ([]*ty
if querier == nil {
return nil, &ErrUnsupported{Op: "ListGates", Backend: "nil"}
}
expression := GateListExpression
if req.ExcludeTemplates {
expression += " AND template=false"
}
limit := req.Limit
page, err := querier.Query(ctx, QueryRequest{
Expression: GateListExpression,
Expression: expression,
IncludeClosed: req.All,
Limit: &limit,
})

View file

@ -49,6 +49,38 @@ func TestListGatesAsksTheQuerierTheRawQuestion(t *testing.T) {
}
}
// TestListGatesExcludesTemplatesInTheQuery: ExcludeTemplates (bd gate check's
// listing) puts the template exclusion in the expression the Querier answers,
// so the cut at Limit counts only the gates that remain. Dropping templates
// from the returned page instead let a page of a persisted proto's gates fill
// the limit and hide every live gate behind it. Without it the listing names
// no template field: `bd gate list` lists templates.
func TestListGatesExcludesTemplatesInTheQuery(t *testing.T) {
nonTemplate := func(all bool, limit int) issueops.QueryRequest {
return issueops.QueryRequest{Expression: "type=gate AND template=false", IncludeClosed: all, Limit: &limit}
}
cases := []struct {
name string
req issueops.GateListRequest
want issueops.QueryRequest
}{
{"open gates, limit 1", issueops.GateListRequest{ExcludeTemplates: true, Limit: 1}, nonTemplate(false, 1)},
{"unlimited", issueops.GateListRequest{ExcludeTemplates: true}, nonTemplate(false, 0)},
{"all gates", issueops.GateListRequest{All: true, ExcludeTemplates: true, Limit: 7}, nonTemplate(true, 7)},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
q := &currentQueryRecorder{responses: []currentQueryResponse{{ids: []string{"g-1"}}}}
if _, err := issueops.ListGates(context.Background(), q, tc.req); err != nil {
t.Fatalf("ListGates: %v", err)
}
if len(q.calls) != 1 || !reflect.DeepEqual(q.calls[0], tc.want) {
t.Fatalf("Query calls = %#v, want one %#v", q.calls, tc.want)
}
})
}
}
func TestListGatesNeverTakesAFailureForAnEmptyList(t *testing.T) {
refusal := &issueops.ErrUnsupported{Op: "Querier.Query", Backend: "http"}
q := &currentQueryRecorder{responses: []currentQueryResponse{{err: refusal}}}