beads/internal/httpapi/close.go
Julian Knutsen b858a29e4a
fix(contracts): JSON contract fixes before 1.3.0 freezes them — string revision tokens, --set-metadata scalar typing, create --json nano timestamps (#6053)
* fix(metadata): restore v1.2.2 scalar typing for --set-metadata values

8ebd53e88 moved the CLI's metadata-edit helpers into
internal/storage/metadata.go and, in the move, replaced v1.2.2's
toJSONValue inference with a MetadataEditValue that always json.Marshals
the value as a string. The intent was GH#4146 (a numeric-looking id or a
version like "1e3" came back as a JSON number, breaking map[string]string
Go consumers), but it silently changed the shipped CLI contract:
`bd update --set-metadata k=5` started storing "5" instead of 5. That
regression shipped only in the deprecated/superseded v1.2.1, never in
v1.2.2, and 1.3.0 would freeze it.

Restore v1.2.2's inference body-for-body inside MetadataEditValue, so the
parity claim stays checkable against `git show v1.2.2:cmd/bd/update.go`:
"null" is JSON null, "true"/"false" are booleans, a spelling that scans
as a float AND is valid JSON on its own is emitted as that literal
(5 -> 5, 1e3 -> 1e3, 0.50 -> 0.50, while 05, +5 and NaN stay strings),
and everything else is a string. Every --set-metadata caller funnels
through this one function: embedded (cmd/bd/update.go ->
storage.ApplyMetadataEdits), proxied (parseSetMetadataFlags) and the
update executor's []string path.

GH#4146's concern is served by the explicitly typed routes, which are
untouched: --metadata / --metadata-json on the CLI (MergeMetadataJSON)
and the typed set map on the HTTP/Go path (applyTypedMetadataEdits).

No data migration: a "5" written by an in-window build is
indistinguishable from a string a user set intentionally through the
typed paths, so normalizing stored metadata would corrupt intended data.
Re-running the same --set-metadata k=5 retypes any given key.

Tests: new table in internal/storage/metadata_edit_value_test.go pinning
the v1.2.2 matrix plus an ApplyMetadataEdits end-to-end; the tests that
pinned the string-always behavior are flipped (domain/db mergeops suite,
update_lifecycle patch mapping, cmd/bd toJSONValue table) and the
embedded + proxied CLI suites now assert a stored JSON number. The
contract corpus is regenerated: its update/list/ready goldens run
`--set-metadata phase=2`, so `"phase": "2"` becomes `"phase": 2` and
nothing else moves.

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

* fix(create): echo full-precision timestamps from bd create, not the DATETIME(0) re-read

b92442d1a routed `bd create` through issueops.Lifecycle, whose executors
build their result by re-reading the row they just wrote
(HydrateIssueOperationResult -> GetIssueInTx). created_at/updated_at are
DATETIME(0) columns, so that snapshot comes back at whole-second
precision and cmd/bd/create.go echoes it. v1.2.2 returned the caller's
own in-memory struct, which PrepareIssueForInsert stamps with
time.Now().UTC() including nanoseconds, so `bd create --json` used to
print e.g. 2026-08-28T12:34:56.789012345Z. Agents that create several
issues in a burst order them by exactly that fraction; rounded to the
second they all tie.

Restore the echo with OverlayCreateTimestamps: after hydration, put back
the created_at/updated_at (and closed_at, when the write set one) the
INSERT actually sent. Everything else on the snapshot stays DB-truth,
which is the facade's contract — only create, and only these fields.

Applied at every create body, not just the two the CLI reaches:
issueops.ExecuteCreate and ExecuteCreateBatch behind the dolt and
embedded-dolt stores, and the unit-of-work provider's own Create,
CreateBatch and batch-apply create item, which is what the proxied server
and POST /v0/beads/issues run on. Fixing only the shared executors would
have left the two bodies disagreeing about the same promise.

What is NOT changed, because it is not a regression: `bd show --json` and
`bd list --json` answered whole seconds in v1.2.2 too — they read the
column — and update/close/reopen re-read after writing there as well.
Widening the columns to DATETIME(6) would give reads sub-second times
too, but that is a schema migration on user Dolt DBs and touches paths
that deliberately compare at second precision; it is a 1.4 change.

The accepted skew, which predates this and stays: the engine ROUNDS to
the column rather than truncating, so a later read of the same row can
sit up to half a second either side of the create echo.

Tests: two new conformance cases, run on all three backends —
RunLifecycleCreateEchoesSubSecondTimestamps (a caller-supplied
nanosecond stamp must survive to the result exactly; the auto-stamped arm
stays a bounded smoke check, since requiring a non-zero fraction off the
clock is flaky by a billionth) and RunBatchCreatorEchoesSubSecondTimestamps
(per item, since both bodies build the result one snapshot at a time).
Plus a cmd/bd embedded test asserting `bd create --json` prints a
fractional part, and one httpapi assertion that the wire does not round
the role's snapshot on the way out. All four fail on the pre-fix tree.

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

