Skip to content

Fix mock installation ID stability across server restarts - #5196

Open
tomsmith8 wants to merge 1 commit into
masterfrom
claude/keen-pascal-i7lsts
Open

Fix mock installation ID stability across server restarts#5196
tomsmith8 wants to merge 1 commit into
masterfrom
claude/keen-pascal-i7lsts

Conversation

@tomsmith8

Copy link
Copy Markdown
Collaborator

Summary

Fixes a critical bug where mock GitHub installation IDs were generated from an in-memory counter that reset on server restart, causing P2002 unique 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 range
    • Both use FNV-1a hashing with username-specific seeds to ensure distinct, collision-resistant IDs
  • Added collision resolution:

    • resolveMockInstallationId(): Handles rare hash collisions between mock usernames by probing for the next free ID, ensuring idempotency for existing orgs
  • Updated mock workspace creation:

    • ensureMockWorkspaceForUser() and ensureStakworkMockWorkspace() now call resolveMockInstallationId() within the transaction to allocate safe IDs
    • Removed reliance on the module-level mockGitHubIdCounter
  • Added comprehensive unit tests (mockSetup.test.ts):

    • Verify ID stability across multiple calls
    • Verify distinct IDs for distinct usernames
    • Verify IDs stay within Postgres INT4 range and avoid the fixed mock org ID
    • Verify collision resolution and re-seeding behavior

Implementation Details

  • IDs are mapped into [1e9, 2e9), which is inside Postgres INT4 range and clear of real GitHub installation IDs and the fixed MOCK_ORG_INSTALLATION_ID (999001)
  • FNV-1a hash is seeded with installation: or user: prefix to ensure GitHub user IDs don't collide with installation IDs for the same username
  • Collision resolution probes up to 1000 times before throwing an error (extremely unlikely in practice)
  • The solution is fully backward compatible and doesn't require database migrations

https://claude.ai/code/session_01R2QCg1FLW3AFd1E3BFR2TR

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants