Skip to content

refactor(access): make isValidProject a type predicate, deleting three ProjectSlug casts - #712

Merged
sydevs-bot merged 2 commits into
mainfrom
claude/refactor-is-valid-project
Sep 6, 2026
Merged

sydevs-bot merged 2 commits into
mainfrom
claude/refactor-is-valid-project

Conversation

@sydevs-bot

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

Copy link
Copy Markdown
Contributor

Summary

  • isValidProject returned boolean, so all three callers followed it with as ProjectSlug to tell the compiler what the check had just established. All three casts are gone.
  • The predicate narrows to the generated ProjectSlug. refactor(access): make isValidProject a type predicate, deleting three ProjectSlug casts #697 asked which name the call sites want — this is the answer, with reasoning below.
  • That narrowing was only true by generation, not by anything the compiler held. PROJECTS now says it, and the statement was confirmed to fail on drift.

No user-visible behaviour changes.

Changes

File Change
plugins/access/config/projects.ts isValidProject(value): value is ProjectSlug | null, and PROJECTS ends as const satisfies Record<ProjectSlug, unknown>
collections/Managers/endpoints/setProject.ts .refine moves from the object onto the field; the update's cast goes
app/(payload)/api/openapi.json/route.ts ? projectParam : null
plugins/openapi/scalarPlugin.ts Same shape
tests/unit/is-valid-project.spec.ts Unchanged cases; the docblock now points at the satisfies clause

Which name the predicate should speak

The ticket asks this explicitly, so here is the reasoning rather than just the result.

InternalProjectSlug is keyof typeof PROJECTS. ProjectSlug is generated from the CMS schema. They are the same union today, so either narrowing deletes the casts — TypeScript unions are structural, and neither would need a cast at any call site.

The predicate speaks ProjectSlug because that is the name the call sites already declare: payload.update's data wants it, and both openapi routes annotate their local as ProjectSlug | null. Narrowing to the internal name would put a second name for one shape at the boundary, which src/types/AGENTS.md warns against. InternalProjectSlug stays on the internal lookup tables, where its derivation from PROJECTS is the honest thing to say.

The pin, and where it belongs

The predicate reads PROJECTS at runtime and claims ProjectSlug at the type level. That is honest only while the two name the same set, and nothing in the repo said so.

They are not two independent lists that might disagree: accessPlugin.ts:151 feeds getProjectSlugs() — Object.keys(PROJECTS) — into the ProjectSlug jsonSchema definition. So the one drift reachable is a stale payload-types.ts: someone edits PROJECTS and skips pnpm generate:types.

That is a "you forgot to regenerate" check, so it belongs at the declaration, inside the gate the build runs:

} as const satisfies Record<ProjectSlug, unknown>

Confirmed failing first, both directions, with this repo's tsc:

Drift pnpm typecheck
A key added to PROJECTS that the CMS schema lacks projects.ts(91,3): error TS2353
A key renamed, so the schema's slug is missing projects.ts(77,3): error TS2353, plus 18 consumer errors

Both name projects.ts itself, at the line being edited. An earlier revision of this PR put the same claim in tests/unit/is-valid-project.spec.ts as an Equals type assertion. It worked, but it ran only in pnpm typecheck:tests, needed a barrel-bypassing deep import of InternalProjectSlug, and stated the drift as a convention between two lists rather than a regeneration someone skipped. The spec keeps its three runtime cases.

The refine had to move to the field

On the object, .refine gated the parse but left currentProject as string | null in parsed.data — so a predicate there would establish nothing for the caller, and the cast would have survived. On the field, the predicate flows into the parsed type.

The 400 is unchanged: zod fills a field refine's path in, so the explicit path: ['currentProject'] was redundant rather than load-bearing. set-project-endpoint.int.spec.ts covers all three 400 cases and passes unchanged.

Acceptance criteria

  • isValidProject returns a type predicate
  • No as ProjectSlug remains at any of its three call sites — grep over all three files is empty
  • setProject's zod schema produces the narrow type, rather than a wide type plus a cast
  • The predicate's accepted set is unchanged — is-valid-project.spec.ts passes with no edits to its three cases, and the int spec's three 400s still hold

