Skip to content

fix(nextly): one predicate every cache asks, and an epoch that identifies its own store - #1847

Merged
mobeenabdullah merged 5 commits into
mainfrom
fix/one-predicate-every-cache-asks
Sep 13, 2026
Merged

mobeenabdullah merged 5 commits into
mainfrom
fix/one-predicate-every-cache-asks

Conversation

@mobeenabdullah

Copy link
Copy Markdown
Collaborator

Follow-up to #1829, which merged before this round's findings were worked. Three corrections to the shared RBAC epoch, and each is a cache that went on answering after the answer stopped being true.

A batch stopped reaching a cache it never owned

#1829 deferred the shared announcement to a batch's exit, which was right, and emptied the caches permissions.ts holds. An API key's copied grants are not one of those. They live in another module, so no list of maps to clear was ever going to reach them, and a key went on answering with revoked grants for the batch's whole length — unbounded for a seeder.

Adding a fourth clear would have been the same mistake with a longer list. What every tier already asks is stampIsCurrent, so the batch's untrusted state moved into it: retirements now lives in the epoch module beside the trust it decides, duringRetirement scopes it, and a batch holds one open across its whole body.

resolvedUnderCurrentRevision is now nothing but stampIsCurrent. The flush depth it used to add was precisely the thing the other tiers could not see, and permissionFlushDepth is gone.

Scoped rather than begin-and-end so a throw cannot leak it — a leaked one would leave the install permanently uncached.

A write that landed but was never read back looked settled

If the upsert succeeds and the read after it fails, the count is clear — the row really did move — while this process still holds the value from before the write. Every stamp comparison then starts passing again, against a stamp that predates the change.

Trust needs both halves, so observed is its own fact rather than inferred from an empty backlog: cleared when a write lands, set when a read succeeds.

An empty store had no identity

A table with no row answered :0 — what every never-yet-invalidated store answers. So a failover onto a different one, or a restore from a backup taken before the first role change, left every cached answer looking current. That is exactly what generation exists to catch, and epoch zero is where a fresh install sits for longest.

The row is created rather than imagined, and read back. Adding zero is the same statement the bump path makes, so it is that statement: raiseSharedRevision(by) serves both, and the row is created with an identity when absent and left alone when present — decided by the database rather than by a check-then-insert two instances can both win.

Evidence

4 mutation controls, all red, each killing exactly the case named for it:

mutation what went red
stampIsCurrent stops asking about retirements serves nothing while a retirement is running
trust ignores whether the value was observed stays untrusted when the write landed but the read did not
an empty store is not seeded gives a store with no row an identity instead of a shared zero (+ the different-store control)
a key's grants are not covered by the batch loses it inside a batch, which announces nothing until it exits

The identity cases needed a new fake that actually holds the row: the existing one reports writes as successful without storing anything, so the seeding was unassertable against it — the assertion would have been satisfied by a state that predated the change.

Gates: 950 files / 12,148 unit, 12 files / 95 integration, types, lint, comment convention, fallow zero introduced, changeset covers all 26 lockstep packages (verified with a control that removing one package makes the check refuse).

One unrelated commit, and why it is here

main is currently red on the comment-convention gate for every open branch: a comment merged with #1834 names the pull request it arrived in. It is one line in a test, the behaviour is untouched, and leaving it would have meant opening this PR into a red gate that is not mine. Called out here rather than buried.

…fies its own store

Round five is three findings, and the first is a regression the round before
introduced.

## A batch stopped reaching a cache it never owned

Deferring the announcement to the batch's exit was right, and the local
retirement that went with it empties the maps this module holds. A key's copied
grants are not one of those: they live in another module, so no list of maps to
clear was ever going to reach them, and a key went on answering with revoked
grants for the batch's whole length — unbounded for a seeder.

Adding a fourth clear would have been the same mistake with a longer list. What
every tier already asks is `stampIsCurrent`, so the batch's untrusted state
moved INTO it: `retirements` lives in the epoch module beside the trust it
decides, `duringRetirement` scopes it, and a batch holds one open across its
whole body. `resolvedUnderCurrentRevision` is now nothing but `stampIsCurrent` —
the flush depth it used to add was exactly the thing the other tiers could not
see, and `permissionFlushDepth` is gone.

