Fix mock installation ID stability across server restarts - #5196
Open
tomsmith8 wants to merge 1 commit into
Open
Fix mock installation ID stability across server restarts#5196tomsmith8 wants to merge 1 commit into
tomsmith8 wants to merge 1 commit into
Conversation
…sions Mock GitHub ids came from a module-level counter that resets to 100000 on every server restart. On a long-lived dev DB, the SourceControlOrg upsert matches on githubLogin but creates with githubInstallationId (unique), so a new mock username could be assigned an installation id already persisted by a previous run's seed. Prisma then threw P2002, the signIn callback returned false, and the user landed on /api/auth/error?error=AccessDenied. Retrying advanced the counter and could accidentally succeed, hiding the bug. Replace the counter with ids derived deterministically from the mock username (FNV-1a, mapped into [1e9, 2e9) — inside Postgres INT4 range and clear of the fixed mock-org id), so the same username always maps to the same id across restarts. A probe inside the seeding transaction steps past the rare case where a different login already holds the derived id. Both ensureMockWorkspaceForUser and ensureStakworkMockWorkspace are covered; the stakwork variant hashes its "-stakwork"-suffixed username so the two never collide for the same user. Add unit tests for id derivation (stability, distinctness, range) and the collision probe. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01R2QCg1FLW3AFd1E3BFR2TR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a critical bug where mock GitHub installation IDs were generated from an in-memory counter that reset on server restart, causing
P2002unique constraint violations when signing in with a new mock username. Installation IDs are now derived deterministically from the username using FNV-1a hashing, ensuring stability across restarts.Changes
Added stable ID derivation functions:
deriveMockInstallationId(): Maps usernames to stable installation IDs in the range [1e9, 2e9)deriveMockGitHubUserId(): Maps usernames to stable GitHub user IDs using the same rangeAdded collision resolution:
resolveMockInstallationId(): Handles rare hash collisions between mock usernames by probing for the next free ID, ensuring idempotency for existing orgsUpdated mock workspace creation:
ensureMockWorkspaceForUser()andensureStakworkMockWorkspace()now callresolveMockInstallationId()within the transaction to allocate safe IDsmockGitHubIdCounterAdded comprehensive unit tests (
mockSetup.test.ts):Implementation Details
MOCK_ORG_INSTALLATION_ID(999001)installation:oruser:prefix to ensure GitHub user IDs don't collide with installation IDs for the same usernamehttps://claude.ai/code/session_01R2QCg1FLW3AFd1E3BFR2TR