The four remaining as ProjectSlug casts in src/ are on getProjectsFromRoles and getProjectOptions, different functions the ticket does not name. Left alone, and recorded in #713 rather than folded in here.

Test results

  • Lean gate: ✓ pnpm lint, pnpm typecheck, pnpm typecheck:tests, pnpm test:unit (124 files, 1589 tests)
  • Targeted integration spec: ✓ set-project-endpoint (10)
  • Build: N/A — Railway builds the preview

Notes for reviewer

  • securityReview.triggerPattern does not match these paths, so /security-review is gated off. The change is type-level: Object.hasOwn(PROJECTS, value) is byte-identical, so the set the endpoint accepts cannot have moved.
  • Branched off main after fix(ci): restore Railway preview URLs, fail closed when none is found #703 merged, so this is among the first PRs whose smoke lane actually runs.

Closes #697

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

🤖 Generated with Claude Code

https://claude.ai/code/session_018wFscaRw5hg99qiqtBfqHv

`isValidProject` returned `boolean`, so all three callers followed it with
`as ProjectSlug` to tell the compiler what the check had just established.
A hand-maintained agreement between a runtime check and a cast — the same
shape as the `''` sentinel #671 removed, a fact the type system could hold
being held in a cast instead.

The predicate narrows to the **generated** `ProjectSlug`, not the internal
`keyof typeof PROJECTS`. #697 asked which name the call sites want, and
they want the generated one: it is what `payload.update` and both openapi
routes already declare, and `src/types/AGENTS.md` says not to introduce a
second name for one shape at a boundary. `InternalProjectSlug` stays where
it belongs, on the internal lookup tables derived from `PROJECTS`.

That narrowing is honest only while the two name the same set, and nothing
said so — they agreed by convention. `is-valid-project.spec.ts` now pins
them to each other with a type-level equality check. Confirmed failing
first: adding a project to `PROJECTS` that the CMS schema lacks turns
`pnpm typecheck:tests` red at that line. `pnpm typecheck` alone stays
green through it, so the pin is the only thing standing between a drifted
`PROJECTS` and a predicate claiming a slug Payload would reject.

In `setProject`, the `.refine` moves from the object onto the field. On the
object it gated the parse but left `currentProject` as `string | null`; on
the field the predicate flows into `parsed.data`, so the update needs no
cast. The 400 keeps its path, since zod fills a field refine's path in.

No user-visible behaviour changes.

Gate: lint, typecheck, typecheck:tests, test:unit (124 files, 1589 tests),
plus `set-project-endpoint.int.spec.ts` (10) — the accepted set is the
thing that must not move, and it did not.

Closes #697

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

railway-app Bot commented Sep 5, 2026 •

Copy link
Copy Markdown

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

Service Status Web Updated
SahajCloud ✅ Success (View Logs) Web Sep 5, 2026 at 7:13 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

Holistic. The diff matches #697: three casts named, three casts gone, and the ticket's one open question — which name the predicate should speak — answered in the body with reasoning rather than just a result. The behavioural claim holds; I checked it rather than taking it. Where the diff outgrows the problem is the pin: six lines of real change carry roughly twenty-three lines of type-level scaffolding and comment, and that scaffolding sits in a test-only lane when one satisfies clause at the PROJECTS declaration would do the same job inside the shipping typecheck. Nothing here blocks the merge.

Findings

  • should-fix — the drift pin belongs on PROJECTS as satisfies Record<ProjectSlug, unknown>, not in the spec; and ProjectSlug is generated from PROJECTS, so "agree by convention" misnames what can drift → tests/unit/is-valid-project.spec.ts:26
  • consider — four identical as ProjectSlug casts survive the PR, with no follow-up ticket recording them → src/plugins/access/config/projects.ts:267
  • consider — the schema comment spends four of its six lines on the design that was not chosen → src/collections/Managers/endpoints/setProject.ts:11-15

What was checked

  • #697 read first, then measured against the diff. All four acceptance criteria hold, including the unchanged accepted set.
  • Read whole: projects.ts, setProject.ts, lib/endpoints/parseBody.ts, is-valid-project.spec.ts, set-project-endpoint.int.spec.ts, accessPlugin.ts (where ProjectSlug is generated), src/types/AGENTS.md, .github/workflows/ci.yml.
  • The 400 really is unchanged. Ran the old object-level refine and the new field-level one side by side under this repo's zod 4.3.5 over six bodies — 'not-a-project', 'toString', '', a missing key, a number, an extra key. Byte-identical issues arrays each time: code: 'custom', path: ['currentProject'], same message. Worth stating that the int spec asserts only status === 400, so it would not have caught a path change — this comparison does.
  • The pin is enforced, not decoration: ci.yml runs pnpm typecheck:tests as its own step.
  • The satisfies alternative was compiled, not guessed: three probe files through this repo's tsc — clean today, TS2353 on an extra key, TS1360 on a missing one.
  • grep -rn isValidProject src/ tests/ — three call sites plus the two barrels, nothing else affected by the predicate return type. grep -rn "as ProjectSlug" src/ — four survivors, all on getProjectsFromRoles / getProjectOptions.
  • Ran green: pnpm typecheck, pnpm typecheck:tests, eslint --max-warnings 0 over the five changed files, is-valid-project.spec.ts (3 passed).
  • Not re-run: the author-side six-lens pass, and the int lane (needs Postgres).

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


Generated by Claude Code

Comment thread tests/unit/is-valid-project.spec.ts Resolved
Comment thread src/plugins/access/config/projects.ts Resolved
The pin belonged in the shipping typecheck, not the test lane.

`ProjectSlug` is generated FROM `PROJECTS` — `accessPlugin.ts:151` feeds
`getProjectSlugs()` into the jsonSchema definition. So the two never
"agree by convention": they agree by generation, and the only reachable
drift is a stale `payload-types.ts`, where someone edited `PROJECTS` and
skipped `pnpm generate:types`.

`as const satisfies Record<ProjectSlug, unknown>` states that at the
declaration, so the failure names the line being edited and fails
`pnpm typecheck` rather than `pnpm typecheck:tests` only. Confirmed on
both drift directions with this repo's tsc: an added key and a renamed
key each give TS2353 at `projects.ts`.

That deletes the spec-side `Equals` scaffolding and its barrel-bypassing
deep import of `InternalProjectSlug`. The three runtime cases are
unchanged.

Also trims the `setProject` schema comment to the one line that outlives
the first read. The design that was not chosen is in the PR body.

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

Copy link
Copy Markdown
Contributor Author

Revised. All three review threads applied and resolved.

The drift pin moved from the spec to the PROJECTS declaration: as const satisfies Record<ProjectSlug, unknown>. It now fails pnpm typecheck rather than pnpm typecheck:tests only, names projects.ts at the line being edited, and drops the Equals scaffolding plus its barrel-bypassing deep import. Confirmed failing first in both directions — an added key and a renamed key each give TS2353 there.

The comment was also wrong about what can drift. ProjectSlug is generated from these same keys, so the only reachable case is a stale payload-types.ts. It says that now.

The four surviving as ProjectSlug casts stay out: #697 named three call sites, and widening this PR would put two refactors in one diff. #713 records them, with the one-line signature change on each function.

Gate green: lint, typecheck, typecheck:tests, test:unit (124 files, 1589 tests).

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


Generated by Claude Code

