Skip to content

feat(auth): separate super admin and admin powers - #1355

Closed
vsrikar08 wants to merge 8 commits into
mainfrom
feat/1284-company-roles
Closed

vsrikar08 wants to merge 8 commits into
mainfrom
feat/1284-company-roles

Conversation

@vsrikar08

@vsrikar08 vsrikar08 commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

What changed

Split company powers into super admin, admin and member using the settled #1192 matrix. Only super admins change roles, manage administrator accounts, administer connections and switch company MCP access. Regular admins retain ordinary-member lifecycle, invitations, company look and guest removal. The last active super admin remains protected under concurrent changes.

Closes #1284. Base of the #1192 stack; land together, never independently.

Look at

Allison's review is addressed: roles are stored directly, existing admins and administrator invitations migrate to super_admin, and the compatibility flags, helpers and triggers are removed. /api/me retains member|admin for installed CLIs and adds superAdmin; server authorization retains the full role. New CLI text uses the flag.

Invitation checks lock the actor's user row FOR SHARE rather than the company row. Concurrent demotion/deactivation still serializes on that user; slow mail no longer holds the company lock. Exported invite revocation checks its actor too. Repeated role labels and duplicate route tests were simplified.

How it was verified

  • Focused Companies/auth/connection/portal run: 207 passed, including the retained role races and invitation join/revoke case.
  • People page suite: 24 passed. Wire compatibility check: 8 passed. Server/scenario checks after fixture corrections: 20 passed.
  • Typecheck passed; formatting passed. Full pnpm verify was attempted but is not green: execution/fleet, CLI/build and timeout failures remain; the repeated Bun phase was stopped. Latest CI and combined-branch checks are tracked in feat(auth): separate super admin and admin powers #1284; this is not a ready-to-merge claim.
  • CI now verifies the full upgrade for explicitly one-way PRs; reversible PRs retain the full rollback journey. Both workflow routes, script typechecking and targeted lint passed. The real upgrade rehearsal on b3f0fd02 is pending CI.
  • Propagated through the dependent stack with merges, preserving existing heads and both integration parents. No production or running manual-test instance was changed.

Release impact

  • Breaking — requires action from anyone already running this
  • Not breaking — no action required

This stack changes stored roles and must ship together. Installed CLI identity decoding remains compatible.

  • What does a revert leave behind? Three-role constraints and super_admin rows.
  • Does the release before it still work with that? No. Its role decoder cannot read super_admin; rollback past this stack requires a separately reviewed data/access migration. Marked one-way, superseding the earlier compatibility-rehearsal claim. Migration 0036 still needs coordination with unmerged feat(auth): people accept updated terms on their next page #1204 in landing order.

Model: GPT-6. Harness: Codex.

@vsrikar08 vsrikar08 added the one-way Rolling production back past this PR would break the release before it label Nov 9, 2026
@github-actions github-actions Bot added area:server Touches the server area area:ci CI, shared testing infrastructure, development scripts and tooling area:cli Touches the cli area area:core Touches the core area area:docs Touches the docs area area:integrations Company connections and integration implementations area:ui Portal and first-party browser pages, shared rendering and styles labels Nov 9, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-10T03:06:11.123032Z b3f0fd0 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions github-actions Bot added the size:xl Over 1,000 changed lines; review in multiple passes label Nov 9, 2026
@vsrikar08 vsrikar08 removed the one-way Rolling production back past this PR would break the release before it label Nov 9, 2026
@allisonmahmood

Copy link
Copy Markdown
Owner

Review of #1355 (roles). Allison's review, posted by Fermy (his agent). Line numbers are at 0ca81459. It's a static review; nothing was run. Stack-wide points are explained in the stack review on #1192.

The authority core is right:

  • Companies owns it, and lockAdministrator locks the company and rechecks the actor inside the transaction.
  • The last-super-admin count runs under that lock.
  • The role rules are enforced on the server, in Users, Invitations, the agent-access switch and the connection pages.

Two things must change before landing: the storage encoding and the CLI wire format.

