beads/internal/httpclient/lifecycle_create_test.go
Julian Knutsen 1f1e82ab08
fix(issueops): one default priority for every create path (#7426)
* fix(issueops): one create priority default; HTTP absent priority is P2

The CLI defaulted `bd create` to P2, but issues:create, issues:batchCreate
and batch:apply create items left Issue.Priority at 0 when `priority` was
absent, so a gc/curl create stored P0 (critical) against the spec's "absent
means the default".

The default now lives in one place, the shared create preparation
(PreparePublicCreateRequest), behind a presence flag because Issue.Priority's
zero value is P0:
- issueops.CreateRequest.DefaultPriority, BatchCreateItem.DefaultPriority and
  CreateItem.DefaultPriority ask for issueops.DefaultCreatePriority (2);
  setting it with a non-zero priority is ErrValidation
  (ValidateCreatePriority, shared with the HTTP client);
- every Lifecycle.Create, BatchCreator and BatchApplier leg (dolt, embedded,
  uow) reaches it; CloneCreateBatchRequest now keeps scalar item members;
- the three handlers set the flag for an absent member and never make up 0;
  the HTTP client omits `priority` for it (wirePriority) only when the
  server advertises the new additive issues.create.defaultPriority token,
  read from the cached handshake before the dial; an older server reads an
  absent priority as P0, so the client sends DefaultCreatePriority (2)
  explicitly to it (pinCreateDefaultPriority). An explicit 0 is still sent;
- `bd create` without --priority, `bd create --graph` nodes without priority
  and `bd create --file` templates without ### Priority rely on the flag; the
  flag's displayed default and the dry-run previews read the library
  constant. CLI output is byte-identical (checked against origin/main).

Spec: the three priority descriptions now say the create default is P2 on
a server advertising issues.create.defaultPriority (apigen regenerated); the
wireshape golden already had priority optional, so no wire-shape change.

Conformance: RunLifecycleCreateAppliesTheDefaultPriority,
RunBatchCreatorAppliesTheDefaultPriority and
RunBatchApplyAppliesTheDefaultPriority on dolt, embedded, uow and http, plus
handler and client encoding tests, and
TestCreateSendsTheDefaultPriorityToAServerThatPredatesIt (new client,
handshake without the token: all three create roles send 2).

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

* fix(issueops): apply review fixups for the default create priority (bd-kb61wg)

Apply the minor and nit findings from the review of #7426:

- CHANGELOG: tell out-of-tree storage backends they must honor
  DefaultPriority (store issueops.DefaultCreatePriority, refuse it with a
  non-zero priority as ErrValidation) and which conformance cases check
  it; scope "CLI output is unchanged" to the in-tree backends.
- bd create --file: pin the default-priority path. The e2e test asserts
  the printed priorities, and a buildMarkdownBatchRequest unit test pins
  the flag and priority for an absent, a 0 and a 1 `### Priority`.
  Deleting `DefaultPriority: !template.PrioritySet` now fails both.
- bd create --file: warn on stderr about a `### Priority` that does not
  parse, as the `### Type` case already does, instead of dropping it
  silently.
- httpclient: document pinCreateDefaultPriority's same-build assumption
  (the handshake is cached per Store, so a stale one is silent here), and
  test its no-context and failed-handshake branches on all three roles.
- Formula steps, `bd q` and `bd todo add` spell
  issueops.DefaultCreatePriority instead of the literal 2.
- Conformance: the batch-creator and batch-apply default-priority cases
  also pin DefaultPriority with a non-zero priority as ErrValidation with
  nothing written.

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

* fix(issueops): mark the out-of-tree DefaultPriority change BREAKING (bd-0tcn1o)

Apply the minor findings from the relaunch review of #7426:

- CHANGELOG: move the obligation on out-of-tree storage backends out of
  the `### Fixed` entry into its own `### Changed` entry, led by
  "BREAKING (out-of-tree storage backends)" as the close-guard change
  and the earlier out-of-tree contract changes are; RELEASING.md asks
  breaking changes to carry their migration. The entry says what such a
  backend used to receive from `bd create`, `--graph` and `--file`
  (priority 2), what it receives now (priority 0 with DefaultPriority
  set), the migration, and the conformance cases that check it. The
  `### Fixed` entry keeps the HTTP P2 fix, points at the new entry, and
  names the stale-handshake residual that only a code comment named: a
  create that lands on an older build behind the same URL still stores
  P0.
- bd cook: the proto root, the gate issue and the cooked formula root
  spell issueops.DefaultCreatePriority instead of the literal 2, as the
  formula steps in the same file already do. No behavior change.

Not taken:
- The literal 2s in create_form.go, gate.go and linear.go, and the
  "default 2" in todo.go's --priority help: explicit P2s outside the
  migrated paths (the help string also feeds the generated CLI
  reference). Left for a follow-up sweep.
- Reusing in.priorityDefault in the direct bd create route: that route
  builds no createInput (gatherCreateInput runs only on the proxied and
  --file routes, and each returns), so there is nothing to reuse.
- A member-level range check in the single-create HTTP decode, as the
  batch shapes have: the asymmetry predates this PR, and the role still
  refuses an out-of-range priority on all three shapes.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 01:37:27 +00:00

289 lines
12 KiB
Go

// Contributed by gascity from bd-enterprise (internal/enterprise/httpstore/lifecycle_create_test.go@49d1df2f6)
// to OSS beads under the MIT license.
package httpclient
import (
"errors"
"reflect"
"slices"
"sort"
"strings"
"testing"
"github.com/steveyegge/beads/internal/httpapi/apigen"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/issueops"
)
// The SINGLE create's refuse-not-drop gate, and the counterpart of
// TestBatchCreateRefusesEveryMemberTheWireExcludes.
//
// The two operations flatten the same types.Issue and they flatten DIFFERENT
// slices of it — createIssue publishes twenty members where batchCreateIssues'
// item publishes eight — so neither gate can stand in for the other. What they
// do share is the ROLE's own ignore list, which is one fact about one role and
// is one table here.
//
// The four directions:
//
// carried -> wire the twenty members the client claims to carry are
// members apigen.CreateIssueRequest really publishes.
// source -> partition every populatable field of types.Issue is carried,
// ignored by the role, refused by the role, or refused by
// the wire. There is no fifth arm, so a member added
// upstream cannot arrive unclassified.
// behavior every wire-refused member really fails, citing its
// ledger row, WITHOUT dialing.
// request the request's own excluded member — IDPrefix — refuses
// the same way wherever it would act.
func TestCreateRefusesEveryMemberTheWireExcludes(t *testing.T) {
t.Run("carried members are the wire's own", func(t *testing.T) {
published := bodyMembers(t, reflect.TypeOf(apigen.CreateIssueRequest{}))
for field, wireMember := range createCarriedIssueMembers {
if _, ok := reflect.TypeOf(issueops.Issue{}).FieldByName(field); !ok {
t.Errorf("the carried table names Issue.%s, which does not exist", field)
}
if !published[wireMember] {
t.Errorf("Issue.%s claims wire member %q, which CreateIssueRequest does not publish", field, wireMember)
}
}
// The complement is the REQUEST's own members rather than the issue's,
// so this direction stops at "published"; the whole-body bijection is
// the createIssue writeShape's, which claims every member of this body
// exactly once.
for _, wireMember := range createCarriedIssueMembers {
if strings.TrimSpace(wireMember) == "" {
t.Error("the carried table names an empty wire member")
}
}
})
refused := createWireRefusedIssueMembers(t)
t.Run("the partition covers every member", func(t *testing.T) {
if len(refused) == 0 {
t.Fatal("no member is wire-refused; the partition tables have swallowed the population this gate exists for")
}
// The creation stamp and the classification plumbing are the two
// populations the operation's own description says it cannot set, and a
// create that dropped either is a row the caller believes they wrote.
// CreatedBy stays here because only a value NAMING THE ACTOR rides the
// server's stamp — see "a CreatedBy naming the actor" below.
for _, name := range []string{"CreatedAt", "CreatedBy", "SpecID", "StorageClass", "MolType", "Pinned"} {
if !slices.Contains(refused, name) {
t.Errorf("Issue.%s is not in the refused population; a create that dropped it is data loss", name)
}
}
// And the mirror image: the members this operation DOES publish that
// the batch's narrower item does not. A regression that copied the
// batch's table onto this operation would refuse all six.
for _, name := range []string{"ID", "Status", "Notes", "Metadata", "Ephemeral", "Sender"} {
if slices.Contains(refused, name) {
t.Errorf("Issue.%s is refused, but createIssue publishes it — this is the operation that fixed the batch's gap", name)
}
}
for field := range roleIgnoredCreateIssueMembers {
if _, dup := createCarriedIssueMembers[field]; dup {
t.Errorf("Issue.%s is both carried and ignored", field)
}
}
})
t.Run("every refused member fails without dialing", func(t *testing.T) {
for _, name := range refused {
t.Run(name, func(t *testing.T) {
issue := &issueops.Issue{Title: "t"}
setNonZero(t, reflect.ValueOf(issue).Elem().FieldByName(name))
w := &stubWire{}
_, err := createRole(t, w).Create(t.Context(), issueops.CreateRequest{
Actor: "writer", Issue: issue,
})
assertRefusedBy(t, err, "W-CreateRequest.Issue")
if !strings.Contains(err.Error(), name) {
t.Errorf("the refusal does not name the member: %v", err)
}
if len(w.calls) != 0 {
t.Errorf("the refused member reached the wire: %v", w.calls)
}
})
}
})
t.Run("the role's own two are ErrValidation", func(t *testing.T) {
for name := range createRoleRefusedIssueMembers {
issue := &issueops.Issue{Title: "t"}
setNonZero(t, reflect.ValueOf(issue).Elem().FieldByName(name))
w := &stubWire{}
_, err := createRole(t, w).Create(t.Context(), issueops.CreateRequest{Actor: "writer", Issue: issue})
if !errors.Is(err, issueops.ErrValidation) {
t.Errorf("Issue.%s error = %v, want ErrValidation — a local backend refuses it too", name, err)
}
if len(w.calls) != 0 {
t.Errorf("Issue.%s reached the wire: %v", name, w.calls)
}
}
})
t.Run("a CreatedBy naming the actor rides the server's stamp", func(t *testing.T) {
// Every create shape stamps created_by from the actor, so the one value
// that stamp writes is carried BY it — and it is what every CLI create
// sends. Any other value would be silently replaced, which is why the
// sweep above still refuses CreatedBy: "sentinel" is never the actor.
w := &stubWire{}
if _, err := createRole(t, w).Create(t.Context(), issueops.CreateRequest{
Actor: "writer",
Issue: &issueops.Issue{Title: "t", CreatedBy: "writer"},
}); err != nil {
t.Fatalf("Create with CreatedBy == Actor = %v, want it carried by the server's stamp", err)
}
if w.lastCreate.Actor != "writer" {
t.Errorf("sent actor = %q, want the creator the stamp will write", w.lastCreate.Actor)
}
})
t.Run("the request's own excluded member refuses where it would act", func(t *testing.T) {
w := &stubWire{}
_, err := createRole(t, w).Create(t.Context(), issueops.CreateRequest{
Actor: "writer",
IDPrefix: "other",
Issue: &issueops.Issue{ID: "other-1", Title: "t"},
})
assertRefusedBy(t, err, "W-CreateRequest.IDPrefix")
if len(w.calls) != 0 {
t.Errorf("the prefix override reached the wire: %v", w.calls)
}
})
t.Run("the request's own excluded member is inert where it would not act", func(t *testing.T) {
// The role reads IDPrefix only to check an explicit, unforced id. `bd
// create` sends the workspace's prefix on EVERY create, so refusing it
// on a minted or forced id would refuse every create in a workspace
// whose config.yaml names a prefix.
for name, req := range map[string]issueops.CreateRequest{
"a minted id": {Issue: &issueops.Issue{Title: "t"}},
"a forced id": {Issue: &issueops.Issue{ID: "other-1", Title: "t"}, ForceIDPrefix: true},
} {
t.Run(name, func(t *testing.T) {
w := &stubWire{}
request := req
request.Actor = "writer"
request.IDPrefix = "other"
if _, err := createRole(t, w).Create(t.Context(), request); err != nil {
t.Fatalf("Create = %v, want the inert override sent without", err)
}
if got := derefOr(w.lastCreate.Id, ""); got != request.Issue.ID {
t.Errorf("sent id = %q, want %q", got, request.Issue.ID)
}
if forced := w.lastCreate.ForceIdPrefix != nil && *w.lastCreate.ForceIdPrefix; forced != request.ForceIDPrefix {
t.Errorf("sent force_id_prefix = %v, want %v", forced, request.ForceIDPrefix)
}
})
}
})
t.Run("a metadata blob that is not JSON is ErrValidation, not a marshal fault", func(t *testing.T) {
// The blob's CONTENT is the role's business and travels verbatim; its
// well-formedness is this layer's, because bytes that are not JSON
// cannot go on a JSON wire and would otherwise fail inside
// json.Marshal — a transport fault where the contract promises a
// deterministic validation failure.
for name, req := range map[string]issueops.CreateRequest{
"on the issue": {
Issue: &issueops.Issue{Title: "t", Metadata: []byte(`{"k":`)},
},
"on an edge": {
Issue: &issueops.Issue{Title: "t"},
Dependencies: []issueops.CreateDependency{{TargetID: "bd-9", Type: types.DepBlocks, Metadata: `not json`}},
},
} {
t.Run(name, func(t *testing.T) {
w := &stubWire{}
request := req
request.Actor = "writer"
if _, err := createRole(t, w).Create(t.Context(), request); !errors.Is(err, issueops.ErrValidation) {
t.Errorf("Create with a malformed metadata blob = %v, want ErrValidation", err)
}
if len(w.calls) != 0 {
t.Errorf("the malformed blob reached the wire: %v", w.calls)
}
})
}
})
t.Run("the edge's one", func(t *testing.T) {
// ONE, not the batch's three: createIssue's edge publishes `reverse`
// and `metadata`, because a create has an id for a target to point back
// at. Only the thread has no member on any operation.
w := &stubWire{}
_, err := createRole(t, w).Create(t.Context(), issueops.CreateRequest{
Actor: "writer",
Issue: &issueops.Issue{Title: "t"},
Dependencies: []issueops.CreateDependency{
{TargetID: "bd-9", Type: types.DepBlocks, ThreadID: "th-1"},
},
})
assertRefusedBy(t, err, "W-CreateDependency.ThreadID")
if len(w.calls) != 0 {
t.Errorf("the thread id reached the wire: %v", w.calls)
}
})
t.Run("the request has exactly the members this gate accounts for", func(t *testing.T) {
// The CONTAINER above types.Issue, which no other gate can see: the
// createIssue writeShape classifies these fields against the body, but
// a field added to CreateRequest tomorrow and then carried by a new
// wire member would leave this gate's own reasoning stale without
// anything saying so.
got := populatableFields(reflect.TypeOf(issueops.CreateRequest{}))
sort.Strings(got)
want := []string{
"Actor", "DefaultPriority", "Dependencies", "ForceIDPrefix", "IDPrefix",
"InheritLabelsFromParent", "Issue", "ParentID", "WaitsFor",
}
if !reflect.DeepEqual(got, want) {
t.Errorf("CreateRequest carries %v, want %v.\n"+
"A new member reaches no wire member and no refusal: carry it in createBody, or refuse it with a ledger row.", got, want)
}
})
}
// createRoleRefusedIssueMembers are the two members the ROLE refuses in its own
// right, so the refusal is ErrValidation on every backend rather than a
// divergence: "Issue.Comments and Issue.Dependencies must be empty; supply edges
// through the request's own Dependencies field" (issueops.CreateRequest.Issue).
var createRoleRefusedIssueMembers = map[string]string{
"Comments": "the role refuses a create carrying comments",
"Dependencies": "the role refuses an inline edge list; edges ride the request's own Dependencies",
}
// createWireRefusedIssueMembers is the complement the gate drives: every
// populatable field of types.Issue that is neither carried by this operation's
// vocabulary, nor ignored by the role, nor refused by the role in its own right.
func createWireRefusedIssueMembers(t *testing.T) []string {
t.Helper()
var out []string
for _, name := range populatableFields(reflect.TypeOf(issueops.Issue{})) {
if _, ok := createCarriedIssueMembers[name]; ok {
continue
}
if _, ok := roleIgnoredCreateIssueMembers[name]; ok {
continue
}
if _, ok := createRoleRefusedIssueMembers[name]; ok {
continue
}
out = append(out, name)
}
return out
}
func createRole(t *testing.T, w *stubWire) issueops.Lifecycle {
t.Helper()
lifecycle, err := stubStore(t, w).IssueLifecycle()
if err != nil {
t.Fatalf("IssueLifecycle(): %v", err)
}
return lifecycle
}