mastra-ai/mastra · error

Factory session repository not found

Error message

Factory session repository not found

What it means

Thrown by resolveSourceSession after the factory session itself resolves but its linked project repository cannot be found. The session row stores a projectRepositoryId; when storage.projectRepositories.get({orgId, id: session.projectRepositoryId}) returns nothing, the session is dangling — it references a repository record that no longer exists (or never existed) in that org.

Source

Thrown at mastracode/factory/src/rules/start-coordinator.ts:95

  }
  const args = invocation.arguments.trim();
  const content = `${formatSkillActivation(skill)}${args ? `\n\nARGUMENTS: ${args}` : ''}`.trim();
  return `<skill name="${skill.name}">\n${escapeSkillBoundary(content)}\n</skill>`;
}

async function resolveSourceSession(
  storage: SourceControlStorageHandle,
  request: FactoryStartRequest,
): Promise<SourceControlSession> {
  const session = await storage.sessions.getBySessionId(request.sessionId);
  if (!session || session.orgId !== request.orgId || session.userId !== request.userId) {
    throw new Error('Factory session not found');
  }
  const projectRepository = await storage.projectRepositories.get({
    orgId: request.orgId,
    id: session.projectRepositoryId,
  });
  if (!projectRepository) throw new Error('Factory session repository not found');
  const connection = await storage.connections.get({ orgId: request.orgId, id: projectRepository.connectionId });
  if (!connection || connection.factoryProjectId !== request.factoryProjectId) {
    throw new Error('Factory session does not belong to this project');
  }
  return session;
}

async function configureThread(session: FactorySession, request: FactoryStartRequest): Promise<string> {
  const threadId = session.thread.requireId();
  await session.thread.rename({ title: request.threadTitle });
  const settings = { ...(request.threadTags ?? {}), factorySessionId: request.sessionId };
  await Promise.all(Object.entries(settings).map(([key, value]) => session.thread.setSetting({ key, value })));
  return threadId;
}

export class FactoryStartCoordinator {
  readonly #controller: FactoryController;
  readonly #storage: WorkItemsStorage;

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Re-register/reconnect the project repository for this org so a projectRepositories row exists with the ID stored on session.projectRepositoryId.
  2. Inspect the session row's projectRepositoryId and verify a matching record exists via storage.projectRepositories.get({orgId: request.orgId, id: session.projectRepositoryId}).
  3. If the repository binding is permanently gone, delete the orphaned session and start a fresh factory session on the reconnected repo.
  4. Check for cross-org mixups: the repo may exist but under a different orgId than request.orgId.

Example fix

// before: session references a deleted repo row -> throws
await factory.prepare({ sessionId, orgId, userId });

// after: reconnect the repo first, then repair the session pointer if the id changed
const repo = await storage.projectRepositories.create({ orgId, connectionId, ... });
await storage.sessions.update(sessionId, { projectRepositoryId: repo.id });
await factory.prepare({ sessionId, orgId, userId });
Defensive patterns

Strategy: validation

Validate before calling

const session = await storage.sessions.getBySessionId(request.sessionId);
const repo = session && await storage.projectRepositories.get({ orgId: request.orgId, id: session.projectRepositoryId });
if (!repo) throw new Error(`Session ${request.sessionId} references a project repository that no longer exists in org ${request.orgId}.`);

Type guard

function hasProjectRepository(session: FactorySession, repo: ProjectRepository | undefined): repo is ProjectRepository {
  return !!repo && repo.id === session.projectRepositoryId;
}

Try / catch

try {
  const prepared = await coordinator.prepare(request);
} catch (err) {
  if (err instanceof Error && err.message === 'Factory session repository not found') {
    console.error('The session’s repository binding was removed; reconnect the repo or start a new session.');
  }
  throw err;
}

Prevention

When it happens

Trigger: Starting a factory session whose session row survived but whose projectRepositories row was deleted (repo disconnected/removed from the org), the session was created before the repo was registered, cross-org data inconsistency (repo exists under a different orgId), or the session points at a stale projectRepositoryId after a migration.

Common situations: An admin disconnected or deleted the GitHub/GitLab repository binding from the org while old sessions still reference it; partial cleanup scripts removed repo rows but not sessions; restoring data from backups where only part of the graph was restored.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/48a4b0279621fc82. Report an issue: GitHub.