You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat: public access context, threaded through api, client, search-cms, MCP, hooks and events - #26
Merge chain: step 10 of 24. Base: main. Merge it right after #33. The branch is main3b4a112, then one commit for each PR before this one in the merge order, then this PR's own commit a2c8059 (tree 74b8b62).
What
The access context is public API.@tanstack/cms-core/server exports:
the AccessContext and actor types;
the builders publicContext(), accessContext({ session, mode, agent }), systemContext(reason, { actor }) (reason required), previewContext, previewTokenContext and withMode;
the checks canReadCollection, canReadDocument, assertReadable and redactFor;
cmsAs(cms, ctx) and every …As(ctx, …) read verb.
Hooks and events get the context.
postRead/afterRead receive { doc, id, access }.
Event handlers get { event, cms, access }, where access is a system context naming the handler.
The outbox client reads as that context.
runOp(name, input, actor, access) and cmsTools(actor, { access }) take an explicit context. A preview context can't run ops.
@tanstack/cms-api.
requestAccessContext(request) builds the per-request context. The x-cms-read-mode header can narrow it. The default is edit, the widest the caller may read.
ops.ts reads through the context instead of doing its own read checks.
WebMCP and /api/mcp run as an agent context per request, and cached servers never hold a caller's context.
@tanstack/cms-api/start builds the context from getRequest(). It also exports getCmsAccessContext({ mode? }) for app server functions; mode defaults to live.
Client.createCmsClient({ mode }) sends the read mode and keys its cache by it.
Search.createCmsSearch({ access }) searches as that context, defaulting to publicContext().
Old forms keep working until 2c removes them: session/publishedOnly/publicRead, runWithSession and createCmsSearch({ session }).
Docs.building-pages.md no longer says "the store skips the session" and is rewritten around cmsAs(cms, ctx). cms-core-server.md gains an "Access contexts" section, and the API, client and hook reference pages are updated.
Changeset: minor for core, api, client and search-cms.
Breaking changes
CmsOpContext now requires access. Code that builds one by hand must add access: accessContext({ session, mode: 'edit' }). In this repo, only app/base's tests did.
getPublicDocument now skips a draft the caller can't read and returns the next match. Before, that draft hid the match and the call returned null.
Assumptions (tasks 8–12 defaults)
Actor shape (task 8 not reviewed). I used the most conservative mapping, derived from today's CmsSession: user | agent | system | scheduler | import | public | preview.
An agent is a session with x-cms-agent or an explicit agent name, and keeps that session's permissions.
system, scheduler and import require a reason and bypass every rule.
Recorded actor names are unchanged (mcp, mcp:<agent>, webmcp, content-agent).
Read modes. Request contexts default to edit, so CMS readers still see drafts as before. getCmsAccessContext() defaults to live.
Divergence from plan
cmsAs(cms, ctx) stays a function, not cms.as(ctx). It also accepts the defineCms() result, because that's what apps hold.
Write checks still use the session, and write hooks get no context yet. Writes are phase 5.
runWithSession still wraps op runs and MCP, so app ops calling currentSession() keep working. 2c removes it.
A legacy read without a session still maps to a system context. The fail-closed switch is 2c.
No starters or examples are migrated yet.
How verified
New tests:
core access-context.test.ts and an events test;
api access-context.test.ts, which also checks that a system context is refused over HTTP;
search-cms and client mode tests.
pnpm test:pr passes on 137 affected tasks except the cms-api oxlint error that main already has. Core ran 556 tests and the API ran 202. The Postgres tests were skipped because the VM had no Docker.
pnpm test:dts is clean.
Merge chain (local verification, CI down)
This PR's commits are squashed into one commit on the linear merge chain. That commit holds this PR's own changes, resolved against every PR before it, with the integration rehearsal's fixes for this PR applied. One conflict, in the docs/schema/building-pages.md frontmatter. The replay starts from e568873.
The chain tip's tree is exactly the integration rehearsal plus #46 (c4e6d7c), and the tip passes the full local suite. I simulated merging every PR in order, with a merge commit or a fast-forward at each step, and every step is clean. A plain squash of the next PR would conflict, because the squash commit isn't an ancestor of the next branch. To squash instead, first rebase the branch's one commit onto the new main; that rebase is always clean.
Summary by CodeRabbit
New Features
Added live, preview, and edit read modes across CMS requests, clients, and search, with results filtered according to the caller’s access.
Added context-aware CMS reads, revision history, and read-hook access details.
Client cache keys now distinguish configured read modes.
Bug Fixes
Public document lookups skip unreadable drafts so they no longer hide a matching readable document.
Documentation
Updated API, integration, and schema guides with access-context and read-mode guidance. Legacy session-based read options remain supported but are deprecated.
The pull request threads explicit access contexts through core reads, operations, events, API requests, client read modes, caching, and search. It adds context-aware APIs, preserves deprecated session options, and adds tests and documentation.
Changes: Access context contracts and core reads
Layer / File(s)
Summary
Access context contracts and builders packages/core/src/lib/access-context.ts, packages/core/src/server/access-context.ts, packages/core/src/ops.ts, packages/core/src/schema.ts
The core defines access-context types, builders, read-mode parsing, validation, actor identities, and access-aware hook arguments. CmsOpContext.access is required.
Bound reads and ...As functions use access contexts for filtering, redaction, revisions, relations, translations, and read hooks.
Events and tools packages/core/src/server/dispatcher.ts, packages/core/src/server/cms-tools.ts, packages/core/src/events.ts
Event handlers receive system access contexts. Tool calls select runtime or configured access contexts before operation dispatch.
API, client, and search integration
Layer / File(s)
Summary
API request and operation access packages/api/src/ops.ts, packages/api/src/start.ts, packages/api/src/http.ts, packages/api/src/mcp.ts
Requests resolve access contexts from sessions, options, and x-cms-read-mode. Operations, MCP, WebMCP, event visibility, and media checks use those contexts.
Client read modes and cache keys packages/client/src/client.ts, packages/client/src/keys.ts
Clients can send a configured read mode. Mode-specific cache keys distinguish live and edit reads.
Search accepts an explicit access context and uses context-aware query and grouping functions. Legacy session and read options remain supported and are deprecated.
Supporting updates
Layer / File(s)
Summary
Application helpers and tests app/base/src/schema-registry.server.ts, app/base/tests/*, packages/api/tests/*, packages/core/tests/*, packages/client/tests/*
Application and test contexts now include edit-mode access contexts. Tests cover read modes, authorization, hooks, events, operations, client caching, and search totals.
Documentation and release metadata .changeset/access-context-threading.md, docs/api/*, docs/architecture/*, docs/schema/*, docs/tutorials/*, .claude/skills/search-integration/SKILL.md
Reviewing files that changed from the base of the PR and between 0decb56 and ad79df1.
Files selected for processing: 50
.changeset/access-context-threading.md
.claude/skills/search-integration/SKILL.md
app/base/src/schema-registry.server.ts
app/base/tests/base.test.ts
app/base/tests/schema-registry.test.ts
app/base/tests/worker.test.ts
docs/api/cms-api.md
docs/api/cms-client.md
docs/api/cms-core-server.md
docs/architecture/events-and-hooks.md
docs/schema/building-pages.md
docs/schema/collections-and-fields.md
docs/tutorials/faceted-search.md
packages/api/src/http.ts
packages/api/src/index.ts
packages/api/src/interface-ops.ts
packages/api/src/mcp.ts
packages/api/src/media-ops.ts
packages/api/src/ops.ts
packages/api/src/start.ts
packages/api/tests/access-context.test.ts
packages/client/src/client.ts
packages/client/src/index.ts
packages/client/src/keys.ts
packages/client/tests/client.test.ts
packages/core/src/events.ts
packages/core/src/index.ts
packages/core/src/lib/access-context.ts
packages/core/src/ops.ts
packages/core/src/schema.ts
packages/core/src/server.ts
packages/core/src/server/access-context.ts
packages/core/src/server/cms-as.ts
packages/core/src/server/cms-ops.ts
packages/core/src/server/cms-tools.ts
packages/core/src/server/dispatcher.ts
packages/core/src/server/interface-schedules.ts
packages/core/src/server/interfaces.ts
packages/core/src/server/store.ts
packages/core/src/server/store/internal.ts
packages/core/src/server/store/query.ts
packages/core/src/server/store/read.ts
packages/core/src/server/store/review.ts
packages/core/src/server/store/revisions.ts
packages/core/tests/access-context.test.ts
packages/core/tests/events.test.ts
packages/core/tests/interface-schedules.test.ts
packages/core/tests/interfaces.test.ts
packages/search-cms/src/index.ts
packages/search-cms/tests/cms.test.ts
Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.
…, MCP, hooks and events (#26)
Chain step 10 of the linear merge chain onto main 3b4a112: this PR's own changes, resolved against the PRs before it in the merge order.
Co-authored-by: jherr <jherr@pobox.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements cms-refactor-plan.md § Phase 2b (item 4, "Access context for reads"; 13c).
Merge chain: step 10 of 24. Base:
main. Merge it right after #33. The branch ismain3b4a112, then one commit for each PR before this one in the merge order, then this PR's own commita2c8059(tree74b8b62).What
@tanstack/cms-core/serverexports:AccessContextand actor types;publicContext(),accessContext({ session, mode, agent }),systemContext(reason, { actor })(reason required),previewContext,previewTokenContextandwithMode;canReadCollection,canReadDocument,assertReadableandredactFor;cmsAs(cms, ctx)and every…As(ctx, …)read verb.postRead/afterReadreceive{ doc, id, access }.{ event, cms, access }, whereaccessis a system context naming the handler.runOp(name, input, actor, access)andcmsTools(actor, { access })take an explicit context. A preview context can't run ops.@tanstack/cms-api.requestAccessContext(request)builds the per-request context. Thex-cms-read-modeheader can narrow it. The default isedit, the widest the caller may read.ops.tsreads through the context instead of doing its own read checks./api/mcprun as an agent context per request, and cached servers never hold a caller's context.@tanstack/cms-api/startbuilds the context fromgetRequest(). It also exportsgetCmsAccessContext({ mode? })for app server functions;modedefaults tolive.createCmsClient({ mode })sends the read mode and keys its cache by it.createCmsSearch({ access })searches as that context, defaulting topublicContext().session/publishedOnly/publicRead,runWithSessionandcreateCmsSearch({ session }).building-pages.mdno longer says "the store skips the session" and is rewritten aroundcmsAs(cms, ctx).cms-core-server.mdgains an "Access contexts" section, and the API, client and hook reference pages are updated.Breaking changes
CmsOpContextnow requiresaccess. Code that builds one by hand must addaccess: accessContext({ session, mode: 'edit' }). In this repo, onlyapp/base's tests did.getPublicDocumentnow skips a draft the caller can't read and returns the next match. Before, that draft hid the match and the call returnednull.Assumptions (tasks 8–12 defaults)
CmsSession:user | agent | system | scheduler | import | public | preview.x-cms-agentor an explicit agent name, and keeps that session's permissions.system,schedulerandimportrequire a reason and bypass every rule.mcp,mcp:<agent>,webmcp,content-agent).edit, so CMS readers still see drafts as before.getCmsAccessContext()defaults tolive.Divergence from plan
cmsAs(cms, ctx)stays a function, notcms.as(ctx). It also accepts thedefineCms()result, because that's what apps hold.runWithSessionstill wraps op runs and MCP, so app ops callingcurrentSession()keep working. 2c removes it.How verified
access-context.test.tsand an events test;access-context.test.ts, which also checks that a system context is refused over HTTP;modetests.pnpm test:prpasses on 137 affected tasks except thecms-apioxlint error thatmainalready has. Core ran 556 tests and the API ran 202. The Postgres tests were skipped because the VM had no Docker.pnpm test:dtsis clean.Merge chain (local verification, CI down)
This PR's commits are squashed into one commit on the linear merge chain. That commit holds this PR's own changes, resolved against every PR before it, with the integration rehearsal's fixes for this PR applied. One conflict, in the
docs/schema/building-pages.mdfrontmatter. The replay starts frome568873.The chain tip's tree is exactly the integration rehearsal plus #46 (
c4e6d7c), and the tip passes the full local suite. I simulated merging every PR in order, with a merge commit or a fast-forward at each step, and every step is clean. A plain squash of the next PR would conflict, because the squash commit isn't an ancestor of the next branch. To squash instead, first rebase the branch's one commit onto the newmain; that rebase is always clean.Summary by CodeRabbit