{"record":{"id":"48a4b0279621fc82","repo":"mastra-ai/mastra","slug":"factory-session-repository-not-found","errorCode":null,"errorMessage":"Factory session repository not found","messagePattern":"Factory session repository not found","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/factory/src/rules/start-coordinator.ts","lineNumber":95,"sourceCode":"  }\n  const args = invocation.arguments.trim();\n  const content = `${formatSkillActivation(skill)}${args ? `\\n\\nARGUMENTS: ${args}` : ''}`.trim();\n  return `<skill name=\"${skill.name}\">\\n${escapeSkillBoundary(content)}\\n</skill>`;\n}\n\nasync function resolveSourceSession(\n  storage: SourceControlStorageHandle,\n  request: FactoryStartRequest,\n): Promise<SourceControlSession> {\n  const session = await storage.sessions.getBySessionId(request.sessionId);\n  if (!session || session.orgId !== request.orgId || session.userId !== request.userId) {\n    throw new Error('Factory session not found');\n  }\n  const projectRepository = await storage.projectRepositories.get({\n    orgId: request.orgId,\n    id: session.projectRepositoryId,\n  });\n  if (!projectRepository) throw new Error('Factory session repository not found');\n  const connection = await storage.connections.get({ orgId: request.orgId, id: projectRepository.connectionId });\n  if (!connection || connection.factoryProjectId !== request.factoryProjectId) {\n    throw new Error('Factory session does not belong to this project');\n  }\n  return session;\n}\n\nasync function configureThread(session: FactorySession, request: FactoryStartRequest): Promise<string> {\n  const threadId = session.thread.requireId();\n  await session.thread.rename({ title: request.threadTitle });\n  const settings = { ...(request.threadTags ?? {}), factorySessionId: request.sessionId };\n  await Promise.all(Object.entries(settings).map(([key, value]) => session.thread.setSetting({ key, value })));\n  return threadId;\n}\n\nexport class FactoryStartCoordinator {\n  readonly #controller: FactoryController;\n  readonly #storage: WorkItemsStorage;","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/factory/src/rules/start-coordinator.ts#L77-L113","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Re-register/reconnect the project repository for this org so a projectRepositories row exists with the ID stored on session.projectRepositoryId.","Inspect the session row's projectRepositoryId and verify a matching record exists via storage.projectRepositories.get({orgId: request.orgId, id: session.projectRepositoryId}).","If the repository binding is permanently gone, delete the orphaned session and start a fresh factory session on the reconnected repo.","Check for cross-org mixups: the repo may exist but under a different orgId than request.orgId."],"exampleFix":"// before: session references a deleted repo row -> throws\nawait factory.prepare({ sessionId, orgId, userId });\n\n// after: reconnect the repo first, then repair the session pointer if the id changed\nconst repo = await storage.projectRepositories.create({ orgId, connectionId, ... });\nawait storage.sessions.update(sessionId, { projectRepositoryId: repo.id });\nawait factory.prepare({ sessionId, orgId, userId });","handlingStrategy":"validation","validationCode":"const session = await storage.sessions.getBySessionId(request.sessionId);\nconst repo = session && await storage.projectRepositories.get({ orgId: request.orgId, id: session.projectRepositoryId });\nif (!repo) throw new Error(`Session ${request.sessionId} references a project repository that no longer exists in org ${request.orgId}.`);","typeGuard":"function hasProjectRepository(session: FactorySession, repo: ProjectRepository | undefined): repo is ProjectRepository {\n  return !!repo && repo.id === session.projectRepositoryId;\n}","tryCatchPattern":"try {\n  const prepared = await coordinator.prepare(request);\n} catch (err) {\n  if (err instanceof Error && err.message === 'Factory session repository not found') {\n    console.error('The session’s repository binding was removed; reconnect the repo or start a new session.');\n  }\n  throw err;\n}","preventionTips":["When deleting/disconnecting a repository, also clean up or migrate its dependent sessions.","Run periodic integrity checks that every session.projectRepositoryId resolves within the org.","Restore backups as a complete graph (sessions + repositories together)."],"tags":["repository","referential-integrity","storage","not-found"],"backgroundTag":"resource-not-found","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}