* fix(httpapi,types)!: revision / expected_version tokens are opaque JSON strings

The optimistic-concurrency token is minted by freshRowLock over the FULL
int64 range (crypto-random, non-zero) and published on the wire as a JSON
number: `revision` on responses, `expected_version` on guarded requests.
A JSON number does not survive a consumer that decodes into an IEEE-754
double — JavaScript's JSON.parse, Go's `any`, Python's `float`, jq. Live
tokens run past 5e17, where a double's ulp is already 64, so such a
client reads back a value NEAR the token, echoes that as its guard, and
earns a 409 against a row nobody else touched. The spec already carried
"DECODE IT AS A 64-BIT INTEGER" paragraphs telling clients to work around
this; that is a workaround for a wire type that should not have been a
number.

Emit and accept the token as a decimal STRING instead. It is documented
as opaque and EQUALITY-ONLY — compare it, never order or interpret it —
and a string round-trips exactly in every JSON consumer. It also leaves a
future backend free to mint a token that is not an int64 at all.

Internals are untouched: freshRowLock, the row_lock BIGINT column, its
migration, types.Issue.RowVersion and the ExpectedVersion *int64 guards
on the issueops requests all stay int64. Only the wire projection moves,
through the one spelling of the encoding — types.RevisionToken and
types.ParseRevisionToken.

STRICT STRING ONLY, with no transitional number-or-string acceptance.
This API already refuses unknown members by name and keeps one spelling
of the guard, and a number is precisely the shape a double-based client
has already rounded before it left the client — accepting it would
preserve the defect. A numeric guard is a 400 naming the member.

Chosen over clamping freshRowLock to 53 bits: a clamp does not fix rows
already minted with full-range tokens by in-window builds, so a JS
consumer reading one still rounds past 2^53 — repairing that would need a
row_lock normalization sweep over issues and wisps, a migration days
before a tag.

Surface: types.IssueDetails.Revision (what `bd show --json` prints and
GET /v0/beads/issues/{id} returns), the five hand-assigned httpapi
response fields, applyVersionGuardMember (now the reader for the batch
update item too, ahead of its typed decode, so a mistyped token still
earns a 400 naming the member), Problem.expected_version, and 14 OpenAPI
members regenerated through `make api-gen`. actual_version goes with them
— it is documented as "the `revision` the row was found holding", so
leaving it an integer would make the expected/actual pair disagree about
its own type; it is declared but never populated, so nothing changes
behaviorally.

Nothing shipped in a tagged release ever carried this member: v1.2.2 has
no internal/httpapi at all and no `revision` anywhere on the wire, so the
whole surface is in-window and can still be changed compatibly. Legacy
migration-0054 rows read as the string "0" with the same CAS semantics.

Corpus goldens are UNCHANGED: the canonicalizer already replaced any
`revision` value with the string "<REVISION>" whatever its JSON type.

Tests: types-level pins that the marshaled member matches `"revision":
"-?\d+"` and that RevisionToken/ParseRevisionToken round-trip MinInt64,
MaxInt64 and 2^53+1, plus a JS-realistic decode of 2^53+1 with a float64
control proving the token really is one a number would corrupt; httpapi
refusal tests for a numeric guard on update, close, reopen, delete and
both batch item kinds; a 409 echo asserted as the string; and the proxied
serve integration tests now splice the response's own token back in
verbatim rather than re-rendering a parsed one.

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

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-29 05:20:27 +00:00

285 lines
12 KiB
Go