Scoped rather than begin-and-end so a throw cannot leak it, and a leaked one
would leave the install permanently uncached.

## A write that landed but was never read back looked settled

If the upsert succeeds and the read that follows it fails, the count is clear —
the row really did move — while this process still holds the value from BEFORE
the write. Every stamp comparison then starts passing again, against a stamp
that predates the change, and a check that began before a revocation can finish
after it and refile the old answer.

Trust needs both halves, so `observed` is tracked as its own fact rather than
inferred from the backlog being empty: cleared when a write lands, set when a
read succeeds, and `epochIsTrustworthy` requires it.

## An empty store had no identity

A table with no row answered `:0`, which is what EVERY never-yet-invalidated
store answers — so a failover onto a different one, or a restore from a backup
taken before the first role change, left every cached answer looking current.
That is precisely what `generation` exists to catch, and epoch zero is where a
fresh install sits for longest.

The row is created rather than imagined, and read back. Adding zero is the same
statement the bump path makes, so it IS that statement: `raiseSharedRevision(by)`
now serves both, and the row is created with an identity when absent and left
alone when present, decided by the database rather than by a check-then-insert
two instances can both win. `selectSharedRow` is the other half, so the read has
one spelling too.

Where the create does not take, the answer is still zero and still not a
failure: a table that exists with nothing in it has never invalidated, and
refusing an authorization check over it would be worse than the staleness.
@coderabbitai

coderabbitai Bot commented Sep 13, 2026

Copy link
Copy Markdown

Warning

Review limit reached

Next included review available in 28 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: 6fcbdf35-ac12-4946-8a10-eabd2313a3cb

📥 Commits

Reviewing files that changed from the base of the PR and between d745705 and 9ac8c4a.

