* fix(issueops): enforce the close guards on every close leg The template read-only guard, the pin guard and the assignee authority fence (be-035) lived only in cmd/bd (validateIssueClosable), so a close through bd serve, issues:batchClose, a batch:apply close item or a library caller of Lifecycle.Close / BatchCloser / BatchApplier closed what bd close refused. They now run inside the close's own transaction in CloseIssueCheckedInTx (enforceCloseGuardsInTx), the body every checked close shares (dolt, embedded, uow, and the server behind httpapi), for a target that is not already closed: - a template refuses with *issueops.TemplateReadOnlyError (no bypass); - unless Force (CloseRequest, CloseBatchRequest, CloseItem) is set, a pinned issue refuses with *issueops.PinnedError and another actor's bead with *issueops.CloseNotAssigneeError (matches ErrNotOwner). Wire: 409 template_read_only, issue_pinned and not_assignee (holder in `assignee`, also on a batch-close outcome); the client rebuilds the typed errors. The decision is one exported pure rule over the pre-image, issueops.CheckClosable (internal/storage/issueops), which the in-tx guard calls; bd close's direct and proxied preflights call it too, ahead of gate satisfaction under the same already-closed skip, so bd close output, refusal order and exit codes are unchanged (validateIssueClosable is gone). bd close --if-revision now applies the guards too. Conformance: RunLifecycleCloseEnforcesTheCloseGuards, RunBatchCloserItemsAnswerToTheCloseGuards and RunBatchApplyCloseItemsAnswerToTheCloseGuards on dolt, embedded, uow and http, plus handler mapping tests, a CheckClosable table test, and a cmd/bd test pinning that the guards outrank gate satisfaction. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(httpclient): send force on the raw close; scope the close guards Review fixes for #7425 (adopt-pr bd-0h0ao4, iterations 1 and 2). The raw close keeps parity over http. httpclient.Store.CloseIssue maps the off-role raw close onto the role's closeIssue op and sent force=false. With the guards in the role, a pinned or claimed target then refused (409 issue_pinned / not_assignee) on http only: dolt, embedded and proxied run the raw close as issueops.CloseIssueInTx, which applies neither the guards nor close policy. The molecule auto-close, `bd gate resolve`, `bd todo done` and `bd human dismiss` all make that call, and its signature carries no force a caller could pass. It now sends force. Over http that also stops applying close policy to the raw close, which it did at base and no other backend does; parity is the point, so that goes too. The template guard has no bypass on the wire, so a template still refuses the raw close over http where the other backends close it; no raw caller is known to close one. TestServedRawCloseKeepsParityWithTheOtherBackends closes both pin spellings, a held bead and a parent with an open child through Store.CloseIssue and requires the template refusal; with force back to false it fails on 409 issue_pinned. The close guards stay on the close verbs. A status update that crosses into the done category (`bd update -s closed`, PATCH, a batchApply update item) still answers to close policy only, so it closes a pinned issue or another actor's bead on every backend, as it did before this PR. Guarding it changes what the update legs admit and needs its own refusal mapping on PATCH and batchApply, so it is left for a follow-up; the update.go and domain/db comments now say the exclusion is deliberate. CHANGELOG: the entry moves from Fixed to Changed as BREAKING, is scoped to the close operations, names the two paths the guards do not cover, and adds a Migration line (send force or reclaim; templates are never closable). Docs: the OpBatchCloseIssues item codes, the CloseIssueChecked and CloseIssueOptions.Force docs (storage, dolt, embeddeddolt), the oracle-a scenario strings that named the retired validateIssueClosable, and the TestCloseCheckOne_CloseGuardsOutrankGateSatisfaction doc, which claimed a proxied route it does not drive. Iteration 2: the wire contract says what force waives. The OpenAPI `force` members of CloseIssueRequest, BatchCloseRequest and ApplyCloseItem said force bypasses close policy "and nothing else"; it also waives the pin and the assignee guard, and never the template guard. The batchClose "only two answers" paragraph now says the same, and types.gen.go is regenerated (`make api-gen`). "Close guards" names the close operations instead of "every route to a close" and adds that a status update into the done category is not one, and the PATCH done-crossing paragraph says a patch is never refused `issue_pinned` or `not_assignee`. The conformance comments that quoted the old Force sentence (batch_apply_contract.go, batch_closer_contract.go, lifecycle_close_reopen_contract.go) quote the current docs, and the batch_apply failure message no longer says Force does nothing else. CHANGELOG: a `close` line of `bd batch` is named among the raw closes, and "a template is never closable" becomes "the close operations never close a template, forced or not". The storage interface CloseIssue gains a doc that states its HTTP exception. TestCloseGuardRefusalsPrintTheGuardSentenceOnEveryRoute pins the sentence `bd close` prints for each guard (template, pin, assignee) through each printer (closeDirectRefusal, closeDirectTypedRefusal, closeProxiedRefusal, closeProxiedTypedRefusal), with the refusal bare, wrapped by the use case and the SQL repository, or inside a served problem envelope. One pinned refusal also goes through closeProxiedOutcomes and closeProxiedFailures, where the stderr line and the `--json` error field must both be the sentence. Six overlay mutants fail it: closeGuardRefusal recognizing no guard, or not the pin, and each of the four printers skipping its call to closeGuardRefusal. Not done here: a CLI test that `--if-revision` exits 1 on a guard refusal, not ExitGuardMismatch. External PR #7372 carries per-id fence tests for that route (6 of 7 pass at this head); they are better absorbed with credit than rewritten. Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Gas City Adopt-PR Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
163 lines
6 KiB
Go
163 lines
6 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/steveyegge/beads/issueops"
|
|
)
|
|
|
|
// The close guards — template read-only, the pin, the assignee fence — are the
|
|
// ROLE's (issueops.Lifecycle.Close and the batch roles that share its body), so
|
|
// these cases drive a fake role that refuses the way the real one does and pin
|
|
// what the wire makes of it on each of the three close routes. The real role
|
|
// behind a real server is the served leg of the conformance contracts
|
|
// (internal/httpclient: TestServedLifecycleCloseEnforcesTheCloseGuards and its
|
|
// batch siblings), which also proves the client rebuilds the typed error.
|
|
|
|
// closeGuardCases are the three refusals as a unit-of-work backend returns
|
|
// them: wrapped by the use case and the repository, so the mapping has to find
|
|
// them by type rather than by being handed the bare value.
|
|
var closeGuardCases = []struct {
|
|
name string
|
|
err error
|
|
code Code
|
|
assignee any
|
|
}{
|
|
{
|
|
name: "template",
|
|
err: &issueops.TemplateReadOnlyError{IssueID: "bd-1"},
|
|
code: CodeTemplateReadOnly,
|
|
},
|
|
{
|
|
name: "pinned",
|
|
err: &issueops.PinnedError{IssueID: "bd-1"},
|
|
code: CodeIssuePinned,
|
|
},
|
|
{
|
|
name: "not assignee",
|
|
err: &issueops.CloseNotAssigneeError{IssueID: "bd-1", Assignee: "bob", Actor: "alice"},
|
|
code: CodeNotAssignee,
|
|
assignee: "bob",
|
|
},
|
|
}
|
|
|
|
func wrapAsUOW(err error) error {
|
|
return fmt.Errorf("close bd-1: db: IssueSQLRepository.CloseChecked bd-1: %w", err)
|
|
}
|
|
|
|
// assertNoRoleProse checks a detail is this server's own words: the role's
|
|
// sentence names the actor the caller sent and the holder, and a refusal's
|
|
// detail is not where a client reads either.
|
|
func assertNoRoleProse(t *testing.T, where string, detail any) {
|
|
t.Helper()
|
|
text, _ := detail.(string)
|
|
if text == "" {
|
|
t.Errorf("%s detail = %v, want this server's own sentence", where, detail)
|
|
}
|
|
for _, leaked := range []string{"cannot modify", "cannot close", "IssueSQLRepository"} {
|
|
if strings.Contains(text, leaked) {
|
|
t.Errorf("%s detail %q carries the role's prose (%q)", where, text, leaked)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCloseGuardsAreTypedConflicts(t *testing.T) {
|
|
for _, tc := range closeGuardCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
lifecycle := &roleLifecycle{closeErr: wrapAsUOW(tc.err)}
|
|
ts := newCloseServer(t, lifecycle)
|
|
|
|
resp := ts.closeIssue(t, closePath, `{"actor":"alice"}`)
|
|
if resp.StatusCode != http.StatusConflict {
|
|
t.Fatalf("status = %d, want 409: %s", resp.StatusCode, readAll(t, resp))
|
|
}
|
|
body := decodeBody(t, resp)
|
|
if body["code"] != string(tc.code) {
|
|
t.Errorf("code = %v, want %s", body["code"], tc.code)
|
|
}
|
|
if body["assignee"] != tc.assignee {
|
|
t.Errorf("assignee = %v, want %v", body["assignee"], tc.assignee)
|
|
}
|
|
if _, ok := body["open_children"]; ok {
|
|
t.Errorf("a close guard carries open_children, which would read as close policy")
|
|
}
|
|
assertNoRoleProse(t, "close", body["detail"])
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestCloseForwardsForceToTheGuards pins that the handler hands `force` to the
|
|
// role and decides nothing itself: whether a guard is waived is the role's call.
|
|
func TestCloseForwardsForceToTheGuards(t *testing.T) {
|
|
lifecycle := &roleLifecycle{closeErr: wrapAsUOW(&issueops.TemplateReadOnlyError{IssueID: "bd-1"})}
|
|
ts := newCloseServer(t, lifecycle)
|
|
|
|
resp := ts.closeIssue(t, closePath, `{"actor":"alice","force":true}`)
|
|
if resp.StatusCode != http.StatusConflict {
|
|
t.Fatalf("status = %d, want the role's 409 under force: %s", resp.StatusCode, readAll(t, resp))
|
|
}
|
|
if got := lifecycle.closeRequests(); len(got) != 1 || !got[0].Force {
|
|
t.Fatalf("the role received %+v, want one request carrying Force", got)
|
|
}
|
|
}
|
|
|
|
func TestBatchCloseItemsCarryTheCloseGuards(t *testing.T) {
|
|
outcomes := []issueops.CloseOutcome{{IssueID: "bd-0", Issue: closedIssue("bd-0"), Changed: true}}
|
|
for _, tc := range closeGuardCases {
|
|
outcomes = append(outcomes, issueops.CloseOutcome{IssueID: "bd-1", Err: wrapAsUOW(tc.err)})
|
|
}
|
|
ts := newBatchCloseServer(t, &roleBatchCloser{result: issueops.CloseBatchResult{Outcomes: outcomes}})
|
|
|
|
resp := ts.batchClose(t, `{"actor":"alice","items":[{"id":"bd-0"},{"id":"bd-1"},{"id":"bd-1"},{"id":"bd-1"}]}`)
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("status = %d, want 200 carrying per-item refusals: %s", resp.StatusCode, readAll(t, resp))
|
|
}
|
|
got := outcomesOf(t, resp)
|
|
if len(got) != 4 {
|
|
t.Fatalf("outcomes = %d, want 4", len(got))
|
|
}
|
|
if _, refused := got[0]["code"]; refused {
|
|
t.Errorf("outcome 0 = %v, want the plain item's success", got[0])
|
|
}
|
|
for i, tc := range closeGuardCases {
|
|
item := got[i+1]
|
|
if item["code"] != string(tc.code) {
|
|
t.Errorf("%s item code = %v, want %s", tc.name, item["code"], tc.code)
|
|
}
|
|
if item["assignee"] != tc.assignee {
|
|
t.Errorf("%s item assignee = %v, want %v", tc.name, item["assignee"], tc.assignee)
|
|
}
|
|
if _, ok := item["issue"]; ok {
|
|
t.Errorf("%s item carries an issue snapshot beside its refusal", tc.name)
|
|
}
|
|
assertNoRoleProse(t, tc.name+" item", item["detail"])
|
|
}
|
|
}
|
|
|
|
func TestApplyBatchCloseItemGuardsAreTypedConflicts(t *testing.T) {
|
|
for _, tc := range closeGuardCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
applier := &roleBatchApplier{err: itemErr(1, issueops.ItemClose, "", "bd-1", wrapAsUOW(tc.err))}
|
|
ts := newApplyBatchServer(t, applier)
|
|
|
|
resp := ts.claim(t, batchApplyPath, `{"actor":"alice","items":[{"kind":"create","create":{"title":"one"}}]}`)
|
|
if resp.StatusCode != http.StatusConflict {
|
|
t.Fatalf("status = %d, want 409: %s", resp.StatusCode, readAll(t, resp))
|
|
}
|
|
body := decodeBody(t, resp)
|
|
if body["code"] != string(tc.code) {
|
|
t.Errorf("code = %v, want %s", body["code"], tc.code)
|
|
}
|
|
if body["assignee"] != tc.assignee {
|
|
t.Errorf("assignee = %v, want %v", body["assignee"], tc.assignee)
|
|
}
|
|
if body["item_index"] != float64(1) || body["item_kind"] != "close" || body["item_issue_id"] != "bd-1" {
|
|
t.Errorf("item members = %v/%v/%v, want 1/close/bd-1", body["item_index"], body["item_kind"], body["item_issue_id"])
|
|
}
|
|
assertNoRoleProse(t, "applyBatch", body["detail"])
|
|
})
|
|
}
|
|
}
|