Skip to content

Route token grant picker and mutations through the shard-aware service layer - #198

Merged
brylie merged 1 commit into
mainfrom
issue-188-token-shard-grants
Sep 3, 2026
Merged

Route token grant picker and mutations through the shard-aware service layer#198
brylie merged 1 commit into
mainfrom
issue-188-token-shard-grants

Conversation

@brylie

@brylie brylie commented Sep 3, 2026

Copy link
Copy Markdown
Owner

Summary

Closes #188 — the /settings/tokens grant picker read Documents/Collections straight off the default Y.Doc, so anything created via the service layer since #113/#120 (its own real shard) was silently invisible there even though the sidebar and MCP listing surfaces saw it fine. The route also owned token mutation, Space validation, actor construction, and audit calls directly, outside the service manifest/request-context conventions the rest of the app follows.

  • Added src/lib/services/tokens.tscreateToken/revokeToken, each a single function owning validate → mutate → audit, per service-layer.md. createToken validates every submitted allowedSpaceIds entry against the workspace's real Spaces (UnknownSpaceError), same behavior as before, just moved out of the route. spaces.ts's createSpace was the existing precedent for this shape; tokens.ts follows it (and, like spaces.ts, isn't registered in services/manifest.ts's MCP/UI wiring table — no MCP tool exposes token minting, so that table's parity check doesn't apply).
  • src/routes/settings/tokens/+page.server.ts now loads Documents/Collections via listDocuments/listCollections ($lib/services, shard-aware) with locals.requestContext.caller, matching +layout.server.ts/space/[spaceId]/+page.server.ts's existing pattern — no more route-local CURRENT_USER literal.
  • Preserved: one-time raw-token display, existing Space/Document/Collection grant behavior, create_token/revoke_token audit action names.
  • docs/specifications/service-layer.md §3's module layout now lists tokens.ts (previously only anticipated in prose) alongside spaces.ts, with a note on why neither needs MCP manifest registration.

Test plan

  • npm run check — typecheck clean
  • npm run lint — prettier + eslint clean
  • npm run test — 1010 tests pass, including new coverage: src/lib/services/tokens.test.ts (create/revoke audit-exactly-once, human vs. token-caller attribution, unknown-Space rejection with no partial token persisted) and a new regression test in src/routes/settings/tokens/page.server.test.ts that creates a Document and Collection through the service layer (therefore in their own real shards) and verifies both are listed by load().
  • npm run test:e2e:tier-a — 16 tests pass, unaffected.
  • Manually verified in the browser: created a Document through the normal UI flow (service layer, real shard), confirmed it now appears in the token page's "Allowed Documents" picker (previously it would have been silently omitted), then created and revoked a token scoped to it end-to-end.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added token creation and revocation from token settings.
    • Tokens can be scoped to specific documents, collections, and Spaces.
    • Token actions now record audit activity and preserve the initiating actor.
  • Bug Fixes

    • Invalid Space selections are rejected before a token is saved.
    • Token and content listings now consistently include items across separate workspace storage areas.

…e layer

The /settings/tokens page read Documents and Collections straight off the
default Y.Doc, so anything created via the service layer since #113/#120
(its own real shard) was silently missing from the grant picker. It also
defined its own CURRENT_USER and did Space validation/persistence/audit
inline in the route.

Adds src/lib/services/tokens.ts (createToken/revokeToken: validate ->
mutate -> audit, in one place) and points the route's load/actions at it
and at the shard-aware listDocuments/listCollections via
locals.requestContext.caller, matching the pattern already used by
+layout.server.ts and space/[spaceId]/+page.server.ts.

Closes #188

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@coderabbitai

coderabbitai Bot commented Sep 3, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Essentials

Run ID: ffdcd0fc-8a78-4631-bbc0-22ab1a76795b

📥 Commits

Reviewing files that changed from the base of the PR and between e2e7a19 and f8be5ec.

📒 Files selected for processing (6)
  • docs/specifications/service-layer.md
  • src/lib/services/index.ts
  • src/lib/services/tokens.test.ts
  • src/lib/services/tokens.ts
  • src/routes/settings/tokens/+page.server.ts
  • src/routes/settings/tokens/page.server.test.ts

Limit details: You’ve used all 2 included reviews currently available. Your 55 included PR review attempts over the past 7 days set your current allowance at 2 reviews per hour.


📝 Walkthrough

Walkthrough

The token settings flow now uses service-layer token operations and request callers. Token creation validates Space grants before persistence and records audits. The route loads Documents and Collections through shard-aware services, with regression coverage for sharded content.

Changes

Token settings service flow

Layer / File(s) Summary
Token service contracts and operations
src/lib/services/tokens.ts, src/lib/services/index.ts, src/lib/services/tokens.test.ts, docs/specifications/service-layer.md
Added token creation and revocation services, Space validation, audit recording, public input types, service exports, tests, and service-layer documentation.
Request-scoped settings integration
src/routes/settings/tokens/+page.server.ts, src/routes/settings/tokens/page.server.test.ts
The route uses request callers and shard-aware service queries. Tests cover request context, sharded Documents and Collections, and scoped tokens.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: ⚪ Minimal · up to f8be5

Token settings now use shard-aware resource listing and centralized token operations while retaining validation, auditing, and one-time token display behavior. No current merge-blocking risk remains.

Sequence Diagram(s)

sequenceDiagram
  participant Request
  participant SettingsRoute
  participant TokenService
  participant Persistence
  participant AuditLog
  Request->>SettingsRoute: Submit token action
  SettingsRoute->>TokenService: Create or revoke token with caller
  TokenService->>Persistence: Validate and persist token state
  Persistence-->>TokenService: Return token result
  TokenService->>AuditLog: Record one audit event
  TokenService-->>SettingsRoute: Return result or validation error
  SettingsRoute-->>Request: Return action response
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 5 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: routing token grant selection and mutations through the shard-aware service layer.
Linked Issues check ✅ Passed The changes satisfy issue #188. The route uses shard-aware document and collection listings, the canonical request-context caller, and token services for validation, persistence, and auditing. Tests c…
Out of Scope Changes check ✅ Passed The changes are limited to the token settings route, token service, related tests, service exports, and documentation required by issue #188. No unrelated authentication, storage-format, permission, o…
Full details: Linked Issues check

Explanation

The changes satisfy issue #188. The route uses shard-aware document and collection listings, the canonical request-context caller, and token services for validation, persistence, and auditing. Tests cover sharded resources, caller attribution, audit behavior, unknown Spaces, and revocation.

Full details: Out of Scope Changes check

Explanation

The changes are limited to the token settings route, token service, related tests, service exports, and documentation required by issue #188. No unrelated authentication, storage-format, permission, or administration changes are present.

Full details: Docstring Coverage

Explanation

Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 5 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch issue-188-token-shard-grants

Usage-based review receipt

Note

This review was completed with usage-based billing: files reviewed beyond your plan's included limits are billed at $0.25/file. View usage-based billing.


Comment @coderabbitai help to get the list of available commands.

@brylie
brylie merged commit 3f576e0 into main Sep 3, 2026
2 checks passed
@brylie
brylie deleted the issue-188-token-shard-grants branch September 3, 2026 20:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Token grant picker omits sharded Documents and Collections

1 participant