Skip to content

feat(roles): custom roles and a permission catalog - #2867

Open
JeremyFunk wants to merge 53 commits into
mainfrom
feat/custom-roles
Open

JeremyFunk wants to merge 53 commits into
mainfrom
feat/custom-roles

Conversation

@JeremyFunk

@JeremyFunk JeremyFunk commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Custom roles for Maple. Roles become permission sets: the four built-in roles (Analyst, Member, Engineer, Admin) are presets, and organizations can define their own roles with any of the catalog's permissions. Every check now reads a permission instead of a role rank.

What changed

  • Permission catalog (packages/domain/src/policy): 52 permissions with stable codes. Built-in roles are explicit presets with the same abilities as before; on-call schedules and SLOs/budgets are split out of alerts:manage (Engineer still holds all three). source_maps:upload (from main) is an Engineer permission.
  • Custom roles live in Clerk, no migration of existing members:
    • definitions in the organization's public metadata (maple_roles, versioned);
    • a member on one holds the Clerk role org:custom, with the role id and permission codes on their membership.
  • Admin only: members, roles, the organization, full-access API keys, SSO, the warehouse and agent-feedback triage can't be given to a custom role.
  • Telemetry by default: every permission implies Read telemetry unless it was checked to need none (billing, own devices, MCP and restricted keys, the support channel, status page updates, the public ingest key).
  • Enforcement: tenants carry permissions; every HTTP endpoint declares EndpointAccess (unmarked non-GET is refused, endpoint and group rules both apply, and a GET no requires or "personal" covers needs telemetry:read); every MCP tool declares a permission; chat approvals, electric-sync shapes, digests, share links, mobile pushes and raw SQL (query:sql) all check. The six member-level permissions that were UI-only are now enforced on the server.
  • Role writes are serialized per org with a Postgres advisory lock (RoleWriteLock), so two admins can't drop each other's edit or demote each other out of the last Admin. A holder Clerk refuses doesn't stop the others; the save reports it and sending it again finishes them. The Clerk webhook checks an org:custom membership against the role it names, so a role set by hand can't revive deleted or narrowed codes.
  • Credentials keep current behaviour: API keys and MCP/CLI/device credentials pin the creator's role; a custom role also pins its permission codes. A demotion retires credentials, share links, digests and phones the new role no longer covers (alert destinations keep routing).
  • Roles API (/internal/roles, admin-only writes, audited) and dashboard UI: Roles page, role editor (compare with another role, implied permissions tick themselves), create and delete (with reassign) dialogs, members-tab empty state, Members page picker with custom roles, "Needs " denial tooltips, a read-only banner on built-in roles, and every control gated on the permission the server checks.
  • Ops notes in docs/custom-roles.md.

Screenshots

Roles Role editor
roles editor
Create role Delete with reassign
create delete
Members picker Built-in role (read-only) Members tab, empty Non-admin view
picker builtin empty locked

Verification

  • Full suites green locally after merging main: domain 1465, auth 94, backend ~5700, api ~820, ai 2192, web 5720, electric-sync 81 tests (load timeouts rerun alone and pass).
  • New tests cover the policy and implications, the endpoint gate (every non-GET endpoint in all four APIs is marked; configuration reads need telemetry:read), custom-role session resolution, pinned codes and demotion, RolesService (validation, versioned writes, the org lock, partial holder failures and retry, reconcile, reassign, webhook settling), MCP tool gating, chat approvals, digest/push/share filtering (raw SQL shares need query:sql), and the roles UI scenes.
  • Seven rounds of independent security and correctness review, plus a final review whose findings are fixed here.

