{"record":{"id":"d8db0b144b46f543","repo":"mastra-ai/mastra","slug":"source-control-session-id-already-exists","errorCode":null,"errorMessage":"Source-control session ID already exists","messagePattern":"Source-control session ID already exists","errorType":"exception","errorClass":"UniqueViolationError","httpStatus":409,"severity":"error","filePath":"mastracode/factory/src/storage/domains/source-control/inmemory.ts","lineNumber":362,"sourceCode":"      }\n    },\n    getForBranch: async ({\n      projectRepositoryId,\n      userId,\n      branch,\n    }: {\n      projectRepositoryId: string;\n      userId: string;\n      branch: string;\n    }): Promise<SourceControlSession | null> =>\n      this.sessionsRows.find(\n        row => row.projectRepositoryId === projectRepositoryId && row.userId === userId && row.branch === branch,\n      ) ?? null,\n    create: async (input: CreateSourceControlSessionInput): Promise<SourceControlSession> => {\n      const existing = await this.sessions.getForBranch(input);\n      if (existing) return existing;\n      if (this.sessionsRows.some(row => row.sessionId === input.sessionId)) {\n        throw new UniqueViolationError('Source-control session ID already exists');\n      }\n      const now = new Date();\n      const session: SourceControlSession = {\n        id: randomUUID(),\n        ...input,\n        title: input.title ?? null,\n        visibility: input.visibility ?? 'org',\n        sandboxId: null,\n        sandboxWorkdir: null,\n        materializedAt: null,\n        firstMessageAt: null,\n        firstMeaningfulExecAt: null,\n        createdAt: now,\n        updatedAt: now,\n      };\n      this.sessionsRows.push(session);\n      return session;\n    },","sourceCodeStart":344,"sourceCodeEnd":380,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/factory/src/storage/domains/source-control/inmemory.ts#L344-L380","documentation":"The in-memory source-control storage enforces that each session ID is unique. When create() is called with a sessionId that already exists in storage (and getForBranch did not return that existing session for the same project/user/branch), it throws UniqueViolationError instead of silently duplicating the row.","triggerScenarios":"Calling sourceControlStorage.sessions.create() with a sessionId string that was already used by a different project, user, or branch — the branch-scoped dedupe lookup misses it, but the global sessionId uniqueness check catches it.","commonSituations":"Generating session IDs client-side with a fixed or derived value (e.g. based on repo name), retrying a create after a partial failure with the same sessionId, or importing/seeding data that reuses historical session IDs.","solutions":["Generate a fresh unique sessionId (e.g. randomUUID()) for each create call","Catch UniqueViolationError and look up the existing session by sessionId instead of re-creating","If resuming an existing session, fetch it first via getForBranch or a session lookup rather than calling create"],"exampleFix":"// before\nawait storage.sessions.create({ sessionId: 'my-session', ... });\n// after\nconst existing = await storage.sessions.getForBranch(input);\nconst session = existing ?? await storage.sessions.create({ sessionId: randomUUID(), ... });","handlingStrategy":"try-catch","validationCode":"const existing = await storage.sessions.getForBranch(input);\nif (existing) return existing;","typeGuard":"null","tryCatchPattern":"try {\n  session = await storage.sessions.create(input);\n} catch (e) {\n  if (e instanceof UniqueViolationError && e.message.includes('session ID already exists')) {\n    session = await findSessionBySessionId(input.sessionId);\n  } else throw e;\n}","preventionTips":["Always generate sessionId values with randomUUID() or an equivalent unique generator","Check for an existing session before create when resuming workflows","Never derive sessionId from user-controlled names like repo or branch"],"tags":["storage","uniqueness","source-control"],"backgroundTag":"unique-constraint-violation","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}