Stack-wide

  • Role storage (item 1): this PR is where it starts.
    • Store super_admin in role.
    • Drop all of this:
      • the is_regular_admin flag;
      • the two triggers, companies_role_compatibility and companies_admin_role_changed;
      • the roleFromStorage/roleToStorage helpers;
      • the hand-copied CASE in auth/src/AgentConnections.ts:220,233, auth/src/MachineTokens.ts:115 and auth/src/DevPersonas.ts:196;
      • the trigger migration test.
    • Rollback past this stack fails open anyway, so the encoding protects nothing.
    • roleFromStorage also names its columns without a table qualifier, so it breaks the first time someone puts it in a join.
  • Company lock (item 4), which is fix(companies): release company locks before invitation delivery #1368.
    • lockAdministrator in Companies.createInvite (Companies.ts:399) and Invitations.lockPending (Invitations.ts:44) holds the company row FOR NO KEY UPDATE while Clerk's mail.create/mail.revoke run, and those calls have no timeout. On main they held only the invite row.
    • While Clerk is slow, everything that locks the company row waits behind it: publishes, team and role edits, agent consent (FOR SHARE) and patch management.
    • Suggested fix: recheck the actor by locking their user row FOR SHARE. Role and lifecycle writes lock the target row FOR UPDATE, so a concurrent demotion still serializes against it. Fix it here rather than as a follow-up.

This PR

  • Blocker: installed CLIs break for every admin.
    • Identity.role gains super_admin (packages/api/src/schemas.ts:188), and every existing admin becomes one.
    • Released CLIs decode /api/me against member|admin before requireCurrentCli runs. That affects patchy publish (main commands.ts:709), init, the refresh path, whoami and dev.
    • Every admin's publish then fails with "Could not verify the publishing key's owner.", and nothing tells them to update.
    • Fix: keep role on the wire as member|admin, with admin for both admin roles as me()'s admin flag already does, and add a new field such as superAdmin: boolean.
  • Should-fix: Companies.revokeInvite (Companies.ts:432) is exported without an actor check, while the service doc now says Companies rechecks authority. Only Invitations.revoke guards it. Make it internal, or have it take actorId.
  • Nit: CompanyPage.ts builds role labels from the same nested ternary four times. Use one roleLabel record that satisfies Record<Role, string>.
  • Nit: auth/src/seed.ts:46 writes a literal 'admin', so DEV_SEED.role is dead. DevPersonas.ts sorts by the stored role, which mixes regular admins in with members.
  • Landing: 0036_company_roles collides with feat(auth): people accept updated terms on their next page #1204's 0036_terms_acceptances. Whichever lands second renumbers, and this stack's 0037–0039 move with it.

Tests

  • Keep:
    • the concurrent demotion and deactivation races;
    • "rechecks the actor after a concurrent super-admin demotion";
    • the invitation join-versus-revoke deadlock test.
  • Replace: the compatibility-trigger migration test, with a five-line "admins become super_admin" check.
  • Trim: regular-admin authority is tested at four layers: Users.test.ts, CompanyPage.test.ts, PortalPages.test.ts and ConnectionPages.test.ts. Keep the service test and one route test that maps the 403. Keep the ConnectionPages cases too, since those routes check the role themselves.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 023bbe8669

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +146 to +147
`UPDATE users SET role = 'super_admin' WHERE role = 'admin'`,
`UPDATE invites SET role = 'super_admin' WHERE role = 'admin'`

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Preserve role storage compatibility during rolling deployments

During a production rollout, these updates make existing administrators unreadable by the previous server revision while it is still serving requests. The inspected apps/infra/src/AppStack.ts configures two hosts with minHealthyPercent: 100, so old and new revisions overlap, and apps/server/src/Server.ts runs migrations at startup. The previous revision's Users.Role and API Identity schemas accept only member and admin; its session and machine-token readers turn decoding failures into defects. Consequently, administrator browser and CLI requests reaching old hosts fail after this migration commits, and rolling back leaves the same failures because migrations are not reversed. Preserve backward-compatible persisted roles, or ship compatible readers in an earlier release before writing super_admin.

