beads/internal/httpapi/batch_apply.go
Julian Knutsen 6d21d337ad
feat(httpclient): OSS HTTP client store and roles + a served http conformance leg (contributed from bd-enterprise under MIT) (#7247)
* feat(httpclient): lift the HTTP client store and roles from bd-enterprise (MIT contribution)

Contributed by gascity from bd-enterprise (github.com/gascity/bd-enterprise,
internal/enterprise/httpstore @ 49d1df2f6) to OSS beads under the MIT
license, with the owner's explicit approval. Every lifted file carries a
provenance header naming its source path.

Lifted into internal/httpclient: the Store (DoltStorage over the wire), the
dial/register glue, the read roles (reader, querier, counters, edges,
relations, graph, tree, cycles, stats, config, memories, journal), the write
roles (lifecycle, claimer, releaser, deleter, dependency editor, metadata
CAS, commenter, batch closer/creator/applier, ready claimer, sweeper), and
their tests. The store transport seam (WireClient/WireDialer) and the
store-dependent tests deferred from the S2 lift are restored here.

Not lifted: the hosted gateway (httpgw/gwauth), box-login, storage profiles
and gasworks. dial.go only mentions the gateway in comments.

This commit is the mechanical lift and does NOT build yet. It references
enterprise-only APIs absent from OSS: issueops.ReadyLister,
journalops.Watcher, storage.ServerEnforcedPolicy, the
NonCommitGraphBackend/RemoteWorkspaceBackend markers, DoltStorage
differences, and BatchCloseRequest.claim_next. The following commits
reconcile it with OSS (S3/S4/S5).

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

* fix(httpclient): reconcile the bd-enterprise lift's compile-time API surface against OSS

The mechanical lift (02a9dad17) carried several bd-enterprise-only shapes
that do not exist in OSS beads:

- ReadyLister, journalops.Watcher/StreamWire, and ServerEnforcedPolicy have
  no OSS wire counterpart; drop the role, its served/journal-watch tests,
  and the policy enforcement path entirely rather than leave dead code that
  cannot compile against OSS's apigen types.
- claim_hook_gate_test.go exercised a hook surface OSS does not publish.
- recordingWire/fakeWire no longer embed a StreamWire double now that no
  read role can reach a watch stream; testTarget's two conflicting
  package-scope declarations (store_test.go and helpers_test.go) are split
  apart as pinnedProjectTarget vs. the generic helper.
- unsupported_gen.go is regenerated from scratch against OSS's actual
  storage.DoltStorage via a new, from-scratch reflection-based generator
  (internal/storage/unsupportedgen) rather than hand-copied from
  bd-enterprise's generated output; completeness_test.go's
  legitimatelyUnsupported map grows the handful of OSS-only or
  previously-missed methods (AddRemoteWithRef, RenameLabel, etc.) this
  regeneration surfaces.
- write_ledger_test.go and the divergence ledger doc gain the
  force_notes_overwrite and claim pending rows OSS's apigen.UpdateRequest
  and SweepRequest actually carry, and drop the Remaining/LiveDependent
  assertions SweepResult/SweepSkips have no wire member to ever populate.

go build ./... and go vet ./... are clean after this change. No behavior
beyond the removed surfaces changes here; read-role and write-role
reconciliation land in follow-up commits.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(httpclient): stop sending listIssues a reverse/sort vocabulary OSS does not publish

The bd-enterprise lift's sortedPage pushdown assumed GET /v0/beads/issues
(listIssues) shared the same sort vocabulary and a reverse parameter with
GET /v0/beads/issues:query (handleQueryIssues) -- a different operation.
OSS's openapi.v0.yaml publishes listIssues a CLOSED two-value `sort` enum
(created, priority; deliberately smaller than the nine values :query
takes) and no `reverse` parameter at all. Sending either would earn an
unknown-parameter or invalid_value refusal from the real server, not a
silently-wrong answer -- which is why this only surfaces against a served
backend, not a Go-side unit test of the request builder.

Add listSortPushdownEligible to gate the pushdown fast path to the two
keyset orders listIssues can actually page, unconditionally refusing any
reversed request (listIssues has no parameter that names a reversed
direction, so a reversed request of any order walks and sorts
client-side). Drop the unconditional `reverse` send from sortedPage.

served_list_test.go's three affected cases encoded the wrong,
enterprise-only wire shape (expecting reverse=false on the fallback path,
expecting a reversed order to push down with sort+reverse, and expecting a
Go-side/SQL-side mix in listIssues' sort enum); corrected to match OSS's
actual, provable wire contract rather than left red. list_sort_test.go's
publishedParamEnum helper now reads the OpenAPI document via the
go:embed'd spec.OpenAPIV0() accessor instead of a relative os.ReadFile, so
it no longer depends on the test's working directory (this also fixes the
same test failing under Bazel's sandbox, which does not expose arbitrary
relative-path files).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(httpclient): reconcile write-role tests against OSS's BatchClose/Update shapes

OSS's apigen.BatchCloseRequest and BatchCloseResponse publish no
claim_next/claimed_next member at all (ledger row
W-CloseBatchRequest.ClaimNext) -- bd-enterprise's claim-folded-into-close
shape has no OSS wire counterpart. Replace the three tests that exercised
decoding a served claim (TestBatchCloseRefusesAServedClaimCarryingNoIssue,
TestBatchCloseRefusesAServedClaimTheRequestNeverEarned,
TestBatchCloseKeepsTheClaimItEarned) with
TestBatchCloseRefusesClaimNextUnconditionally, which pins the actual OSS
behavior: a CloseBatchRequest naming ClaimNext refuses before any dial, on
both the unserved and served legs, regardless of what a server would
otherwise answer. Drop the now-dead "claim-next" case and helper claim
variable from TestBatchCloseServesTheSingleItemShapeAndRefusesTheRest.

Also:
- Add the ForceNotesOverwrite case to
  TestUpdateRefusesEveryMemberTheWireExcludes's hand-maintained member
  map -- a ledgered-pending UpdateRequest member (see
  W-UpdateRequest.ForceNotesOverwrite) the lift's test left uncovered.
- Remove write_roles_test.go's duplicate `ptr[T]` generic helper, which
  redeclared the one already in helpers_test.go and never compiled.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* test(conformance): pin post-write RowVersion propagation across Update/Close/Reopen

types.Issue.RowVersion is json:"-", so an implementation that forgot to
populate it on the way back from storage would still compile and still
pass every existing content assertion in lifecycle_close_reopen_contract.go
-- none of them read RowVersion. Add
RunLifecycleResultsCarryThePostWriteRowVersion, which chains each write's
result token into the next write's ExpectedVersion (Update -> Close ->
Reopen) with no intervening read, so a dropped or stale token fails closed
on the next write's optimistic-concurrency guard rather than passing
silently.