⛔ Files ignored due to path filters (1)
  • .changeset/one-predicate-every-cache-asks.md is excluded by !.changeset/**
📒 Files selected for processing (4)
  • packages/nextly/src/services/lib/permissions.ts
  • packages/nextly/src/services/lib/rbac-epoch.test.ts
  • packages/nextly/src/services/lib/rbac-epoch.ts
  • packages/nextly/src/services/lib/super-admin-cache-invalidation.integration.test.ts

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

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 13, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-13T10:27:02.699894Z 9ac8c4a Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@mobeenabdullah

Copy link
Copy Markdown
Collaborator Author

@codex review

Follow-up to #1829, which merged before this round's findings were worked. All three are fixed here.

The one worth the closest look is the first: the batch's untrusted state moved INTO stampIsCurrent rather than a fourth cache being added to a list of maps to clear, and resolvedUnderCurrentRevision is now nothing but that predicate. If there is a caller for which the retirement window is now too wide, this is where it would show.

Also worth checking: raiseSharedRevision(0) seeds an empty store on the read path, so a first refresh can now write. That is once per install and it is the same statement the bump path makes, but it is a write where there was none.

@mobeenabdullah

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Sep 13, 2026

Copy link
Copy Markdown
⚠️ Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f50806be48

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/nextly/src/services/lib/rbac-epoch.ts
@pkg-pr-new

pkg-pr-new Bot commented Sep 13, 2026

Copy link
Copy Markdown

Open in StackBlitz

@nextlyhq/adapter-drizzle

npm i https://pkg.pr.new/@nextlyhq/adapter-drizzle@9ac8c4a

@nextlyhq/adapter-mysql

npm i https://pkg.pr.new/@nextlyhq/adapter-mysql@9ac8c4a

@nextlyhq/adapter-postgres

npm i https://pkg.pr.new/@nextlyhq/adapter-postgres@9ac8c4a

@nextlyhq/adapter-sqlite

npm i https://pkg.pr.new/@nextlyhq/adapter-sqlite@9ac8c4a

@nextlyhq/admin

npm i https://pkg.pr.new/@nextlyhq/admin@9ac8c4a

@nextlyhq/admin-css

npm i https://pkg.pr.new/@nextlyhq/admin-css@9ac8c4a

@nextlyhq/blocks-engine

npm i https://pkg.pr.new/@nextlyhq/blocks-engine@9ac8c4a

@nextlyhq/blocks-react

npm i https://pkg.pr.new/@nextlyhq/blocks-react@9ac8c4a

@nextlyhq/builder

npm i https://pkg.pr.new/@nextlyhq/builder@9ac8c4a

create-nextly-app

npm i https://pkg.pr.new/create-nextly-app@9ac8c4a

@nextlyhq/eslint-plugin

npm i https://pkg.pr.new/@nextlyhq/eslint-plugin@9ac8c4a

nextly

npm i https://pkg.pr.new/nextly@9ac8c4a

@nextlyhq/plugin-form-builder

npm i https://pkg.pr.new/@nextlyhq/plugin-form-builder@9ac8c4a

@nextlyhq/plugin-mcp

npm i https://pkg.pr.new/@nextlyhq/plugin-mcp@9ac8c4a

@nextlyhq/plugin-page-builder

npm i https://pkg.pr.new/@nextlyhq/plugin-page-builder@9ac8c4a

@nextlyhq/plugin-sdk

npm i https://pkg.pr.new/@nextlyhq/plugin-sdk@9ac8c4a

@nextlyhq/plugin-seo

npm i https://pkg.pr.new/@nextlyhq/plugin-seo@9ac8c4a

@nextlyhq/storage-s3

npm i https://pkg.pr.new/@nextlyhq/storage-s3@9ac8c4a

@nextlyhq/storage-uploadthing

npm i https://pkg.pr.new/@nextlyhq/storage-uploadthing@9ac8c4a

@nextlyhq/storage-vercel-blob

npm i https://pkg.pr.new/@nextlyhq/storage-vercel-blob@9ac8c4a

@nextlyhq/ui

npm i https://pkg.pr.new/@nextlyhq/ui@9ac8c4a

commit: 9ac8c4a

@github-actions

github-actions Bot commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

Whole-Repository Code Hygiene Summary

Full dead-code, duplication, and complexity report for the PR branch as it stands now. Playground is excluded. Quality gate enforcement on introduced issues is performed by the Changed files job.

🌿 Fallow

Warning

Review needed

⚠️ 73 code issues · ⚠️ 677 clone groups · ⚠️ 1035 health findings

See inline review comments for per-finding details.

Code issues (73)
Category Count
Unused files 2
Unused exports 5
Unused dependencies 19
Unused devDependencies 6
Unresolved imports 2
Unlisted dependencies 1
Circular dependencies 38
Duplication (677 groups · 28396 lines · 3.9%)
Locations Lines Tokens
schemas/_dialect-bundles/mysql.relations.ts:40-134
schemas/_dialect-bundles/postgres.relations.ts:40-134
schemas/_dialect-bundles/sqlite.relations.ts:40-134
95 593
cli/commands/db-sync-demote.ts:70-75
cli/commands/db-sync-promote.ts:38-43
cli/commands/dev-build.ts:100-105
cli/commands/dev-build.ts:179-184
cli/commands/dev-build.ts:299-304
cli/commands/dev-build.ts:411-416
cli/commands/dev-build.ts:552-557
cli/commands/dev-server.ts:575-580
cli/commands/dev-server.ts:840-845
cli/commands/dev-server.ts:1143-1148
cli/commands/migrate-field-groups.ts:110-115
6 70
entries/EntryList/EntryTableSkeleton.tsx:74-98
collection/components/CollectionTableSkeleton.tsx:94-118
field-group/components/FieldGroupTableSkeleton.tsx:90-114
plugins/components/PluginsTableSkeleton.tsx:86-110
singles/components/SinglesTableSkeleton.tsx:77-101
src/components/table-skeleton.tsx:100-124
25 89
collections/config/validate-config.ts:380-433
field-groups/config/validate-field-group.ts:185-238
singles/config/validate-single.ts:190-243
54 152
dispatcher/handlers/collection-dispatcher.ts:925-967
field-groups/services/field-group-table-provisioning.ts:186-236
singles/services/reconcile-single-companion.ts:110-160
51 149

… and 672 more groups.

Across 425 files.

Complexity (1035 functions above threshold)
File Function Severity Cyclomatic Cognitive CRAP Lines
singles/services/single-mutation-service.ts:966 <arrow> critical 246 ! 308 ! 13317.5 ! 1650
collections/services/collection-mutation-service.ts:6327 <arrow> critical 165 ! 153 ! 6045.6 ! 1260
src/init/reload-config.ts:1417 applyReload critical 143 ! 211 ! 4560 ! 1470
shared/lib/entry-validation.ts:245 validateFieldValue critical 109 ! 157 ! 2675.3 ! 432
dynamic-collections/services/dynamic-collection-schema-service.ts:941 generateAlterTableMigration critical 103 ! 220 ! 2394.5 ! 738

5104 files, 79151 functions analyzed (thresholds: cyclomatic > 20, cognitive > 15, CRAP >= 30)

Codebase health

Metric Value
Maintainability 91.7 / 100
Avg complexity 1.8

Tip

Run fallow fix --dry-run to preview auto-fixes.
Add /** @public */ above exports to preserve them.