package httpapi
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"unicode/utf8"
"github.com/steveyegge/beads/internal/httpapi/apigen"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/issueops"
)
// closeRequestMembers is the document's member list for CloseIssueRequest. The
// schema is additionalProperties: false, so anything else is refused BY NAME —
// which is why the body is decoded as raw members first, the posture claim and
// batchCreate already take.
var closeRequestMembers = []string{"actor", "reason", "session", "force", expectedVersionMember}
// handleClose closes one issue: the second half of the loop this surface exists
// to serve, and the half that still forked a subprocess.
//
// 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, exactly as for
// POST /v0/beads/issues/{id}:claim. The only durable effect is the single
// storage commit the role makes inside its own transaction.
//
// Everything above the role is argument validation: the media type, the body
// shape, and the actor, reason and session rules. The close itself — the
// done-status normalization, first-close-wins on reason and session, the open-
// children and live-blocker policy, the transaction retry — belongs to
// issueops.Lifecycle, reached through the provider's own accessor.
//
// PLANES. Unlike the claim, this resolves ids across BOTH planes, because the
// role does and this surface serves the same work contract the CLI serves. A
// close whose target is a wisp lands on the unversioned plane and records no
// durable history entry.
func (s *Server) handleClose(w http.ResponseWriter, r *http.Request) {
// The custom-method dispatcher split the id off the segment and bounded it
// before this handler was chosen at all; see customMethodTarget.
id := r.PathValue(customMethodIDValue)
if !s.requireNoQuery(w, r) {
return
}
if !s.requireJSONContent(w, r) {
return
}
request, ok := s.closeRequest(w, r, id)
if !ok {
return
}
lifecycle, err := s.lifecycle(r)
if err != nil {
s.failClose(w, r, request, err)
return
}
result, err := lifecycle.Close(r.Context(), request)
if err != nil {
s.failClose(w, r, request, err)
return
}
// `already_closed` is the wire's name for the idempotent re-close, which is
// exactly the case the role reports as an unchanged result — and the case
// whose `reason` and `session` were NOT rewritten.
//
// `revision` is the row's post-close concurrency token, read off the same
// snapshot rather than computed. It is on the wire because `expected_version`
// is: a guard whose token no response carries is a guard a caller cannot
// fill. types.Issue.RowVersion is `json:"-"`, so the Issue body cannot carry
// it and this member is where it lives — updateIssue's arrangement exactly.
writeJSON(w, apigen.CloseIssueResponse{
Issue: *result.Issue,
AlreadyClosed: !result.Changed,
OpenChildren: result.OpenChildren,
Revision: types.RevisionToken(result.Issue.RowVersion),
})
}
// closeRequest decodes and validates the body, and reports whether the request
// may proceed. Every refusal here happens BEFORE any database work, which is
// what lets the 400s reflect the caller's own input back.
func (s *Server) closeRequest(w http.ResponseWriter, r *http.Request, id string) (issueops.CloseRequest, bool) {
members, res := decodeJSONObjectBody(w, r)
if res != nil {
s.fail(w, r, *res)
return issueops.CloseRequest{}, false
}
if offender, unknown := unknownMember(members, closeRequestMembers); unknown {
s.failUnknownMember(w, r, offender, closeRequestMembers)
return issueops.CloseRequest{}, false
}
actor, ok := s.bodyActor(w, r, members)
if !ok {
return issueops.CloseRequest{}, false
}
reason, ok := s.storedTextMember(w, r, members, "reason")
if !ok {
return issueops.CloseRequest{}, false
}
session, ok := s.storedTextMember(w, r, members, "session")
if !ok {
return issueops.CloseRequest{}, false
}
force, ok := s.booleanMember(w, r, members, "force")
if !ok {
return issueops.CloseRequest{}, false
}
// The guard the response's `revision` exists to feed. Read through the
// shared int64 decoder rather than a local one so a malformed token is
// refused with the same sentence on every operation that takes one.
expectedVersion, res := applyVersionGuardMember(members, "")
if res != nil {
s.fail(w, r, *res)
return issueops.CloseRequest{}, false
}
return issueops.CloseRequest{
Actor: actor,
IssueID: id,
Reason: reason,
Session: session,
Force: force,
ExpectedVersion: expectedVersion,
}, true
}
// bodyActor validates `actor` under the claim's rules, shared rather than
// restated: the value lands in the same columns and the same storage commit
// message, so a newline forges the same audit-trail lines.
//
// It is batchCreateActor's body, lifted so that every operation carrying an
// actor in an object body reads it the same way. The claim keeps its own
// because its body has exactly one member and it refuses the rest by name
// before looking at any of them.
func (s *Server) bodyActor(w http.ResponseWriter, r *http.Request, members map[string]json.RawMessage) (string, bool) {
raw, ok := members[claimActorMember]
if !ok {
s.fail(w, r, InvalidArgument(claimActorMember, ReasonInvalidValue, "`"+claimActorMember+"` is required"))
return "", false
}
// Through a POINTER so that `null` reaches the type-mismatch branch, for
// the reason claimActor gives: unmarshaling JSON null into a string is a
// no-op, which would report a null as "empty after trimming" — the right
// status attached to prose that misdescribes what the client sent.
var actor *string
if err := json.Unmarshal(raw, &actor); err != nil || actor == nil {
s.fail(w, r, InvalidArgument(claimActorMember, ReasonInvalidValue, "`"+claimActorMember+"` must be a string"))
return "", false
}
trimmed, res := validateActor(*actor)
if res != nil {
s.fail(w, r, *res)
return "", false
}
return trimmed, true
}
// storedTextMember reads an optional string member destined for a stored
// column, or reports the refusal it earned. An absent member is the empty
// string, which is what the role reads as "not supplied".
//
// The bound is types.CheckFieldLen keyed on storage's own constant rather than
// a second copy of the schema's number — the actor precedent, where the
// schema's maxLength is prose and the constant is binding. Control characters
// are refused for the actor's reason: these values land in columns that
// renderers print, so an unfiltered C1 introducer makes a close reason an
// escape-sequence payload in anything that shows it.
//
// Explicit `null` is a 400 naming the member rather than a clear. This surface
// has no clearable member here — the role reads absence as "not supplied" and
// first-close-wins means a re-close writes neither — so admitting null would
// publish a third state with no meaning behind it.
func (s *Server) storedTextMember(w http.ResponseWriter, r *http.Request, members map[string]json.RawMessage, name string) (string, bool) {
raw, ok := members[name]
if !ok {
return "", true
}
refuse := func(detail string) bool {
s.fail(w, r, InvalidArgument(name, ReasonInvalidValue, detail))
return false
}
var value *string
if err := json.Unmarshal(raw, &value); err != nil || value == nil {
return "", refuse("`" + name + "` must be a string")
}
switch {
case types.CheckFieldLen(name, *value) != nil:
return "", refuse(fmt.Sprintf("`%s` is %d characters; storage holds at most %d",
name, utf8.RuneCountInString(*value), types.MaxFieldLen))
case strings.ContainsFunc(*value, isControlChar):
return "", refuse("`" + name + "` must not contain control characters")
}
return *value, true
}
// booleanMember reads an optional boolean member, defaulting to false — the
// document's default for every flag on this surface. Explicit `null` is a 400
// naming the member, by storedTextMember's rule and for its reason.
func (s *Server) booleanMember(w http.ResponseWriter, r *http.Request, members map[string]json.RawMessage, name string) (bool, bool) {
raw, ok := members[name]
if !ok {
return false, true
}
var value *bool
if err := json.Unmarshal(raw, &value); err != nil || value == nil {
s.fail(w, r, InvalidArgument(name, ReasonInvalidValue, "`"+name+"` must be a boolean"))
return false, false
}
return *value, true
}
// failClose answers a failed close: the precondition's 409, then the shared
// classification with the extension member the open-children 409 carries.
//
// THE PRECONDITION ARM IS MATCHED TYPED AND FIRST, obeying the rule
// ClassifyError states rather than restating it: that function cannot build
// this 409, because the refusal echoes the value the REQUEST guarded on and it
// is handed an error alone.
//
// The cost of forgetting the arm is not a worse 4xx. Neither leg wraps
// ErrVersionMismatch in ErrValidation — the store legs return CheckVersionInTx's
// error through runIssueOperationTx unchanged and the unit of work returns its
// own — so it arrives as a bare sentinel, falls through failErr's default, and
// every guard miss on this operation becomes a GENERIC 500. Verified by
// mutation rather than asserted: removing the arm turns
// TestCloseRefusesAStaleGuard into a 500.
//
// The open-children count comes from *issueops.CloseOpenChildrenError's own
// field, which the role fills inside the transaction that refused — never from
// parsing the sentinel's message ("%d open child issue(s)"). That substring
// classification is what a client adopting this endpoint gets to delete, and it
// can only delete it if the server never does it either.
//
// The live-blocker refusal shares the code and carries NO member, which is what
// makes member presence the discriminator the document promises.
func (s *Server) failClose(w http.ResponseWriter, r *http.Request, request issueops.CloseRequest, err error) {
if errors.Is(err, issueops.ErrVersionMismatch) {
s.fail(w, r, versionPreconditionResult(request.ExpectedVersion))
return
}
var openChildren *issueops.CloseOpenChildrenError
if !errors.As(err, &openChildren) {
s.failErr(w, r, err)
return
}
res := ClassifyError(err)
if res.Problem.Code == string(CodeNotClosable) {
res = res.WithOpenChildren(openChildren.OpenChildren)
}
s.fail(w, r, res)
}
// versionPreconditionResult builds the 409 for the row-version guard that
// missed, naming the member and echoing what the request asked for.
//
// It is shared by the close, the reopen and the delete because all three
// publish ONE guard, where updatePreconditionResult has to choose between
// three. The rule is updatePreconditionResult's unchanged: the expected value
// comes from the REQUEST rather than from a read, and the observed value is
// absent, because the refusal rolled its transaction back and a read afterwards
// would describe a row the refusal never saw. See PreconditionFailed.
//
// A nil expectation cannot reach here from any of the three handlers — the
// role raises this sentinel only when a guard was sent — but it is handled
// rather than dereferenced, so a role that returns it unprompted is a 409
// without the echoed member instead of a panic on a live server.
func versionPreconditionResult(expected *int64) Result {
res := PreconditionFailed()
member := expectedVersionMember
res.Problem.Param = &member
if expected != nil {
res = res.WithExpectedVersion(*expected)
}
return res
}
// expectedVersionMember is the one spelling of the row-version guard, shared by
// the three operations that publish it so the member name a client reads off
// `param` cannot drift from the member name it sent.
const expectedVersionMember = "expected_version"