Register it in role_bundle_cases.go's roleContractCases dispatch table at
its declaration-order position -- TestRoleContractCasesCoverEveryContractCase
was failing because the function existed with no dispatcher entry.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* chore(bazel): sync BUILD files via gazelle for the S3 reconciliation

bazel run //:gazelle after the compile/read-role/write-role reconciliation
commits:

- internal/httpclient/BUILD.bazel: regenerate srcs/deps for the files
  added, removed and newly imported above (drops the deleted
  policy/journal-watch/readylister files, adds the spec dep for
  list_sort_test.go's embedded OpenAPI read). Manually add
  `data = glob(["*.go"])` to the go_test rule, matching the idiom already
  used by //internal/httpclient/encode: completeness_test.go and
  divergence_citation_gate_test.go read their own package's .go sources at
  runtime (a regex scan of unsupported_gen.go and an AST walk of the
  package directory), and Bazel's go_test sandbox only copies in files a
  rule declares as `data` -- `srcs` are compiled away, not left readable.
  A `# keep` directive protects the attribute across future gazelle runs
  (confirmed gazelle does not manage `data` on this rule).
- internal/storage/BUILD.bazel,
  internal/storage/unsupportedgen/BUILD.bazel: pick up the new
  from-scratch unsupportedgen generator package.

bazel test //internal/httpclient/... //backend/conformance/... now passes
all 4 targets (previously 4 of the package's tests failed only under
Bazel's sandbox: TestInterfaceCompleteness,
TestTheCountGroupVocabularyMatchesTheSharedOne,
TestFlaglessListSortIsTheSameOrderNamed,
TestDivergenceParkCitationsResolveToLiveLedgerRows).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(conformance): close two pre-existing leg-wiring gaps found while registering http

- RunLifecycleResultsCarryThePostWriteRowVersion (added in 536561124) was
  never wired into dolt/embeddeddolt/uow's own lifecycle_close_reopen test
  tables, only into the http leg's. TestEveryLegWiresEveryRoleContract
  catches this once a fourth leg forces the test to actually run again;
  wire the missing t.Run into all three.
- The three StatsReporter "suppressed rows" cases (GateIssues/TemplateIssues
  breakdown) were never added to served_graph_aux_test.go even though the
  wire already carries both fields end-to-end (types.Statistics is shared
  verbatim by apigen.StatsResponse and the client's decode). Wire them; all
  three pass over HTTP with no server or client change needed.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* feat(conformance): register the http leg in the role-contract lock

Registers "http" (wiringRoot internal/httpclient) in contractLegRegistry.
The served-surface harness (served_harness_test.go + 16 served_*_test.go
files) already wired 495 of 576 role-contract entrypoints from the
bd-enterprise lift; this change:

- Waives the 3 RunCycleDetectorIncludeTracks* cases: issueops.
  DetectCyclesRequest.IncludeTracks has no plumbing on either side of the
  wire today (cycles.go always calls DetectCycles with a zero-value
  request; the client's DetectCycles ignores its request parameter). No
  slice through S13 adds one, so the waiver says a new slice is needed
  rather than pointing at an existing one.
- Sets adoptionCeiling: 75 for the remaining, not-yet-wired write-role and
  internal-plumbing entrypoints (lifecycle writes, DependencyEditor,
  MetadataCAS, Lease, Sweeper, VersionReconciler, Bootstrapper/
  InitVerifier, cross-record invariants) that S4 and later write slices
  pick up and ratchet down.
- Adds //internal/httpclient:go_srcs to storage_test's data: the leg's
  wiring root lives outside internal/storage, and the Bazel sandbox has no
  other reason to materialize those sources for a test that reads them at
  runtime.

TestEveryLegWiresEveryRoleContract, TestEveryRoleMethodHasAContractCase,
and RunUnsupportedContract (already wired pre-lift in
internal/httpclient/completeness_test.go) all pass for the http leg.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(httpclient): close review's HIGH 2-5 gaps and reconcile their test doubles

HIGH 2 (BatchCloser), HIGH 3 (created_by stamping, absent-parent 404/400,
force_notes_overwrite carriage) and HIGH 4 (CapabilityError now unwraps to a
typed *storage.ErrUnsupported with Capability) were implemented in prior
commits on this branch; this change finishes HIGH 5 and the verification the
review asked for:

- role_reader.go's Reader.Get and bridge.go's raw GetIssue both now stitch
  Revision back onto Issue.RowVersion via parseRevision, so a guarded write
  built off either read carries the row's real token instead of a decoded
  zero. Both are pinned by round-trip served tests that guard a write with
  the read's token and require a stale-token replay to refuse.
- Fixed create_test.go's stale assertion that created_by must stay empty
  (contradicted the HIGH 3 stamping fix it predated).
- Fixed recordingWire's canned IssueDetails{} response (roles_test.go): it
  omitted Revision, which is a required field on every getIssue response
  per the OpenAPI spec, and the HIGH 5 stitch now reads it unconditionally.
- Synced internal/httpclient/wire/BUILD.bazel via gazelle: handshake_test.go's
  new internal/storage import had no corresponding dep.
- gofmt'd write_roles_test.go's stubWire struct tags.

Verified: served HTTP conformance tier (BEADS_TEST_EMBEDDED_DOLT=1
BEADS_HTTP_TEST_REQUIRED=1 go test ./internal/httpclient/, -timeout=40m)
600 PASS / 0 FAIL / 72 SKIP (all ledgered known-divergence waivers). One
TLS-handshake-timing test failed in that run and passed 3/3 in isolation
(unrelated file, pre-existing flake under machine load, not reproducible).
Also green: default go test (minus the pre-existing, unrelated
TestEngdocsAndRootMarkdownLinks doc-link drift and the environmental
internal/storage/uow dolt-server-spawn hang), bazel build/test for the
touched packages, make api-check, and gofmt/vet.

Live-mutated and reverted 3 guards to confirm their tests are load-bearing:
role_reader.go's RowVersion stitch, bridge.go's RowVersion stitch, and
lifecycle.go's force_notes_overwrite wiring.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* feat(ci): add served HTTP conformance as Tier 3 of the conformance gate

The served tier (internal/httpclient's served_*_test.go corpus) never ran
in CI, so HIGH 2-5's regressions (and any future ones) would merge
undetected. Add it as Tier 3 of scripts/conformance.sh, the existing
required entrypoint conformance.yml already runs on every PR/push/merge_group
(not a new workflow, not wired through ci-gate.sh's aggregation -- this
repo's convention for this gate is a single standalone job):

  CGO_ENABLED=1 BEADS_TEST_EMBEDDED_DOLT=1 BEADS_HTTP_TEST_REQUIRED=1 \
    go test -timeout=40m ./internal/httpclient/

Left unsharded: Tier 1 and Tier 2 are themselves single unsharded
invocations, and Tier 3 is one Go package, not a multi-package corpus the
repo's existing package-sharding scripts apply to. No Bazel-lane addition:
bazel-coverage's embedded/dolt_server outputs only ever substitute for
main.yml/pr-risk.yml jobs, never for conformance.yml's own job, so there is
no existing Bazel-lane relationship for this gate to extend.

Bumped the job's timeout-minutes 45 -> 100 (30m + 10m + 40m budgeted
sub-timeouts plus checkout/setup-go overhead) and updated the three policy
tests that pin this script/workflow byte-for-byte
(TestConformanceScriptUsesExplicitTimeoutBudgets,
TestConformanceScriptPropagatesGoTestFailures,
TestConformanceWorkflowHasOuterTimeoutBudget), re-included
internal/httpclient/** in both path filters (push and pull_request, kept
identical) since served_*_test.go files are the suite itself and would
otherwise be excluded by the shared !**_test.go negation, and documented the
new filter entry in ci_f7c_advisory_test.go's own-entries map.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(httpclient): root-cause and fix the Happy-Eyeballs TLS flake in store_dependent_test.go

TestTransportForSucceedsWithEnvCAFile (and 3 Handshake-based siblings that
assert on a successful dial through a configured CA: SidecarCAFile,
CAFileEnvAgreeingWithSidecarSucceeds, DialWithNilTransportHTTPClientGetsCAInjectedOnACopy)
flaked once with "tls: first record does not look like a TLS handshake".

Root cause: our code, same class S2 already fixed for TransportFor's direct
callers (forceIPv4Loopback in ca_test.go/ca_wrong_ca_test.go). Every server
these tests dial is addressed by hostname "localhost" (ca.startServer
rewrites it there deliberately, so SNI is actually sent), which Go's
default dialer races a tcp6 dial to ::1:<port> against the real tcp4 dial to
127.0.0.1:<port> for (RFC 6555). An unrelated local process already bound to
a colliding ephemeral port on ::1 can occasionally win that race before the
real dial completes, and the client speaks TLS to that unrelated process
instead -- the misleading handshake error, not a real hang or a product bug.

These four tests go through Handshake with DialOptions{} (HTTPClient left
nil deliberately, since that is itself the nil-HTTPClient branch of DialWith
under test), so they never get a *http.Transport back to mutate the way
forceIPv4Loopback's existing callers do. Fixed via warmIPv4OnlyCATransport:
pre-build the target's CA-scoped transport through the same public
TransportFor the product code itself calls (so it lands in the exact
(path, content)-keyed cache transportForFile's cache hit returns), force
tcp4-only on that cached pointer, and let Handshake's own DialWith run
unchanged -- same branch under test, same cached transport, no sleeps, no
retries. Verified with -race -count=50 on all four: 200/200 pass.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(httpclient): refuse DetectCycles(IncludeTracks=true) instead of silently ignoring it

roles_graph.go's httpCycleDetector.DetectCycles took its request as `_`,
forwarding nothing: a caller asking for the widened (tracks-following) walk
silently got the narrower default walk back with no error. listDependencyCycles
has no include_tracks wire parameter at all, so there is no way to honor the
field — only to refuse it loudly or drop it silently. Switch to the typed,
ledger-cited refusal (encode.RefusedError via refuse(), citing new row
"L-cycles-tracks") that CountEdges' own wire bound already uses, add the
ledger row and its Op, regenerate the auto-generated ledger doc, and pin the
behavior with a unit test (no served-tier case needed: the refusal is
pre-dial by design, same reasoning as the existing edge-count bound pin).

Also fix leg_contract_wiring_test.go's cycleTracksWireGapWaiverReason, which
had gone stale: it described the http leg as silently forwarding nothing for
this field, which was true before this change and is not after. The waiver
itself (the three RunCycleDetectorIncludeTracks* contract cases stay unwired
for the http leg) is unaffected — they were never wired, and still are not —
only the reason text needed updating to describe a typed refusal instead of a
silent ignore.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(storage): name the http leg's permanent contract gaps instead of budgeting them

The http leg's role-contract adoption ceiling (internal/storage/contract_leg_registry_test.go)
sat at a flat 75, with a prose "adopting" note that bundled every skip into
one vague bucket — including MetadataCAS, which is in fact fully wired (0
missing cases), a false claim left over from an earlier wiring pass.

Twenty-six of the 75 skips are not S4-and-later work at all: they are
PERMANENT gaps that no future slice closes, because the contract itself has
no publicops role for an HTTP client to route through:

  - Importer (4 cases): uow is Importer's one accessor, same precedent the
    dolt and embeddeddolt legs already carry (importerOneAccessorWaiverReason).
  - RetentionFixture/EpochFixture (15 cases) and CrossRecordInvariantFixture
    (7 cases): both fixtures carry raw storage hooks (Resolve, Erase, Mint,
    CurrentEpoch, GuardedWrite, EnforceCrossRecordInvariant, ...) with no
    publicops field at all. dolt, embeddeddolt and uow wire these because
    they ARE the storage engine this address/epoch/retention and
    cross-record transaction bookkeeping belongs to; the http leg is a
    remote client with none of its own to report. There is no wire surface
    for this to ever route through short of a new role being designed from
    scratch — a new slice, not a missing test line.

These 26 are now named, reasoned waivers in unwiredContractEntrypoints
(two new reason constants: retentionEpochNoRoleWaiverReason and
crossRecordInvariantNoRoleWaiverReason), and the ceiling drops from 75 to 49
to match. The "adopting" note is rewritten to correctly name every remaining
category under budget (lifecycle writes, DependencyEditor, DualWrite,
Sweeper, VersionReconciler, Bootstrapper/InitVerifier, ExpectedRevision/
Refusal) and to drop the false MetadataCAS claim.

Also documents, at conformanceEntrypointsWiredBy, why a leg wiring a
conformance.RunX reference with every hook left nil would read as "adopted"
by this lock's static source grep while actually running as a skip —
matching the loophole internal/httpclient's divergence_citation_gate_test.go
already closes for skipKnownDivergence call sites. No leg currently does
this (verified by inspection); the note sets the review expectation for
future wiring rather than papering over the gap with no record of the policy.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(licensing): scrub internal program/org naming and a leaked private ID from OSS-visible text

MEDIUM 8 of the S3 ship review: remove licensing-hygiene leaks found by a
read-only repo-wide survey, while leaving every legitimate "Contributed by
gascity from bd-enterprise ... to OSS beads under the MIT license." provenance
header and every genuine public gascity/gastownhall self-reference (the real
OSS org, beads.gascity.com docs domain, RBE infra) untouched:

- Delete release-gates/ga-7psli2-3-1-baseline-cmd-bd-fix-gate.md: the only
  file in release-gates/ keyed by a private `ga-` tracker ID instead of the
  directory's established public `be-` convention (30 other files), and the
  only one leaking internal filesystem paths (/home/jaword/...,
  gc-management/.gc/artifacts) and internal routing terms (mayor/mpr,
  "deployer session"). Not referenced from anywhere else in the tree.
- .golangci.yml: drop two private `ga-2ltro.12`/`ga-26w10` history citations
  used as nolint-comment justifications; reworded to the same explanatory
  content without naming a private tracker ID.
- Reword 19 "gc native-program" occurrences (14 files, comments and one
  design doc) to drop the internal program name while keeping the "S3
  reconciliation (2026-10)" dating they were attached to.
- cmd/bd/if_revision.go, if_revision_test.go, update.go,
  update_proxied_server.go: drop literal citations of gascity's private
  bdstore_conditional.go classifier (including a test function name,
  TestIfRevisionEnvelopeGascityContract -> ...DownstreamContract) while
  keeping the substantive explanation of why this envelope's shape is
  load-bearing for a downstream consumer.
- internal/httpclient/lifecycle.go: drop a citation of gascity's private
  internal/beads/bdstore.go.
- internal/httpclient/dial.go: drop the internal/enterprise/httpgw package
  path, keeping "a gateway dialer outside this repo".
- internal/httpclient/register.go: reword away from naming "the enterprise
  distribution" while keeping the build-tag-opt-in behavior it documents.

Verified: go build ./..., go vet ./internal/httpclient/... ./cmd/bd/...,
go test ./internal/httpclient/... and ./cmd/bd/... -run TestIfRevision all
pass; TestDivergenceLedgerDocMatchesLedger still passes (ledger.go and its
doc were edited in lockstep). gofmt -l reports no diffs on any touched file.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(httpclient/wire): correct the CapExternalDependencies doc's implied false claim

MEDIUM 9 of the S3 ship review. The old comment on CapExternalDependencies
said this client "has no external-dependency policy layer to gate" and that
"OSS publishes no storage.ExternalDependencyQueryStore-backed client policy
... to compose" immediately after naming bd-enterprise's removed policy.go --
easily misread as "OSS has no external-dependency decorator at all." It does:
internal/storage/externaldeps's decorator is unconditional (no build tag) and
is wired into every local storage chain by cmd/bd/storage_chain.go.

Reworded to say precisely what is and is not true: the decorator exists and
runs for local backends; what this http client lacks is a client-side
composition of that decorator around a remote store, which is moot anyway
because a server advertising this capability already applies the policy
itself before answering. (Rolled in the same "gc native-program" wording
removal from the MEDIUM 8 commit, since both edits touch this comment block.)

A second claim from the same review item -- that OSS is said somewhere not to
serve events:watch -- could not be located despite an exhaustive repo-wide
search (this file, engdocs/, docs/reference/events-journal.md,
SERVE_RUNBOOK.md, the divergence ledger, CHANGELOG.md, and a dedicated
read-only research pass). internal/httpapi/events_watch.go,
routes.go's events.watch route entry, and internal/httpclient/wire/stream.go
all confirm OSS serves it unconditionally (gated only per-workspace by the
events journal setting, not by build/distribution). No corresponding false
claim exists in this worktree to fix; noting this rather than editing
something that already reads correctly.

Verified: go build ./internal/httpclient/..., go vet, go test
./internal/httpclient/wire/... pass. gofmt -l clean.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* test(unsupportedgen): add a freshness check for the generated shell

LOW item from the S3 ship review: internal/storage/unsupportedgen had no
tests of its own, so the generator could drift from the checked-in
internal/httpclient/unsupported_gen.go (a storage.DoltStorage interface
change, or a hand-written method added/removed on Store) with nothing to
catch it short of a human noticing the diff. TestInterfaceCompleteness
(completeness_test.go) already pins which methods belong in the shell by
name, but not whether the checked-in bytes match what the generator would
produce today.

TestGeneratedShellIsFresh runs the same scan-then-generate pipeline main()
runs for the registered DoltStorage/httpclient/Store consumer and diffs the
result against the checked-in file -- mirrors the existing
TestDivergenceLedgerDocMatchesLedger pattern in
internal/httpclient/encode/ledger_render_test.go. Added
TestScanHandWrittenMethodsExcludesOwnOutput alongside it to pin the
correctness property the freshness check depends on (scanning must not see
the generator's own prior output as hand-written).

BUILD.bazel: added the go_test rule by hand (gazelle cannot infer the
cross-package `data` dependency a relative-path os.ReadFile needs under the
Bazel sandbox) with data = ["//internal/httpclient:go_srcs"], following the
existing cross-package data pattern in
internal/httpclient/encode/BUILD.bazel. Placed go_test before the
go_srcs.py-managed filegroup block, which `make bazel-sync-check` requires.
Verified: `bazel run //:gazelle` leaves the rule untouched, `bazel test
//internal/storage/unsupportedgen:unsupportedgen_test` passes, `make
bazel-sync-check` is clean, go build/vet/test ./... all pass.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(httpclient/encode): scrub a private tracker-ID cluster from the shipped divergence-ledger doc

Follow-up to the MEDIUM 8 licensing scrub: internal/httpclient/encode's
ledger_render.go header and ledger.go's comments/pinnedElsewhere constant/two
Why strings cited "ga-b8ddd.12/.19/.23/.30" by name. These feed directly into
engdocs/design/http-divergence-ledger.md -- a shipped, generated OSS design
doc, the most visible surface this private ID cluster reached (Fork A's
MEDIUM 8 survey found ~40 occurrences of "ga-b8ddd" across the httpclient
package; this commit addresses the subset inside package encode that is
checked-in prose/doc text, not test-fixture or assertion values). Reworded to
keep the same substantive explanation (what closed, what's still deferred,
which dual run resolved L17) without naming the private ID.

The remaining ~30 occurrences of "ga-b8ddd" outside package encode
(offrole.go, metadata.go, store.go, roles_test.go, wire/*_test.go,
served_*_test.go) are left for a follow-up pass: scrubbing them individually
needs the same per-file verification this commit did, and is a larger,
separate blast radius than was practical in this pass.

Regenerated engdocs/design/http-divergence-ledger.md via
`go test ./internal/httpclient/encode -run TestDivergenceLedgerDocMatchesLedger
-update-ledger-doc`; diff confirmed only the ga-b8ddd text changed. go build
./..., go vet ./internal/httpclient/..., and go test ./internal/httpclient/...
all pass.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* docs(httpclient): drop a private host reference from a test comment

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

* revert(licensing): keep the scrub to the contributed client code

The previous scrub also edited upstream files this PR doesn't otherwise
touch (.golangci.yml, cmd/bd --if-revision files from #7203) and deleted an
upstream release-gate record. Restore them; the provenance scrub applies only
to the code contributed from bd-enterprise.

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

* feat(httpclient): wire S8 count-scope fields on the client

Encode ParentID/NoParent/ExcludeTypes/ExcludeStatus onto countIssues
(table.go + encode.go), add issues.count.scope to the client capability
table (wire/handshake.go, wire/vocabulary_test.go), and refuse locally
before dialing when the server hasn't advertised the token and any scope
field is set (counter.go's refuseUnservedScope), returning a typed
*storage.ErrUnsupported{Capability:"issues.count.scope"} rather than
dropping fields or taking a guaranteed-400 round trip.

Adds encoding-parity coverage (counts_test.go), a masked-token skew test
proving the pre-dial refusal and zero dials (counts_test.go), roundtrip
and skew-table fixes for the newly-encoded fields (encode/roundtrip_test.go,
encode/skew.go), and wires S8's nine shared Counter conformance cases into
the served http leg (served_counter_test.go), including AddDependency so
the wisp/compose cases run instead of skipping. List stays nil on this
fixture since listTable() has pre-existing, unrelated permanent refusals
for ListRequest.NoParent/ExcludeTypes, so the three *MatchesListCardinality
cases skip here by design.

internal/storage's TestEveryLegWiresEveryRoleContract/http ceiling check
remains red (53 skipped vs ceiling 49) after this change, but strictly
improved: before this change the inherited baseline was 62 skipped (13
over ceiling), all nine of S8's new entrypoints unwired. This change wires
all nine, closing exactly that gap; the remaining 4-over are named
RunBootstrapper*/RunDependencyEditor* cases unrelated to count-scope,
pre-existing debt from other already-merged slices whose ceiling was
never bumped for them. Left the ceiling constant alone rather than
raising it to paper over debt this change doesn't own.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(storage): close the http leg's role-contract ceiling regression

TestEveryLegWiresEveryRoleContract/http went red after the rebase (62
skipped, 13 past its ceiling of 49) before any work in this session; the
prior commit closed 9 of those (S8's Counter cases). The remaining 4-over
were RunBootstrapper* (9 entries) and RunDependencyEditor* (2 entries),
both newly-merged contracts this leg had never classified.

RunBootstrapper*: internal/httpclient/accessors.go already names
Bootstrapper PERMANENTLY UNSERVABLE over http (bd init resolves its
substrate identity entirely client-side, before any server exists to
dial) — add a new named waiver, bootstrapperPermanentlyUnservableWaiverReason,
covering all nine entrypoints. VersionReconciler and InitVerifier are
named permanently unservable by the same accessors.go comment but are
left under the ceiling for their own reviewed change, out of scope here.

RunDependencyEditor*: the OSS server already serves add/remove and the
http leg already wires nearly all of DependencyEditor's contract. The two
missing cases (SameTypeReAddWithChangedMetadataMintsOneVersion,
SameTypeReAddWithIdenticalMetadataIsANoOp) only needed
SetVersionedHistoryEnabled wired onto the fixture, the same way
SetJournalEnabled already is — wired it for real; both pass served.

With Bootstrapper waived and DependencyEditor fully wired, the leg's
unwaived-missing count drops from 53 to 42, so adoptionCeiling moves
from 49 to 42 to keep the ratchet exact, with the adopting prose updated
to match.

TestEveryLegWiresEveryRoleContract is green. Served tier (real, CGO +
embedded dolt): 611 PASS, 0 FAIL, 72 SKIP (up from 609). bazel build/test
and make api-check pass for the touched targets.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* build(bazel): auto-sync BUILD files

Applied from the bazel-sync-patch artifact of https://github.com/gastownhall/beads/actions/runs/37307836753
(`make bazel-sync`: gazelle, tools/bazel/go_srcs.py, bazel mod tidy).
Only BUILD.bazel, MODULE.bazel and MODULE.bazel.lock are ever pushed; see
scripts/bazel-autofix-push.sh.

* fix(conformance): pass -tags gms_pure_go to Tier 3's served httpclient run

scripts/conformance.sh:61 ran a bare `go test -timeout=40m ./internal/httpclient/`
for Tier 3 (served HTTP client/role conformance), which `make ci-pr-policy`'s
build-tag policy check refuses ("bare go test without -tags=gms_pure_go").
Pass the same -tags Tier 1 already does, and update
TestConformanceScriptUsesExplicitTimeoutBudgets' pinned argv to match.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(bazel): give //internal/httpclient:httpclient_test room to run

The package takes ~350s under --config=ci (BEADS_TEST_SKIP=dolt, no
BEADS_TEST_EMBEDDED_DOLT) — mostly wall-clock wait on the CA tests'
dialer/keep-alive timeouts, not CPU — but the go_test had no size/timeout
override, so it inherited the medium default's 300s and timed out
intermittently under Bazel (and took "Go test equivalence" down with it).
Set size = "large", timeout = "long", matching
//internal/storage/embeddeddolt's go_test.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(httpclient): fix data race in caContextHandler test helper

TestTransportForRaisesMaxIdleConnsPerHostForBursts drives caContextHandler
with 8 concurrent requests; handler() wrote requestSeen/host/sni from every
server goroutine with no synchronization, and the other nine callers (all
single-request) never exercised the race. go test -race reproduced it as
3x DATA RACE across repeated runs.

Guard the fields with a mutex and expose them only through accessor methods
(sawRequest/seenHost/seenSNI), updating every call site in
store_dependent_test.go accordingly.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(unsupportedgen): canonicalize reflect-visible stdlib aliases

TestGeneratedShellIsFresh fails on Go >= 1.27: encoding/json.RawMessage
became an alias of encoding/json/jsontext.Value, and reflect only ever sees
an alias's underlying type, so formatType rendered
storage.DoltStorage.MergeMetadata's signature with the new, non-portable
name even though the signature itself did not change. A contributor on
1.27 following the freshness test's own advice ("go generate
./internal/httpclient") would commit a shell that does not compile on the
1.26.7 SDK this repo pins.

Add reflectAliasCanonical, a table mapping a reflect-visible alias target
back to the name a human wrote in source, consulted by namedType. Add an
entry here for any future stdlib alias this generator's output needs to
stay stable across.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(httpclient): claimOnlyUpdate propagates the claim's RowVersion

bridge.go's GetIssue already stitches details.Revision onto
hydrated.RowVersion when claimOnlyUpdate rehydrates labels/CreatedBy onto
the claim response, but the claim response itself (issueops.Claimer's
bare-row contract) carries no revision at all, and the copy-back only
touched Labels and CreatedBy. So Lifecycle.Update({Actor, IssueID, Claim:
true}) — `bd update X --claim` — handed back Issue.RowVersion == 0 over
http where the local leg returns the real token, and feeding that into the
next write's ExpectedVersion got a spurious mismatch instead of none.

Copy RowVersion back too. Extend
RunLifecycleResultsCarryThePostWriteRowVersion (shared lifecycle/close/reopen
conformance contract) with a claim-shaped first link in the chain: claim,
then guard the next write with the claim result's own token, proving the
claim-only path populates the real version rather than a nonzero constant.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(httpclient): stitch RowVersion on SearchIssues' exact-id fast path

resolve.go's getIssuesByExactID decodes the same IssueDetails shape
bridge.go's GetIssue does, but never parsed details.Revision the way
GetIssue's own stitch does, so SearchIssues(ctx, "<exact id>") answered
RowVersion 0 — a one-line gap: Issue.RowVersion is json:"-", so the decode
left it at zero, and only the explicit parseRevision stitch recovers it from
the response's own Revision string member.

Add TestSearchIssuesExactIDPopulatesRowVersionForAGuardedWrite, pinning the
same guarded-write round trip TestBridgeGetIssuePopulatesRowVersionForAGuardedWrite
already pins for the plain GetIssue path.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(httpclient): route every write role's refusals through inexpressible

Review finding 7: write-side refusals were not uniformly *ErrUnsupported.
Read roles already wrap encoder refusals through Store.inexpressible, which
reaches both encode.RefusedError and encode.CapabilityError; the write side's
refuse() returned a bare *encode.RefusedError instead, so a caller doing
errors.As(err, &unsupported) caught the count-scope refusal but missed
Sweeper.Sweep(Limit), DetectCycles(IncludeTracks), ReadyClaimer.ClaimNext,
BatchApplier.ApplyBatch, BatchCloser.CloseBatch, Deleter.Delete,
DependencyEditor.AddDependencies, and BatchCreator.CreateBatch.

Give InexpressibleError a detail field that preserves the original error's
composite text (an "items[%d]..." or "Issue.%s:" prefix a deeper encoder
helper already adds) instead of collapsing the message to the bare ledger
row text, and wrap every remaining write role's exported method in a single
named-return defer calling store.inexpressible, matching the pattern
lifecycle.go's Create/Update/Reopen already use.

Add TestWriteRoleRefusalsMatchTheReadShape, pinning three write-side
refusals (Sweeper.Sweep Limit, CycleDetector.DetectCycles IncludeTracks,
ReadyClaimer.ClaimNext) now classify via errors.As(*storage.ErrUnsupported)
the same way a read-side refusal does.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(cmd/bd): stop spelling out Status: StatusOpen on a markdown batch create

Review nit 8: batchcreator.go's refuseUnwirableIssue refuses a populated
Status (apigen.BatchCreateItem has no wire slot for it), and
buildMarkdownBatchRequest always set Status: types.StatusOpen explicitly, so
`bd create --file` refused on every call against an http-backed store.

PreparePublicCreateRequest already defaults an empty Status to StatusOpen on
every backend, including the http client store's own server, so the explicit
assignment here was never a different outcome - only a populated member the
wire can't carry. Leave it unset; the created issue is identical on every
backend, and the http leg's role-level accounting for why Status and
CreatedBy are excluded from its allowlists is documented on
batchCreateCarriedIssueMembers (internal/httpclient/batchcreator.go).

CreatedBy is left as-is: that member genuinely has no wire slot AND the
server does not yet stamp it from Actor (a gap already tracked in this PR's
Follow-ups), so its refusal is correct, not a bug to route around.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* docs(httpapi): correct create.go's CreatedBy comment to caller-asserted

Review nit 8: the comment claimed CreatedBy is stamped from the
"authenticated actor," but actor is parsed straight from the request body
(s.bodyActor) and never authenticated - exactly as this file's own doc
comment states a few lines above. Reword to "caller-asserted actor" and
cross-reference that doc comment so the two stay consistent. No behavior
change: the stamp itself and its test coverage were already correct.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(httpclient): raise MaxIdleConnsPerHost for a supplied client's nil Transport

Review nit 8: DialWith's doc comment promises the raised MaxIdleConnsPerHost
ceiling on every dial, "whether or not a CA is configured," but the
opts.HTTPClient.Transport == nil arm only injected a transport when the
target's CA was configured (resolved.configured()). A caller-supplied
*http.Client with a nil Transport and no CA fell through untouched, so it
defaulted to http.DefaultTransport's own ceiling of 2 at request time instead
- silently exempting this one shape from the doc's promise and forcing a
fresh TLS handshake on most of a concurrent burst even once the pool is warm.

Route this arm through the same dialTransport helper the opts.HTTPClient ==
nil arm above already uses (the CA-scoped transport when a CA is configured,
the shared baseline otherwise), injected onto a copy so the caller's own
*http.Client fields (Timeout, CheckRedirect, Jar) stay untouched.

Add TestDialWithSuppliedClientNilTransportNoCARaisesMaxIdleConnsPerHost: an
end-to-end burst through wire.Client.Do, traced with httptrace, asserting the
connection pool settles into reuse at a burst size the default ceiling of 2
cannot sustain.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* docs(httpclient): stop citing nonexistent design docs from new source files

Review nit 8: several comments added by this lift cited
engdocs/design/http-client-backend.md, "D8 row N", and beads-design/slices.json
- none of which exist in this repository (the design lived in the
bd-enterprise-internal source this code was lifted from).

Point each citation at what actually ships in-repo instead:
engdocs/design/http-divergence-ledger.md (the generated, human-readable
rendering of the divergence ledger in encode/ledger.go) for the D7/D8/D9
discipline citations in encode.go, ledger.go, bijection_test.go,
write_ledger_test.go, wire/credential.go and store.go's package doc; and a
direct description of the wire-gap it records, rather than a slices.json
scope reference, in leg_contract_wiring_test.go's cycle-tracks waiver
rationale. Regenerate http-divergence-ledger.md from ledger_render.go (which
had the same stale citation) via
`go test ./internal/httpclient/encode -run TestDivergenceLedgerDocMatchesLedger -update-ledger-doc`
so the shipped doc and its generator agree.

No behavior change; comment and generated-doc text only.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* feat(httpclient): implement issueops.BatchGetter for the HTTP client leg

The rebase onto origin/main (item 9 of PR #7247's review) brought in
upstream #7248's new issueops.BatchGetter role on storage.DoltStorage,
breaking the HTTP client leg's interface satisfaction. This wires it up
as a read role (no actor, no version guard, writes nothing — dialed as
a POST only because MaxGetManyIDs ids cannot ride a GET query string),
mirroring Counter/Querier rather than the WriteWire-wrapped roles.

- wire/paths.go: add PathIssuesBatchGet ("/v0/beads/issues:batchGet").
- batchgetter.go: httpBatchGetter.GetMany dials the server and stitches
  each resolved issue's RowVersion from apigen.BatchGetIssue.Revision
  (the role's only wire spelling of the row token, since RowVersion is
  json:"-"). checkGetManyIDs pre-validates the cap and blank-id rules,
  in the same order internal/storage/issueops.ValidateGetManyRequest
  applies them, because batch_get.go's failBatchGetErr collapses every
  validation failure into one generic, code-less InvalidArgument the
  client could never decode back into *issueops.TooManyIDsError.
- accessors.go: add the BatchGetter() accessor (26th wire-backed role).
- unsupported_gen.go: regenerated, dropping the now-implemented stub.
- completeness_test.go: move BatchGetter into wireBackedAccessors and
  bump the asserted accessor-matrix total from 28 to 29.
- served_test.go / served_harness_test.go: both package-local "by hand"
  httpapi.Config builders (serveRoles, serveConfig) now wire
  cfg.BatchGetter, fixing ~56 served-tier tests that broke once
  BatchGetter joined httpapi.Listen's required role set.
- served_batch_getter_test.go: wire the HTTP leg into all 14
  backend/conformance.RunBatchGetterXxx cases, following
  served_graph_counter_test.go's pattern. This surfaced a real bug:
  GetMany built Missing via append([]string(nil), body.Missing...),
  which stays nil when the server reports no misses, violating the
  role's "Missing is never nil on success" contract — fixed to build
  via make([]string, len(body.Missing)) + copy.

Verified: go build/vet clean; internal/httpclient suite green
(including the embedded-dolt served tier); TestEveryLegWiresEveryRoleContract
holds the http leg's adoption ceiling at exactly 42 with no change needed;
TestInterfaceCompleteness and TestAccessorMatrixMatchesDesign pass.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* build(bazel): auto-sync BUILD files

Applied from the bazel-sync-patch artifact of https://github.com/gastownhall/beads/actions/runs/37450731721
(`make bazel-sync`: gazelle, tools/bazel/go_srcs.py, bazel mod tidy).
Only BUILD.bazel, MODULE.bazel and MODULE.bazel.lock are ever pushed; see
scripts/bazel-autofix-push.sh.

* test(httpclient): lock contextServer.seen against concurrent handler goroutines

TestDialWithSuppliedClientNilTransportNoCARaisesMaxIdleConnsPerHost drives
the stub with bursts of 8 concurrent requests; the handler appended to
seen without a lock. Readers only look at seen after their requests return.

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

* build(bazel): sync go_srcs managed blocks for unsupportedgen after the main merge

The PR adds internal/storage/unsupportedgen on a base that predates #7320,
which gave every Bazel package a managed repo_files filegroup. On the merge,
`python3 tools/bazel/go_srcs.py --check` (TestBazelGoSrcsBlocksCurrent) found
3 stale blocks: the package's own repo_files block, its entry in the root
BUILD.bazel repo_files list, and its entry in tools/bazel/BUILD.bazel.
`make bazel-sync` regenerated exactly those; gazelle changed nothing else.

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>

* fix(httpclient): refuse DepAddItem.HasSpawner and ThreadID on applyBatch after the main merge

Main's #7209 gave issueops.DepAddItem two members, HasSpawner and
ThreadID, and a conformance case, RunBatchApplyStampsSpawnerIDOnlyWhenNamed.
ApplyDepAddItem on the v0 wire publishes source, target, type and
metadata only, so on the merge the http leg dropped both silently and
two of the PR's gates went red:

- TestEveryWireExcludedWriteMemberCarriesALedgerRow: the reflective
  census found two wire-excluded DepAddItem members with no ledger row.
- TestEveryLegWiresEveryRoleContract/http: the new case left the leg
  at 43 unwired cases against its exact ceiling of 42.

applyDepAddItemBody now refuses each member where the role would store
something the wire cannot say, under two new KindRefuse rows,
W-DepAddItem.HasSpawner and W-DepAddItem.ThreadID. The spawner flag
refuses only on a waits-for edge, because the role ignores it on every
other type and stores the same row either way. The ledger doc is
regenerated (264 rows).

The served tier wires the new case as a park on W-DepAddItem.HasSpawner,
owned by the family's existing bead, which brings the http leg back to
its ceiling. With the skip removed the case fails on the client's
refusal; with the refusal also removed it fails on the drop the row
names: spawner_id "" where the role stamps the resolved target.

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>

* fix(httpclient): address #7247 relaunch review iteration 1

Review iteration 1 of the relaunched adoption raised six majors. This
commit fixes all six. Five are the majors the first adoption's
iteration 1 raised. Its fixup, 696803f250 (tag
adopt-pr/bd-f5illd/stranded-fixup-696803f250), fixed them but never
reached the branch. That fix is ported here onto the contributor's
current head, with its reasoning unchanged; the sixth is new.

Creates refused what the CLI sends. Every `bd create` carries
CreatedBy, a workspace whose config.yaml names a prefix also carries
IDPrefix, and the client refused both.
- The server stamps created_by from the actor on all three create
  shapes: batch_create.go and batch_apply.go now match create.go.
- The client sends a CreatedBy that names the actor and refuses any
  other value, which the server's stamp would silently replace.
  createIssue and the apply create item share
  refuseUnwirableIssueMembers.
- IDPrefix refuses only where the role would act on it: an explicit,
  unforced id.
- `bd create --file` stays refused over http. Dropping its Status was
  safe but not enough: the batch create still refuses its Provenance
  and Owner. The markdown.go and batchcreator.go comments that called
  it servable now say so.

keysetFilter ignored AfterPriority. It now compares priority first,
mirroring sqlbuild's KeysetPriorityCreatedAtIDPredicate, and a new
served case resumes a priority keyset position. AfterID or AfterPriority
without AfterCreatedAt no longer reads as a zero-instant position that
discarded every row (the AfterID minor).

Claim with a patch was refused on a false ledger claim. `claim`,
`force_assignee_transfer` and `force_close_policy` now ride updateBody
like `force_notes_overwrite`, so a claim with a patch or a version guard
is one updateIssue call. A claim ALONE sent to a server that predates
the member (#6890) falls back to claimIssue. The three ledger rows are
retired, and the served cases parked on them now run.

OpenReadOnly returns a writable store. It stays writable, as a
documented exception: backends.Backend.OpenReadOnly also serves the
root pre-run's classified reads, and `bd ready --claim` writes through
that store. The exception is named in the backends.Backend contract,
the store_factory invariant and the store doc, and
TestReadOnlyOpenServesTheClaimBdReadyMakes pins it. An OpenNonMutating
backend hook is the follow-up.

Security surface.
- http_target.json is in the doctor gitignore template and its required
  patterns.
- BEADS_HTTP_TOKEN and BEADS_HTTP_TOKEN_COMMAND are host-scoped with
  BEADS_HTTP_CA_FILE's "host[:port]=value" syntax. A value scoped to
  another server falls through to the next rung, and a command scoped
  elsewhere never runs.
- LoadTarget and SaveTarget refuse a url that carries userinfo, and
  Target.String drops userinfo.

The served Tier 3 had no CI home. The new target
//internal/httpclient:httpclient_served_test is an `embedded`-tagged
sh_test. It reruns httpclient_test's binary through
tools/bazel/go_test_variant.sh with the script's
BEADS_TEST_EMBEDDED_DOLT=1 and BEADS_HTTP_TEST_REQUIRED=1, in 24 shards
on the embedded lane's race build. bazel.yml's bazel-embedded job
already runs `bazel test //... --config=embedded`, so no workflow
changes.
- -test.timeout=19m, not the script's 40m: Go's own timeout then fires,
  and dumps every goroutine, before the action's 1200s limit.
- TestBazelRetiredLanesCannotBeNarrowed pins its args and env. The new
  TestConformanceServedTierRunsUnderBazel ties the script's Tier 3
  switches to the rule.
- The new untagged TestServedTierIsLinkedWhenRequired fails a required
  run built without cgo, which drops every served file (the sentinel
  minor).
- A first 12-shard run found two test defects; both are fixed.
  TestServedBatchApplyBoundsTheItemCount's 1000-item apply outlasts the
  client's 60s per-request timeout under -race, so a
  race_detector_{on,off}_test.go pair scales its "at bound" half to 150
  there, as embeddeddolt's TestBatchApplyContract does. And
  TestTheCompositionCatchesAWrongParameter's dropped-filter case leaned
  on rows earlier tests had seeded: run alone, or first in its shard,
  it failed. It now seeds its own row outside the scope.
- conformance.sh, its script test and
  engdocs/CI_REQUIRED_CHECK_TOPOLOGY.md name the target.

Deviations from the review's suggested fixes:
- A bare BEADS_HTTP_TOKEN(_COMMAND) fails closed rather than falling
  back, and the error does not echo the value: a bare token split at an
  "=" would put token text into the pattern half. Dropping the bare
  syntax is safe today: the backend has no Register call site and no
  docs/ page names the variables.
- Target.String drops userinfo instead of using url.URL.Redacted, which
  masks only the password, so a token used as the username would print.
- The pre-#6890 claimIssue fallback leaves RowVersion unset instead of
  borrowing the follow-up read's token. That read is a second snapshot,
  so its token could postdate a write this claim never saw and arm a
  guard that should miss; a guard built from the unset token fails
  closed. Against a current server the claim rides updateIssue, whose
  `revision` is stitched as for any update.

Also fixed: the nit about the completeness failure message, which now
says to regenerate the shell, and the offrole.go tombstone comment that
named a skip list which no longer exists. Left for follow-up (all
latent, since httpclient.Register has no production caller): vocabOnce
latching a failed fetch, snapshot holding s.mu across the handshake
dial, unchunked graph anchors, private tracker IDs in park owners, and
the bounded journal limit above 10000.

Validation:
- git diff --check; gofmt -l; go vet over internal/httpclient/... and
  scripts
- go test -tags gms_pure_go over internal/httpapi/...,
  internal/httpclient/..., internal/storage/backends/... and
  backend/conformance/...; the doctor gitignore tests; the cmd/bd
  events-journal construction guard; ./scripts
- bazel test --config=embedded
  //internal/httpclient:httpclient_served_test on the lane's remote
  executors: 24 shards, 702 top-level tests, 632 pass, 70 skip (69
  ledgered KNOWN DIVERGENCE, plus
  TestCAFileHygieneRefusesGroupWritableFile, which finds no
  supplementary group on the executor), 0 fail, 0 data races; slowest
  shard 283s. tools/bazel/check_testcases.py --bep passes.
- each of the 702 tests run alone (go test -run '^Name$', without
  -race): 0 fail
- the whole tier under go test without -race, with the script's
  switches and -timeout=40m, before the two test fixes: 633 pass, 69
  skip, 0 fail
- removing BEADS_HTTP_TEST_REQUIRED from the rule fails both policy
  tests
- bazel test --config=ci over //internal/httpclient/...,
  //internal/httpapi/..., //scripts/..., //test/docsync/...,
  //internal/storage/backends/..., //backend/conformance/... and
  //cmd/bd/doctor/...: 32 tests pass
- make ci-pr-lint: nogo native plus windows/darwin, passes

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 (1M context) <noreply@anthropic.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Eddie the Engineer <adopt-pr@gascity.com>
2026-10-08 12:01:09 +00:00

1367 lines
54 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{"metadata", "source", "target", "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)
}
// created_by is stamped from the actor on every create item, createIssue's
// rule (create.go): the item publishes no created_by and the role copies the
// issue's rather than stamping one.
for _, item := range items {
if item.Create != nil {
item.Create.Issue.CreatedBy = 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
}
item := &issueops.CreateItem{Key: derefString(wire.Key), Issue: issue}
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)
}
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"), ""))
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...))
}