Skip to content

fix(meditations): version history 400s — a read beforeOperation hook also fires on findVersions - #768

Merged
sydevs-bot merged 9 commits into
mainfrom
claude/fix-745-versions-read-hook-guard
Sep 12, 2026
Merged

sydevs-bot merged 9 commits into
mainfrom
claude/fix-745-versions-read-hook-guard

Conversation

@sydevs-bot

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

Copy link
Copy Markdown
Contributor

Summary

Changes

  • src/lib/utilities/versionsRead.ts — new isVersionsRead. Tells a versions read from a document read by the draft argument, which only find/findByID carry.
  • src/collections/Meditations/hooks/filterMeditationsByLocale.ts — stops appending { locale: { equals } } to a versions query.
  • src/collections/Events/hooks/excludeFinishedEvents.ts — same, for schedule.lastDate.
  • src/collections/AGENTS.md — the mapping written down, where the next hook author reads it, plus the rule for choosing between declining a filter and translating it.
  • tests/int/meditations.int.spec.ts — three versions cases, one of them through the access gate as a meditations-editor manager.

Preview

Test results

  • Lean gate: ✓ pnpm lint, pnpm typecheck, pnpm typecheck:tests, pnpm test:unit (1683 passed), re-run at f9c16f1
  • Targeted specs: meditations.int + events.int + role-based-access.int — 100 passed
  • Full integration lane: ✓ 1213 passed (73 files), before the review rounds
  • Build: N/A — Railway builds the preview

Manual verification

  1. Open a meditation in the admin panel and click the Versions tab. It lists rows instead of erroring.

Notes for reviewer

The one open question is settled. @Ardnived asked whether the draft check is the right approach (thread) and answered "leave it as it is" on 2026-09-12. No code changed in response. The evidence is below. For the record, the only design that removes the check is moving the meditations locale scoping into access.read — which reclassifies a scoping filter as authorization and drops it for overrideAccess: true callers. excludeFinishedEvents cannot move there at all: its schedule.lastDate opt-out ("a client asking for past events deliberately gets them") is not expressible in access control.

The discriminator reads a Payload-internal argument shape — and Payload's own code decides it, not ours. There is no public "is this a versions read" signal: the hook receives operation: 'read' for all four operations, and 'id' in args only separates findByID from find. draft is the one argument that splits them. All eight read call sites live in payload 3.86.0's dist, under collections/endpoints/ and collections/operations/local/. The four document reads name draft as a literal key in the object they build — so the key is present even when its value is undefined, as a REST query with no ?draft= gives — and the four versions reads never name it. buildBeforeOperation.js marks the mapping itself // TODO: v4 should not need this mapping.

It fails in the widening direction, which is why the false branch is pinned too. If an upgrade stopped supplying draft, both filters would silently switch off rather than error. Every meditations locale case passed draft: true by hand, which would have hidden that, so there is now one that leaves draft to Payload. listIds in the events suite already covered it by omitting draft; a comment now says so.

Adversarial review round 1: three findings, all adopted. One should-fix — the ticket's first acceptance criterion was unpinned, because every versions case passed overrideAccess: true and so skipped readVersions. Two consider — the repo answered this hazard twice with no rule for choosing, and one 3-line predicate carried four prose restatements.

Two things I found and did not fix — flagging rather than widening the PR:

  1. ensureWebPathDeps is a third read hook that also fires on a versions read. It mutates select, not where, and sanitizeSelect drops unknown keys rather than throwing, so it is benign today. It is out of this ticket's scope, but it is the same shape.
  2. Neither hook covers readDistinct (findDistinct), so the admin's list-filter dropdown on meditations is not locale-scoped. Pre-existing, unchanged here. Say the word and I will file either.
Detail