@github-actions github-actions Bot added scope: core nextly type: docs Documentation only labels Sep 13, 2026
`observed` records whether the epoch this process answers with came from the
row, and it was cleared only where a bump had been sent. The seed path did not
clear it, so a process holding one store's identity that failed over to a store
with no row kept every cached stamp trusted: the insert that would have
established the new identity is exactly what a read-only credential rejects, and
a rejected insert reaches the catch having changed nothing. The stale stamp then
matched for the life of the process, which is a revoked grant still being served
and the generation check defeated by the case it exists for.

Trust now drops the moment the row is found missing, ahead of the write, and
comes back only where a row was actually adopted. The read-back failing after an
accepted insert lands in the same place, so both doors are shut rather than the
louder one.

A store that accepts the create and stays empty still answers zero rather than
refusing an authorization check, but it says so: the warning names which of the
two situations it is, because a missing table is what `nextly db:sync`
reconciles and a row that will not persist is a permission or a storage problem
no schema command can fix.
@mobeenabdullah
mobeenabdullah force-pushed the fix/one-predicate-every-cache-asks branch from f50806b to 3895fe3 Compare September 13, 2026 09:12
@chatgpt-codex-connector

Copy link
Copy Markdown

💡 Codex Review

reportDegraded(TABLE_NOT_RECONCILED, String(error));

P2 Badge Report seed failures as row-establishment failures

When the initial select succeeds with no row but either raiseSharedRevision(0) rejects (for example, with read-only credentials) or the seeded row cannot be read back, this catch reports TABLE_NOT_RECONCILED, telling operators to run nextly db:sync even though the table was just read successfully and the actual remedy is permissions or storage availability. Catch failures inside the missing-row branch and report ROW_NOT_ESTABLISHED there so the new diagnostic distinction works for real database failures, not only for a silent insert no-op.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@mobeenabdullah

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3895fe3b46

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/nextly/src/services/lib/rbac-epoch.ts Outdated
The two degraded reports exist because the remedies differ, and the report was
chosen in the catch, which cannot see which of them applies. A store answering
reads and rejecting the create is the read-only credential case: the table is
demonstrably there, and the operator was sent to `nextly db:sync`, the one
remedy that cannot help. They would run it, see nothing change, and have no
reason to look at the credential.

The empty-row path now marks itself as entered, so a failure from that point on
is reported as a row that would not persist. Both of its failures are covered:
the create rejecting, and the read back after an accepted create failing. The
control holds the other direction, where a table that cannot be read at all is
still the schema's to reconcile.
@mobeenabdullah

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9ac8c4af67

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +202 to 203
function reportDegraded(message: string, detail?: string): void {
if (degraded) return;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Emit a warning when the degradation mode changes

The rejected-seed case is now classified correctly in isolation, but this single degraded flag suppresses a later, different diagnosis. If a process first starts before db:sync, then the table is created while its runtime credentials remain unable to establish the row, subsequent refreshes reach ROW_NOT_ESTABLISHED but emit nothing because the earlier table warning set this flag. The operator therefore sees only the obsolete db:sync remedy. Track the reported degradation category (or clear it once the table is readable); the new diagnostic test currently resets module state between failure modes, so it cannot expose this transition.

AGENTS.md reference: AGENTS.md:L228-L233

Useful? React with 👍 / 👎.

@mobeenabdullah
mobeenabdullah merged commit 4e91f14 into main Sep 13, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: core nextly type: docs Documentation only

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant