beads/internal/httpapi/cursor.go
CI Bot 1c35a769d3 feat(issueops): add the Reader role and wire both front doors through it
Reader is Lifecycle's read counterpart, built as its mirror image: a role in
the leaf `issueops` package, reached through a capability accessor on the
storage seam, implemented at the same six sites. `Storage` gains
`IssueReader() (issueops.Reader, error)` beside `IssueLifecycle()`, and the
governing rule holds — a new capability got a new role and a new accessor,
not four more methods on an existing one.

SUBSUME, NOT COMPOSE. Reader takes the whole high-level request and performs
filter and default construction INTERNALLY, supplying its own ConfigSource
from the store or unit of work it already holds. Under compose both front
doors would each build a ConfigSource, load config, call a builder and execute
— two callers again, which is the topology that shipped a bare IssueFilter to
a viewer and silently served in_progress rows as "ready". Under subsume the
ritual is not callable from a front door: handlers and RunE bodies can only
say rd.List(ctx, req), so the failure mode is unwritable rather than merely
discouraged.

The three read endpoints are implemented against the frozen OpenAPI v0
document and consume the accessor; the 501 stubs and their self-deleting
exemption list are gone. `bd show --json` reaches the same role through the
same accessor, resolving the canonical id first so fuzzy and cross-repo
lookup stay CLI-local.

The workapi builders are re-typed onto the leaf request types, so there is no
second params struct to drift; both recorded goldens replay byte-identical,
which is the proof the re-typing changed nothing.

Load-bearing details:

- `sort` is forwarded EXPLICITLY as priority when absent. The storage layer
  reads an empty policy as hybrid, which demotes older high-priority work and
  therefore changes the item SET once a limit truncates — while the document
  still reads correctly. Pinned by a handler test on the recorded filter and
  by the parity oracle's order assertion.
- The parity oracle compares FULL ITEM JSON, not id sets. An id-set oracle
  passes while ready ships a bare Issue against the CLI's IssueWithCounts.
- internal/workapi no longer imports internal/storage/uow; it names the slice
  of a unit of work it reads through, so the uow provider can implement the
  role without closing an import cycle.
- internal/utils' cgo store test moves to an external test package for the
  same reason, with its two unexported-symbol cases split out.

Verified: issueops is still a leaf (internal/types + stdlib only); the
protocol corpus is unchanged structurally and behaviourally under
BEADS_PROTOCOL_REQUIRE_DOLT=1; a real `bd serve` answers all six operations
against a real workspace with CLI and HTTP JSON identical field for field.
2026-07-31 18:37:25 +00:00

83 lines
2.9 KiB
Go

package httpapi
import (
"encoding/base64"
"encoding/json"
"strings"
"time"
"github.com/steveyegge/beads/internal/types"
)
// cursorVersion prefixes every token this server mints. It is the ONLY thing
// that invalidates a cursor: the token carries a position and nothing else, so
// it does not expire, does not die with a restart, and is not tied to the
// connection that issued it. Bumping this is how an encoding change is made
// safely — every older token then decodes to invalid_cursor, whose documented
// recovery is to restart paging.
const cursorVersion = "v1"
// cursorPosition is the keyset position a token carries: one row's place in
// the (created_at DESC, id ASC) total order.
//
// The token deliberately does NOT carry the filters it was minted under. That
// is a documented property with a documented consequence — a cursor reused
// under different filters is not refused, it just resumes from the old
// position — and it is what keeps the token from becoming a second, opaque
// copy of the request that can disagree with the request itself.
type cursorPosition struct {
CreatedAt time.Time `json:"t"`
ID string `json:"i"`
}
// encodeCursor mints the opaque token for a page's last row.
//
// The encoding is server-private and clients are told never to parse it, which
// is the whole point: base64 of a JSON object is legible enough that someone
// WILL read it, so the contract is enforced by the version prefix rather than
// by obscurity. A client that constructs its own token gets invalid_cursor the
// moment the encoding moves.
func encodeCursor(pos cursorPosition) string {
blob, err := json.Marshal(pos)
if err != nil {
// Unreachable: the struct is a time and a string.
return ""
}
return cursorVersion + "." + base64.RawURLEncoding.EncodeToString(blob)
}
// decodeCursor reads a token this server minted. Every failure mode —
// wrong version, undecodable base64, malformed JSON, an empty position — is
// the same answer, because they are the same client situation: the position
// cannot be salvaged and re-sending the value cannot succeed.
func decodeCursor(token string) (cursorPosition, bool) {
rest, ok := strings.CutPrefix(token, cursorVersion+".")
if !ok {
return cursorPosition{}, false
}
blob, err := base64.RawURLEncoding.DecodeString(rest)
if err != nil {
return cursorPosition{}, false
}
var pos cursorPosition
if err := json.Unmarshal(blob, &pos); err != nil {
return cursorPosition{}, false
}
if pos.CreatedAt.IsZero() {
return cursorPosition{}, false
}
return pos, true
}
// cursorFor returns the token that resumes after the last row of a page, or ""
// when there is nothing to resume from.
func cursorFor(items []*types.IssueWithCounts) string {
if len(items) == 0 {
return ""
}
last := items[len(items)-1]
if last == nil || last.Issue == nil {
return ""
}
return encodeCursor(cursorPosition{CreatedAt: last.CreatedAt, ID: last.ID})
}