Before production

  • Create the Clerk role org:custom on the production instance, in the default role set, holding only org:sys_memberships:read (done on the dev instance). Production custom roles need Clerk's B2B Authentication add-on.
  • New nullable columns for pinned custom-role permissions (packages/db/drizzle/20261124232933_custom_role_pinned_permissions, dated after main's newest), applied by the prd deploy.

… widgets and query insights; restore dashboard approvals
…e digests, shares, raw-SQL alert rules and OAuth completion; demotion cleans up; orphaned holders hold nothing
…o-role members, recommendation/service-map gating, raw SQL edits need query:sql
…iews, code review, anonymous connectors; safe role drafts; tolerant session read
…ds query:sql, unreadable role metadata fails safe, digests retry after Clerk outages, stale role codes cleared; ops doc
…y telemetry:read, drop racy webhook role clearing, bound push owner lookups, evict cache on join
# Conflicts:
#	apps/api/test/offline-http-ports.ts
#	packages/backend/src/services/auth/OrgMembershipService.ts
#	packages/backend/src/services/digest/DigestService.ts
#	packages/backend/src/services/org/OrganizationDeletionService.ts
@JeremyFunk
JeremyFunk deployed to preview-tinybird November 24, 2026 23:24 — with GitHub Actions Active
@maple-review-bot

This comment has been minimized.

Brings main's source maps, on-call calendar feed, error grouping rules,
LLM evals and incident actions onto the permission model: source_maps:upload
joins the catalog (Engineer), new endpoints and MCP tools declare their
permission, and the pinned-permissions migration is regenerated after main's
newest so the deploy accepts it.

@maple-review-bot maple-review-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 inline note from Maple's review. The score and summary are in the review comment above.

… reads

Review follow-ups for custom roles:
- Role writes hold a per-org Postgres advisory lock (RoleWriteLock), so two
  admins can no longer drop each other's edit or demote each other out of
  the last Admin.
- Narrowing a role rewrites every holder even when Clerk refuses one, then
  fails with roles_unavailable; sending the same save again finishes them.
- The Clerk webhook checks an org:custom membership against the role it
  names, so a role set by hand cannot revive deleted or narrowed codes.
- A GET no requires or personal mark covers needs telemetry:read, so a
  role without it no longer reads alert rules, SLOs, keys or sources;
  the alert_rules, api_keys and alert_destinations shapes need it too.
- The demo seed needs sources:manage; sharing a board or widget holding
  raw SQL needs query:sql.
- Compare column marks only differences, as Added or Removed chips, with a
  summary beside the picker; a shared permission is no longer a second
  check mark next to the checkbox.
- The Required by note sits on the title line, so ticking a permission no
  longer pushes the rows below down.
- Built-in and Custom use the same quiet chip, every role's meter one tone,
  added rows lose the primary tint, permission codes are quieter, and the
  header drops the permission count the tab already shows.
@Makisuo
Makisuo deployed to preview-tinybird November 24, 2026 23:47 — with GitHub Actions Active
@maple-review-bot

This comment has been minimized.

@maple-review-bot maple-review-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 inline notes from Maple's review. The score and summary are in the review comment above.

1 finding sits outside this diff and is only in the summary comment.

- Built-in and read-only roles list a check or a dash per permission
  instead of disabled checkboxes, and their rows no longer hover.
- Group rows drop the inner bordered box; the chevron sits in a
  checkbox-wide slot so group and permission names line up.
- Duplicate to customize is the primary action; the meta line is the
  member count only, since the chip and banner already say the type.
- Tabs use tabular numerals and segmented options reserve their
  selected weight, so counts and picks never move their neighbours.
- The create dialog holds two lines for the starting role's detail,
  so picking one never resizes it.
- The members role prompt's info icon is no longer primary.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Makisuo
Makisuo deployed to preview-tinybird November 24, 2026 23:56 — with GitHub Actions Active
@maple-review-bot

This comment has been minimized.

- main's new alerts tests used adminRoles; the branch calls it adminGrants.
- The Clerk webhook tests provide RolesService, which the webhook now uses.
- The electric-sync shape test now expects configuration shapes to need
  telemetry:read.
main now requires saveBar for unsaved edits; it carries the change count,
the floating pill and Mod+S.
@Makisuo
Makisuo deployed to preview-tinybird November 25, 2026 00:12 — with GitHub Actions Active
@maple-review-bot

This comment has been minimized.

- code_review:auto_approve stays with Admin; custom roles cannot hold it.
- Sharing checks raw SQL in a stat's sparkline too (holdsRawSql), and
  rotating a share link takes the same query:sql check as minting one.
- Unticking a permission unticks the ones it alone had ticked; one ticked
  by hand stays.
- "1 member holds" for a single holder.
@Makisuo
Makisuo deployed to preview-tinybird November 25, 2026 00:26 — with GitHub Actions Active
@maple-review-bot

maple-review-bot Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Maple review

🟡 Confidence 6/10 · needs attention. Held at 6 because 140 reviewed files were not read.
no findings · tests covered · risk high · 1/1 new units observable · 140 files not read · 8 fixes not yet re-checked against the whole diff

Custom roles replace role-rank checks across HTTP, MCP, chat and the dashboard. The five earlier findings are fixed with tests. About 130 remaining changed files (tool declarations, tests, web pages) were covered by eight fan-out passes and the earlier passes, not re-read by me.

  • toggled tracks autoTicked, so unticking also unticks what that permission alone ticked
  • holdsRawSql/addsRawSql move the raw-SQL guard into @maple/widgets, sparklines included
  • rotateShare now runs the raw-SQL guard createShare already had
  • code_review:auto_approve is customRoles: false, so custom roles cannot hold it

Before merge

Fixed since the last review

  • ✅ F1 · Holder group label reads "1 member hold " for a single holder
  • ✅ F5 · code_review:auto_approve is now grantable to custom roles, against its own comment
  • ✅ F6 · requireRawSqlToShare misses raw SQL in a stat's sparkline
  • ✅ F7 · rotateShare mints a token without the raw-SQL guard
  • ✅ F8 · Unticking a permission leaves the ones it implied ticked
Files not reviewed (140)

The review ended before it read these diffs, so nothing above vouches for them.

  • apps/ai/src/chat/agents.test.ts
  • apps/ai/src/chat/apply-tenant.test.ts
  • apps/ai/src/chat/code-mode.test.ts
  • apps/ai/src/chat/maple-toolkit.test.ts
  • apps/ai/src/chat/pr-review-replay.test.ts
  • apps/ai/src/chat/run-connector.test.ts
  • apps/ai/src/chat/run-input.test.ts
  • apps/ai/src/chat/run-review-fanout.test.ts
  • apps/ai/src/chat/run-tool-failures.test.ts
  • apps/ai/src/chat/sandbox-breaker.test.ts
  • apps/ai/src/chat/turn-actor.test.ts
  • apps/ai/src/evals/config/world.ts
  • apps/ai/src/evals/playground/tool.playground.ts
  • apps/ai/src/mcp/__evals__/eval-runtime.ts
  • apps/ai/src/mcp/__evals__/utils.ts
  • apps/ai/src/mcp/app.test.ts
  • apps/ai/src/mcp/dispatcher.test.ts
  • apps/ai/src/mcp/lib/dashboard-mutations.test.ts
  • apps/ai/src/mcp/lib/data-sources.test.ts
  • apps/ai/src/mcp/lib/resolve-actor.test.ts
  • apps/ai/src/mcp/lib/resolve-tenant.oauth.test.ts
  • apps/ai/src/mcp/tool-descriptors-render.ts
  • apps/ai/src/mcp/tools/__tests__/alert-silence-tools.test.ts
  • apps/ai/src/mcp/tools/__tests__/alert-tools.test.ts
  • apps/ai/src/mcp/tools/__tests__/configuration-tools.test.ts
  • apps/ai/src/mcp/tools/__tests__/dashboard-raw-sql.test.ts
  • apps/ai/src/mcp/tools/__tests__/data-source-tools.test.ts
  • apps/ai/src/mcp/tools/__tests__/error-grouping-tools.test.ts
  • apps/ai/src/mcp/tools/__tests__/error-notification-policy.test.ts
  • apps/ai/src/mcp/tools/__tests__/escalation-chain-tools.test.ts
  • and 110 more

0af1a96 · What was checked, coverage and telemetry details are on the Maple / review check. Reply "won't fix" to dismiss a finding, or mention @maple-review-bot to ask about one.

This branch had an error being deployed

1 failed and 1 active deployments
pr-preview — 0af1a96e Deployed Nov 25, 2026 by Makisuo via deploy-pr-preview #15218
preview-tinybird — 0af1a96e Deployed Nov 25, 2026 by Makisuo via branch #4838
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

preview Deploy a full PR preview stack (Cloudflare + AWS ingest); removing it tears it down

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants