Skip to content

Narrow TypedAuthUser onto Manager / Client, and delete the admin-view '' - #698

Merged
sydevs-bot merged 6 commits into
mainfrom
claude/refactor-drop-project-sentinel
Sep 5, 2026
Merged

sydevs-bot merged 6 commits into
mainfrom
claude/refactor-drop-project-sentinel

Conversation

@sydevs-bot

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

Copy link
Copy Markdown
Contributor

Summary

  • Deletes the '' sentinel from Managers.currentProject — the option, the beforeChange that undid it, and the database enum member.
  • Narrows TypedAuthUser onto the generated Manager / Client types. Once the sentinel is gone the two shapes agree, so currentProject, type and _status become picks.
  • Closes a latent sidebar bug, and a 500-instead-of-400 in set-project that two review passes found on the way.

Changes

  • src/collections/Managers/Managers.ts — the '' option and its beforeChange both go
  • src/migrations/20260905_130913_drop_manager_project_sentinel.ts — nulls '' rows, narrows the enum
  • src/plugins/access/types.ts — TypedAuthUser picks three members instead of restating them
  • src/plugins/access/config/projects.ts — isValidProject uses Object.hasOwn, not in
  • src/types/AGENTS.md — records why roles is not a restatement, so a future sweep does not "fix" it
  • tests/int/set-project-endpoint.int.spec.ts — pins the 400 the Object.hasOwn fix produces

Preview

The Railway deploy succeeded (SahajCloud-pr-698, 13:34Z), but its commit status description reads only Success and 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

  • New migration: src/migrations/20260905_130913_drop_manager_project_sentinel.ts
  • Impact: managers.current_project only. Rows holding '' become NULL, which is the value every consumer already treats as the admin view. No other column uses enum_managers_current_project (checked against the Drizzle snapshot), so DROP TYPE has no dependency to fail on.
  • Reversible: yes, for the schema — down restores '' to the enum. Not for the data: a row nulled by up stays null. That is intended, since '' had no meaning NULL did not already carry.

The migration is augmented, and that is the point. migrate:create emits the enum narrowing alone, whose 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 cleanly. This is src/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 prodMigrations on boot, and that boot succeeded.

Test results

Locally, on this exact tree:

  • Lean gate: ✓ pnpm lint, pnpm typecheck, pnpm typecheck:tests, pnpm test:unit (122 files, 1570 tests)
  • Targeted specs: set-project-endpoint 10 passed. Earlier: project-visibility, role-based-access
  • Migration: applied and reversed against a local Postgres 16, seeded with an '' row
  • Build: N/A — Railway builds the preview

CI 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. Restoring value in PROJECTS turns the new endpoint case red with expected 500 to be 400.

Manual verification

  1. In the preview admin, sign in as a manager with no project selected. Every project-assigned collection should still appear in the sidebar. This is the behaviour the sentinel could have broken.
  2. Switch projects with the sidebar selector, then switch back to "All Content". The selection should persist across a reload.
  3. POST /api/managers/set-project with {"currentProject":"toString"} should now answer 400 Invalid project., not 500.

Notes for reviewer

  • The currentProject verdict in Narrow TypedAuthUser onto Manager / Client — and decide where the admin-view '' is handled #671 was an inversion, and this PR takes the corrected reading. The hand-written ProjectSlug | null was 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 a Pick.
  • isValidProject is a scope call I would like checked. The in → Object.hasOwn fix 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 three as ProjectSlug casts delete themselves — is filed as refactor(access): make isValidProject a type predicate, deleting three ProjectSlug casts #697 rather than widened into this PR.
  • roles stays hand-written on TypedAuthUser, 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 ?? null and reintroducing the visibility.ts shape. Deleting it makes the state unrepresentable, and — because TypedAuthUser.currentProject is now the generated type — re-adding an option would make visibility.ts:53 stop compiling rather than silently misbehave.

The latent bug. visibility.ts:53 reads user.currentProject ?? null, and ?? 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, 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 — hasPermission does not read currentProject.

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 in set-project-endpoint.int.spec.ts beside the invalid-slug case. is-valid-project.spec.ts stays — tests/AGENTS.md puts a pure validator in the unit lane — but loses the case that iterated getProjectSlugs() through Object.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-review at high effort, plus /security-review, which the diff triggers via securityReview.triggerPattern on src/plugins/access/ and src/collections/Managers/. Security found nothing. Findings applied: the stale header comment on TypedAuthUser, a redundant test and the non-null assertions it existed to guard, a cast that asserted away the string arm of (string | OptionObject)[], and the removed sentinel being narrated in three files. Findings dismissed: collapsing Partial<A> & Partial<B> into Partial<A & B> (taste; the current form shows which member comes from which collection), adopting findFieldByName for the test lookup (it would trade one cast for another), swapping collection: 'managers' | 'clients' for AuthCollectionSlug (a deliberate narrowing — roles assumes 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 (managers is 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-review ran 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

`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
@railway-app

railway-app Bot commented Sep 5, 2026 •

Copy link
Copy Markdown

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

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

@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

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

Comment thread tests/unit/is-valid-project.spec.ts Resolved
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
@railway-app
railway-app Bot temporarily deployed to sahajcloud / SahajCloud-pr-698 September 5, 2026 15:09 Destroyed
@sydevs-bot

Copy link
Copy Markdown
Contributor Author

Both review threads adopted and resolved, pushed as e365ff9.

  • The 500-vs-400 case moved to set-project-endpoint.int.spec.ts, where the defect was observable. It fails first — restoring value in PROJECTS gives expected 500 to be 400.
  • is-valid-project.spec.ts keeps its load-bearing cases and loses the one that compared PROJECTS with itself. The file stays in the unit lane, per tests/AGENTS.md.
  • Both spec docblocks now point at Narrow TypedAuthUser onto Manager / Client — and decide where the admin-view '' is handled #671 in one line, instead of narrating the removed system.

Lean gate green, set-project-endpoint 10 passed. CI re-runs on this push. The body no longer says CI was still running — it passed on e52de47 at 13:53Z.

This still needs your approving review to merge.

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


Generated by Claude Code

@Ardnived Ardnived removed the awaiting label Sep 5, 2026
@sydevs-bot
sydevs-bot merged commit 917a6ea into main Sep 5, 2026
6 checks passed
@github-project-automation github-project-automation Bot moved this from In progress to Done in SYDevelopers Technical Roadmap Sep 5, 2026
Ardnived pushed a commit that referenced this pull request Nov 5, 2026
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

This branch was successfully deployed

No deployments
sahajcloud / SahajCloud-pr-698 — e365ff9a Deployed Sep 5, 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.

Narrow TypedAuthUser onto Manager / Client — and decide where the admin-view '' is handled

3 participants