What the access-gate case does and does not pin. It needs the hook's guard: deleting that turns all three meditations versions cases red with The following path cannot be queried. It does not pin withVersionHistoryAccess's Where translation — meditations-editor's update grant is unconditional, so disabling the translation leaves the case green. Verified both ways, and the case comment says so and points at role-based-access.int.spec.ts, which pins the translation on pages. A second half with a path-editor manager asserts the same read is refused, so the positive half cannot pass with the gate silently skipped.

The ticket's framing of the events half was slightly off, and the spec says so. It expected a client's findVersions on events to be getting a 400 where a 403 is honest. In fact findVersionsOperation runs executeAccess(access.readVersions) before validateQueryPaths, and no client role grants events: update, so a client was already getting the 403. The 400 was reachable only with overrideAccess: true — an internal read forwarding a client req — and that is the shape the regression pin uses. The access-gate case is kept beside it, with a comment saying plainly that it passes before the fix as well.

Only findVersions ever reached the 400. Both hooks already exited on 'id' in args, which findVersionByID carries. The helper still answers for both, so a hook that does not check id is covered.

Declining versus translating is now a stated rule, not a size judgement. src/collections/AGENTS.md: a filter that scopes a listing (locale, finished events) is declined, because a versions read is not that listing and nobody's authority widens. A filter that decides authority is translated, never declined, because declining it widens who may read a version row — which is exactly what withVersionHistoryAccess does with Payload's appendVersionToQueryKey.

Rejected alternative: a wrapper or a plugin-level guard. usagePlugin is the only place that injects beforeOperation hooks across collections, and a blanket versions-skip there would be wrong — validateClientQueryParamsHook and the usage counter should keep firing on a versions read. The hazard is narrower than "read hooks", and grep "args.where =" returns exactly the two hooks fixed here.

Why no unit test for the helper. A unit test would assert the object shape this code already assumes, and agree with itself. The pin is a real findVersions through a booted Payload; a version bump that changes the argument shape turns it red. Both branches were verified by reintroducing the defect: deleting the guard fails the versions cases with The following path cannot be queried, and forcing the helper to return true fails the false-branch case.

Where the rule lives now. The helper's doc comment carries the argument table and the call-site evidence for it, and nothing else. src/collections/AGENTS.md owns the rule, the consumer list, the silent-failure warning, and what to pin when a third hook needs the guard.

Fixture pre-mortem. The specs assume both collections enable versions — checked against src/collections/Meditations/Meditations.ts:136 and src/collections/Events/Events.ts:105, not assumed. The false-branch case needs published documents, because meditations fixtures default to draft; it creates its own so the German one is excluded by the locale filter rather than by being a draft.

No migration. Hooks only, no schema change.

Security review skipped — no path in the diff matches securityReview.triggerPattern in .claude/workflow.json. The access reasoning is in the Summary instead.

Closes #745

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

🤖 Generated with Claude Code

https://claude.ai/code/session_01LA8ocXYFkcRKFCyLLN2vJq

…filters

Payload maps `find`, `findByID`, `findVersions` and `findVersionByID` onto
the single `read` hook operation (`operationToHookOperation`). So a
`beforeOperation` hook guarding on `operation === 'read'` also runs on a
versions read, and the `where` it appends is applied to the *versions*
collection, where a document's fields live under `version.` and its id is
`parent`.

`filterMeditationsByLocale` appended `{ locale: { equals: req.locale } }`,
which is not a queryable path there. Query validation runs before the read
and regardless of `overrideAccess`, so `payload.findVersions({ collection:
'meditations' })` threw `QueryError: The following path cannot be queried:
locale` for every caller, and the admin version-history tab for meditations
was broken. `excludeFinishedEvents` appended `schedule.lastDate` the same
way, for API clients.

`'id' in args` did not stop either: it separates `findByID` from `find`, not
a versions read from either. The new `isVersionsRead` tells them apart by
the `draft` argument, which every `find`/`findByID` call site passes (both
the Local API and the REST handler, in both operations) and neither versions
read passes at all. It gates on the `read` hook operation, so `count` — which
has no `draft` either — is untouched, and `countVersions` maps to its own
hook operation.

