feat: one identity table for every integration
One identity table for every integration, replacing the one-table-per-provider shape and folding users__slack_users into it.
Supersedes #6544, which was cut before main took 0078 and did not include the Slack fold-in.
The table
user_identities
├── user_id → auth.users, cascade
├── organization_id → organizations, cascade
├── provider text -- "github" | "slack" | "google" | …
├── external_id text -- the provider's stable id
├── external_scope_id text null -- workspace/tenant; null when global
├── handle text null -- display only
├── display_name text null
└── metadata jsonb null -- typed per provider
UNIQUE (organization_id, provider, external_scope_id, external_id) NULLS NOT DISTINCT
external_scope_id carries the workspace, tenant or account for providers whose user ids aren't global — five of the seven targets. Slack, Linear, Teams and PagerDuty scope theirs; GitHub and Google don't. NULLS NOT DISTINCT is what makes the global ones dedupe against themselves.
metadata is a union per provider in TypeScript, not Record<string, unknown>. Slack's chosen model is its only occupant. When a provider's entry stops being a field or two, that's the signal to give it a table — and it'll be visible in the type rather than buried in the column.
Org-scoped, multiple accounts allowed
Matching how Linear actually behaves — verified by experiment. Their FAQ says "you can only connect your GitHub account to a single Linear workspace"; disconnecting in one workspace demonstrably leaves the other connected. The docs are wrong.
Nothing limits a person to one account per provider, so a work and a personal account can both be linked. me resolves to a set, and a PR from either is still yours. Vercel enforces a strict 1:1 and it's a long-standing complaint; Linear-as-documented would be the same.
The Slack fold-in
All six call sites move: the link writer, the model-select write, the disconnect delete, and three reads (app home, mention, assistant message). model_preference moves into metadata rather than being dropped — it's live, shown on the Slack app home and used to pick the model on every mention, so losing it would silently reset those users.
The copy and the code move together, deliberately. A backfill inherits its source's writers: if the copy landed days before the reads switched, every model change in between would be invisible to it. That's the next_run_at drift bug, and this is the shape that avoids it.
The old table is left in place, unread, to drop separately.
Rehearsed — and it caught a real failure
The full 81-migration chain, twice, because the first two runs failed:
ERROR: column "metadata" is of type jsonb but expression is of type text
An untyped NULL in a SELECT is text, so both backfills clashed with the jsonb column. That would have failed the production migration. Fixed with NULL::jsonb in both.
Then, with real-shaped data:
| Same account linked in two orgs | allowed |
| Two accounts (work + personal) in one org | allowed |
| Same account twice in one org | rejected |
| Slack fold-in preserves model preferences | 2 of 3, correctly |
| One person in two Slack workspaces | two rows, distinct scope |
| GitHub backfill links only where an installation exists | pass |
| Re-running either backfill | no-op |
Plus nine matching cases: me matching a work account, matching a personal one once linked, not matching an unlinked one, and a renamed login still matching by id.
Backfill sizes
GitHub is scoped to orgs that actually have an installation — elsewhere no GitHub event can arrive, so the row would be noise. 2,029 rows rather than 29,190. Slack is all 110 rows.
Follow-up
Dropping users__slack_users once this is deployed and nothing reads it.
Summary by cubic
Unifies external identities in a single user_identities table and folds users__slack_users into it. GitHub matching moves from login to numeric user ID and “me” resolves via linked identities per org; Slack lookups are now org-scoped to prevent cross‑org collisions.
Key changes
- Database: adds
user_identitieswith UNIQUE (organization_id, provider, external_scope_id, external_id) NULLS NOT DISTINCT and typedmetadata. - Backfill: seeds GitHub identities from
auth.accountsonly for orgs with a GitHub installation; seeds Slack identities fromusers__slack_users, preservingmodel_preferenceinmetadata; old table remains for a follow-up drop. - Matching:
apps/apiextractsactorId/subjectAuthorId;packages/sharedcompares GitHub ids; webhook resolves each automation owner’s GitHub id set fromuser_identitiesper org so “me” matches work and personal accounts. - Slack: all reads/writes use
user_identitiesviafind-slack-user-link; every lookup includesorganizationId;apps/apiinteractions route resolves the org fromintegrationConnectionsbyteamIdand no-ops if none; model selection writes tometadata; disconnect deletes the identity; re-linking claims the account for the latest linker. - Admin: deletes identities via
user_identities. - Rollout: run migrations
0079_user_identitiesand0080_backfill_identities; dropusers__slack_usersin a follow-up after deploy.
Summary by CodeRabbit
- New Features
- Added centralized identity linking for Slack and GitHub accounts.
- Improved GitHub automation matching using stable account IDs, including more reliable “me” matching.
- Preserved Slack workspace associations and model preferences when linking accounts.
- Bug Fixes
- Improved Slack event and interaction handling for linked users.
- Updated account deletion to remove linked identity data correctly.
- Data Migration
- Migrated existing Slack and GitHub account links to the new identity system.