Skip to content

Subscribe a permission check only to the fields its rules read - #2953

Open
verveguy wants to merge 1 commit into
instantdb:mainfrom
liminisapp:fix/rule-fields-only-subscribe
Open

verveguy wants to merge 1 commit into
instantdb:mainfrom
liminisapp:fix/rule-fields-only-subscribe

Conversation

@verveguy

@verveguy verveguy commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Fixes #2951.

A permission check fetched every field of the entity to build data, and fetching is what subscribes a reactive query. So a query with :fields still refreshed on a change to any field of any entity it returned.

This collects the data fields that each rule reads when it is compiled (cel/collect-data-field-uses, stored on the rule program as :data-field-uses). It then fetches only those, plus id, for the view rule and the field rules of an etype (instaql/rule-attr-ids).

  • A rule that uses data in a way that can't be narrowed to named fields, such as data[key], returns :all, and the check fetches every field as before.
  • data.ref(...) only needs data.id, which is always fetched.
  • If the query already fetched the whole entity, the check reuses that result instead of fetching again.

entity-map keeps its four-argument arity, which fetches every field. The five-argument arity takes the attr ids to fetch, so seeks_permission_helpers_test's stub takes the extra argument.

Tests:

  • cel-test: which fields collect-data-field-uses finds, and when it returns :all.
  • instaql-test: the entity map has the fields the rule reads.
  • session-test: view-rule-fields-refresh-queries-with-fields, field-rule-fields-refresh-queries-with-fields, ref-rule-paths-refresh-queries-with-fields and unnarrowable-rules-refresh-on-any-field. Each subscribes, makes one write, and checks whether the write marked the query stale. A write to a field that the rule reads, or that the query fetches, still refreshes it. A write to any other field doesn't. Without the change, the three *-refresh-queries-with-fields tests fail.

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

🤖 Generated with Claude Code

https://claude.ai/code/session_013K45h1bruWQBs4zfS2PjtD

To check a view rule, the server fetches the entity, and fetching is what
subscribes a reactive query. It fetched every field, so a query with
`:fields` still refreshed on a change to any field of any entity it returned.

Collect the `data` fields each rule reads when it's compiled, and fetch only
those plus `id`. A rule that uses `data` in a way we can't narrow, e.g.
`data[key]`, still fetches every field. If the query already fetched the
whole entity, the check reuses it.

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 →

Walkthrough

Rule programs now record which data fields their CEL expressions use. Permission checks use this information to load only the relevant entity attributes when possible. Tests cover field tracking, permission checks, and reactive query refresh behavior.

Changes: Permission Rule Field Tracking

Layer / File(s) Summary
Collect CEL data-field uses
server/src/instant/db/cel.clj, server/src/instant/model/rule.clj, server/test/instant/db/cel_test.clj
The CEL AST scanner collects direct data.field reads and returns :all when it cannot account for every data identifier. Compiled rule programs include this field-use information. Tests cover direct reads, data.ref, indirect or dynamic access, and bind expressions.
Load rule-referenced attributes
server/src/instant/db/instaql.clj, server/test/instant/db/instaql_test.clj, server/test/instant/db/seeks_permission_helpers_test.clj
Permission checks derive required attribute IDs from rule field uses and pass them to entity-map loading. Loading falls back to all attributes when field usage is unavailable or marked :all. Tests cover a permissioned query without a fields selection and update the entity-map test stub for additional arguments.
Validate reactive query refresh behavior
server/test/instant/reactive/session_test.clj
Tests cover refreshes caused by selected fields, rule-read fields, hidden rows, reference paths, and dynamic data[...] access. They also check changes that do not trigger refreshes.

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

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant RuleProgram
  participant CELScanner
  participant PermissionCheck
  participant EntityMapLoader
  RuleProgram->>CELScanner: collect data-field uses from AST
  CELScanner-->>RuleProgram: field set or :all
  PermissionCheck->>EntityMapLoader: request attributes used by rules
  EntityMapLoader-->>PermissionCheck: entity map with requested attributes
Loading

Suggested reviewers: dwwoelfel, stopachka

Merge Risk: Minimal · up to af9af

No concrete issue requiring a fix before merge was established. The topic-helper documentation suggestion can be considered separately.

Pre-merge checks: Passed 5

Check name Status Explanation
Title check Passed The title names the main change: permission checks now subscribe only to the entity fields their rules read. The raw summary and objectives confirm this is the primary change, since rule-attr-ids and …
Description check Passed The description states the bug (permission checks fetch every field, which subscribes reactive queries to all fields), the fix (collect data fields per rule and fetch only those plus id), the edge cas…
Linked Issues check Passed Directly linked issue #2951 has coding requirements only. It asks that permission checks for view rules and field rules fetch only the fields the rule reads, so that writes to other fields do not refr…
Out of Scope Changes check Passed Every changed area supports the linked objective of narrowing permission-check fetches for view and field rules:
- cel.clj: new collect-data-field-uses helper. It computes the fields used for narrow…
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: permission checks now subscribe only to the entity fields their rules read. The raw summary and objectives confirm this is the primary change, since rule-attr-ids and collect-data-field-uses narrow the fetched fields for view and field rules.

Full details: Description check

Explanation