Both filters are scoping, not authorization: `locale=all` and an explicit
`schedule.lastDate` clause already opt any caller out of them, and
`access.readVersions` (#719) is unchanged. So declining them on a versions
read widens no one's authority.

The specs are pinned by reintroducing the defect: with the guard deleted,
the three new cases fail with the QueryError above, and the two
"guards the guard" cases stay green.

Closes #745

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01YTcPYAyJXziiqJFJwUmPCt
Collapse the #745 rationale into one place. The mechanism now lives in
`isVersionsRead`'s doc comment; the two hooks and both specs point at it in
one line each instead of restating it.

Also drop two new test cases that duplicated assertions already sitting a
few lines above them in the same `describe` ("still filters an ordinary
find" / "still filters an ordinary client list read"), and align the guard's
placement in both hooks — after the operation gate, which now type-checks
since the helper takes the hook's own operation union.

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

An adversarial pass found the guard under-tested in the direction that fails
silently. `isVersionsRead` reads an argument Payload supplies, so if an
upgrade stopped supplying it, both filters would switch off for ordinary
reads rather than erroring — a wrong result, not a 400. Every meditations
locale case passed `draft: true` by hand, which makes the test true whatever
Payload does, so nothing there would have noticed.

Adds a case that leaves `draft` to Payload, on published fixtures so the
German meditation is excluded by the filter rather than by being a draft.
Verified by making the helper return `true` unconditionally and watching it
go red. `listIds` in the events suite already covered this by omitting
`draft`; a comment now says so, since adding one would quietly remove the pin.

Also corrects an over-claim: only `findVersions` ever reached the 400. Both
hooks already exited on `'id' in args`, which `findVersionByID` carries.

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

The mapping is the part a future hook author cannot see from the code, and
this is the second hook to trip on it. Adds the rule to
`src/collections/AGENTS.md` with the guard's shape, names the other `read`
hooks that legitimately keep firing on a versions read, and warns that the
guard fails silently in the widening direction.

Also lists `versionsRead` in `src/AGENTS.md`'s `utilities/` inventory, and
records the new coverage in `tests/COVERAGE.md`.

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

railway-app Bot commented Sep 12, 2026 •

Copy link
Copy Markdown

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

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

@sydevs-bot

sydevs-bot commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor Author

🤖 no session running

recent dispatches
  • 16:02Z address-review · attempt 1 · session · done
  • 16:02Z address-review · attempt 1 · session
  • 05:23Z address-review · attempt 1 · session · done
  • 05:23Z address-review · attempt 1 · session
  • 03:52Z address-review · attempt 1 · session · done

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

@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 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 matches the ticket. Two hooks, one shared guard, no smuggled refactor.

I re-derived the discriminator from payload 3.86.0, and it holds. find and findByID have two call sites each, and all four pass draft. Neither versions read passes it. Declining is safe too: readVersions already scopes meditations versions via withVersionHistoryAccess. Both open questions are about shape, not correctness.

Findings

  • should-fix — the ticket's first acceptance criterion is unpinned. tests/int/meditations.int.spec.ts:356
  • consider — a second answer to a hazard this repo already answers. src/lib/utilities/versionsRead.ts:48
  • consider — one 3-line predicate, four prose restatements. src/lib/utilities/versionsRead.ts:5

What was checked

Ticket #745. The whole diff. Payload 3.86.0 dist: operationToHookOperation, all four callers of findOperation and findByIDOperation, both versions handlers. Read whole: both hooks, ensureWebPathDeps (benign as claimed), accessConfigs.ts, both spec blocks.

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


Generated by Claude Code

Comment thread tests/int/meditations.int.spec.ts Resolved
Comment thread src/lib/utilities/versionsRead.ts Resolved
Comment thread src/lib/utilities/versionsRead.ts Resolved
@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
AC 1 of #745 asked for a versions read that resolves for a manager at a
single locale. Both new cases passed `overrideAccess: true`, which skips
`readVersions`, so the suite pinned query validation but never the path
the bug was reported on.

Adds one case with `overrideAccess: false` and a `meditations-editor`
manager, plus a negative half proving the gate is consulted rather than
skipped. Verified by deleting the hook's guard (red, "path cannot be
queried") and by disabling the `Where` translation in
`withVersionHistoryAccess` (still green — that grant is unconditional, so
the comment says the translation is pinned elsewhere rather than here).

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

The repo now answers "a document `Where` reaching a versions read" twice —
`withVersionHistoryAccess` translates it, these two hooks decline it — and
the AGENTS.md section gave the next hook author no rule for choosing.

Adds the rule: a filter that scopes a listing is declined, a filter that
decides authority is translated, because declining that one widens access.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01M9ax3qykqyrNQ4dW7oC9W9
One 3-line predicate carried four prose restatements — a 43-line doc
comment, a 49-line AGENTS.md section, an `src/AGENTS.md` bullet, and both
hooks' own comments. Roughly 110 of this PR's 255 added lines said the
same thing.

Keeps the argument table, which cannot live anywhere else, plus a pointer
to the AGENTS.md section that now owns the rule. Nothing is lost: the
consumers, the silent-failure warning, and what to pin are all stated
there.

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

Copy link
Copy Markdown
Contributor Author

Three threads, all adopted. Pushed e773e3a, which also merges current main.

One correction, in the first thread: that case does not pin withVersionHistoryAccess's translation. The grant is unconditional, verified by disabling it.

Gate green: lint, both typechecks, 1683 unit. meditations.int + events.int + role-based-access.int — 100 passed.

🤖 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 04:09
@sydevs-bot
sydevs-bot requested a review from Ardnived September 12, 2026 04:09
Comment thread src/lib/utilities/versionsRead.ts Resolved
@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
The old wording — "every `find` and `findByID` call site passes `draft`" —
read as a claim about our callers, which makes the discriminator look
caller-dependent and so brittle. It is not.

All eight read call sites live in payload's dist, under
`collections/endpoints/` and `collections/operations/local/`. The four
document reads name `draft` as a literal key in the object they build, so
the key is present even when its value is `undefined` — a REST query with
no `?draft=` still carries it. The four versions reads never name it.

Comment only. No behaviour change.

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

Copy link
Copy Markdown
Contributor Author

One thread, answered rather than adopted. Pushed f9c16f1 — comment only, no behaviour change.

  • Is the draft check brittle? → rebutted, with a question back. Payload owns all 8 read call sites: the 4 document reads name draft as a literal key, present even when undefined. The 4 versions reads never do. buildBeforeOperation.js marks the mapping // TODO: v4 should not need this mapping.

f9c16f1 moves that evidence into the doc comment, replacing wording that read as a claim about our callers.

Your call, and the PR waits on it. The one design that removes the check is moving locale scoping into access.read — authorization, not scoping, and gone for overrideAccess: true callers. excludeFinishedEvents cannot move there at all.

Gate green at f9c16f1: lint, both typechecks, 1683 unit.

🤖 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
@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

One thread, settled by the reviewer. No commit, nothing pushed — the head stays f9c16f1.

The PR body no longer says this PR waits on an answer. Every other thread was already adopted and resolved in the earlier rounds.

🤖 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 merged commit 974316a into main Sep 12, 2026
45 of 46 checks passed
@sydevs-bot sydevs-bot moved this from Revising to Done in SYDevelopers Technical Roadmap Sep 12, 2026
@sydevs-bot
sydevs-bot deleted the claude/fix-745-versions-read-hook-guard branch September 12, 2026 16:06

This branch was successfully deployed

No deployments
sahajcloud / SahajCloud-pr-768 — f9c16f14 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(meditations): version history 400s — a read beforeOperation hook also fires on findVersions

3 participants