Repository navigation
Narrow TypedAuthUser onto Manager / Client, and delete the admin-view '' - #698
Conversation
`currentProject` carried an `''` option meaning the admin "All Content"
view, plus a `beforeChange` hook that mapped `''` straight back to `null`.
The pair was self-cancelling. The option was also dead UI: the field is
`admin.hidden`, so Payload never renders that select, and the one writer
— `POST /api/managers/set-project` — already sends `null`.
The cost was in the generated type. `Manager['currentProject']` admitted
`''` because the DB enum declared it, and `visibility.ts:53` reads
`user.currentProject ?? null` — `??` does not catch `''`. A row holding
the sentinel reached `isCollectionVisibleInProject` as a project name,
matched no collection, and hid every project-assigned collection in the
sidebar. `null` would have shown them all. Nothing writes such a row
today (`isValidProject('')` is false, so the endpoint 400s), so this was
latent. The migration is what makes it unreachable rather than unlikely.
The migration is augmented, deliberately. `migrate:create` emits the enum
narrowing alone, and its closing
`USING "current_project"::"enum_managers_current_project"` throws
`invalid input value for enum … ""` on any surviving row. Verified both
ways against a local Postgres 16: the generated UP fails on a seeded `''`
row, and the shipped UP nulls it, narrows the enum, and reverses. This is
`src/migrations/AGENTS.md`'s "augment only when the migration fails
without the edit" case.
The new spec reads the real collection config rather than a fixture, so
no assumption about the shape can be wrong. It was confirmed to fail:
re-adding the option and the hook turns two of its four assertions red.
Refs #671
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B7mJKJkraRWeZs9X463dtt
`TypedAuthUser` restated three members the generated types already own: `type` (identical to `Manager['type']`), `_status` (identical to `Client['_status']`), and `currentProject`. `currentProject` was the one that looked like a refactor and was not. The hand-written `ProjectSlug | null` was always correct — the generated type was the wrong one, widened by the `''` the DB enum declared. With the previous commit removing it, the two shapes agree and this is a mechanical `Pick`, not a rewrite of every access consumer. `Partial` wraps both picks because a client carries no `type` and a manager no `_status`, while `Manager['type']` is non-optional. Taking `_status` from `Client` also admits `null`, which the generated type has always allowed. `bypassPermissions.ts:45` compares `!== 'published'`, so it reads `null` correctly. `roles` stays hand-written. It is the only member no generated type can express: the collection declares `roles` as `localized`, so Payload generates the single-locale array, while `hydrateLocalizedRoles` produces a per-locale record during authentication (#665). The comment now says that, rather than describing the record alone. Closes #671 Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01B7mJKJkraRWeZs9X463dtt
`src/types/AGENTS.md` lists three shapes that are deliberately not restatements of a generated type. `TypedAuthUser.roles` is a fourth kind and was not covered: Payload generates the single-locale type for a `localized` field, so the per-locale record `hydrateLocalizedRoles` builds during authentication is not spellable from it (#665). Without the entry, the next sweep applying that rule would replace it with `Pick<Manager, 'roles'>`. That compiles, and silently drops every locale but the default — the failure #665 exists to prevent. Refs #671 Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01B7mJKJkraRWeZs9X463dtt
- `types.ts`: drop the header's "Role structure is auto-detected" bullets. The inline comment on `roles` states the same thing, with the #665 reason. - `manager-current-project.spec.ts`: narrow the field lookup once at module scope, so the three non-null assertions and the test that only guarded them both go. Normalize `options` the way `splash-block.spec.ts` does rather than casting away the `string` arm of `(string | OptionObject)[]` — a bare-string option would have read back as `undefined` with the type-checker silent. - `Managers.ts` and `setProject.ts`: state the rule at the code, and leave the removed sentinel's story to the spec docblock. It was told three times. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01B7mJKJkraRWeZs9X463dtt
`value in PROJECTS` walks the prototype chain, so `isValidProject` returned true for `toString`, `constructor`, `valueOf`, `hasOwnProperty` and `__proto__`. The cost was a wrong status code, not a widened grant. Those values passed `set-project`'s zod refine, Payload's own select validation then refused the write, and the handler's `catch` turned that into a 500 `Failed to change project.` where the schema promises a 400 `Invalid project.` Pre-existing, and fixed here because this branch's own comment now cites `isValidProject` as what keeps a non-member out of the field. Two independent review passes flagged the same line for that reason. `Object.hasOwn` needs ES2022, which `tsconfig.json` already targets. The spec was confirmed to fail: restoring `in` turns the prototype-key assertion red. The deeper change both reviews proposed — making this a type predicate so the `as ProjectSlug` casts at three call sites delete themselves — is filed separately rather than widened into this PR. Refs #671 Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01B7mJKJkraRWeZs9X463dtt
|
🚅 Deployed to the SahajCloud-pr-698 environment in sahajcloud
1 service not affected by this PR
|
sydevs-bot
left a comment
There was a problem hiding this comment.
🧐 Adversarial review
Shape matches #671 — step 1 and step 2, nothing else. I re-derived the migration's two load-bearing claims: the pre-change snapshot has enum_managers_current_project on one column only (managers.current_project, no default, no _v table); the new one narrows it. visibility.ts:53's story holds, and re-adding an option would fail to compile there. The isValidProject scope call reads right: in→hasOwn here, the predicate rewrite in #697.
Judgement call: ~40 lines of ticket work arrive with ~90 of new spec — defensible singly, together most of the hand-written diff.
Findings
consider—tests/unit/is-valid-project.spec.ts:18: a new file for a guard the int suite already owns.consider—tests/unit/manager-current-project.spec.ts:9: a docblock describing the removed system.
Checked — #671 + decision comment; the diff minus the snapshot; Managers.ts, setProject.ts, projects.ts, types.ts, visibility.ts, bypassPermissions.ts; both snapshots parsed (prevId: 0000… is the norm here). Cross-repo: WeMeditateWeb's types:cms copy still holds '', reads currentProject nowhere. No seed, script or spec writes ''.
Generated by Claude Code
Review threads on #698 asked for two things. Both are adopted. `tests/unit/is-valid-project.spec.ts` pinned the helper, not the defect. The bug was observable as a 500 where `set-project` promises a 400, so that contract now has a case in `tests/int/set-project-endpoint.int.spec.ts` beside the invalid-slug case it belongs with. Confirmed it fails first: restoring `value in PROJECTS` gives `expected 500 to be 400`. The unit file stays. `tests/AGENTS.md` puts a pure validator in the unit lane, and Tier 1 answers in about a second where the integration lane boots Payload. Its tautological case goes instead — iterating `getProjectSlugs()` through `Object.hasOwn(PROJECTS, …)` compares the constant with itself and cannot fail. Both spec docblocks narrated the removed sentinel system. That history is already in #671, the PR body and `projects.ts`. A header that narrates code only drifts from it, so each now points at #671 in one line. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01A6p13bPyPaiysoorzsPDcK
|
Both review threads adopted and resolved, pushed as
Lean gate green, This still needs your approving review to merge. 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
Review threads on #698 asked for two things. Both are adopted. `tests/unit/is-valid-project.spec.ts` pinned the helper, not the defect. The bug was observable as a 500 where `set-project` promises a 400, so that contract now has a case in `tests/int/set-project-endpoint.int.spec.ts` beside the invalid-slug case it belongs with. Confirmed it fails first: restoring `value in PROJECTS` gives `expected 500 to be 400`. The unit file stays. `tests/AGENTS.md` puts a pure validator in the unit lane, and Tier 1 answers in about a second where the integration lane boots Payload. Its tautological case goes instead — iterating `getProjectSlugs()` through `Object.hasOwn(PROJECTS, …)` compares the constant with itself and cannot fail. Both spec docblocks narrated the removed sentinel system. That history is already in #671, the PR body and `projects.ts`. A header that narrates code only drifts from it, so each now points at #671 in one line. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01A6p13bPyPaiysoorzsPDcK
Summary
''sentinel fromManagers.currentProject— the option, thebeforeChangethat undid it, and the database enum member.TypedAuthUseronto the generatedManager/Clienttypes. Once the sentinel is gone the two shapes agree, socurrentProject,typeand_statusbecome picks.set-projectthat two review passes found on the way.Changes
src/collections/Managers/Managers.ts— the''option and itsbeforeChangeboth gosrc/migrations/20260905_130913_drop_manager_project_sentinel.ts— nulls''rows, narrows the enumsrc/plugins/access/types.ts—TypedAuthUserpicks three members instead of restating themsrc/plugins/access/config/projects.ts—isValidProjectusesObject.hasOwn, notinsrc/types/AGENTS.md— records whyrolesis not a restatement, so a future sweep does not "fix" ittests/int/set-project-endpoint.int.spec.ts— pins the 400 theObject.hasOwnfix producesPreview
The Railway deploy succeeded (
SahajCloud-pr-698, 13:34Z), but its commit status description reads onlySuccessand publishes no host, so there is no alias to discover and link. Reach it from the Railway environment. Not constructing the URL by hand on purpose — the documented slug rule is a guess about a host we do not own.Migration
src/migrations/20260905_130913_drop_manager_project_sentinel.tsmanagers.current_projectonly. Rows holding''becomeNULL, which is the value every consumer already treats as the admin view. No other column usesenum_managers_current_project(checked against the Drizzle snapshot), soDROP TYPEhas no dependency to fail on.downrestores''to the enum. Not for the data: a row nulled byupstays null. That is intended, since''had no meaningNULLdid not already carry.The migration is augmented, and that is the point.
migrate:createemits the enum narrowing alone, whose closingUSING "current_project"::"enum_managers_current_project"throwsinvalid input value for enum … ""on any surviving row. Verified both ways against a local Postgres 16: the generatedupfails on a seeded''row, and the shippedupnulls it, narrows the enum, and reverses cleanly. This issrc/migrations/AGENTS.md's "augment only when the migration fails without the edit" case.The deploy above is also the migration's first real application — Railway runs
prodMigrationson boot, and that boot succeeded.Test results
Locally, on this exact tree:
pnpm lint,pnpm typecheck,pnpm typecheck:tests,pnpm test:unit(122 files, 1570 tests)set-project-endpoint10 passed. Earlier:project-visibility,role-based-access''rowCI passed on the first ready commit (
e52de47,Lint, Test & Smoke, 13:53Z), answering the note this section used to carry. The review push above re-runs it.Every new assertion was confirmed to fail before it passed. Reintroducing the option and the hook turns two of
manager-current-project.spec.ts's assertions red. Restoringvalue in PROJECTSturns the new endpoint case red withexpected 500 to be 400.Manual verification
POST /api/managers/set-projectwith{"currentProject":"toString"}should now answer 400Invalid project., not 500.Notes for reviewer
currentProjectverdict in NarrowTypedAuthUserontoManager/Client— and decide where the admin-view''is handled #671 was an inversion, and this PR takes the corrected reading. The hand-writtenProjectSlug | nullwas always right — the generated type was the wrong one, widened by the''the enum declared. That is why item 4 looked like a refactor of every access consumer and is in fact aPick.isValidProjectis a scope call I would like checked. Thein→Object.hasOwnfix is pre-existing and strictly narrowing, and I took it because this branch's own new comment cites that function as what keeps a non-member out of the field. The deeper change both reviews proposed — making it a type predicate so threeas ProjectSlugcasts delete themselves — is filed as refactor(access): make isValidProject a type predicate, deleting three ProjectSlug casts #697 rather than widened into this PR.rolesstays hand-written onTypedAuthUser, deliberately. It is the one member no generated type can express.Detail
Why deleting the sentinel, rather than normalising at the auth boundary. Decided on the ticket 2026-09-03. Normalising leaves
''in the generated union, so nothing stops the next consumer writing?? nulland reintroducing thevisibility.tsshape. Deleting it makes the state unrepresentable, and — becauseTypedAuthUser.currentProjectis now the generated type — re-adding an option would makevisibility.ts:53stop compiling rather than silently misbehave.The latent bug.
visibility.ts:53readsuser.currentProject ?? null, and??does not catch''. A row holding the sentinel reachedisCollectionVisibleInProjectas a project name, matched no collection, and hid every project-assigned collection in the sidebar.nullwould have shown them all. Nothing writes such a row today, so this was latent rather than live; the migration is what makes it unreachable rather than merely unlikely. It is nav visibility (admin.hidden), never an access grant —hasPermissiondoes not readcurrentProject.The review revision (
e365ff9). Two threads on the spec layout, both adopted. The 500-vs-400 defect was observable at the endpoint, so its case now sits inset-project-endpoint.int.spec.tsbeside the invalid-slug case.is-valid-project.spec.tsstays —tests/AGENTS.mdputs a pure validator in the unit lane — but loses the case that iteratedgetProjectSlugs()throughObject.hasOwn(PROJECTS, …), which compared the constant with itself. Both spec docblocks narrated the removed sentinel system, which #671 and this body already hold, so each now points at #671 in one line.Review passes run.
/simplify(four angles) and/code-reviewat high effort, plus/security-review, which the diff triggers viasecurityReview.triggerPatternonsrc/plugins/access/andsrc/collections/Managers/. Security found nothing. Findings applied: the stale header comment onTypedAuthUser, a redundant test and the non-null assertions it existed to guard, a cast that asserted away thestringarm of(string | OptionObject)[], and the removed sentinel being narrated in three files. Findings dismissed: collapsingPartial<A> & Partial<B>intoPartial<A & B>(taste; the current form shows which member comes from which collection), adoptingfindFieldByNamefor the test lookup (it would trade one cast for another), swappingcollection: 'managers' | 'clients'forAuthCollectionSlug(a deliberate narrowing —rolesassumes exactly those two shapes, so widening to a future auth collection would be wrong), and rewriting the migration as a rename dance to save one table rewrite (managersis a staff table of tens of rows, and deviating from the generated shape costs more review than it saves).Note on tooling. The finalize pipeline calls for
/pr-review-toolkit:review-pr all. That plugin is not installed in this environment, so/code-reviewran instead — the pass the toolkit replaced. Flagged so the substitution is visible rather than assumed.Closes #671
🤖 Written by the sydevs autonomous loop — see what this is
🤖 Generated with Claude Code
https://claude.ai/code/session_01A6p13bPyPaiysoorzsPDcK