mastra-ai/mastra · error · UniqueViolationError
Source-control session ID already exists
Error message
Source-control session ID already exists
What it means
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.
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
Example fix
// before
await storage.sessions.create({ sessionId: 'my-session', ... });
// after
const existing = await storage.sessions.getForBranch(input);
const session = existing ?? await storage.sessions.create({ sessionId: randomUUID(), ... }); Defensive patterns
Strategy: try-catch
Validate before calling
const existing = await storage.sessions.getForBranch(input); if (existing) return existing;
Type guard
null
Try / catch
try {
session = await storage.sessions.create(input);
} catch (e) {
if (e instanceof UniqueViolationError && e.message.includes('session ID already exists')) {
session = await findSessionBySessionId(input.sessionId);
} else throw e;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Source-control installation not found for this organization…
- Version number already exists for MCP server
- Version with id already exists
- A work item cannot relate to itself.
- Access denied: unable to verify message ownership
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/d8db0b144b46f543.
Report an issue: GitHub.
Appendix: source
Thrown at mastracode/factory/src/storage/domains/source-control/inmemory.ts:362
}
},
getForBranch: async ({
projectRepositoryId,
userId,
branch,
}: {
projectRepositoryId: string;
userId: string;
branch: string;
}): Promise<SourceControlSession | null> =>
this.sessionsRows.find(
row => row.projectRepositoryId === projectRepositoryId && row.userId === userId && row.branch === branch,
) ?? null,
create: async (input: CreateSourceControlSessionInput): Promise<SourceControlSession> => {
const existing = await this.sessions.getForBranch(input);
if (existing) return existing;
if (this.sessionsRows.some(row => row.sessionId === input.sessionId)) {
throw new UniqueViolationError('Source-control session ID already exists');
}
const now = new Date();
const session: SourceControlSession = {
id: randomUUID(),
...input,
title: input.title ?? null,
visibility: input.visibility ?? 'org',
sandboxId: null,
sandboxWorkdir: null,
materializedAt: null,
firstMessageAt: null,
firstMeaningfulExecAt: null,
createdAt: now,
updatedAt: now,
};
this.sessionsRows.push(session);
return session;
},View on GitHub (pinned to 75dd419e61)