Skip to content

feat(server): return a since-cursor on query responses - #436

Merged
baer merged 5 commits into
ci/crash-fuzzer-workflowfrom
http/query-since-cursor
Nov 25, 2026
Merged

baer merged 5 commits into
ci/crash-fuzzer-workflowfrom
http/query-since-cursor

Conversation

@baer

@baer baer commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

What & why

A client that reads a query and then polls /v1/since can miss a write that commits and folds between the two. Query responses now carry _meta.since_cursor for the exact state the read returned, and the React poll starts from it. A CursorExpired subscriber resumes from the re-read's cursor.

Key points

  • Additive response field, so compatible.
  • _meta is kept on the by-id 404 so a subscriber on a missing row still gets a cursor.
  • Client growth is rebaselined in its own commit (client.js and client-react.js).

Verification

| pnpm verify:agent | clean |
| pnpm build:agent | clean |
| pnpm test:agent | 6435 passed |
| pnpm test:randomize | 6213 passed |
| pnpm bundle-sizes | 14/14 after rebaseline |

Notes for reviewers

Stacked on the crash-fuzzer workflow PR only for a shared base; they do not depend on each other.

baer added 5 commits November 25, 2026 12:17
A subscriber that renders a read and bootstraps /v1/since with a
separate cursor="" request takes the two positions at different
moments, so a change that commits and folds between them is never
delivered, and a change the read already folded can be delivered
again.

GET /v1/c/:collection, GET /v1/c/:collection/:id and GET /v1/count now
add _meta.since_cursor: the cursor positioned exactly after the
snapshot plus log tail the read folded. It reuses the /v1/since
position forms through a new sinceCursorAt(manifest, tail): position
tail - 1 in the manifest's generation, or the before-seq-0 cursor
while the tail is 0 or no current.json exists. The empty-cursor poll
now mints its position through the same helper. ReadResult carries
the head and probed tail the read already holds, so the cursor costs
no storage call and request-counts.json is unchanged.

The 404 from GET /v1/c/:collection/:id carries the same _meta beside
its error envelope, so a subscription to a row that does not exist
yet seeds from the miss.

A fast-check property drives the router with inserts, updates,
deletes, folds, retirements, uncertified reclaims, reads and polls.
The seeding read is a list, a .where() list, a count, or a by-id get
that hits or misses. Writes and a fold land between a read and its
first poll, inside a list read after it loaded current.json, and
between long-poll cycles. The state a read saw is recovered from its
result, never from its cursor. A subscriber seeded from the read's
cursor must be handed every later change once, in order, with its
view matching the committed state, or get CursorExpired only for a
reclaimed entry. A fixed case pins that a separate cursor="" seed
fails the same checks. HTTP conformance pins the field on every read
route and the 404, and that polls from it resume after the read.

Bundle: http.js, cloudflare.js, node.js and dev-vite.js grow by the
cursor minting in the read routes and the 404 envelope (http.js +69 B
min-gz); index.js moves by the ReadResult fields.
The subscription pool ran a query's first read and a cursor="" poll
concurrently, and again after each CursorExpired. The poll's position
came from a different moment than the read, so a change that
committed and folded between the two was never delivered (the query
kept rendering the old rows until the next change), or a change the
read already showed was delivered again.

Each table poll now waits, with no cursor, for a fetch on its table
to finish and starts from that read's _meta.since_cursor. The scoped
client a fetch runs against reports every read's cursor (the by-id
404 included) through withSignal; request() picks it out of the
envelope. A fetch dispatched after the seed arrived read at or past
it. One already running at that moment is kept only if every cursor
it read equals the seed, and otherwise reruns once, after the seed.
Cursors stay opaque and are compared only for equality.

A fetch that ends without a cursor for the table (it failed, was
cancelled, did not read it, or the server predates the field) does
not seed while another fetch that may is still running, since
seeding "" then would accept that one unchecked. Only once none is
left does the poll start from "", the previous bootstrap. A fetch's
cursor sink is detached when it settles, so a scoped client the app
keeps cannot grow it.

Against the previous pool, the tests for the first poll waiting for
and sending the read's cursor, the 404 seed, CursorExpired reseeding,
the rerun of a query that read before the seed, unmount and remount
while waiting, refetch after a terminal poll, and a stale fetch
across CursorExpired fail. The failed-fetch-while-another-runs and
sink tests also fail against the pool before the seeder accounting.

Bundle: client-react.js grows by the seeding logic (+418 B min-gz) and
client.js by the cursor reporting in request() (+78 B min-gz).
Invariant 13 in sync-protocol.md now states the read cursor: which
routes carry it, the forms it takes, that it costs no storage call,
and how the React pool seeds from it. causal-consistency-checking.md
names the property test and pool tests that witness it. API.md,
RECIPES.md, the cheatsheet and the backups guide tell hand-rolled
subscribers to seed from the read they render, and to resume from
the re-read's cursor after CursorExpired, instead of cursor="".

The changeset covers the server field and the React pool change.
…ursor

The CursorExpired resolution, sent in every 410, told clients to
re-read and then resume /v1/since with cursor: "". That bootstrap
reads the log at a different moment than the re-read, so a change
that commits and folds between the two is missed. The resolution now
says to resume from the re-read's _meta.since_cursor, and notes that
cursor: "" still resumes but can miss such a change.

The same advice is updated in the CursorExpired JSDoc in errors.ts,
the /v1/spec IR summary (baerly.spec.json regenerated with
pnpm gen:spec), the server contract's status table, the API.md error
table and the cheatsheet. The changeset names the new text. The
resolution test and the HTTP conformance 410 case now require
_meta.since_cursor in it.

Bundle: every closure carrying the error-code table grows by the
longer resolution text (index.js +48 B min-gz, http.js +52 B,
cloudflare.js +98 B, observability.js +49 B).
client.js and client-react.js grow with the query since-cursor
plumbing and the React poll that starts from it. Rebaselined after
rebasing onto current main.
@baer
baer added this pull request to stack #437 November 25, 2026 20:21
@baer baer changed the title http/query since cursor feat(server): return a since-cursor on query responses Nov 25, 2026
@baer
baer merged commit 868a44a into main Nov 25, 2026
7 checks passed
@baer
baer deleted the http/query-since-cursor branch November 25, 2026 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant