Skip to content

fix: unconstrained liveArrays handle inserts and deletes from their own subscription - #45

Merged
jimmy-phantom merged 4 commits into
mainfrom
fix/query-subscription-collection-routing
Aug 5, 2026
Merged

fix: unconstrained liveArrays handle inserts and deletes from their own subscription#45
jimmy-phantom merged 4 commits into
mainfrom
fix/query-subscription-collection-routing

Conversation

@jimmy-phantom

@jimmy-phantom jimmy-phantom commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

What was broken

A liveArray declared without constraints never gained or lost rows in response to its own query's subscribe. Updates to rows already in the list did apply, which made the failure easy to miss: values changed, but nothing was ever added or removed.

Event from the query's subscribe Before After
update to a row already in the list applies applies
update that changes the sort field re-sorts re-sorts
update for a row not in the list ignored inserts
create for a row not in the list ignored inserts
delete ignored removes

This is the shape a paginated list with a live stream wants: fetch a page over REST, then keep it current from a subscription.

Why

A live collection is registered against the entity that owns it. For a query result that owner is the result's root entity, but a query's subscribe tagged its events with the query key instead. The two are derived from different inputs, so the tag matched no collection and membership events were dropped.

Row updates still worked because they take the entity merge path, which doesn't depend on that tag.

The fix

Tag events with the root entity's key, which is what the collection is actually registered under. Entity __subscribe already works this way, so this makes the query path consistent with it.

Nothing else changes. Constrained collections, entity subscriptions, and external mutation events behave exactly as before, and an unconstrained collection still ignores everything except its own source.

Tests

  • Membership events from the query's own subscription: an unseen update and a create insert, delete removes, sort order is maintained throughout.
  • Scoping, two ways: same query with different params, and two different queries with identical params. In both cases an event reaches only the collection belonging to the query that emitted it.

Full suite passes: 1247 passed, 2 skipped, 65 files.

Implementation notes

createLiveCollection gives an unconstrained collection the default constraint [[EVENT_SOURCE_FIELD, parent.key]], meaning "events originating from my own source." reconcileSubscription set __eventSource to queryKey (a hash of the query definition and raw params), while the root entity's key comes from the injected QUERY_ID (a hash of extracted params). ConstraintMatcher.routeEvent therefore computed a hash matching no registered binding.

rootEntity is undefined until the first apply, and reconcileSubscription deliberately runs before the first fetch so stream-resolved adapters are subscribed in time. Events in that window leave the source undefined, which skips collection routing while still merging into the entity store. There is no collection to route into yet, and __eventSource is already optional on all three event types.

No changes were needed below the routing layer. LiveArrayInstance.onEvent already handles create, update when the key is absent, and delete, with _keys guards making both directions idempotent.

Supersedes #44, which pinned the previous behavior.

🤖 Generated with Claude Code

Live collections register under their parent entity's key, but a query's
`config.subscribe` stamped the query key as the event source. The two are
derived from different inputs and never matched, so `create` and `update`
for an unseen id never inserted and `delete` never removed. Field updates
to rows already present took the entity merge path and worked, which made
the gap silent.

Stamp the result root entity's key instead, matching how entity
`__subscribe` already stamps its own key. Entity-subscription routing and
constrained collections are unchanged.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@jimmy-phantom
jimmy-phantom requested a review from pzuraq as a code owner August 4, 2026 00:28
Collections are keyed to their root entity, whose identity is method, path,
and extracted params. Two queries on different paths with identical params
therefore resolve to separate root entities, and an event from one
subscription must not reach the other's collection.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@jimmy-phantom jimmy-phantom changed the title fix: route query-subscription events into unconstrained live collections fix: unconstrained liveArrays handle inserts and deletes from their own subscription Aug 4, 2026
Both blocks restated their own first clause. Keep the two facts a reader
needs at the assignment (why not the query key, why undefined is fine) and
drop the repetition.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@jimmy-phantom
jimmy-phantom merged commit d22fe02 into main Aug 5, 2026
1 check passed
@jimmy-phantom
jimmy-phantom deleted the fix/query-subscription-collection-routing branch August 5, 2026 18:18
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.

2 participants