beads/internal/httpapi/batch_apply.go
Julian Knutsen c4e667cb14
feat(http): list/show counts/dep tree/ping over http, the advertised external-dependency policy, one-lookup not-found (S6b/S6c/S6d) (#7471)
* feat(http): serve list/show counts/dep tree/ping, close+reopen under a session, one-lookup not-found (S6b/S6c/S6d)

S6c. The external-dependency decorator rebuilt its role views from the
legacy DoltStorage seam (storereader over itself, IsBlocked,
GetDependencyTree, ClaimReadyIssue), which a remote store only partly
serves: `bd list`/`bd children` refused on the SearchIssuesWithCounts stub,
`bd show --json` printed zero counts, the default `bd dep tree` refused,
`bd ready --claim` and `bd close` of an externally blocked issue reached
unsupported paths. For a storage.RemoteBackendStore each view now passes the
inner role through and lays the policy over it in role terms
(externaldeps/remote_roles.go): ready reads and the ready claim exclude the
workspace-wide blocked set over a widened window; per-issue checks (claim,
close, batch close, list annotations, tree leaves) read only the named
issues' edges through EdgeReader instead of every edge in the workspace. A
server advertising policy.external_dependencies (MC's does) gets a plain
passthrough. `bd ping` uses a new storage.Pinger, which the http store
answers with its authenticated context read.

S6d. A store reporting storage.CallerAttributionLimitedStore (http) cannot
carry a Provenance label or a patch's closed_by_session. The library still
refuses both; the CLI omits them where nothing is lost: `bd reopen`'s fixed
"bd: reopen <id>" label (the server labels the same entry), and the
closed_by_session of `bd update -s closed` (attribution only, usually the
ambient CLAUDE_SESSION_ID; an explicit --session prints a notice; `bd close`
still records it). See cmd/bd/remote_backend.go.

S6b. utils.ExactIDLookupStore: the resolver stops at exact lookups for a
store with no substring search, so a missing prefixed id is a not-found
(beadserrors.ErrNotFound, "no issue found matching") after one getIssue.
The general resolver no longer repeats an identical normalized probe. The
http SearchIssueIDs answers D11's partial-id refusal, which now unwraps to
ErrNotFound. Contributor auto-routing for a remote workspace reads local
config only, so the not-found fallback pays no settings round trip.
`bd update <absent> --claim` (gc's hook claim) answers a gc-classifiable
not-found in at most two requests.

Served e2e (backend/http/e2e_remote_roles_test.go): every gc list shape and
`bd children` against embedded, `show --json` count parity, default dep
tree parity, external blocking still enforced, close/reopen under
CLAUDE_SESSION_ID, missing-id request counts, absent-id claim against the
current server and the MC handshake mask, policy passthrough under the MC
mask, ping.

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

* test(externaldeps): role-level remote reader and claimer cases

bd ready and bd update --claim reach the policy through the legacy seam and
the lifecycle guard, so the remote Reader.Ready exclusion and the remote
Claimer guard (what bd serve and library callers reach) are pinned at the
role level with a fake remote store.

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

* feat(http): bd serve advertises policy.external_dependencies; clients pass through

bd serve composes every ready, claim, claim-next, close, blocking and tree
role through the external-dependency policy, but never said so. Every
client therefore ran the policy a second time, with the CLIENT machine's
external-project config, and claim-next became a non-atomic read-then-claim
loop on the client.

The server now advertises the token exactly when the roles it serves were
composed through the policy: httpapi.Config.ExternalDependencyPolicy, which
bd serve derives from its actual chain (externaldeps.Applied for the store
arm, externaldeps.AppliedToProvider for the provider arm). A server handed
raw roles still withholds it, so the token stays truthful.

Against a server that advertises it, every remote role view hands the
request to the server's own role untouched: no client validation, no edge
reads, claim-next is the server's one atomic call, and the ready count is
the server's countReadyWork (new remoteReadyCounter). The client-side
policy remains only for servers that do not advertise the token.

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

* fix(create): default created_by to the request's actor in the create role

created_by was stamped in two front doors and never in the library: the
CLI (creationActorStamp, with a remote-workspace special case) and the
three httpapi create handlers. A library caller of Lifecycle.Create or
BatchCreator with an empty CreatedBy stored "" locally and the actor over
http.

PreparePublicCreateRequest, which every create shape prepares through
(Lifecycle.Create, BatchCreator items, batch-apply create items), now
defaults an empty CreatedBy to request.Actor; an explicit value is kept.
The handlers no longer stamp, `bd create` and the state event create pass
only the actor, and creationActorStamp/isRemoteBackendWorkspace are gone.

The CLI sites that still create through the raw CreateIssue seam (swarm,
todo, gate, the create form) keep stamping currentActor(): that seam is
shared with import, where defaulting would re-attribute imported rows, and
the http store refuses it anyway, so the remote special case was never
reachable there.

Conformance: the lifecycle-create, batch-creator and batch-apply contracts
now pin created_by == actor for an item that names none, on every leg.

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

* fix(httpclient): GetIssue answers a miss with storage.ErrNotFound, like the local stores

The http store's raw GetIssue answered a missing id with (nil, nil), where
DoltStore and the embedded store return a wrapped storage.ErrNotFound. A
caller written against the backend contract read the http miss as a hit:
`bd create --graph` refused every explicit-id plan over http (patched in
cmd/bd on feat/http-mc-corpus; that workaround is reverted there).

GetIssue now returns the miss (and the empty id) as storage.ErrNotFound,
spelled as issueops.GetIssueInTx spells it. GetLabels, which reads through
GetIssue, keeps the local read's "no labels" answer for a missing id; the
`bd list --parent` walk reads the typed miss as "parent issue not found".

The served bridge test now holds the local reference store and the http
store to the same miss: ErrNotFound with the same text.

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

* test(createbatchequiv): re-record the apply golden for the created_by default

c2866ea7fe made the create role default an empty CreatedBy to the
request's actor, so every create item of the apply scenario's batch
(actor "apply-writer") now stores created_by "apply-writer" instead of "".
TestCreateBatchFastPathsMatchPerRow_*/apply compared the stored rows to a
digest recorded before that change and failed on exactly the three tables
the column reaches: issues and wisps (the row) and events (the update
event's old-value snapshot). Fast and per-row bodies still agree; the
batch-apply conformance contract pins the same value.

Bisected with bazel: passes at be92efe40f's integ twin 696f7acc8e, fails
from the created_by commit on. Only apply.json's three digests move.

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

* style: gofmt ledger.go and the externaldeps remote-roles test

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

* fix(cmd/bd): bd create reads routing settings local-only on a remote backend too

determineAutoRoutedRepoPath skipped a remote backend's settings document
(routing targets are local paths that one shared document cannot name for
every client), but getRoutingConfigValue, which `bd create` and
migrate-personal read routing.* and contributor.* through, still asked it.
The same workspace could route a create one way and a lookup another, and
every create paid a settings round trip per key.

Both now go through routingSettingsStore: nil for a remote backend, so
routing comes from config.yaml / the environment only there, and a local
store is still consulted. A unit test holds both reads to the rule with a
store that counts settings reads.

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

* fix(cmd/bd): say so when update -s closed drops an ambient CLAUDE_SESSION_ID

Over http `bd update -s closed` cannot carry closed_by_session, so the CLI
drops it rather than refusing the close. An explicit --session printed a
notice; the ambient CLAUDE_SESSION_ID every agent under Claude Code exports
was dropped without a word, a silent data difference from a local backend
(the served corpus's mc-update-meta-status-closed). The drop is now named
on stderr in both cases, once per command, pointing at `bd close`, whose
wire operation records the session. The served e2e asserts the notice.

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

* test(backend/http): hold bd list --limit 2 to each side's own unlimited order

TestE2E_ListWithGCFiltersMatchesEmbedded compared the --limit 2 rows of
the served and embedded workspaces as sets. The seed has four P2 rows, so
the limit cuts a priority tie whose created_at order differs between two
workspaces seeded seconds apart: a test-only flake, the order itself being
total. Each side's two rows must now be, in order, the first two of that
same side's --limit 0 listing; the unlimited listings are still compared
across sides by the shape loop.

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

* test: unit-tier pins for the created_by default, the policy token and the GetIssue miss

Three review mutations were caught only by a heavier tier (the uow
conformance legs, the served httpclient tier) or by one package's test
alone. Each now has a unit test where its rule lives:

- issueops: PreparePublicCreateRequest defaults an empty CreatedBy to the
  actor and keeps an explicit one, without mutating the caller's request.
- externaldeps: the policy layer AppliedToProvider finds becomes the
  handshake's policy.external_dependencies through httpapi.Listen, the
  composition bd serve performs, and a raw provider's handshake lacks it.
- httpclient (no embedded engine): GetIssue over the real wire client
  against a stub server answers a 404 not_found, and the empty id without
  dialing, as storage.ErrNotFound spelled like the local stores.

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

* docs(changelog): S6b/c/d entries match the code

The S6 entry still said `bd create` suppresses created_by on a remote
workspace and that the external-dependency policy runs client-side. Since
S6b/c/d, created_by defaults to the actor in the library on every backend,
and every bd serve composed through the policy advertises
policy.external_dependencies, so clients pass those operations through;
the client-side policy (now in role terms) runs only against a server that
does not advertise it.

New entries: the advertised policy token, bd ping over http, the created_by
library default, closed_by_session dropped by an http `bd update -s closed`
(with a notice, typed or ambient), reopen without a Provenance label over
http, local-only routing.* / contributor.* on a remote backend, GetIssue's
miss as storage.ErrNotFound, and list/children/show counts/dep tree over
http.

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

* fix(externaldeps): keep S6's list --ready, --claim-next refusal and lost-race error on the S6c composition

The re-adopted S6 head (#7288) carries the review fixup's remote role
composition; S6c replaces it with its own (rolesAreRemote, the widened
ready window, the policy passthrough). Two behaviours S6 shipped and its
reviews pinned would otherwise go:

- bd close --claim-next against a server that does not apply the policy
  is refused whole, before anything is read or closed, with "the served
  batch close cannot carry a next claim, so nothing was closed; close
  without --claim-next, then run `bd ready --claim`". S6c passed the batch
  to the http closer, whose W-CloseBatchRequest.ClaimNext refusal drops the
  remedy, so TestE2E_BDOverHTTP's two refuseClaimNext steps went red. A
  request invalid on every backend is still ErrValidation first, and a
  server that applies the policy still gets the request untouched.
- remoteReadyClaimer.ClaimNext reports a page on which every candidate was
  lost to another actor as "claim ready: lost N races ...", not as the
  empty answer that means there is no work.

- `bd list --ready` (List with ReadyFlag) is answered by the policy
  store's own reader over the served ready reads, as S6 does. S6c passed
  it to the served listing, which cannot carry ReadyFlag (httpclient
  ledger row E-ListRequest.ReadyFlag), so it exited 1 over http with or
  without external refs; TestE2E_BDOverHTTP and
  TestServedRolesKeepTheExternalDependencyPolicy were red on it.
  TestRemoteReaderAnswersAReadyListingWithoutTheWireFlag pins it, with the
  fake's served List refusing ReadyFlag as the wire does.

S6's composition tests now run against S6c as remote_roles_composition_test.go.
Their fake serves Ready the way a server does (the S6 composition read
ready work through the legacy policy reader instead), and two call logs
gain that Ready. TestRemoteBatchCloserRefusesANextClaim,
TestRemoteBatchCloserValidatesANextClaimFirst and
TestRemoteReadyClaimerReportsLostRaces were red on S6c before this change.

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

* fix(externaldeps): unassigned claim-next and fail-closed close reads

Review fixes for the S6c composition against a server that does not
advertise policy.external_dependencies: the two majors of review
attempt 1, plus the minors on the same code.

- ReadyClaimer: the composed claim-next reads only open, unassigned
  ready rows (Unassigned=true, Assignee cleared), the candidates the
  local ClaimReadyIssueInTx selects. A row another actor holds, or one
  already assigned to the caller, is no longer taken or counted as a
  lost race. Each pass reads 25 candidates past the blocked rows, as
  httpclient's composed claim does, and a full window lost end to end
  is read once more: claimed rows leave the unassigned set. Only a
  claim that tried candidates and won none is an error.
- BatchCloser: an externally blocked item travels to the server only
  when its status read finds it closed or not found (liveRefusal). Any
  other read error is that item's outcome, so a transient or auth
  failure no longer forwards a refused close to a server that will not
  refuse it.
- TreeWalker: a failed capability probe renders the external leaves
  over the walked tree instead of failing a walk the server already
  answered. appendTreeExternalReferences skips an id the tree holds,
  so an enforcing server's own leaves are not doubled.
- remoteEdges names each anchor once and skips an empty one, which the
  http EdgeReader refuses with ErrValidation.

The composition fakes now honor Unassigned/Assignee as
workapi.BuildReadyFilter does, model a rival claim landing first and a
failing GetIssue, and refuse an empty edge anchor as httpclient does.
New: TestRemoteReadyClaimerTakesOnlyUnassignedWork,
TestRemoteReadyClaimerWalksAContendedFront,
TestRemoteBatchCloserAnswersAFailedReadWithIt,
TestRemoteEdgesNamesEachAnchorOnce, and the probe-failure cases of
TestRemoteTreeWalkerAddsExternalLeaves. Each is red with its fix
reverted.

From review attempt 2, three nearby cleanups:

- TestServedRolesKeepTheExternalDependencyPolicy seeds open rows held
  by another actor and by the claimant ahead of the free work, then
  claims with and without an assignee filter. The unassigned-only claim
  is now pinned over a real served server, not only through the fakes:
  red with the pre-fixup remote_roles.go, with filter.Unassigned
  dropped, and (the filtered claim) with an explicit assignee honored.
- The capabilities description in openapi.v0.yaml (and the regenerated
  types.gen.go comment) no longer calls the whole list build-level:
  policy.external_dependencies depends on how the server composed its
  roles, as the paragraph above it says.
- createbatchequiv's golden note names
  issueops.PreparePublicCreateRequest instead of a commit id that is
  not on this branch.

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

* test(backend/http): count a not_found read as reaching the remote in #7469's credential doors

main's #7469 added TestExplicitCredentialIgnoresTheEnvPlaintextGrant. Its
OpenWith and Open doors read bd-1 from plaintextRemote, which answers
every issue read not_found, and the granted leg needs that read to return
a nil error. That held while the http client answered a miss with
(nil, nil). This branch's "fix(httpclient): GetIssue answers a miss with
storage.ErrNotFound, like the local stores" makes the same read return
storage.ErrNotFound. So on main plus this branch both doors fail at
per_open_credential_test.go:392 ("with Target.AllowInsecureCredential:
not found: issue bd-1") under plain go test. The merge is textually
clean, so only a build of the merge shows it.

The two store doors now count storage.ErrNotFound as success, since that
answer came off the wire. The assertions are unchanged. The refused leg
still needs the "refusing to send a credential" error and no request on
the transport. The granted leg still needs exactly one request carrying
"Bearer explicit-token": a granted read that never reaches the wire still
fails at :408.

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 18:11:56 +00:00

1392 lines
56 KiB
Go

package httpapi
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"time"
"unicode/utf8"
"github.com/steveyegge/beads/internal/httpapi/apigen"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/issueops"
)
// The wire adapter over issueops.BatchApplier: an ORDERED, heterogeneous plan
// applied as one transaction or not at all.
//
// It carries the claim's posture verbatim. The actor is caller-ASSERTED
// provenance for the audit trail and not authenticated identity; hooks do not
// fire and the per-command auto-commit machinery does not run. The only durable
// effect is the single storage commit the role makes inside its own
// transaction.
//
// EVERYTHING ABOVE THE ROLE IS ARGUMENT VALIDATION. What the values MEAN — how
// a key resolves and which way it may reach, whether a precondition holds
// as-modified, whether a waits-for gate is one of the two, what the graph the
// whole request built permits — belongs to issueops.BatchApplier. This file
// decodes a body four levels deep, refuses the shapes the document refuses, and
// maps the role's TYPED refusals onto the frozen codes.
const (
// maxApplyBatchItems is the document's cap on `items`, and the role's own
// bound restated at the edge so an over-long request costs no database work.
// It bounds how long one request may hold a write transaction — not batch
// semantics, which have no size in them.
maxApplyBatchItems = issueops.MaxApplyBatchItems
// maxApplyBatchBodyBytes bounds the request body. A thousand items each
// carrying a description, a design, acceptance criteria and a metadata
// document is the shape this has to admit, so it refuses the absurd before
// any of it is parsed.
//
// Raised from 4 MiB to 16 MiB alongside maxApplyBatchItems's 100->1000
// raise: the measured max shape's body (102 creates, descriptions
// totaling about 878 KB) is about 1.1 MB, and 1000 items at the same
// density is comfortably inside 16 MiB with headroom for a caller that
// writes longer descriptions than that sample.
//
// See TestCapBatchApplyLargeTiesAllThreeLimits: this constant,
// largeApplyItemThreshold and issueops.MaxApplyBatchItems are pinned
// together with CapBatchApplyLarge, the token that advertises all three.
//
// See maxInflight's doc comment (server.go) for the worst-case concurrent
// decode memory this implies: maxInflight requests may each be decoding a
// body up to this size at once, before the large-apply semaphore narrows
// how many of them can go on to actually run.
maxApplyBatchBodyBytes = 16 << 20
// largeApplyItemThreshold is where a request crosses from "the original,
// always-supported shape" to "the raised envelope": a request with MORE
// than this many items holds the largeApplySem slot (serializing
// oversized transactions one at a time, Server.acquireLargeApply) and
// runs under a whole separate, EXTENDED budget (Server.largeApplyCeiling)
// instead of the ordinary requestDeadline every other request gets. It is
// deliberately the OLD cap, not some fraction of the new one: every
// request at or under this threshold runs under EXACTLY the deadline it
// always has — route() applies requestDeadline unconditionally, and
// nothing in this file touches it for such a request.
largeApplyItemThreshold = 100
)
// The document's member list at each of this body's levels. Every schema is
// additionalProperties: false, so anything else is refused BY NAME — which is
// why each level is decoded as raw members first, and why the levels below the
// request are checked in the same pass that projects them.
//
// PRESENCE IS THE SIGNAL at three of these levels and that is the second reason
// for the raw decode: an item's payload members carry the tagged union's
// disagreement cases, a patch member present is written where an absent one is
// untouched, and a metadata member present holding `null` is a value.
var (
applyBatchRequestMembers = []string{"actor", "force_id_prefix", "items", "provenance", "skip_per_edge_cycle_check"}
applyItemMembers = []string{"close", "create", "dep_add", "kind", "update"}
applyRefMembers = []string{"id", "key"}
applyCreateItemMembers = []string{
"acceptance_criteria", "assignee", "defer_until", "description", "design",
"due_at", "ephemeral", "estimated_minutes", "external_ref", "id",
"issue_type", "key", "labels", "metadata", "metadata_refs", "no_history",
"notes", "owner", "priority", "sender", "status", "title",
}
applyUpdateItemMembers = []string{
"expected_assignee", "expected_status", "expected_version",
"force_assignee_transfer", "force_close_policy", "force_notes_overwrite", "patch", "target",
}
applyPatchMembers = []string{
"acceptance_criteria", "append_notes", "assignee", "defer_until",
"description", "design", "due_at", "estimated_minutes", "external_ref",
"issue_type", "labels", "metadata", "notes", "owner", "priority",
"status", "title",
}
applyLabelPatchMembers = []string{"add", "remove", "replace"}
applyMetadataPatchMembers = []string{"merge", "replace", "set", "unset"}
applyCloseItemMembers = []string{"expected_version", "force", "reason", "session", "target"}
applyDepAddItemMembers = []string{"has_spawner", "metadata", "source", "target", "thread_id", "type"}
// applyItemKinds is the tag vocabulary, paired with the payload member each
// value names. It is one map rather than a switch so the enum, the member
// names and the agreement check cannot drift into three opinions.
applyItemKinds = map[issueops.ItemKind]string{
issueops.ItemCreate: "create",
issueops.ItemUpdate: "update",
issueops.ItemClose: "close",
issueops.ItemDepAdd: "dep_add",
}
// applyNullablePatchMembers is the closed set on which explicit `null`
// CLEARS rather than refuses, exactly as it is for PATCH
// /v0/beads/issues/{id}: they are the members the role models as Field[*T],
// because a pointer is the only thing a clear has to write.
applyNullablePatchMembers = map[string]bool{
"estimated_minutes": true,
"external_ref": true,
"due_at": true,
"defer_until": true,
}
)
// handleApplyBatch applies every item in the request body in order, or applies
// none of them.
//
// The transaction boundary, the ref resolution, the as-modified preconditions,
// the close policy, the assignee fence, the metadata splice and the end gate
// all belong to issueops.BatchApplier.
func (s *Server) handleApplyBatch(w http.ResponseWriter, r *http.Request) {
if !s.requireNoQuery(w, r) {
return
}
if !s.requireJSONContent(w, r) {
return
}
request, ok := s.applyBatchRequest(w, r)
if !ok {
return
}
// One-wide "large write" semaphore: a request carrying more than
// largeApplyItemThreshold items serializes against every other large
// apply, so at most one oversized transaction holds a write connection
// at a time, and runs under an EXTENDED budget built fresh at the moment
// it is admitted (Server.acquireLargeApply). An ordinary (<=threshold)
// request never touches this, is never waited on by one, and keeps
// r.Context() exactly as route() built it — requestDeadline,
// unconditionally, the same as it always has.
if len(request.Items) > largeApplyItemThreshold {
runCtx, release, err := s.acquireLargeApply(r.Context())
if err != nil {
s.failApplyBatch(w, r, request, err)
return
}
defer release()
r = r.WithContext(runCtx)
}
applier, err := s.batchApplier(r)
if err != nil {
s.failApplyBatch(w, r, request, err)
return
}
result, err := applier.ApplyBatch(r.Context(), request)
if err != nil {
s.failApplyBatch(w, r, request, err)
return
}
writeJSON(w, applyBatchResponse(result))
}
// applyBatchRequest decodes and validates the body, and reports whether the
// request may proceed. Every refusal here happens BEFORE any database work,
// which is what lets these 400s reflect the caller's own input back.
func (s *Server) applyBatchRequest(w http.ResponseWriter, r *http.Request) (issueops.ApplyBatchRequest, bool) {
refuse := func(res *Result) (issueops.ApplyBatchRequest, bool) {
s.fail(w, r, *res)
return issueops.ApplyBatchRequest{}, false
}
members, res := decodeJSONObject(w, r, maxApplyBatchBodyBytes)
if res != nil {
return refuse(res)
}
if offender, unknown := unknownMember(members, applyBatchRequestMembers); unknown {
s.failUnknownMember(w, r, offender, applyBatchRequestMembers)
return issueops.ApplyBatchRequest{}, false
}
actor, ok := s.bodyActor(w, r, members)
if !ok {
return issueops.ApplyBatchRequest{}, false
}
provenance, res := applyTextMember(members, "", "provenance")
if res != nil {
return refuse(res)
}
forceIDPrefix, res := applyBoolMember(members, "", "force_id_prefix")
if res != nil {
return refuse(res)
}
skipPerEdge, res := applyBoolMember(members, "", "skip_per_edge_cycle_check")
if res != nil {
return refuse(res)
}
items, res := applyItems(members)
if res != nil {
return refuse(res)
}
// No created_by stamp: the role defaults each create item's to the actor.
return issueops.ApplyBatchRequest{
Actor: actor,
Items: items,
Provenance: provenance,
ForceIDPrefix: forceIDPrefix,
SkipPerEdgeCycleCheck: skipPerEdge,
}, true
}
// applyItems validates `items` and projects it onto the role's items, in
// request order — which this operation never changes.
func applyItems(members map[string]json.RawMessage) ([]issueops.ApplyItem, *Result) {
refuse := func(detail string) *Result {
res := InvalidArgument("items", ReasonInvalidValue, detail)
return &res
}
raw, ok := members["items"]
if !ok {
return nil, refuse("`items` is required")
}
var rawItems []map[string]json.RawMessage
if err := json.Unmarshal(raw, &rawItems); err != nil || rawItems == nil {
return nil, refuse("`items` must be an array of objects")
}
switch {
case len(rawItems) == 0:
return nil, refuse("`items` must carry at least one item; a request that writes nothing is refused rather than answered")
case len(rawItems) > maxApplyBatchItems:
return nil, refuse(fmt.Sprintf("`items` carries %d items; the limit is %d per request", len(rawItems), maxApplyBatchItems))
}
items := make([]issueops.ApplyItem, 0, len(rawItems))
for i, rawItem := range rawItems {
item, res := applyItem(i, rawItem)
if res != nil {
return nil, res
}
items = append(items, item)
}
return items, nil
}
// applyItem projects one tagged item onto the role's item.
//
// IT ENFORCES THE TAG BY HAND, and that is the cost of the document's own
// doctrine rather than an oversight here. The item is a single-shape object
// with a required `kind` and four optional payload members, because a schema
// alternation would need a composition keyword this document does not use — so
// nothing in the generated type stops a client sending two payloads, or a
// payload the kind does not name. Both are answered here, before the role sees
// a request it would have to refuse for the same reason.
func applyItem(index int, raw map[string]json.RawMessage) (issueops.ApplyItem, *Result) {
prefix := applyItemParam(index, "")
if raw == nil {
res := InvalidArgument(prefix, ReasonInvalidValue, "an item must be a JSON object")
return issueops.ApplyItem{}, &res
}
if offender, unknown := unknownMember(raw, applyItemMembers); unknown {
return issueops.ApplyItem{}, applyUnknownMember(prefix+".", offender, applyItemMembers)
}
kindText, res := applyRequiredText(raw, prefix+".", "kind")
if res != nil {
return issueops.ApplyItem{}, res
}
kind := issueops.ItemKind(kindText)
named, known := applyItemKinds[kind]
if !known {
res := InvalidArgument(prefix+".kind", ReasonInvalidValue,
"`kind` must be one of "+strings.Join(applyKindNames(), ", "))
return issueops.ApplyItem{}, &res
}
// The tag and the payloads have to agree in both directions: a kind with no
// payload is an item that does nothing, and a payload the kind does not name
// is an item whose two halves disagree.
var present []string
for _, member := range []string{"create", "update", "close", "dep_add"} {
if _, ok := raw[member]; ok {
present = append(present, member)
}
}
switch {
case len(present) == 0:
res := InvalidArgument(prefix+"."+named, ReasonInvalidValue,
"an item of kind `"+kindText+"` must carry its `"+named+"` payload")
return issueops.ApplyItem{}, &res
case len(present) > 1:
res := InvalidArgument(prefix+"."+present[1], ReasonInvalidValue,
"an item carries exactly one payload; this one carries "+strings.Join(present, " and "))
return issueops.ApplyItem{}, &res
case present[0] != named:
res := InvalidArgument(prefix+"."+present[0], ReasonInvalidValue,
"this item is kind `"+kindText+"` but carries the `"+present[0]+"` payload; the two must name the same verb")
return issueops.ApplyItem{}, &res
}
payload, res := applyObjectMember(raw, prefix+".", named)
if res != nil {
return issueops.ApplyItem{}, res
}
item := issueops.ApplyItem{Kind: kind}
payloadPrefix := prefix + "." + named + "."
switch kind {
case issueops.ItemCreate:
create, res := applyCreateItem(payloadPrefix, raw[named], payload)
if res != nil {
return issueops.ApplyItem{}, res
}
item.Create = create
case issueops.ItemUpdate:
update, res := applyUpdateItem(payloadPrefix, raw[named], payload)
if res != nil {
return issueops.ApplyItem{}, res
}
item.Update = update
case issueops.ItemClose:
closeItem, res := applyCloseItem(payloadPrefix, payload)
if res != nil {
return issueops.ApplyItem{}, res
}
item.Close = closeItem
case issueops.ItemDepAdd:
depAdd, res := applyDepAddItem(payloadPrefix, payload)
if res != nil {
return issueops.ApplyItem{}, res
}
item.DepAdd = depAdd
}
return item, nil
}
// applyCreateItem projects a create payload onto the role's create item.
//
// It decodes into the GENERATED struct after the raw member check, which is
// what makes a member's type the DOCUMENT's type: `priority: "high"` is refused
// here rather than reaching a role that would have to guess what the caller
// meant.
func applyCreateItem(prefix string, encoded json.RawMessage, raw map[string]json.RawMessage) (*issueops.CreateItem, *Result) {
refuse := func(member, detail string) *Result {
res := InvalidArgument(applyParam(prefix, member), ReasonInvalidValue, detail)
return &res
}
if offender, unknown := unknownMember(raw, applyCreateItemMembers); unknown {
return nil, applyUnknownMember(prefix, offender, applyCreateItemMembers)
}
var wire apigen.ApplyCreateItem
if err := json.Unmarshal(encoded, &wire); err != nil {
return nil, refuse("", "a `create` member carries the wrong JSON type")
}
if strings.TrimSpace(wire.Title) == "" {
return nil, refuse("title", "`title` is required and must not be blank")
}
// The role validates the type, the status and the id prefix against the
// workspace's own configuration, which this server cannot read without a
// transaction; what is checked here is only what the schema declares. A
// SLICE and not a map, so an item breaking two rules always names the same
// offender: `param` is what a client dispatches on and it must not depend on
// map order.
for _, bounded := range []struct {
member string
value *string
}{
{"title", &wire.Title}, {"id", wire.Id}, {"key", wire.Key},
{"issue_type", wire.IssueType}, {"status", wire.Status},
{"assignee", wire.Assignee}, {"owner", wire.Owner},
{"external_ref", wire.ExternalRef}, {"sender", wire.Sender},
} {
if res := applyBoundedText(prefix, bounded.member, bounded.value); res != nil {
return nil, res
}
}
if wire.Ephemeral != nil && *wire.Ephemeral && wire.NoHistory != nil && *wire.NoHistory {
return nil, refuse("no_history", "`ephemeral` and `no_history` select different retention modes; send one")
}
issue := &types.Issue{
ID: derefString(wire.Id),
Title: wire.Title,
Description: derefString(wire.Description),
Design: derefString(wire.Design),
AcceptanceCriteria: derefString(wire.AcceptanceCriteria),
Notes: derefString(wire.Notes),
Status: types.Status(derefString(wire.Status)),
IssueType: types.IssueType(derefString(wire.IssueType)),
Assignee: derefString(wire.Assignee),
Owner: derefString(wire.Owner),
EstimatedMinutes: wire.EstimatedMinutes,
ExternalRef: wire.ExternalRef,
DueAt: wire.DueAt,
DeferUntil: wire.DeferUntil,
Sender: derefString(wire.Sender),
Metadata: wire.Metadata,
}
if wire.Priority != nil {
if *wire.Priority < 0 || *wire.Priority > 4 {
return nil, refuse("priority", fmt.Sprintf("`priority` is %d; the range is 0 to 4", *wire.Priority))
}
issue.Priority = *wire.Priority
}
if wire.Labels != nil {
if res := applyBoundedLabels(prefix, "labels", *wire.Labels); res != nil {
return nil, res
}
issue.Labels = *wire.Labels
}
if wire.Ephemeral != nil {
issue.Ephemeral = *wire.Ephemeral
}
if wire.NoHistory != nil {
issue.NoHistory = *wire.NoHistory
}
// An absent `priority` is the role's default (CreateItem.DefaultPriority),
// never a 0 — which is P0 — made up here.
item := &issueops.CreateItem{Key: derefString(wire.Key), Issue: issue, DefaultPriority: wire.Priority == nil}
refs, res := applyMetadataRefs(prefix, raw)
if res != nil {
return nil, res
}
item.MetadataRefs = refs
return item, nil
}
// applyMetadataRefs decodes the one member whose keys may reach FORWARD. The
// refs are read raw rather than off the generated map so an unknown member
// inside one is refused by name like every other one.
func applyMetadataRefs(prefix string, raw map[string]json.RawMessage) (map[string]issueops.Ref, *Result) {
member, ok := raw["metadata_refs"]
if !ok {
return nil, nil
}
var entries map[string]json.RawMessage
if err := json.Unmarshal(member, &entries); err != nil || entries == nil {
res := InvalidArgument(prefix+"metadata_refs", ReasonInvalidValue,
"`metadata_refs` must be an object whose values are refs")
return nil, &res
}
refs := make(map[string]issueops.Ref, len(entries))
for key, encoded := range entries {
ref, res := applyRef(prefix+"metadata_refs."+key, encoded)
if res != nil {
return nil, res
}
refs[key] = ref
}
return refs, nil
}
// applyUpdateItem projects an update payload onto the role's update item.
func applyUpdateItem(prefix string, encoded json.RawMessage, raw map[string]json.RawMessage) (*issueops.UpdateItem, *Result) {
if offender, unknown := unknownMember(raw, applyUpdateItemMembers); unknown {
return nil, applyUnknownMember(prefix, offender, applyUpdateItemMembers)
}
// The guard is read BEFORE the whole-item decode, and through the same
// reader every other operation uses. It is the one member of this item whose
// wire type is not its Go type — a decimal string standing for an int64 —
// so letting the struct decode reach it first would answer a mistyped token
// with the item-level "wrong JSON type" instead of a 400 naming the member.
expectedVersion, res := applyVersionGuardMember(raw, prefix)
if res != nil {
return nil, res
}
var wire apigen.ApplyUpdateItem
if err := json.Unmarshal(encoded, &wire); err != nil {
res := InvalidArgument(applyParam(prefix, ""), ReasonInvalidValue, "an `update` member carries the wrong JSON type")
return nil, &res
}
target, res := applyRequiredRef(prefix, raw, "target")
if res != nil {
return nil, res
}
for _, bounded := range []struct {
member string
value *string
}{{"expected_status", wire.ExpectedStatus}, {"expected_assignee", wire.ExpectedAssignee}} {
if res := applyBoundedText(prefix, bounded.member, bounded.value); res != nil {
return nil, res
}
}
patchRaw, res := applyObjectMember(raw, prefix, "patch")
if res != nil {
return nil, res
}
patch, res := applyPatch(prefix+"patch.", raw["patch"], patchRaw)
if res != nil {
return nil, res
}
item := &issueops.UpdateItem{Target: target, Patch: patch, ExpectedVersion: expectedVersion}
if wire.ExpectedStatus != nil {
status := issueops.Status(*wire.ExpectedStatus)
item.ExpectedStatus = &status
}
item.ExpectedAssignee = wire.ExpectedAssignee
if wire.ForceClosePolicy != nil {
item.ForceClosePolicy = *wire.ForceClosePolicy
}
if wire.ForceAssigneeTransfer != nil {
item.ForceAssigneeTransfer = *wire.ForceAssigneeTransfer
}
if wire.ForceNotesOverwrite != nil {
item.ForceNotesOverwrite = *wire.ForceNotesOverwrite
}
return item, nil
}
// applyPatch projects the decoded `patch` member onto the role's IssuePatch.
//
// MEMBER PRESENCE IS THE SIGNAL, which is why this level is read as raw members
// too: a member present sets the role's Field, a member absent leaves the field
// untouched, and the generated struct cannot tell those apart because it models
// both as a nil pointer. Explicit `null` is a third state read straight off the
// raw bytes — a clear on the four nullable members, and a 400 naming the member
// everywhere else.
func applyPatch(prefix string, encoded json.RawMessage, raw map[string]json.RawMessage) (issueops.IssuePatch, *Result) {
refuse := func(member, detail string) (issueops.IssuePatch, *Result) {
res := InvalidArgument(applyParam(prefix, member), ReasonInvalidValue, detail)
return issueops.IssuePatch{}, &res
}
if len(raw) == 0 {
// A write that writes nothing is a client bug, not a no-op to answer.
return refuse("", "`patch` must carry at least one field; an update that updates nothing is refused rather than answered")
}
if offender, unknown := unknownMember(raw, applyPatchMembers); unknown {
return issueops.IssuePatch{}, applyUnknownMember(prefix, offender, applyPatchMembers)
}
// Explicit null, before any typed decode: unmarshaling null into *T yields
// nil, which is indistinguishable from the member being absent, so a null on
// a non-nullable member would otherwise slide through as "untouched" — a
// write the client asked for and the server silently dropped.
for name, value := range raw {
if isJSONNull(value) && !applyNullablePatchMembers[name] {
return refuse(name, "`"+name+"` is not nullable; omit it to leave the field unchanged")
}
}
var wire apigen.ApplyPatchBody
if err := json.Unmarshal(encoded, &wire); err != nil {
return refuse("", "a `patch` member carries the wrong JSON type")
}
for _, bounded := range []struct {
member string
value *string
}{
{"issue_type", wire.IssueType}, {"status", wire.Status},
{"assignee", wire.Assignee}, {"owner", wire.Owner},
{"external_ref", wire.ExternalRef},
} {
if res := applyBoundedText(prefix, bounded.member, bounded.value); res != nil {
return issueops.IssuePatch{}, res
}
}
patch := issueops.IssuePatch{}
set := func(name string) bool { _, present := raw[name]; return present }
if set("title") {
title := *wire.Title
if strings.TrimSpace(title) == "" {
return refuse("title", "`title` must not be blank")
}
if res := applyBoundedText(prefix, "title", &title); res != nil {
return issueops.IssuePatch{}, res
}
patch.Title = issueops.Field[string]{Set: true, Value: title}
}
if set("description") {
patch.Description = issueops.Field[string]{Set: true, Value: *wire.Description}
}
if set("design") {
patch.Design = issueops.Field[string]{Set: true, Value: *wire.Design}
}
if set("acceptance_criteria") {
patch.AcceptanceCriteria = issueops.Field[string]{Set: true, Value: *wire.AcceptanceCriteria}
}
// The role refuses both together too; refusing here keeps the 400 a
// statement about the request rather than a translated storage error.
if set("notes") && set("append_notes") {
return refuse("append_notes", "`notes` replaces the notes and `append_notes` adds to them; send one")
}
if set("notes") {
patch.Notes = issueops.Field[string]{Set: true, Value: *wire.Notes}
}
if set("append_notes") {
patch.AppendNotes = issueops.Field[string]{Set: true, Value: *wire.AppendNotes}
}
if set("priority") {
priority := *wire.Priority
if priority < 0 || priority > 4 {
return refuse("priority", fmt.Sprintf("`priority` is %d; the range is 0 to 4", priority))
}
patch.Priority = issueops.Field[int]{Set: true, Value: priority}
}
if set("issue_type") {
patch.IssueType = issueops.Field[issueops.IssueType]{Set: true, Value: issueops.IssueType(*wire.IssueType)}
}
if set("status") {
patch.Status = issueops.Field[issueops.Status]{Set: true, Value: issueops.Status(*wire.Status)}
}
if set("assignee") {
patch.Assignee = issueops.Field[string]{Set: true, Value: *wire.Assignee}
}
if set("owner") {
patch.Owner = issueops.Field[string]{Set: true, Value: *wire.Owner}
}
// The four nullable members. Set is true whenever the member is present; the
// VALUE is a nil pointer for an explicit null, which is how a clear reaches
// the role.
if set("estimated_minutes") {
patch.EstimatedMinutes = issueops.Field[*int]{Set: true, Value: wire.EstimatedMinutes}
}
if set("external_ref") {
patch.ExternalRef = issueops.Field[*string]{Set: true, Value: wire.ExternalRef}
}
if set("due_at") {
patch.DueAt = issueops.Field[*time.Time]{Set: true, Value: wire.DueAt}
}
if set("defer_until") {
patch.DeferUntil = issueops.Field[*time.Time]{Set: true, Value: wire.DeferUntil}
}
if set("labels") {
labels, res := applyLabelPatch(prefix+"labels.", raw["labels"])
if res != nil {
return issueops.IssuePatch{}, res
}
patch.Labels = labels
}
if set("metadata") {
metadata, res := applyMetadataPatch(prefix+"metadata.", raw["metadata"])
if res != nil {
return issueops.IssuePatch{}, res
}
patch.Metadata = metadata
}
return patch, nil
}
// applyLabelPatch projects the label edit, which is the FULL patch here rather
// than PATCH /v0/beads/issues/{id}'s complete replacement: a plan edits a label
// set it did not compose, and replacement would mean reading it back first.
func applyLabelPatch(prefix string, encoded json.RawMessage) (issueops.LabelPatch, *Result) {
var raw map[string]json.RawMessage
if err := json.Unmarshal(encoded, &raw); err != nil || raw == nil {
res := InvalidArgument(applyParam(prefix, ""), ReasonInvalidValue, "`labels` must be a JSON object")
return issueops.LabelPatch{}, &res
}
if offender, unknown := unknownMember(raw, applyLabelPatchMembers); unknown {
return issueops.LabelPatch{}, applyUnknownMember(prefix, offender, applyLabelPatchMembers)
}
var wire apigen.ApplyLabelPatch
if err := json.Unmarshal(encoded, &wire); err != nil {
res := InvalidArgument(applyParam(prefix, ""), ReasonInvalidValue, "a `labels` member carries the wrong JSON type")
return issueops.LabelPatch{}, &res
}
patch := issueops.LabelPatch{}
for _, edit := range []struct {
member string
value *[]string
assign func([]string)
}{
{"replace", wire.Replace, func(v []string) {
// Presence is the signal, and an EMPTY array clears every label —
// which is why this is a Field rather than a plain slice.
patch.Replace = issueops.Field[[]string]{Set: true, Value: v}
}},
{"add", wire.Add, func(v []string) { patch.Add = v }},
{"remove", wire.Remove, func(v []string) { patch.Remove = v }},
} {
if _, present := raw[edit.member]; !present {
continue
}
if edit.value == nil {
res := InvalidArgument(prefix+edit.member, ReasonInvalidValue, "`"+edit.member+"` must be an array of strings")
return issueops.LabelPatch{}, &res
}
if res := applyBoundedLabels(prefix, edit.member, *edit.value); res != nil {
return issueops.LabelPatch{}, res
}
edit.assign(*edit.value)
}
return patch, nil
}
// applyMetadataPatch projects the metadata edit.
//
// `set` IS READ RAW AND NEVER THROUGH THE GENERATED MAP, and that is a
// correctness fix rather than a shortcut: the generator models a map value as
// *MetadataValue, and encoding/json answers a JSON null against a pointer by
// setting the pointer to nil before any UnmarshalJSON runs — so a caller
// writing a key to JSON null would have it collapse into an absent key, which
// on this plane is the opposite request. The raw bytes carry the literal.
func applyMetadataPatch(prefix string, encoded json.RawMessage) (issueops.MetadataPatch, *Result) {
var raw map[string]json.RawMessage
if err := json.Unmarshal(encoded, &raw); err != nil || raw == nil {
res := InvalidArgument(applyParam(prefix, ""), ReasonInvalidValue, "`metadata` must be a JSON object")
return issueops.MetadataPatch{}, &res
}
if offender, unknown := unknownMember(raw, applyMetadataPatchMembers); unknown {
return issueops.MetadataPatch{}, applyUnknownMember(prefix, offender, applyMetadataPatchMembers)
}
patch := issueops.MetadataPatch{}
if value, present := raw["replace"]; present {
if len(raw) > 1 {
res := InvalidArgument(prefix+"replace", ReasonInvalidValue,
"`replace` writes the whole document and cannot be combined with `merge`, `set` or `unset`")
return issueops.MetadataPatch{}, &res
}
patch.Replace = issueops.Field[json.RawMessage]{Set: true, Value: applyRawCopy(value)}
}
if value, present := raw["merge"]; present {
patch.Merge = issueops.Field[json.RawMessage]{Set: true, Value: applyRawCopy(value)}
}
if value, present := raw["set"]; present {
var entries map[string]json.RawMessage
if err := json.Unmarshal(value, &entries); err != nil || entries == nil {
res := InvalidArgument(prefix+"set", ReasonInvalidValue, "`set` must be an object of metadata values")
return issueops.MetadataPatch{}, &res
}
patch.Set = make(map[string]json.RawMessage, len(entries))
for key, keyValue := range entries {
patch.Set[key] = applyRawCopy(keyValue)
}
}
if value, present := raw["unset"]; present {
var keys []string
if err := json.Unmarshal(value, &keys); err != nil || keys == nil {
res := InvalidArgument(prefix+"unset", ReasonInvalidValue, "`unset` must be an array of strings")
return issueops.MetadataPatch{}, &res
}
patch.Unset = keys
}
return patch, nil
}
// applyCloseItem projects a close payload onto the role's close item.
func applyCloseItem(prefix string, raw map[string]json.RawMessage) (*issueops.CloseItem, *Result) {
if offender, unknown := unknownMember(raw, applyCloseItemMembers); unknown {
return nil, applyUnknownMember(prefix, offender, applyCloseItemMembers)
}
target, res := applyRequiredRef(prefix, raw, "target")
if res != nil {
return nil, res
}
reason, res := applyTextMember(raw, prefix, "reason")
if res != nil {
return nil, res
}
session, res := applyTextMember(raw, prefix, "session")
if res != nil {
return nil, res
}
force, res := applyBoolMember(raw, prefix, "force")
if res != nil {
return nil, res
}
expectedVersion, res := applyVersionGuardMember(raw, prefix)
if res != nil {
return nil, res
}
// There is deliberately no `expected_status` here: a close is idempotent, so
// a guard spelled to refuse an already-closed row asks for a refusal where
// this verb answers with a no-op. See the schema.
return &issueops.CloseItem{
Target: target,
Reason: reason,
Session: session,
Force: force,
ExpectedVersion: expectedVersion,
}, nil
}
// applyDepAddItem projects a dep_add payload onto the role's edge item.
//
// The gate normalization a waits-for edge gets, and the refusal a bad gate
// earns, belong to the role: this checks only that the type IS a value the
// column can hold, never that it is a member of a known-types list, because the
// edge vocabulary is OPEN and a workspace's own type must pass.
func applyDepAddItem(prefix string, raw map[string]json.RawMessage) (*issueops.DepAddItem, *Result) {
if offender, unknown := unknownMember(raw, applyDepAddItemMembers); unknown {
return nil, applyUnknownMember(prefix, offender, applyDepAddItemMembers)
}
source, res := applyRequiredRef(prefix, raw, "source")
if res != nil {
return nil, res
}
target, res := applyRequiredRef(prefix, raw, "target")
if res != nil {
return nil, res
}
edgeType, res := applyRequiredText(raw, prefix, "type")
if res != nil {
return nil, res
}
if !types.DependencyType(edgeType).IsValid() {
res := InvalidArgument(prefix+"type", ReasonInvalidValue,
fmt.Sprintf("`type` must be 1 to %d characters", types.MaxDependencyTypeLen))
return nil, &res
}
item := &issueops.DepAddItem{
Source: source,
Target: target,
Type: types.DependencyType(edgeType),
}
// The blob travels as the bytes the caller sent. The role is the single
// definition of what it will accept, and a re-encode here would be a second
// one — the metadata compare-and-set's rule, applied to an edge.
if metadata, present := raw["metadata"]; present {
item.Metadata = string(metadata)
}
// has_spawner and thread_id are additive members gated client-side by
// issues.batchApply.depAddLineage (routes.go CapBatchApplyDepAddLineage):
// the server always understands them once wired, so there is no
// capability check here — the gate lives entirely in the client that
// decides whether to SEND them against an older server.
hasSpawner, res := applyBoolMember(raw, prefix, "has_spawner")
if res != nil {
return nil, res
}
item.HasSpawner = hasSpawner
threadID, res := applyTextMember(raw, prefix, "thread_id")
if res != nil {
return nil, res
}
// The document's minLength: 1. An ABSENT thread_id already names no thread
// (and a re-add keeps the stored one), so an empty one would only be a
// second spelling of it — refused, as an empty `type` is, rather than read.
if _, present := raw["thread_id"]; present && threadID == "" {
res := InvalidArgument(prefix+"thread_id", ReasonInvalidValue, "`thread_id` must not be empty; omit it to name no thread")
return nil, &res
}
item.ThreadID = threadID
return item, nil
}
// applyRequiredRef reads one ref member that must be present.
func applyRequiredRef(prefix string, raw map[string]json.RawMessage, member string) (issueops.Ref, *Result) {
encoded, ok := raw[member]
if !ok {
res := InvalidArgument(prefix+member, ReasonInvalidValue, "`"+member+"` is required")
return issueops.Ref{}, &res
}
return applyRef(prefix+member, encoded)
}
// applyRef decodes one ref and applies the exactly-one rule the schema cannot
// state: `oneOf` is unavailable in this document, so a client can construct a
// ref with both members or neither and only the server can say no.
//
// Whether a key RESOLVES — and whether it reaches backward far enough — is the
// role's question, because only the role can see the whole request's key index.
func applyRef(param string, encoded json.RawMessage) (issueops.Ref, *Result) {
refuse := func(detail string) (issueops.Ref, *Result) {
res := InvalidArgument(param, ReasonInvalidValue, detail)
return issueops.Ref{}, &res
}
var raw map[string]json.RawMessage
if err := json.Unmarshal(encoded, &raw); err != nil || raw == nil {
return refuse("a ref must be a JSON object naming a `key` or an `id`")
}
if offender, unknown := unknownMember(raw, applyRefMembers); unknown {
return issueops.Ref{}, applyUnknownMember(param+".", offender, applyRefMembers)
}
var wire apigen.Ref
if err := json.Unmarshal(encoded, &wire); err != nil {
return refuse("a ref's `key` and `id` must be strings")
}
for _, bounded := range []struct {
member string
value *string
}{{"key", wire.Key}, {"id", wire.Id}} {
if res := applyBoundedText(param+".", bounded.member, bounded.value); res != nil {
return issueops.Ref{}, res
}
}
key, id := derefString(wire.Key), derefString(wire.Id)
switch {
case key == "" && id == "":
return refuse("a ref must name a `key` or an `id`")
case key != "" && id != "":
return refuse("a ref names a `key` or an `id`, not both")
}
return issueops.Ref{Key: key, ID: id}, nil
}
// applyBatchResponse projects the role's result onto the LEAN wire result.
//
// ItemResult.Issue is deliberately dropped. The Go contract carries a post-item
// snapshot because the completion hooks over this role hand a script the row it
// is being told about — and hooks never fire on this surface, so the snapshot
// has no consumer here and a hundred hydrated issues would be a response an
// order of magnitude larger than the request that produced it. See
// issueops.ItemResult.Issue.
func applyBatchResponse(result issueops.ApplyBatchResult) apigen.ApplyBatchResponse {
// Allocated rather than passed through: `keys` is a required member and a
// request whose creates named nothing must answer with an empty object, not
// the `null` a nil map marshals to.
keys := make(map[string]string, len(result.Keys))
for key, id := range result.Keys {
keys[key] = id
}
items := make([]apigen.ApplyItemResult, 0, len(result.Items))
for _, item := range result.Items {
wire := apigen.ApplyItemResult{
Kind: apigen.ApplyItemResultKind(item.Kind),
IssueId: item.IssueID,
Changed: item.Changed,
Revision: types.RevisionToken(item.RowVersion),
}
if item.DependsOnID != "" {
dependsOn := item.DependsOnID
wire.DependsOnId = &dependsOn
}
items = append(items, wire)
}
return apigen.ApplyBatchResponse{Keys: keys, Items: items}
}
// failApplyBatch answers a refused plan, mapping the role's TYPED refusals onto
// the frozen codes and naming the item each one came from.
//
// EVERY BRANCH READS THE ROLE'S TYPED FIELDS, never its prose. That matters more
// here than anywhere else on the surface: the request is all or nothing, so
// there is no per-item result array a client could find the offender in, and
// the `item_*` problem members are the only place it exists. They come from
// *issueops.ItemError, raised inside the transaction that refused.
//
// Nothing here quotes a role message. 4xx details on this surface reflect the
// caller's own input back rather than server internals, and the real error goes
// to the log with the request id.
func (s *Server) failApplyBatch(w http.ResponseWriter, r *http.Request, request issueops.ApplyBatchRequest, err error) {
var (
refErr *issueops.RefError
itemErr *issueops.ItemError
typeConflict *issueops.DependencyTypeConflictError
hierarchy *issueops.DependencyHierarchyConflictError
endpoint *issueops.DependencyEndpointNotFoundError
openChildren *issueops.CloseOpenChildrenError
claimed *issueops.ClaimConflictError
)
// The item wrapper is read once and shared by every branch below: a refusal
// that names an item names it whatever the inner sentinel turns out to be.
errors.As(err, &itemErr)
at := func(res Result, member string) Result {
if itemErr == nil {
// A refusal the role raised without naming an item — the request's
// own, or a wrapper that lost the identity. `param` still names the
// member that carries them all, because the document promises one on
// every 400 but the body that failed to parse.
if member != "" && res.Problem.Param == nil {
param := "items"
res.Problem.Param = &param
}
return res
}
res = res.WithBatchItem(itemErr.Index, string(itemErr.Kind), itemErr.Key, itemErr.IssueID)
if member != "" {
param := applyItemParam(itemErr.Index, string(itemErr.Kind)+"."+member)
res.Problem.Param = &param
}
return res
}
switch {
// An unresolvable ref is a *RefError and it is the one 400 that carries a
// discriminator: `declared_later` tells an ORDERING mistake from a typo, and
// it is emitted in both polarities so an absent member cannot be misread as
// false. It is matched before ErrValidation because it unwraps to it.
case errors.As(err, &refErr):
s.refusedApplyBatch(r, err)
s.fail(w, r, InvalidArgument(applyRefParam(request, refErr), ReasonInvalidValue,
applyRefDetail(refErr)).
WithBatchItem(refErr.Index, applyKindAt(request, refErr.Index), refErr.Key, "").
WithDeclaredLater(refErr.DeclaredLater))
case errors.As(err, &typeConflict):
s.fail(w, r, at(newResult(CodeDependencyExists,
"this pair already carries an edge of a different type; remove it before re-adding").
WithDependencyTypeConflict(typeConflict.ExistingType, typeConflict.RequestedType), ""))
case errors.As(err, &hierarchy):
s.fail(w, r, at(newResult(CodeDependencyCycle,
"a blocking edge against the issue's own ancestor or descendant would never clear").
WithHierarchyConflict(hierarchy.IssueID, hierarchy.BlockerID, hierarchy.BlockerIsAncestor), ""))
case errors.Is(err, issueops.ErrDependencyCycle):
// No hierarchy members: this is the plain scheduling cycle, and their
// ABSENCE is what tells a client which of the two refusals it got. It may
// come from the per-edge probe or from the END GATE, which is the only
// place an edge that is legal alone and illegal in the graph this request
// built is caught.
s.fail(w, r, at(newResult(CodeDependencyCycle,
"the plan's edges would create a dependency cycle; nothing was written"), ""))
// The edge existence refusals are 400s rather than 404s, conforming to
// POST /v0/beads/dependencies:add: an edge describes a relation rather than
// acting on a row, and its target may legitimately be an "external:"
// reference this database holds nothing for.
case errors.As(err, &endpoint):
s.refusedApplyBatch(r, err)
member, detail := "source", "an edge's source names no issue in this workspace; nothing was written"
if errors.Is(err, issueops.ErrDependencyTargetNotFound) {
member, detail = "target", "an edge's target names no issue this workspace can see; nothing was written"
}
s.fail(w, r, at(InvalidArgument("", ReasonInvalidValue, detail), member))
case errors.Is(err, issueops.ErrSelfDependency):
s.refusedApplyBatch(r, err)
s.fail(w, r, at(InvalidArgument("", ReasonInvalidValue, "an issue cannot depend on itself"), "target"))
// An occupied explicit id is a 409: the body is well-formed and stays
// well-formed, and what refuses it is STATE the client cannot see without
// reading it — so recovery is to look at that state (adopt the row, pick
// another id, or stop) rather than to fix a malformed request. The identical
// body succeeded before the id was taken. Matched before ErrValidation
// because the create path wraps both.
case errors.Is(err, storage.ErrAlreadyExists):
s.refusedApplyBatch(r, err)
s.fail(w, r, at(newResult(CodeAlreadyExists,
"a create item's `id` already names a stored row; nothing was written"), "id"))
case errors.Is(err, issueops.ErrCloseOpenChildren):
res := at(newResult(CodeNotClosable,
"an item closes an issue with open children; close them first, or send the item's force flag"), "")
if errors.As(err, &openChildren) {
res = res.WithOpenChildren(openChildren.OpenChildren)
}
s.fail(w, r, res)
case errors.Is(err, issueops.ErrCloseBlocked):
s.fail(w, r, at(closeBlockedResult(err,
"an item closes a blocked issue", "clear the blocker, or send the item's force flag"), ""))
// An update item naming a template, or the close guards on a close item:
// the shared mapping, carrying the holder for not_assignee, plus the item
// members.
case errors.Is(err, issueops.ErrTemplateReadOnly),
errors.Is(err, issueops.ErrPinned),
errors.As(err, new(*issueops.CloseNotAssigneeError)):
s.fail(w, r, at(ClassifyError(err), ""))
case errors.Is(err, storage.ErrAlreadyClaimed):
res := at(newResult(CodeAlreadyClaimed,
"an update transfers work away from a live foreign owner; send `force_assignee_transfer`, or guard with `expected_assignee`"), "assignee")
if errors.As(err, &claimed) {
if claimed.Assignee != "" {
res = res.WithAssignee(claimed.Assignee)
}
if claimed.Status != "" {
res = res.WithIssueStatus(string(claimed.Status))
}
}
s.fail(w, r, res)
case errors.Is(err, storage.ErrNotesOverwrite):
s.fail(w, r, at(newResult(CodeNotesOverwrite,
"an update's `patch.notes` would replace existing non-empty notes; send `force_notes_overwrite`, or use `patch.append_notes` to preserve history"), "notes"))
case errors.Is(err, issueops.ErrVersionMismatch),
errors.Is(err, issueops.ErrStatusMismatch),
errors.Is(err, issueops.ErrAssigneeMismatch):
s.fail(w, r, applyPreconditionResult(request, itemErr, err, at))
// A target an update or a close NAMED is a resource this request failed to
// address, which is POST /v0/beads/issues:delete's 404 rather than the edge
// refusal's 400 above.
case errors.Is(err, storage.ErrNotFound):
s.fail(w, r, at(NotFound(), ""))
case errors.Is(err, storage.ErrValidation):
s.refusedApplyBatch(r, err)
s.fail(w, r, at(InvalidArgument("items", ReasonInvalidValue,
"an item was refused by this workspace's own validation; nothing was written"), ""))
default:
s.failErr(w, r, err)
}
}
// applyPreconditionResult builds the 409 for a guard that missed, naming the
// guard member and echoing what the request asked for.
//
// The expected value comes from the REQUEST rather than from a read, and the
// observed value is absent, because this operation's refusal rolled its
// transaction back: a read afterwards would describe a row the refusal never
// saw. See PreconditionFailed.
func applyPreconditionResult(request issueops.ApplyBatchRequest, itemErr *issueops.ItemError, err error, at func(Result, string) Result) Result {
res := PreconditionFailed()
switch {
case errors.Is(err, issueops.ErrVersionMismatch):
res = at(res, "expected_version")
if expected := applyExpectedVersion(request, itemErr); expected != nil {
res = res.WithExpectedVersion(*expected)
}
case errors.Is(err, issueops.ErrStatusMismatch):
res = at(res, "expected_status")
if item := applyUpdateAt(request, itemErr); item != nil && item.ExpectedStatus != nil {
res = res.WithExpectedStatus(string(*item.ExpectedStatus))
}
default:
res = at(res, "expected_assignee")
if item := applyUpdateAt(request, itemErr); item != nil && item.ExpectedAssignee != nil {
res = res.WithExpectedAssignee(*item.ExpectedAssignee)
}
}
return res
}
// applyExpectedVersion reads the version guard off the refused item. Both an
// update and a close carry one, which is why this is not applyUpdateAt's caller.
func applyExpectedVersion(request issueops.ApplyBatchRequest, itemErr *issueops.ItemError) *int64 {
item := applyItemAt(request, itemErr)
switch {
case item == nil:
return nil
case item.Update != nil:
return item.Update.ExpectedVersion
case item.Close != nil:
return item.Close.ExpectedVersion
}
return nil
}
// applyUpdateAt reads the refused item's update payload, or nil when the
// refusal named no item or named one of another kind.
func applyUpdateAt(request issueops.ApplyBatchRequest, itemErr *issueops.ItemError) *issueops.UpdateItem {
if item := applyItemAt(request, itemErr); item != nil {
return item.Update
}
return nil
}
// applyItemAt reads the refused item out of the request the handler built.
//
// It is bounds-checked rather than trusted: the index comes from the role, and
// a server that indexed a slice on a number another package computed would turn
// a contract drift into a panic on a live request.
func applyItemAt(request issueops.ApplyBatchRequest, itemErr *issueops.ItemError) *issueops.ApplyItem {
if itemErr == nil || itemErr.Index < 0 || itemErr.Index >= len(request.Items) {
return nil
}
return &request.Items[itemErr.Index]
}
// applyKindAt names the kind of the item at index, for a refusal that carries
// an index but no kind of its own.
func applyKindAt(request issueops.ApplyBatchRequest, index int) string {
if index < 0 || index >= len(request.Items) {
return ""
}
return string(request.Items[index].Kind)
}
// applyRefParam spells `param` for an unresolvable ref.
//
// RefError.Member is DIAGNOSTIC PROSE — "target", "source", or "metadata_ref
// <key>" — rather than a vocabulary, so it is mapped onto the document's own
// member names rather than published. Anything that is not one of the two
// addressing refs is a metadata ref, and the whole member is named because the
// key inside it came from the caller's own object.
func applyRefParam(request issueops.ApplyBatchRequest, refErr *issueops.RefError) string {
kind := applyKindAt(request, refErr.Index)
if kind == "" {
return applyItemParam(refErr.Index, "")
}
member := "metadata_refs"
if refErr.Member == "target" || refErr.Member == "source" {
member = refErr.Member
}
return applyItemParam(refErr.Index, kind+"."+member)
}
// applyRefDetail says which of the two key diagnoses this is, in the server's
// own words. The machine-readable half is `declared_later`.
func applyRefDetail(refErr *issueops.RefError) string {
if refErr.DeclaredLater {
return "this ref names a key declared LATER in the request; a key reaches backward only, so move the item that declares it earlier"
}
return "this ref names a key no item in the request declares"
}
// refusedApplyBatch records the real refusal for the operator. The 4xx path does
// not log by default, and the role's message is the only place the underlying
// reason survives once the response carries the server's own words.
func (s *Server) refusedApplyBatch(r *http.Request, err error) {
s.event("request_refused", "request_id", requestInfo(r.Context()).id, "error", err.Error())
}
// applyItemParam spells the `param` member for a refusal inside `items`, so a
// client dispatching on it learns WHICH item and WHICH member.
func applyItemParam(index int, member string) string {
param := fmt.Sprintf("items[%d]", index)
if member == "" {
return param
}
return param + "." + member
}
// applyParam joins a level's dotted prefix to a member, and names the LEVEL
// itself when the member is empty — a refusal about the whole object rather
// than about one of its members. Without the trim that case would spell a
// `param` ending in a dot, which is a member name no schema declares.
func applyParam(prefix, member string) string {
if member == "" {
return strings.TrimSuffix(prefix, ".")
}
return prefix + member
}
// applyUnknownMember answers an unknown member below the request level, where
// the offender's name has to be qualified by the path that reached it.
func applyUnknownMember(prefix, offender string, allowed []string) *Result {
res := InvalidArgument(prefix+offender, ReasonUnknownParameter,
"this member carries "+strings.Join(allowed, ", ")+" and nothing else")
return &res
}
// applyKindNames lists the tag vocabulary in the document's order, for a
// refusal that has to spell it.
func applyKindNames() []string {
return []string{"create", "update", "close", "dep_add"}
}
// applyObjectMember reads a required member that must be a JSON object, as raw
// members so the level below it can be checked by name.
func applyObjectMember(raw map[string]json.RawMessage, prefix, member string) (map[string]json.RawMessage, *Result) {
encoded, ok := raw[member]
if !ok {
res := InvalidArgument(prefix+member, ReasonInvalidValue, "`"+member+"` is required")
return nil, &res
}
var out map[string]json.RawMessage
if err := json.Unmarshal(encoded, &out); err != nil || out == nil {
res := InvalidArgument(prefix+member, ReasonInvalidValue, "`"+member+"` must be a JSON object")
return nil, &res
}
return out, nil
}
// applyRequiredText reads a required string member, bounded the way every
// stored string on this surface is.
func applyRequiredText(raw map[string]json.RawMessage, prefix, member string) (string, *Result) {
if _, ok := raw[member]; !ok {
res := InvalidArgument(prefix+member, ReasonInvalidValue, "`"+member+"` is required")
return "", &res
}
value, res := applyTextMember(raw, prefix, member)
if res != nil {
return "", res
}
if value == "" {
res := InvalidArgument(prefix+member, ReasonInvalidValue, "`"+member+"` must not be empty")
return "", &res
}
return value, nil
}
// applyTextMember, applyBoolMember and applyVersionGuardMember are the pure twins of
// Server.storedTextMember and Server.booleanMember, with their rules unchanged:
// an absent member is the zero value the role reads as "not supplied", an
// explicit `null` is a 400 naming the member rather than a third state, and a
// string is bounded by what storage holds and refused for control characters —
// because these values land in columns that renderers print.
//
// They are functions rather than methods because this body nests four levels
// deep, and threading a ResponseWriter through every level so each could fail
// in place would put the response machinery in the middle of the projection.
func applyTextMember(raw map[string]json.RawMessage, prefix, member string) (string, *Result) {
encoded, ok := raw[member]
if !ok {
return "", nil
}
var value *string
if err := json.Unmarshal(encoded, &value); err != nil || value == nil {
res := InvalidArgument(prefix+member, ReasonInvalidValue, "`"+member+"` must be a string")
return "", &res
}
if res := applyBoundedText(prefix, member, value); res != nil {
return "", res
}
return *value, nil
}
func applyBoolMember(raw map[string]json.RawMessage, prefix, member string) (bool, *Result) {
encoded, ok := raw[member]
if !ok {
return false, nil
}
var value *bool
if err := json.Unmarshal(encoded, &value); err != nil || value == nil {
res := InvalidArgument(prefix+member, ReasonInvalidValue, "`"+member+"` must be a boolean")
return false, &res
}
return *value, nil
}
// applyVersionGuardMember is the family's revision-token reader, and it names
// the member itself rather than taking one.
//
// Its siblings above are generic because they read many members; this one has
// read exactly one on every operation that publishes a row-version guard, so the
// member name lives in the function instead of at five call sites that could
// disagree about how to spell it.
//
// THE TOKEN IS A STRING ON THE WIRE AND AN int64 INSIDE. It is opaque and
// equality-only, and it spans the full int64 range, which a JSON number does not
// survive in an IEEE-754-double parser; responses emit it as a decimal string
// (types.RevisionToken) and this is where it comes back. STRING ONLY, with no
// number-or-string transitional spelling: this API refuses unknown members by
// name and keeps one spelling of the guard, and a numeric token accepted here
// would be a value the client rounded before it ever reached the server.
//
// prefix stays, because a batch item's guard is reported qualified by the item
// that carried it where a single operation's is spelled bare.
func applyVersionGuardMember(raw map[string]json.RawMessage, prefix string) (*int64, *Result) {
encoded, ok := raw[expectedVersionMember]
if !ok {
return nil, nil
}
refuse := func() (*int64, *Result) {
res := InvalidArgument(prefix+expectedVersionMember, ReasonInvalidValue,
"`"+expectedVersionMember+"` must be a string: the `revision` token a response carried, echoed verbatim")
return nil, &res
}
var value *string
if err := json.Unmarshal(encoded, &value); err != nil || value == nil {
return refuse()
}
parsed, err := types.ParseRevisionToken(*value)
if err != nil {
return refuse()
}
return &parsed, nil
}
// applyBoundedText applies the bounds a stored string carries wherever it is
// spelled, so every level of this body refuses the same values. A nil pointer is
// an absent member and passes.
func applyBoundedText(prefix, member string, value *string) *Result {
if value == nil {
return nil
}
refuse := func(detail string) *Result {
res := InvalidArgument(prefix+member, ReasonInvalidValue, detail)
return &res
}
switch {
case types.CheckFieldLen(member, *value) != nil:
return refuse(fmt.Sprintf("`%s` is %d characters; storage holds at most %d",
member, utf8.RuneCountInString(*value), types.MaxFieldLen))
case strings.ContainsFunc(*value, isControlChar):
return refuse("`" + member + "` must not contain control characters")
}
return nil
}
// applyBoundedLabels applies the same bound to every entry of a label list.
func applyBoundedLabels(prefix, member string, labels []string) *Result {
for i, label := range labels {
if types.CheckFieldLen("label", label) != nil {
res := InvalidArgument(prefix+member, ReasonInvalidValue,
fmt.Sprintf("`%s[%d]` is %d characters; storage holds at most %d",
member, i, utf8.RuneCountInString(label), types.MaxFieldLen))
return &res
}
}
return nil
}
// applyRawCopy copies a raw member rather than aliasing the decoded body, so
// nothing downstream can be surprised by the request buffer's lifetime.
func applyRawCopy(raw json.RawMessage) json.RawMessage {
return json.RawMessage(append([]byte(nil), raw...))
}