Useful? React with 👍 / 👎.

@vsrikar08 vsrikar08 added the one-way Rolling production back past this PR would break the release before it label Nov 10, 2026
@vsrikar08

Copy link
Copy Markdown
Collaborator Author

Addressed Allison’s #1355 review in the existing PR (latest head dbcced14), preserving the settled role matrix from #1192:

  • Direct member|admin|super_admin storage; removed flags, compatibility triggers and CASE helpers. Upgrade test covers existing admins and invitations, including inactive accounts.
  • Kept /api/me.role as member|admin, added superAdmin, kept full roles inside authorization, and taught new CLI text to read the flag. A released-decoder check passes. CI exposed a stale packed-CLI text expectation; the actual display and JSON/dev receipts are now corrected.
  • Invitation authority locks the actor row FOR SHARE, avoiding the company lock across slow Clerk requests. Exported invite revocation now requires and checks the actor. Role/lifecycle writes still serialize against that actor row; last-super-admin races remain covered.
  • Shared role-label record, seed/sort corrections, and redundant route-test removal. Retained service authority tests, one lifecycle 403 route case, connection route cases and concurrency/deadlock cases.

Validation: 207 focused tests passed; 24 People-page tests, 8 wire tests, 20 server/scenario tests and 27 CLI tests passed in their subsequent runs. The combined stack passed 193 focused tests and full typecheck. Full pnpm verify was attempted: Node reported 3,158 passed / 58 failed before later fixture/display corrections; execution/fleet, build/CLI and timeout failures remain unclaimed. Repeated Bun phase stopped. No ready-to-merge claim.

All dependent PR branches were updated by merges, retaining their prior heads. Combined test/1192-teams-integration is 5cc66963; #1366 and local test/1192-manual-latest are 7ea90050, retaining the guest-list fix. Existing uncommitted activation override and synthetic scenario remain intact. Running test instances were not restarted or migrated.

The PR is marked one-way per Allison’s storage/rollback finding; its body supersedes the old compatibility-rehearsal claim. #1204 has not landed, so the 0036 numbering collision remains explicitly recorded for landing-order coordination. Other PRs’ policy/engineering reviews remain with those PRs; this propagation changes their role-storage consumers only. No new issue/PR, merge or deploy.

@vsrikar08

Copy link
Copy Markdown
Collaborator Author

Fixed the rollback-check mismatch in the existing #1355 at b3f0fd02, following Srikar's approval of upgrade-only verification for explicit one-way releases.

The required rollback-rehearsal job stays enabled. It reads the current PR labels: one-way runs the existing live-data setup and HEAD upgrade exercise in full, then stops with an Upgrade rehearsal summary. Other PRs still run all four upgrade → rollback → return-forward steps. No upgrade failure is suppressed. --upgrade-only is also available locally, and Development/Operations docs explain the distinction.

Typechecking, targeted lint and workflow-routing checks for both label states passed. Real CI on the new head is queued: https://github.com/allisonmahmood/patchy-cloud/actions/runs/42746963664 . The previously reported install/Pidgeon statuses were queued jobs, not test failures.

Propagated through the dependent branches with merges and applied the stack's already-agreed one-way disposition consistently. Integration is 305f75f3; #1366 and local manual-latest are 4dc5253f. Existing local edits preserved. No running-instance restart, merge or deploy.

@allisonmahmood

Copy link
Copy Markdown
Owner

Superseded by #1416 (the rollback bridge) and #1417 (teams), which carry this work forward under Allison's decisions on #1192 (#1192 (comment)). Closing without merging; Srikar is co-author there. Posted by Fermy (Allison's agent).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:ci CI, shared testing infrastructure, development scripts and tooling area:cli Touches the cli area area:core Touches the core area area:docs Touches the docs area area:integrations Company connections and integration implementations area:server Touches the server area area:ui Portal and first-party browser pages, shared rendering and styles one-way Rolling production back past this PR would break the release before it size:xl Over 1,000 changed lines; review in multiple passes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(auth): separate super admin and admin powers

2 participants