Skip to content

fix!: one shape for a column ref, and the adapter sends only what the server can read - #65

Merged
grrowl merged 15 commits into
mainfrom
fix/column-refs
Nov 17, 2026
Merged

grrowl merged 15 commits into
mainfrom
fix/column-refs

Conversation

@grrowl

@grrowl grrowl commented Nov 17, 2026

Copy link
Copy Markdown
Owner

The SQL that builds a snapshot and the client's evaluator that decides the deltas must read the same column, or the client holds wrong rows. The review of the missing column fix (#63) found four ways to break that on main. Fixing them on the server exposed a query shape the library's own client sends, so this change also teaches the on-demand adapter to send only what the server can read.

The server

  • A column reference has one shape: exactly { type: "ref", path: [name] }, in where, orderBy and both halves of a cursor. A hand built ref with sourceAlias or another field, or a path of more than one element, is refused like an operator off the floor. Before, orderBy took the last element of a path while where required exactly one, and the 1.0 evaluator read a ref with sourceAlias differently from the SQL.
  • registerSync refuses two table shapes. A generated column named rowid passed the compatibility check and then shadowed the default row order. A column named __proto__ cannot be carried on a row object.
  • Six malformed frames are refused where they threw and got no reply, returned wrong rows, or were ignored.

The client

On @tanstack/db 0.8.6 the client sends a two element orderBy path for an ordinary query: a subquery with orderBy and limit. Main sorted it by the last element, which happened to be right. The strict server rule refused it, and the live query ended ready with no rows. The server cannot tell that path from a nested field or another table's column, where the last element is wrong.

So the on-demand adapter now sends a request the server can read that contains the one core asked for:

  • an orderBy the server cannot sort by is dropped, with its limit;
  • a top level condition of a where that the server cannot read is dropped, and the rest is sent. Nothing is widened inside a not.

The client filters, sorts and cuts the rows it holds, so the query is right. One rule in src/wire/column-ref.ts is used by the compiler and the adapter.

Effects:

  • The 0.8.6 subquery shapes show the right rows again.
  • An orderBy or a where on a nested field, which was refused and read as no results, now works on the socket and in a server render, on every version.
  • A "load more" request is skipped when the covering subscription already holds every matching row.

Breaking change

registerSync throws for a table with a generated column named rowid or a column named __proto__. It runs in the constructor on every wake, so the whole object fails. Rename the column with ALTER TABLE … RENAME COLUMN in a migration that runs before registerSync. Do not drop a stored generated column and add it back; that fails on a table with rows.

Costs to know

  • A query the adapter widens loads every row the rest of its where matches, however small its limit. A where on a nested field alone loads the whole collection, which then stays loaded while any other query on that collection is mounted.
  • A server render ships those rows in the page.
  • An eager collection's own where is not widened. On a nested field it is still refused.
  • On 0.8.x one subquery shape still sorts by the wrong column with no error, as on main: a renaming subquery whose alias equals the inner alias. The README lists it.

Verification

  • Red tests first in every round. tests/client-ref-shapes.test.ts runs live queries through the real adapter and Durable Object, on the socket and in a server render, and compares each with @tanstack/db's own answer over local rows. It also writes down the ref shapes each version line sends, so the weekly job against the newest release fails when core sends another shape.
  • An adversarial review by a different model, in three rounds, with its own runs on 0.8.6, 0.9.2 and 1.0.0-rc.1.5. Round one found the 0.8.6 shape. Round two found that core's tie request carried the nested path in its where and failed a server render. Round three ran 40,000 random where shapes per version (none sent narrower than asked) and a mutant of every adapter rule (each fails a test). Verdict: mergeable.
  • Typecheck and the full suite passed on 1.0.0-rc.1.5, 0.8.6 and 0.9.2 before the merge with main. CI runs the locked version and the floor.

Not pinned by a test: the key order of a rebuilt and (a wrong order costs one extra fetch, no wrong rows), and dehydrate then hydrate for the widened shapes (the reviewer ran it by hand and it converged).

The decisions are dated amendments to ADR-0013, ADR-0023 and ADR-0007.

🤖 Generated with Claude Code

grrowl and others added 15 commits November 17, 2026 16:00
…to__ column

assertSyncCompatible read the table's columns with pragma_table_info, which
does not list generated columns. A generated column named rowid passed the
shadow check, and its probe `SELECT rowid` succeeded, so the default
ORDER BY rowid sorted by the generated value. It now reads
pragma_table_xinfo.

A column named __proto__ cannot be carried on a row object: workerd sets
each column on a plain object, so that name sets the row's prototype (to
null for a NULL) instead of becoming a key. No snapshot or delta holds the
column, and SQL can match on it where the evaluator reads the prototype.
Such a table is now refused at registerSync.

BREAKING CHANGE: registerSync now throws for a table with a generated
column named rowid, or a column named __proto__. Both used to register.

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

The compiler read a ref's path[0] in `where`, and the last path element in
`orderBy`, and ignored every other field. The evaluator reads the whole
ref: on @tanstack/db 1.0 it drops path[0] when it equals sourceAlias, and
it walks a longer path into the value. So a hand built ref with
sourceAlias, or an orderBy path of two elements, gave a snapshot that the
deltas, and the client's own order, disagreed with.

A ref in `where`, `orderBy` and a cursor must now be exactly
{ type: "ref", path: [name] }, which is what the library's client sends
on 0.8.6, 0.9.2 and 1.0.0-rc.1.5 (measured through the adapter). Any
other shape is refused like a query off the floor: reset, an empty page,
or a rejected readSyncSnapshot.

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

ADR-0013: a ref is read only when it is exactly { type: "ref", path: [name] },
the shape the library's client sends on 0.8.6, 0.9.2 and 1.0.0-rc.1.5, in
`where`, `orderBy` and a cursor alike. Records what was measured, the rule,
and what else it refuses.

ADR-0007: registerSync refuses a generated column named rowid and a column
named __proto__, and says why each is a fault of the table.

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

On @tanstack/db 0.8.6 an orderBy pushed through a subquery or a join
reaches the source collection with refs such as ["sub", "body"] or
["f", "name"], and every version sends a nested field as a longer path.
The server refuses those refs, since it cannot tell which of them sorting
by the last element would get right, so such a live query read as no
rows. On main the first of them worked.

An orderBy and a limit only let the server send fewer rows; the client
sorts and cuts what it holds. So when a term is not a column ref in the
one shape the server reads, the on-demand adapter now asks for the whole
`where` subset instead: no orderBy, limit or offset, and for a load more
no cursor. The decision is made once at the top of loadSubset, so the
request key, the watch, the covered fetch and the SSR read all agree.

The rule moves to src/wire/column-ref.ts (columnRefProblem), shared by
the compiler and the adapter. tests/client-ref-shapes.test.ts runs a
fixed list of query shapes through the real adapter and Durable Object
and records, per version line, the refs core asks for; it checks that no
refused ref reaches the wire and that each query ends ready with the rows
@tanstack/db gives over the same data.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An orderBy term of null, a function with no args, and a function named
after an Object.prototype member (`name in COMPARATORS` followed the
prototype chain and wrote broken SQL) threw from compileSubsetQuery, so a
sub or a fetch got no reply and stayed loading. A function whose args is
a map with `length` compiled, while the evaluator throws on it. Each is
now an UnsupportedPredicateError: reset, an empty page, or a rejected
readSyncSnapshot.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The __proto__ check compares the name exactly, as a row's keys are; a
case-insensitive compare passed every test before this one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ADR-0023 amendment: an orderBy the server cannot sort by is not sent;
where the decision is made, how the watch and cover rules treat the
unbounded request, what a later cursor or tie request does, the shared
rule, and the cost.

ADR-0013 amendment (unmerged, corrected): what the client sends now
includes the three 0.8.6 shapes, the rule lives in src/wire/column-ref.ts,
and the four frames that threw are refused.

ADR-0007 amendment (unmerged, corrected): a generated rowid breaks only
reads with no orderBy; registerSync throws on every wake and fails the
whole object, and the column must be renamed in a migration before it.

README: the refused table shapes in the schema note, the cost of an
orderBy the server cannot sort by beside the other ordering notes, and a
where on a nested field under known limitations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On 0.9 and later a limited query ordered by a nested field also makes a
tie request whose `where` holds the nested path. The server refuses it:
on the socket an empty page and an error line, in a server render a
thrown read, so preload() rejected and the query ended in `error`. A
standalone `where` on a nested field read as no rows.

The adapter now sends the tightest request the server can read that
holds every row core asked for. A top-level conjunct holding a ref that
fails columnRefProblem is dropped; any other node holding one is dropped
whole as its conjunct, so nothing is widened inside an `or` or a `not`.
A widened request also drops its orderBy and limit. The client filters,
sorts and cuts the rows it holds. The tie request widens to the base
`where`, which shares the acquisition and sends no frame.

A cursor request whose covering watch was opened with no limit and has
its snapshot already holds every row it asks for, so it sends nothing.

tests/client-ref-shapes.test.ts adds `where` shapes (a nested field alone
and inside an `and`, an `or` and a `not`), a server render of every shape
through SsrSnapshotTransport and readSyncSnapshot, and pins that a
rewritten request is shared and released.

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An orderBy term holding a bigint and no usable expression or col made
JSON.stringify in the error message throw, so a sub or a fetch got no
reply. An orderBy that is not an array was ignored without a word, and
with a limit returned the first rows by rowid. Both are refused now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
RENAME COLUMN works on a generated rowid (virtual or stored) and on a
__proto__ column (plain or generated) with rows, and the table then
registers. Dropping a STORED column and adding it back fails on a table
with rows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ADR-0023 amendment (unmerged, rewritten): the adapter sends the tightest
request the server can read; where the widening is applied, what the
watch's where is, how key and cover treat a widened request, what a
cursor request does (and when it sends nothing), the cost, and the 0.8.x
subquery the rule cannot see.

ADR-0013 amendment (unmerged, corrected): the tie request, the where
widening, and what each of the six malformed frames did.

ADR-0007 amendment (unmerged, corrected): RENAME COLUMN for every case;
dropping and adding a stored generated column fails on a table with rows.

README: the ordering note covers a where and a server render, the limits
state the cost and the 0.8.x subquery that orders by the wrong column.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… server can read

Merges the column reference change. The server reads a ref only when it is
exactly { type: "ref", path: [name] }, and refuses a table with a generated
column named rowid or a column named __proto__. The on-demand adapter drops an
orderBy the server cannot sort by and the conjuncts of a where it cannot read,
and asks for the rest. Adds the changelog entries and the wording the last
review asked for.

BREAKING CHANGE: registerSync throws for a table with a generated column named
rowid or a column named __proto__.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@grrowl
grrowl merged commit 2656c66 into main Nov 17, 2026
8 checks passed
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