Skip to content

fix(auth): skip WorkOS reads when org already linked locally#2844

Merged
bflad merged 1 commit into
mainfrom
bflad-age-2324-authcallback-latency-n1-workos-getorganization-fan-out-on-e14B
May 19, 2026
Merged

fix(auth): skip WorkOS reads when org already linked locally#2844
bflad merged 1 commit into
mainfrom
bflad-age-2324-authcallback-latency-n1-workos-getorganization-fan-out-on-e14B

Conversation

@bflad
Copy link
Copy Markdown
Member

@bflad bflad commented May 14, 2026

https://linear.app/speakeasy/issue/AGE-2324/authcallback-latency-n1-workos-getorganization-fan-out-on-every-login

syncMembershipsFromWorkOS previously made two WorkOS API reads per membership on every login: one direct GetOrganization for the org name/external_id, and one inside EnsureOrgExternalID. A user in ten orgs paid ~20 sequential cross-region round-trips on each login, driving 3-5s auth.callback latency.

The org name and external_id are only needed when the local row is missing or unlinked. Reorder the per-membership work to look up by workos_id first; on hit, skip both WorkOS calls. The rare new-org / link path still fetches from WorkOS and pushes external_id back so disaster recovery via resolveGramOrgID's external_id fallback keeps working.

The background webhook reconcile path already keeps workos_id in sync locally, so steady-state logins after a webhook-only sync now make zero WorkOS reads. Linking external_id remotely is only required when this login is the first to touch the org locally, which is also the only path that still calls EnsureOrgExternalID.

https://linear.app/speakeasy/issue/AGE-2324/authcallback-latency-n1-workos-getorganization-fan-out-on-every-login

`syncMembershipsFromWorkOS` previously made two WorkOS API reads per
membership on every login: one direct `GetOrganization` for the org
name/external_id, and one inside `EnsureOrgExternalID`. A user in ten
orgs paid ~20 sequential cross-region round-trips on each login, driving
3-5s `auth.callback` latency.

The org name and external_id are only needed when the local row is
missing or unlinked. Reorder the per-membership work to look up by
workos_id first; on hit, skip both WorkOS calls. The rare new-org / link
path still fetches from WorkOS and pushes external_id back so disaster
recovery via `resolveGramOrgID`'s external_id fallback keeps working.

The background webhook reconcile path already keeps `workos_id` in sync
locally, so steady-state logins after a webhook-only sync now make zero
WorkOS reads. Linking external_id remotely is only required when this
login is the first to touch the org locally, which is also the only path
that still calls `EnsureOrgExternalID`.
Copy link
Copy Markdown

@claude claude Bot left a comment

Choose a reason for hiding this comment

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

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review to trigger a review and subscribe this PR to future pushes, or @claude review once for a one-time review.

Tip: disable this comment in your organization's Code Review settings.

@vercel
Copy link
Copy Markdown

vercel Bot commented May 14, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
gram-docs-redirect Ready Ready Preview, Comment May 14, 2026 8:32pm

Request Review

@linear-code
Copy link
Copy Markdown

linear-code Bot commented May 14, 2026

AGE-2324

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 14, 2026

🦋 Changeset detected

Latest commit: 1cbd92c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
server Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions Bot added the preview Spawn a preview environment label May 14, 2026
@speakeasybot
Copy link
Copy Markdown
Collaborator

speakeasybot commented May 14, 2026

🚀 Preview Environment (PR #2844)

Preview URL: https://pr-2844.dev.getgram.ai

Component Status Details Updated (UTC)
✅ Database Ready Existing database reused 2026-05-18 13:10:41.
✅ Images Available Container images ready 2026-05-18 13:08:45.

Gram Preview Bot

Copy link
Copy Markdown
Member

@adaam2 adaam2 left a comment

Choose a reason for hiding this comment

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

I think this looks good! I would test on dev first though to ensure no funny business

@bflad bflad added this pull request to the merge queue May 19, 2026
Merged via the queue into main with commit 4aceb60 May 19, 2026
31 checks passed
@bflad bflad deleted the bflad-age-2324-authcallback-latency-n1-workos-getorganization-fan-out-on-e14B branch May 19, 2026 13:53
@github-actions github-actions Bot locked and limited conversation to collaborators May 19, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

preview Spawn a preview environment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants