Repository navigation
refactor(access): make isValidProject a type predicate, deleting three ProjectSlug casts - #712
Conversation
`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
|
🚅 Deployed to the SahajCloud-pr-712 environment in sahajcloud
1 service not affected by this PR
|
sydevs-bot
left a comment
There was a problem hiding this comment.
🧐 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 onPROJECTSassatisfies Record<ProjectSlug, unknown>, not in the spec; andProjectSlugis generated fromPROJECTS, so "agree by convention" misnames what can drift →tests/unit/is-valid-project.spec.ts:26consider— four identicalas ProjectSlugcasts survive the PR, with no follow-up ticket recording them →src/plugins/access/config/projects.ts:267consider— 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(whereProjectSlugis 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-identicalissuesarrays each time:code: 'custom',path: ['currentProject'], same message. Worth stating that the int spec asserts onlystatus === 400, so it would not have caught a path change — this comparison does. - The pin is enforced, not decoration:
ci.ymlrunspnpm typecheck:testsas its own step. - The
satisfiesalternative was compiled, not guessed: three probe files through this repo's tsc — clean today,TS2353on an extra key,TS1360on 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 ongetProjectsFromRoles/getProjectOptions.- Ran green:
pnpm typecheck,pnpm typecheck:tests,eslint --max-warnings 0over 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
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
|
Revised. All three review threads applied and resolved. The drift pin moved from the spec to the The comment was also wrong about what can drift. The four surviving 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 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
@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
…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]>
Summary
isValidProjectreturnedboolean, so all three callers followed it withas ProjectSlugto tell the compiler what the check had just established. All three casts are gone.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.PROJECTSnow says it, and the statement was confirmed to fail on drift.No user-visible behaviour changes.
Changes
plugins/access/config/projects.tsisValidProject(value): value is ProjectSlug | null, andPROJECTSendsas const satisfies Record<ProjectSlug, unknown>collections/Managers/endpoints/setProject.ts.refinemoves from the object onto the field; the update's cast goesapp/(payload)/api/openapi.json/route.ts? projectParam : nullplugins/openapi/scalarPlugin.tstests/unit/is-valid-project.spec.tssatisfiesclauseWhich name the predicate should speak
The ticket asks this explicitly, so here is the reasoning rather than just the result.
InternalProjectSlugiskeyof typeof PROJECTS.ProjectSlugis 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
ProjectSlugbecause that is the name the call sites already declare:payload.update'sdatawants it, and both openapi routes annotate their local asProjectSlug | null. Narrowing to the internal name would put a second name for one shape at the boundary, whichsrc/types/AGENTS.mdwarns against.InternalProjectSlugstays on the internal lookup tables, where its derivation fromPROJECTSis the honest thing to say.The pin, and where it belongs
The predicate reads
PROJECTSat runtime and claimsProjectSlugat 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:151feedsgetProjectSlugs()—Object.keys(PROJECTS)— into theProjectSlugjsonSchema definition. So the one drift reachable is a stalepayload-types.ts: someone editsPROJECTSand skipspnpm generate:types.That is a "you forgot to regenerate" check, so it belongs at the declaration, inside the gate the build runs:
Confirmed failing first, both directions, with this repo's tsc:
pnpm typecheckPROJECTSthat the CMS schema lacksprojects.ts(91,3): error TS2353projects.ts(77,3): error TS2353, plus 18 consumer errorsBoth name
projects.tsitself, at the line being edited. An earlier revision of this PR put the same claim intests/unit/is-valid-project.spec.tsas anEqualstype assertion. It worked, but it ran only inpnpm typecheck:tests, needed a barrel-bypassing deep import ofInternalProjectSlug, 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,
.refinegated the parse but leftcurrentProjectasstring | nullinparsed.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
pathin, so the explicitpath: ['currentProject']was redundant rather than load-bearing.set-project-endpoint.int.spec.tscovers all three 400 cases and passes unchanged.Acceptance criteria
isValidProjectreturns a type predicateas ProjectSlugremains at any of its three call sites —grepover all three files is emptysetProject's zod schema produces the narrow type, rather than a wide type plus a castis-valid-project.spec.tspasses with no edits to its three cases, and the int spec's three 400s still holdThe four remaining
as ProjectSlugcasts insrc/are ongetProjectsFromRolesandgetProjectOptions, different functions the ticket does not name. Left alone, and recorded in #713 rather than folded in here.Test results
pnpm lint,pnpm typecheck,pnpm typecheck:tests,pnpm test:unit(124 files, 1589 tests)set-project-endpoint(10)Notes for reviewer
securityReview.triggerPatterndoes not match these paths, so/security-reviewis gated off. The change is type-level:Object.hasOwn(PROJECTS, value)is byte-identical, so the set the endpoint accepts cannot have moved.mainafter 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