feat(core): a computed field runs only when it is going to be returned (ADR-0027) - #890
Merged
Conversation
#855, ADR-0027) A computed field (any field with a resolveOutput hook, virtual or not) is now computed, and its declared relations (needs) fetched, only when a fragment `query` read is actually going to return it — projection-aware, applied recursively at every nesting level. Bare and include-based reads are unaffected. A computed field's hook also no longer sees another computed field's resolved output as part of its item, on any read path, closing the declaration-order dependency bug ADR-0025 left open. Closes #855 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018wgSw8qrPmMBy8d97LuGoh
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
🦋 Changeset detectedLatest commit: 084ce6e The changes in this PR will be included in the next version bump. This PR includes changesets to release 9 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
Coverage Report for Core Package Coverage (./packages/core)
File Coverage
|
||||||||||||||||||||||||||||||||||||||||||||
Contributor
Coverage Report for UI Package Coverage (./packages/ui)
File CoverageNo changed files found. |
Contributor
Coverage Report for CLI Package Coverage (./packages/cli)
File CoverageNo changed files found. |
Contributor
Coverage Report for Auth Package Coverage (./packages/auth)
File CoverageNo changed files found. |
Contributor
Coverage Report for Storage Package Coverage (./packages/storage)
File CoverageNo changed files found. |
Contributor
Coverage Report for RAG Package Coverage (./packages/rag)
File CoverageNo changed files found. |
Contributor
Coverage Report for Storage S3 Package Coverage (./packages/storage-s3)
File CoverageNo changed files found. |
Contributor
Coverage Report for Storage Vercel Package Coverage (./packages/storage-vercel)
File CoverageNo changed files found. |
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements ADR-0027, the follow-up scoped by #850/#852: a computed field (any field carrying a
resolveOutputhook, virtual or not) is now computed — and its declared relations (needs, ADR-0025) fetched — if and only if the read is actually going to return it, rather than unconditionally on every read.query's own field selection restricts a level this way, applied recursively at every nesting level (a nested fragment selecting a subset computes only that subset; a nestedincludestill computes every computed field there). A bare read or aninclude-based read is completely unaffected — every computed field on the list still computes, exactly as before.readaccess check nor itsresolveOutputhook runs, and its declared relation is never folded into the include.password()wrapper).Key files
packages/core/src/query/index.ts— newFieldSelectionScopetype +buildFieldSelectionScope(), derived from a fragment's field selection.packages/core/src/access/declared-dependencies.ts—getDeclaredRelationNames/foldDeclaredDependenciestake an optional selection scope, foldingneedsonly for fields the read is going to return.packages/core/src/access/field-visibility.ts—filterReadableFieldsskips a field's access check + hook entirely when a selection scope excludes it; a virtual field's hook now reads from a sanitizedcomputedFieldItem(stored columns, minus access-denied/selection-skipped keys, minus nothing declared-only) instead of the previously-resolvedfilteredobject.packages/core/src/context/index.ts—resolveReadIncludebuilds and threads the selection scope through the fragment read path only; every other path (callerinclude, sudo, bare) passes no selection, unchanged.This is a silent break — see the changeset
Two behaviors changed with no thrown error, documented in
.changeset/silent-hooks-compute-once.mdwith detection guidance and a before/after example:resolveOutput) no longer sees that field's resolved value.resolveOutputhook relied on for a side effect no longer runs on a fragment read that doesn't select that field.Test plan
packages/core/tests/computed-field-selective-evaluation.test.ts(10 tests) covering: unselected field runs neither access nor hook and folds noneeds; selected field computes + fetches correctly; two fields sharing a declared relation dedup correctly when only one is selected; nested fragment vs. nested include selectivity; hook item never sees another computed field's resolved output (order-independence + explicit leak check); field-level access still gates a selected field; hookless virtual field never has its access evaluated; bare/includereads unaffected.pnpm testinpackages/core— 49 files / 958 tests passing (958 = 948 pre-existing + 10 new).pnpm build(root, all 11 packages) andpnpm build(core) both succeed.pnpm lint— no new errors (3 pre-existing unrelated warnings).pnpm manypkg fix/pnpm formatrun, no diffs beyond formatting the changeset itself.packages/core/CLAUDE.mdand corrected the now-stale "computed on every read" line in the Virtual Fields section.CONTEXT.md/docs/adr/0027-*.mdalready existed from the prior triage doc PR and needed no changes.Closes #855
Generated by Claude Code