@Ardnived Ardnived removed the awaiting label Sep 6, 2026
@sydevs-bot
sydevs-bot merged commit 8e98cb2 into main Sep 6, 2026
7 checks passed
@github-project-automation github-project-automation Bot moved this from In progress to Done in SYDevelopers Technical Roadmap Sep 6, 2026
sydevs-bot pushed a commit that referenced this pull request Sep 10, 2026
Ardnived asked whether the keys of `PROJECTS` should simply be enforced as
`ProjectSlug` instead of being aliased. They already are — `as const
satisfies Record<ProjectSlug, unknown>` (#712) pins them bidirectionally.

The comment did not answer the question that follows: given that pin, why
keep the alias at all? Checked rather than assumed — deleting it and
writing `ProjectSlug` at all twelve positions passes `pnpm typecheck`. The
cost is two `as ProjectSlug` casts back in `src/` (projects.ts:125, :205),
where `Object.keys` and `Object.entries` widen a key to `string`, which is
the grep #713 requires to stay empty.

So the alias's remaining job is to be that cast target. The comment now
says so, in place of the vaguer "for indexing it and the tables below".

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01PMVYzkDeF2h7omX6isDqFH
sydevs-bot pushed a commit that referenced this pull request Sep 10, 2026
@Ardnived chose the trade on the review thread: one fewer name for the
same shape, at the price of two `as ProjectSlug` casts back in `src/`.

`projects.ts` now writes the generated `ProjectSlug` at all twelve
positions. The two casts are where `Object.keys` and `Object.entries`
widen a key to `string` — `PROJECT_TO_COLLECTIONS`' reducer and
`getProjectSlugs`. Both carry a one-line comment naming the widening,
since the alias's doc comment used to carry that fact.

`getProjectSlugs` now names the type its own output generates. That is
sound rather than circular: the `satisfies Record<ProjectSlug, unknown>`
clause on `PROJECTS` (#712) fails `pnpm typecheck` if `payload-types.ts`
ever goes stale against these keys, so the annotation cannot outlive the
values it describes.

This retires #713's acceptance criterion — `grep -rn "as ProjectSlug"
src/` is no longer empty, by the reviewer's explicit decision. The two
hits it returns are the only ones, and both are assertions over a
widened key, never a boundary restating its own type.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Pbm8oVh7Q9wQxmsvh3XuRb
sydevs-bot added a commit that referenced this pull request Sep 10, 2026
…ss config, deleting the internal alias (#746)

* refactor(access): return ProjectSlug from getProjectsFromRoles and getProjectOptions

Both functions returned `InternalProjectSlug` (`keyof typeof PROJECTS`) while
every consumer declares the generated `ProjectSlug`, so all four call sites
followed them with `as ProjectSlug`. The two unions are the same set, so the
casts asserted nothing — they read as though they did.

#697 and #712 fixed the same no-op on `isValidProject`'s three call sites and
named only those, leaving 3 of 7 instances done. This finishes the other four.

The claim is compiler-held, not asserted: #712 pinned `PROJECTS` with
`as const satisfies Record<ProjectSlug, unknown>`, so a stale
`payload-types.ts` fails `pnpm typecheck` at the declaration. Confirmed by
renaming a `PROJECTS` key — typecheck goes red across the consumers rather
than accepting the drift.

`InternalProjectSlug` stays on the internal lookup tables, where deriving
from `PROJECTS` is the honest thing to say (src/types/AGENTS.md).

No runtime change: only annotations and cast deletions.

Closes #713

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

* refactor(access): make InternalProjectSlug module-private to projects.ts

The first commit left `getProjectsFromRoles` declared `ProjectSlug[]` while
its body still accumulated into `new Set<InternalProjectSlug>()`, one line
down — the seam moved to the signature rather than going away.

`getRoleProject` is the reason. It returns a project outward, so a consumer
compensated: `PermissionsTable.tsx:69` does the same `InternalProjectSlug`
to `ProjectSlug` conversion inside a type predicate. That is an eighth
instance of the pattern this ticket exists to finish, and the ticket's
`grep "as ProjectSlug"` criterion cannot see it — a predicate, not a cast.

Moving `getRoleProject` to `ProjectSlug | undefined` removes the last three
uses of the internal name outside `projects.ts`, so the type is no longer
exported. The rule that decides which signatures use which name was in no
comment, only in three PR bodies — it is now stated at the declaration:
returns speak `ProjectSlug`, parameters keep the internal name, because the
unions are identical and only returns force a call site to re-assert.

The `PermissionsTable` predicate still compiles and still strips `undefined`.
It is now a plain narrowing rather than a rename, and is left alone.

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

* docs(access): name the one return that keeps the internal slug type

The doc comment claimed every outward return speaks `ProjectSlug`.
`getProjectSlugs` does not, and should not: `accessPlugin.ts` builds the
`ProjectSlug` jsonSchema from its output, so declaring it as the type it
generates would state the conclusion. Named as the deliberate exception.

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

* fix(access): drop the redundant ProjectSlug type predicate

`getRoleProject` now returns `ProjectSlug`, so the hand-written
`(project): project is ProjectSlug` at PermissionsTable.tsx:69 no longer
crosses a type boundary — it only strips `undefined`, which TypeScript 5.7
infers on its own (inferred type predicates, 5.5+). This was the eighth
instance of the shape #713 set out to delete, at the site the PR body names.

The `ProjectSlug` import goes with it; `RoleSlug` stays.

Verified rather than assumed: replacing the filter with a non-narrowing
predicate turns `pnpm typecheck` red at the two `getProjectIcon` /
`getProjectLabel` call sites, so the compiler is holding the narrowing
instead of the annotation.

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

* docs(access): cut the InternalProjectSlug comment to what code cannot say

The comment answered three questions in 18 of this PR's 30 added lines, and
the first of them — a derived alias stays local and is never re-exported —
is already stated in `src/types/AGENTS.md`, so restating it here held
nothing.

What survives is the part a reader cannot recover from the code: that
returns use the generated `ProjectSlug` while parameters keep this name, and
that `getProjectSlugs` is the one return that keeps it because
`accessPlugin.ts` builds the `ProjectSlug` jsonSchema from its output.

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

* docs(access): say why the internal slug alias is not just ProjectSlug

Ardnived asked whether the keys of `PROJECTS` should simply be enforced as
`ProjectSlug` instead of being aliased. They already are — `as const
satisfies Record<ProjectSlug, unknown>` (#712) pins them bidirectionally.

The comment did not answer the question that follows: given that pin, why
keep the alias at all? Checked rather than assumed — deleting it and
writing `ProjectSlug` at all twelve positions passes `pnpm typecheck`. The
cost is two `as ProjectSlug` casts back in `src/` (projects.ts:125, :205),
where `Object.keys` and `Object.entries` widen a key to `string`, which is
the grep #713 requires to stay empty.

So the alias's remaining job is to be that cast target. The comment now
says so, in place of the vaguer "for indexing it and the tables below".

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

* fix(access): delete the InternalProjectSlug alias

@Ardnived chose the trade on the review thread: one fewer name for the
same shape, at the price of two `as ProjectSlug` casts back in `src/`.

`projects.ts` now writes the generated `ProjectSlug` at all twelve
positions. The two casts are where `Object.keys` and `Object.entries`
widen a key to `string` — `PROJECT_TO_COLLECTIONS`' reducer and
`getProjectSlugs`. Both carry a one-line comment naming the widening,
since the alias's doc comment used to carry that fact.

`getProjectSlugs` now names the type its own output generates. That is
sound rather than circular: the `satisfies Record<ProjectSlug, unknown>`
clause on `PROJECTS` (#712) fails `pnpm typecheck` if `payload-types.ts`
ever goes stale against these keys, so the annotation cannot outlive the
values it describes.

This retires #713's acceptance criterion — `grep -rn "as ProjectSlug"
src/` is no longer empty, by the reviewer's explicit decision. The two
hits it returns are the only ones, and both are assertions over a
widened key, never a boundary restating its own type.

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

---------

Co-authored-by: Claude <[email protected]>

This branch was successfully deployed

No deployments
sahajcloud / SahajCloud-pr-712 — 41a5160e 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.

refactor(access): make isValidProject a type predicate, deleting three ProjectSlug casts

3 participants