Skip to content

Subscribe a rule's ref lookup to entities that only partly match its path - #2954

Open
verveguy wants to merge 1 commit into
instantdb:mainfrom
liminisapp:fix/ref-partial-path-topics
Open

verveguy wants to merge 1 commit into
instantdb:mainfrom
liminisapp:fix/ref-partial-path-topics

Conversation

@verveguy

@verveguy verveguy commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Fixes #2952.

A data.ref lookup joins from each checked entity, through the path's links, to the value. Its topics came from the rows that join returned. An entity with none of the path, or only part of it, returned no row, so it wasn't subscribed to. A write that completed its path, such as a bookshelf's first link to a user, didn't refresh the query.

This lets a pattern group give the values its symbols may take (:topic-symbol-values). Its topics are then built from those instead of from the rows: each listed symbol is limited to its values, and every other symbol is a wildcard (datalog/seeded-topics). cel/build-query sets the entity symbol to the eids being checked. The ref lookup therefore watches the first link of every entity it checks, plus every change to the later links and to the value. Both get-ref-many and prefetch-data-refs pass it through. Coarse topics still take precedence when they're on.

That is broader than the exact path, but only for these lookups. A rule that dereferences a link can't know which later entities will join until the earlier links exist.

Test: session-test's ref-rule-paths-refresh-for-partial-matches covers a one-link path, where the entity has no link yet, and a two-link path, at each link. It also checks that a write to a field the rule doesn't read still doesn't refresh the query. Without the change, it fails with 4 failures and 3 errors.

It adds a few helpers to session-test. They subscribe, make one write, and check whether the write marked the query stale. #2953 adds the same helpers. Whichever merges second needs a trivial rebase, and I'm happy to do that. With #2953, one more assertion holds: a write to a field of the book that the rule doesn't read doesn't refresh the query.

I also ran the instant.db.*, instant.reactive.* and instant.admin.* suites: all 433 tests passed, with 21,824 assertions.

🤖 Generated with Claude Code

https://claude.ai/code/session_013K45h1bruWQBs4zfS2PjtD

…path

A query's topics come from the rows its whole join returns. For a
`data.ref` lookup, an entity that has the first links of the path but not
the rest returns no row, so it wasn't subscribed to. A write that
completed its path, e.g. a first invitation on a map, didn't refresh the
query.

Let a pattern group give the values its symbols may take, and build its
topics from those instead of the rows. A ref lookup gives the entities it
checks, so it watches their first link, and every change to the later
links and the value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HRKkuGrp63pNCgPK1hVdC7
Copilot AI balanced review requested due to automatic review settings November 7, 2026 21:47
@coderabbitai

coderabbitai Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Reference queries now pass topic-symbol values into datalog topic generation. Reactive session tests cover refreshes when writes complete reference paths, as well as a write to an attribute outside the rule path.

Changes: Reference-path query refresh

Layer / File(s) Summary
Pass topic seeds with reference patterns
server/src/instant/db/cel.clj
build-query provides topic-symbol values for the starting entity type and requested IDs. get-ref-many and prefetch-data-refs pass those values with their pattern groups.
Generate nested topics from supplied seeds
server/src/instant/db/datalog.clj
Nested pattern groups retain topic-symbol values. When coarse topics are disabled, seeded-topics generates topics from the patterns, including nested OR branches.
Test reference-path refresh behavior
server/test/instant/reactive/session_test.clj
Tests verify that adding links along single- and two-link reference paths stales subscribed queries. They also verify that changing an unrelated email does not stale the query.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant get-ref-many
  participant datalog-query-fn
  participant seeded-topics
  participant ReactiveSession
  get-ref-many->>datalog-query-fn: Pass pattern groups and topic-symbol values
  datalog-query-fn->>seeded-topics: Generate topics from pattern metadata and seed values
  seeded-topics-->>datalog-query-fn: Return generated topics
  datalog-query-fn-->>ReactiveSession: Provide query topics
  ReactiveSession->>ReactiveSession: Check changed topics after a write
Loading

Suggested reviewers: dwwoelfel, stopachka

Merge Risk: Minimal · up to 6c981

No PR-introduced issue was established that needs correction before merge; normal checks remain appropriate.

Pre-merge checks: Passed 5

Check name Status Explanation
Title check Passed The title names the main change: ref lookups for rules now subscribe to entities that only partly match the rule path. The raw summary and objectives confirm topic generation for data.ref lookups chan…
Description check Passed The description states the problem (ref lookups did not subscribe to entities with partial path matches), the fix (pattern groups supply :topic-symbol-values and seeded topics are built from them), th…
Linked Issues check Passed Issue #2952 requires that a write completing a data.ref path mark the live query stale. This includes a two-link path completed at either link. The diff implements this in three parts. (1) cel.clj bui…
Out of Scope Changes check Passed All changed hunks relate to #2952. cel.clj changes add topic-symbol-values to build-query and route it through get-ref-many and prefetch-data-refs. datalog.clj changes propagate :topic-symbol-values t…
Docstring Coverage Passed No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Full details: Title check

