Skip to content

fix(access): cut the @/plugins/access barrel out of client code - #772

Merged
Ardnived merged 8 commits into
mainfrom
claude/fix-770-access-barrel-client
Sep 12, 2026
Merged

Ardnived merged 8 commits into
mainfrom
claude/fix-770-access-barrel-client

Conversation

@sydevs-bot

@sydevs-bot sydevs-bot commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Nine browser-executed modules imported the @/plugins/access barrel, which reached @/lib/env/server and shipped the whole access-plugin runtime to every admin page.
  • They now import the pure @/plugins/access/config (or ./adminOnly, ./types). Nothing under src/plugins/access/ changes.
  • The guard that should have caught this was unfailable. It now derives its entries and matches resolved files: offenders across all 92 browser entries go 14 → 0.
  • The barrel is also a SERVER_ONLY row in its own right, so the documented rule holds even if the barrel stops reaching @/lib/env/server.

Changes

  • src/** (9 files) — one import line each, onto a pure leaf.
  • tests/utils/importGraph.ts — onSpec receives the resolved file; the walk stops at 'use server'; file reads and clientEntries() are memoized; the directive scan strips any run of leading comments.
  • tests/unit/client-bundle-safety.spec.ts — entries from clientEntries(), SERVER_ONLY keyed by file and carrying the access barrel, five new controls.
  • tests/unit/public-env-substitution.spec.ts — KNOWN_OFFENDERS emptied.
  • src/AGENTS.md — the client-side import rule, stated as a rule.

Preview

https://sahajcloud-sahajcloud-pr-772.up.railway.app

Test results

  • Lean gate: ✓ pnpm lint && pnpm typecheck && pnpm typecheck:tests && pnpm test:unit (1871 unit tests)
  • Targeted specs: app-status-readiness.int.spec.ts + role-based-access.int.spec.ts — 68 passed
  • Build: ✓ pnpm build — the check that matters here, since CI does not build and this change is about bundling

Manual verification

  1. Open /admin, switch project in the selector, and load a collection list — the switch, the nav, the logo and the permissions table all read from the moved imports.

The risk is low by construction: src/plugins/access/index.ts re-exports these symbols from ./config, ./adminOnly and ./types verbatim, so every moved import resolves to the same module object it did before.

Notes for reviewer

  • Title deviates from the issue's. The issue is named for the defect; this repo squash-merges, so the PR title becomes the commit subject and names the change instead.
  • Eight commits, one behaviour each — the fix, the guard, the docs, the /simplify pass, one defect the guard review turned up, and three from the adversarial review (below).
  • SERVER_ONLY had a fifth way to be vacuous, and this branch nearly shipped it. One of its four rows named @/jobs/VerifyEmbeds/browserRendering; that module has been at lib/embedVerification/ since it moved, so the row matched nothing from feat(access): add embed metadata and canonical ownership to Clients #633 until now. 0bc8540 corrects it and adds a control that fails on any key with no module behind it.
  • The // gap is closed, not deferred. dfdb1a5–910d55a answer the review: the barrel is guarded directly, the directive scan reads behind any leading comment, and TypeScript's own parse is the entry set's independent oracle. The one finding left open is spec:108 — two controls the review calls duplicative, argued in the thread.
  • Two follow-ups the ticket already named, both left alone deliberately: @/lib/status is the same barrel shape (ReadinessGroup pulls nine modules through it for one function), and SERVER_ONLY is still hand-maintained.
  • No path matches securityReview.triggerPattern, so the security review was skipped.
Detail

Why the barrel and not previewSecret. Cutting accessConfigs → @/lib/utilities/previewSecret clears the same 12 chains and is the worse fix: accessConfigs.ts:110 needs hasValidPreviewSecret at runtime, so it means redesigning how previewSecret.ts reaches its secret — a design decision inside src/plugins/access/, which is on this repo's security-review trigger. And it only satisfies the guard; the browser would still download accessPlugin, fieldAccess, accessConfigs and permissions.

The deep imports are not new house style. @/plugins/access/adminOnly is already imported that way by src/fields/slugField.ts, and @/plugins/access/types by src/plugins/openapi/specFilter.ts.

Each change was verified by reintroducing the defect it prevents.

Reintroduce Result
from '@/plugins/access' in ProjectSelector/utils.ts 2 entries red, naming the full chain to ./server
Delete the 'use server' stop red on VerifyForm.tsx and FeedbackForm.tsx
Widen the stop to cover the entry itself red — a server action walked in its own right reports nothing
Revert SERVER_ONLY to specifiers, keep clientEntries() green, with the defect present — the vacuity this PR removes
Restore the old jobs/VerifyEmbeds/ key red on the new existence control
Drop the barrel row, keep lib/env/server.ts, import the barrel from ProjectSelector/utils.ts 3 entries red naming @/plugins/access
A // line above UserMessageStatus.tsx's directive, with the old regex red — the file is absent from clientEntries()

Measurements, walking the real graph from all 92 'use client' files plus instrumentation-client.ts:

  • Before: 14 entries reach lib/env/server.ts when matching resolved files, 0 when matching specifiers.
  • After the import rewrite: 2, both false positives through 'use server' action modules.
  • After the 'use server' stop: 0.
  • The guard's own cost: memoizing file reads took a 92-entry walk from 563 reads to 148. The two guard specs now run 733 ms of test time, against 347 ms before the memo — the TypeScript cross-check parses the ~95 files that mention the directive, and reuses the memoized sources rather than re-reading src/.

Why 'use server' pruning is correct, not a convenience. Next compiles such a module to a client reference — an id the browser posts back — so its body never enters the browser bundle. That makes it neither a bundle-safety offender nor a place a browser reads process.env, which is why both consumers of the walk want it.

Why a loop, not a widened regex. The directive can sit behind a licence block, a // note, or both in either order. The nested quantifier that expresses "any run of either" backtracks badly on a long file that never matches, and this scan runs over every file under src/.

ContentSlug. ProjectSelector/utils.ts borrowed it as Parameters<typeof isCollectionVisibleInProject>[0] to avoid a deep import. This branch makes that import ordinary, and @/plugins/access/types exports the type directly, so the borrow and its comment are gone.

Closes #770

🤖 Written by the sydevs autonomous loop — see what this is

🤖 Generated with Claude Code

https://claude.ai/code/session_01WQJWHhheKp2mGUWNM8dyYw

… barrel

Nine modules a browser executes imported `@/plugins/access`, and that
barrel reaches `@/lib/env/server` through `accessPlugin → fieldAccess →
accessConfigs → @/lib/utilities/previewSecret → @/lib/env`. Twelve of the
14 browser entries that reached the server module did so this way — the
#633 shape, in a second barrel.

Nothing failed at runtime: `@/lib/env/server` imports only zod and parses
lazily, so the deploy survived. What it cost was the access plugin's whole
runtime — `accessPlugin`, `fieldAccess`, `accessConfigs`, `permissions` —
downloaded by every admin page.

`@/plugins/access/config` re-exports exactly the helpers these nine want
and is pure: `config/projects.ts`, `config/roles.ts` and `types.ts`
between them import nothing but types. It is a documented barrel of its
own, so this is not a deep import into the plugin's internals — which was
the objection recorded in `ProjectSelector/utils.ts`. `buildStatusGlobalConfig`
takes `./adminOnly`, equally pure.

Nothing under `src/plugins/access/` changes. Measured over all 92 browser
entries, this takes the count reaching `lib/env/server.ts` from 14 to 2,
and both of those are false positives the next commit removes.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01A8FsJqcyLKyWD3MX6SmsNf
`client-bundle-safety.spec.ts` has been green since #633 with the defect
the previous commit fixes sitting in front of it, for two independent
reasons. It walked four hand-written entries, so 88 browser entries had no
guard at all. And it matched the import **specifier** a caller wrote,
while the edge into `lib/env/server.ts` is `@/lib/env`'s own `./server` —
a specifier no caller writes and no list can enumerate.

Both halves are needed. Derived entries alone still find nothing, because
the matcher never sees the edge. File matching alone still misses every
entry outside the list of four.

So the walk now resolves each specifier before offering it, and passes the
resolved file alongside it. `SERVER_ONLY` holds src-relative file paths
and matches those; the failure message still reports the specifier chain,
which is what names the import to cut. Entries come from `clientEntries()`,
the same derivation `public-env-substitution.spec.ts` already used.

The walk also stops at a `'use server'` module. Next compiles one to a
client reference — the body never enters the browser bundle — so the two
form/action chains it used to report were false positives.

`KNOWN_OFFENDERS` in `public-env-substitution.spec.ts` is now empty: its
one entry was `lib/env/server.ts`, which no browser entry reaches.

Each of the three changes was verified by reintroducing the defect it
prevents and watching the spec go red — including the third, which goes
GREEN with the barrel import restored if the matcher reverts to
specifiers. That vacuity is what this commit removes.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01A8FsJqcyLKyWD3MX6SmsNf
`src/AGENTS.md` told the reader to add new client entry points to
`client-bundle-safety.spec.ts`'s list. There is no list any more, and the
instruction would have read as still current.

Records what the guard's two defects were, since a graph guard tends to
have exactly those two, and the failure is invisible — CI does not build
this app.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01A8FsJqcyLKyWD3MX6SmsNf
Four findings from the review pass, all inside what this branch already
touched:

- `ProjectSelector/utils.ts` borrowed `ContentSlug` through
  `Parameters<typeof isCollectionVisibleInProject>[0]` to avoid a deep
  import. This branch makes the deep import the house style, and
  `@/plugins/access/types` exports `ContentSlug` directly. Six lines of
  comment and an indirection go with it.
- One `hasDirective(file, directive)` replaces two copies of the
  leading-comment-tolerant directive regex. `'use client'` picks the entry
  set and `'use server'` prunes the walk, so a fix to one that misses the
  other makes them disagree about the same file.
- `importGraph` reads each file once per process and memoizes
  `clientEntries()`. A 92-entry walk was 563 reads; it is now 148, and the
  two guard specs run 347ms → 271ms of test time. The walk revisits the
  same module from every edge and `src/` cannot change mid-run.
- `SERVER_ONLY` is keyed by absolute path, so the per-edge callback no
  longer calls `relative()` on every traversed edge.

Also pins the `'use server'` stop, which was the one new way for the walk
to end early and the one thing with no test. Two controls, proven to fail
in both directions: drop the stop and a client entry reports its server
action's chain; widen it to the entry and a server action walked in its
own right reports nothing.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01A8FsJqcyLKyWD3MX6SmsNf
`SERVER_ONLY` named `@/jobs/VerifyEmbeds/browserRendering`, and the module
has been at `src/lib/embedVerification/browserRendering.ts` since it moved.
So one of the guard's four rows matched nothing at all, from #633 until
now, and the previous commit carried the stale name over into the new file
matcher unchanged.

Nothing else would ever say so — an unmatchable key looks exactly like a
rule nobody breaks. A new control walks the map and fails on any key with
no module behind it; it goes red on the old name.

Also corrects the derived-entries control, which claimed to cover the four
entries this spec used to list and named three.
`CanonicalEmbedPicker/model.ts` carries no directive, so it is not an entry
and never was — `public-env-substitution.spec.ts` already pins it as a
module the walk reaches.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01A8FsJqcyLKyWD3MX6SmsNf
@railway-app

railway-app Bot commented Sep 12, 2026 •

Copy link
Copy Markdown

🚅 Deployed to the SahajCloud-pr-772 environment in sahajcloud

Service Status Web Updated
SahajCloud ✅ Success (View Logs) Web Sep 12, 2026 at 5:57 pm UTC
1 service not affected by this PR
  • mailpit

@railway-app
railway-app Bot temporarily deployed to sahajcloud / SahajCloud-pr-772 September 12, 2026 17:26 Destroyed
@sydevs-bot sydevs-bot added the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Sep 12, 2026
@sydevs-bot

sydevs-bot commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor Author

🤖 no session running

recent dispatches
  • 18:09Z address-review · attempt 1 · session · done
  • 18:09Z address-review · attempt 1 · session
  • 17:45Z address-review · attempt 1 · session · done
  • 17:45Z address-review · attempt 1 · session
  • 17:37Z adversarial-review · attempt 1 · session · done

Written by the sydevs dispatcher. Sessions never edit this comment.

@sydevs-bot sydevs-bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

🧐 Adversarial review

The diff is the shape #770 asked for — ten one-line import moves onto pure leaves, one matcher change, no src/plugins/access/ edit — and 0bc8540 fixes a real dead guard row. Two claims stop short: src/AGENTS.md calls the new barrel rule enforced, yet the barrel is caught only by happening to reach lib/env/server.ts; and hasDirective now picks the entry set for both guards, so one file can leave both silently.

Findings:

  • should-fix — the barrel rule is enforced only incidentally · spec:40
  • should-fix — a // before 'use client' drops a file from both guards · importGraph:60
  • consider — the #633→#770 story is told in four places · AGENTS.md:142
  • consider — two new controls duplicate cases above · spec:107

Checked: #770, the PR body, the diff. Read whole: importGraph.ts, both guard specs, both barrels. Confirmed the guard's chain is live (lib/env/index.ts runtime-exports ./server), browserRendering.ts exists at its new path, and VerifyForm.tsx is itself an entry. No cross-repo surface.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

Comment thread tests/unit/client-bundle-safety.spec.ts Resolved
Comment thread tests/utils/importGraph.ts Resolved
Comment thread src/AGENTS.md Resolved
const verifyAction = 'app/(frontend)/events/verify/actions.ts'

it('does not follow a client entry into a server action', () => {
expect(findServerOnlyImport(join(SRC, 'app/(frontend)/events/verify/VerifyForm.tsx'))).toBeNull()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

consider — this control cannot fail on its own.

VerifyForm.tsx carries 'use client', so it is already an entry in the it.each above. Drop the 'use server' stop and both go red together. This case earns its place only on the day that file loses its directive.

Same question on line 74: entries.length > 50 is already asserted on the same shared clientEntries() at public-env-substitution.spec.ts:113. The arrayContaining half is what actually pins this PR's swap.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Both kept, with evidence — your call.

The 'use server' pair is the only place the stop's direction is stated, and its redundancy holds only while VerifyForm.tsx keeps 'use client'. Nothing asserts that.

entries.length > 50 guards this spec's it.each from running zero cases. public-env-substitution.spec.ts:113 cannot do that job, since either file can be deleted alone.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@sydevs-bot sydevs-bot added bot:working Lock: a cloud session is running on this item. Removed when it ends. and removed bot:working Lock: a cloud session is running on this item. Removed when it ends. labels Sep 12, 2026
`src/AGENTS.md` states that client code must not import the
`@/plugins/access` barrel, and called that rule enforced. It was not
enforced directly: the barrel was caught only because it happens to reach
`lib/env/server.ts` through `accessConfigs → previewSecret → @/lib/env →
./server`. Cutting that one edge — a live alternative fix named in #770's
own notes — would have left the barrel green while the browser still
downloaded `accessPlugin`, `fieldAccess`, `accessConfigs` and
`permissions`, which is the cost the ticket is about.

`plugins/access/index.ts` is now a `SERVER_ONLY` row in its own right, so
the documented rule and the guard say the same thing. Verified by
reintroducing `from '@/plugins/access'` in `ProjectSelector/utils.ts`
with the `lib/env/server.ts` row removed: three entries go red naming the
barrel.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WQJWHhheKp2mGUWNM8dyYw
`hasDirective` tolerated a leading block comment but not a `//` line
comment, so a file written that way was not a `'use client'` entry. That
is not one weakened guard: the file leaves `clientEntries()`, so
`client-bundle-safety` and `public-env-substitution` both go quiet about
it at once, and `entries.length > 50` cannot see one file leave.

The scan now strips any run of leading comments, in a loop rather than a
regex — the nested quantifier for "any run of either comment form"
backtracks badly on a long file that never matches. A new control
cross-checks the result against TypeScript's own parse of the directive
prologue, which is the independent oracle the entry set lacked.

Verified both ways on `UserMessageStatus.tsx` with a `//` line added
above its directive: green with the new scan, and with the old regex
restored the control goes red naming the file.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WQJWHhheKp2mGUWNM8dyYw
`src/AGENTS.md` says it is the one home for the bundle-guard story, and
that code sites carry a one-line pointer to it. This branch retold the
#633/#760/#770 history at four code sites as well, which is what that
rule exists to prevent — five copies to update when the story changes.

Each site keeps the part a reader needs there (why files and not
specifiers, why the entry set is derived, what a dead `SERVER_ONLY` row
is) and drops the history behind a pointer. The spec's own header keeps
its incident, since that states what the guard is for rather than
retelling the rule.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WQJWHhheKp2mGUWNM8dyYw
@railway-app
railway-app Bot temporarily deployed to sahajcloud / SahajCloud-pr-772 September 12, 2026 17:54 Destroyed
@sydevs-bot

Copy link
Copy Markdown
Contributor Author

Three findings adopted, one argued. Pushed 910d55a.

  • Adopted — the access barrel is now a SERVER_ONLY row of its own, so src/AGENTS.md's rule is enforced directly rather than incidentally (dfdb1a5). thread
  • Adopted — the directive scan reads behind any leading comment, and a TypeScript parse cross-checks the entry set (24af3c7). thread
  • Adopted — the retold story is cut back to a pointer at four code sites (910d55a). thread
  • Argued, left open — both controls at spec:108 stay. Reasons in the thread; the reviewer settles it. thread

Each adoption was verified by reintroducing the defect it prevents — two new rows in the PR body. Lean gate green: 1871 unit tests.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@sydevs-bot sydevs-bot removed the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Sep 12, 2026
@sydevs-bot
sydevs-bot marked this pull request as ready for review September 12, 2026 18:04
@sydevs-bot
sydevs-bot requested a review from Ardnived September 12, 2026 18:04
@Ardnived

Ardnived commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

@sydevs-bot Would it make sense to add a specific client-safe export barrel to the access plugin? eg. @plugins/access/client, then we would avoid breaking our rule about deep imports as much, and make it explicit why these are being imported that way?

Or is that unnecessary overhead?

@sydevs-bot sydevs-bot added bot:working Lock: a cloud session is running on this item. Removed when it ends. and removed awaiting labels Sep 12, 2026
@sydevs-bot

Copy link
Copy Markdown
Contributor Author

Recommend against it — your call.

The rule points the other way. src/AGENTS.md:113 gives a barrel only to a folder imported as a unit; cherry-picked folders use deep imports and get none. @/plugins/access/config is already that cohesive sub-barrel, so client code imports a public surface, not an internal.

Those leaves' other importers are server code. slugField.ts and specFilter.ts are server-only. buildStatusGlobalConfig.ts builds a Payload global and reaches the browser only through the uncut @/lib/status barrel. A /client path mislabels all three, or leaves them deep-importing anyway.

It buys no enforcement. The guard walks every browser entry and fails on any reaching the barrel or lib/env/server, wherever the import sits.

/config names why it is safe. /client would only say someone judged it so.

Say the word and I will add it — one commit here.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@sydevs-bot sydevs-bot added awaiting and removed bot:working Lock: a cloud session is running on this item. Removed when it ends. labels Sep 12, 2026
@Ardnived
Ardnived merged commit 9d57d57 into main Sep 12, 2026
48 of 54 checks passed
@Ardnived
Ardnived deleted the claude/fix-770-access-barrel-client branch September 12, 2026 18:57
@sydevs-bot sydevs-bot moved this from Revising to Done in SYDevelopers Technical Roadmap Sep 12, 2026
sydevs-bot pushed a commit that referenced this pull request Sep 12, 2026
`main` #772 replaced the hand-written client-entry list in
`tests/unit/client-bundle-safety.spec.ts` with a derived set —
`clientEntries()` over every `'use client'` file. This branch had added
`components/admin/PreviewTarget/PreviewTarget.tsx` to that list.

Took the derived set. `PreviewTarget.tsx` carries a leading `'use client'`
directive, so it is an entry automatically, and the spec's own
`misses no file TypeScript reads as a client entry` case proves that rather
than assuming it. The per-entry comment went with the list: the rule it
recorded — `@/fields/previewTargetField` is types and a plain object literal
so the declaration can cross to the browser — is already stated in that
module's own header.

Nothing else conflicted. The branch's new code imports no `@/plugins/access`
barrel, the new `SERVER_ONLY` row #772 added.

Gate after the merge: lint, typecheck, typecheck:tests, 1952 unit tests, plus
`translations-globals` and `translations-field` (60 tests) — all green.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0158CavRbZEnPZhmp4fnCwa5

This branch was successfully deployed

No deployments
sahajcloud / SahajCloud-pr-772 — 910d55a4 Deployed Sep 12, 2026 by railway-app[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

fix(access): client components reach @/lib/env/server via the access barrel

3 participants