The description states the bug (permission checks fetch every field, which subscribes reactive queries to all fields), the fix (collect data fields per rule and fetch only those plus id), the edge cases (data[key] falls back to all fields, data.ref needs only id, existing whole-entity results are reused), the API signature change, and the tests added. It matches the changeset closely.

Full details: Linked Issues check

Explanation

Directly linked issue #2951 has coding requirements only. It asks that permission checks for view rules and field rules fetch only the fields the rule reads, so that writes to other fields do not refresh a query with fields, including for hidden rows.

Code at the reviewed head implements this:

  • cel/collect-data-field-uses (cel.clj) returns the set of data.&lt;field&gt; names read. It returns :all for indirect access such as data[key] or a bare data. data.ref(...) counts as needing only data.id.
  • model/rule.clj stores the result as :data-field-uses on each compiled rule program. The change sits in the shared compile path, so it covers the view program and the field programs. I read only the diff for this file, and I did not confirm that the field-program path uses the same function.
  • instaql/rule-attr-ids builds the attr ids for an etype from the view and field programs. It always includes id. It returns all etype attrs when any program reports :all or has no :data-field-uses.
  • get-eid-check-result! passes these per-etype attr ids to preload-entity-maps and to the 5-arity entity-map. The batch fetch and the per-entity fetch therefore select only the narrowed attrs. Their topics are what the query subscribes to, so unrelated field writes no longer mark the query stale.
  • :all rules keep the previous full fetch, as the issue does not require narrowing for them.

The author reports new tests in cel-test, instaql-test, and session-test (for example view-rule-fields-refresh-queries-with-fields and unnarrowable-rules-refresh-on-any-field). I did not run them. This verdict relies on the code evidence above. Ref-path fetches (prefetch-data-refs) are unchanged by this PR. I did not inspect that code, so I did not confirm its narrowing beyond the author's test report.

Full details: Out of Scope Changes check

Explanation

Every changed area supports the linked objective of narrowing permission-check fetches for view and field rules:

  • cel.clj: new collect-data-field-uses helper. It computes the fields used for narrowing.
  • model/rule.clj: one line that stores :data-field-uses on compiled programs.
  • instaql.clj: rule-attr-ids, the 5-arity entity-map, and the changed preload-entity-maps and get-eid-check-result! wiring that uses the narrowed ids.
  • instaql.clj: the entity-map cache lookup that reuses a whole-entity query result. It adds no new subscription topics, and it avoids a second fetch of the same entity. It supports the fix and does not change behavior beyond it.
  • seeks_permission_helpers_test.clj: the entity-map stub accepts the extra argument that the new call path passes.
  • cel_test, instaql_test, and session_test additions: tests for the helper, the rule programs, the query reuse, and the refresh behavior.

No change is unrelated to #2951. Unverified: preload-entity-maps now requires four arguments with no older arity. I could not search for other call sites, so I did not confirm that all callers were updated. This is a correctness risk, not a scope violation.

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. (7 skipped: 7 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

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 block approval of the changes that narrow permission-check subscriptions to rule-dependent fields.

No open issues 🎉

What changed in this PR

Addresses #2951 by limiting permission-check subscriptions to the fields read by view and field rules, preventing unrelated writes from refreshing queries that use :fields.

Changes:

  • Collects field dependencies from compiled rules, falling back to all fields when access cannot be narrowed.
  • Fetches only rule-required fields plus id, and reuses cached whole-entity results when available.
  • Adds coverage for dependency collection, cache reuse, and reactive invalidation behavior.
File Description
server/​test/​instant/​reactive/​session_test.clj Tests which writes refresh permissioned queries.
server/​test/​instant/​db/​seeks_permission_helpers_test.clj Updates the entity-map stub to accept the extra argument.
server/​test/​instant/​db/​instaql_test.clj Tests reuse of whole-entity query results.
server/​test/​instant/​db/​cel_test.clj Tests field dependency collection, bindings, and fallback behavior.
server/​src/​instant/​model/​rule.clj Stores field dependencies on compiled rule programs.
server/​src/​instant/​db/​instaql.clj Narrows permission-check fetches and reuses cached results.
server/​src/​instant/​db/​cel.clj Collects named field accesses from rule expressions.

Review effort: Balanced

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Additional comments: 1

server/test/instant/reactive/session_test.clj: 1

1730-1758: Trivial · Maintainability & Code Quality · Low value

Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.

triple-topics builds topics from a raw SQL read, so it can drift from the real invalidator.

The helper reimplements the invalidator's topic derivation. It uses cast(value as text) and parse-uuid on the eav column. If the invalidator changes how it forms topics, these tests can pass or fail without reflecting production behavior. Add a comment that names the invalidator function this mirrors. If a reusable function exists, call it instead.

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: 973b1946-9372-4e69-b7cf-1b4e697f923e
Commits

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

Files selected for processing: 7

  • server/src/instant/db/cel.clj
  • server/src/instant/db/instaql.clj
  • server/src/instant/model/rule.clj
  • server/test/instant/db/cel_test.clj
  • server/test/instant/db/instaql_test.clj
  • server/test/instant/db/seeks_permission_helpers_test.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; 9 remain after this review.

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 query with fields still refreshes on any field of its rows when a view rule reads data

2 participants