Explanation

The title names the main change: ref lookups for rules now subscribe to entities that only partly match the rule path. The raw summary and objectives confirm topic generation for data.ref lookups changed to use pattern-group symbol values. The title is specific, single-sentence, and free of noise.

Full details: Description check

Explanation

The description states the problem (ref lookups did not subscribe to entities with partial path matches), the fix (pattern groups supply :topic-symbol-values and seeded topics are built from them), the test added (ref-rule-paths-refresh-for-partial-matches), and the test results. It matches the changeset in cel.clj, datalog.clj, and session_test.clj.

Full details: Linked Issues check

Explanation

Issue #2952 requires that a write completing a data.ref path mark the live query stale. This includes a two-link path completed at either link. The diff implements this in three parts. (1) cel.clj build-query now returns :topic-symbol-values, which sets the entity symbol to the checked eids. (2) datalog.clj seeded-topics builds topics from those values. Each listed symbol is limited to its values, and every other symbol is a wildcard. This applies when coarse topics are off. (3) get-ref-many and prefetch-data-refs pass :topic-symbol-values into the pattern group. The first link of every checked entity is therefore subscribed. Later links and the value are subscribed for every change. The added test ref-rule-paths-refresh-for-partial-matches covers a one-link path with no existing link, and a two-link path at each link. It also checks that a write to an unread field does not refresh the query. I did not run the test suite. The PASS rests on the diff, and the author reports the test failing without the change and passing with it.

Full details: Out of Scope Changes check

Explanation

All changed hunks relate to #2952. cel.clj changes add topic-symbol-values to build-query and route it through get-ref-many and prefetch-data-refs. datalog.clj changes propagate :topic-symbol-values through nested pattern-group metadata, add seeded-topics, and use it only when the group sets the value and coarse topics are off. The coarse-topics branch keeps precedence, as the PR states. The session_test.clj changes add the new test and its helpers (triple-topics, transact-topics!, refreshed-by?, and others), plus the clojure.set, instant.jdbc.sql, and instant.util.json requires that those helpers use. These support the new test. The seeded-topics path is reached only from cel-built groups, so the behavior change stays limited to data.ref lookups. The diff does not show unrelated behavior changes. Review limits: I read the diff hunks only. I did not inspect named-pattern->topics or the rest of the datalog query path, so I did not confirm every side effect.

Full details: Docstring Coverage

Explanation

No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 0 files. (3 skipped: 3 unsupported.)

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Tip

🎯 Customize your risk assessments

Guide how CodeRabbit assesses merge risk and security architecture risk. Highlight sensitive changes and explain your security boundaries using Merge Risk Instructions and Security Risk Instructions in your review settings. Security architecture risk assessments require the Advanced plan or above.

✅ Automatic approvals, with your rules

Let CodeRabbit approve pull requests once required threads are resolved, pre-merge checks pass, and your risk limits are met. Enable Automatic approvals in review settings, with overrides for teams or file paths.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 98e55105-bd2e-4b1c-8ead-63d45acec9d7
Commits

Reviewing files that changed from the base of the PR and between 15358ad and 6c981da.

Files selected for processing: 3

  • server/src/instant/db/cel.clj
  • server/src/instant/db/datalog.clj
  • server/test/instant/reactive/session_test.clj

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the reference-lookup subscription changes.

No open issues 🎉

What changed in this PR

Fixes #2952 by subscribing data.ref permission lookups to entities with incomplete reference paths, allowing live queries to refresh when a write completes a missing link.

Changes:

  • Adds symbol-seeded topics for reference lookups while preserving coarse-topic precedence.
  • Passes topic seeds through both direct and prefetched CEL reference lookups.
  • Adds regression tests for missing links in one- and two-link paths and unrelated-field writes.
File Description
server/​test/​instant/​reactive/​session_test.clj Adds subscription helpers and partial-path refresh regression tests.
server/​src/​instant/​db/​datalog.clj Carries topic seeds through pattern groups and builds topics independently of joined rows.
server/​src/​instant/​db/​cel.clj Seeds subscriptions with checked entity IDs and forwards them through both reference lookup paths.

Review effort: Balanced

This branch has not been deployed

No deployments
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.

A live query under a data.ref rule doesn't refresh when a write completes the rule's path

2 participants