{"record":{"id":"6a06f5ca6f8112ba","repo":"mastra-ai/mastra","slug":"repository-link-session-projectrepositoryid-was","errorCode":null,"errorMessage":"Repository link ${session.projectRepositoryId} was not found","messagePattern":"Repository link (.+?) was not found","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/factory/src/workspace.ts","lineNumber":288,"sourceCode":"    if (!user?.organizationId || !userId) {\n      throw new Error(`Factory session ${session.sessionId} was resolved without a caller identity`);\n    }\n    // Org-visible sessions open to any member of the owning organization;\n    // only private sessions stay owner-only. Cross-org access never passes.\n    if (user.organizationId !== session.orgId || (session.visibility === 'private' && userId !== session.userId)) {\n      throw new Error(`Factory session ${session.sessionId} is not available to the current user`);\n    }\n    if (!sandboxConfig || !github) {\n      throw new Error('GitHub and a sandbox callback are required to create a Factory session workspace');\n    }\n    const createSessionSandboxInstance = sandboxConfig;\n\n    const storage = github.sourceControlStorage;\n    const projectRepository = await storage.projectRepositories.get({\n      orgId: session.orgId,\n      id: session.projectRepositoryId,\n    });\n    if (!projectRepository) throw new Error(`Repository link ${session.projectRepositoryId} was not found`);\n    // The remaining reads only depend on the repository link — issue them in\n    // parallel instead of paying four sequential storage round-trips.\n    const [connection, repository] = await Promise.all([\n      storage.connections.get({ orgId: session.orgId, id: projectRepository.connectionId }),\n      storage.repositories.get({ orgId: session.orgId, id: projectRepository.repositoryId }),\n    ]);\n    if (!connection || !repository) throw new Error(`Repository link ${session.projectRepositoryId} is incomplete`);\n    const installation = await storage.installations.get({ orgId: session.orgId, id: connection.installationId });\n    if (!installation) throw new Error(`GitHub installation ${connection.installationId} was not found`);\n    const repoFullName = repository.slug;\n\n    // Construct (or fetch) the session's memoized sandbox instance.\n    // Construction is cheap and side-effect-free by the callback contract —\n    // the VM is provisioned on `start()`, which only the materialization\n    // pipeline calls. The workdir is never persisted or trusted from storage\n    // or client input (the stale-workdir incident class came from reusing\n    // `session.sandboxWorkdir` written under a different provider): local\n    // sandboxes derive it at construction, remote sandboxes clone into the","sourceCodeStart":270,"sourceCodeEnd":306,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/factory/src/workspace.ts#L270-L306","documentation":"A Factory session references a project-repository link (projectRepositoryId). If storage.projectRepositories.get returns nothing for that id in the session's org, createWorkspaceFactory throws because it cannot determine which repository the workspace should be built around.","triggerScenarios":"Resolving a session whose projectRepositoryId points to a deleted project-repository link, an id from another org, or a session created against storage that has since been cleared/migrated.","commonSituations":"Repository link deleted by an admin while a session still references it, restoring sessions from a backup without the linked projectRepositories rows, or cross-environment storage mismatch.","solutions":["Recreate the repository link (projectRepositories.create) and update the session's projectRepositoryId","Confirm the link exists in the same org: storage.projectRepositories.get({ orgId: session.orgId, id: session.projectRepositoryId })","Delete and recreate the session against a valid repository link if the old link is unrecoverable"],"exampleFix":"// before\nconst factory = await createWorkspaceFactory({ session }); // link deleted\n// after\nconst link = await storage.projectRepositories.get({ orgId: session.orgId, id: session.projectRepositoryId });\nif (!link) {\n  const newLink = await storage.projectRepositories.create({ orgId: session.orgId, connectionId, repositoryId });\n  await storage.sessions.update({ id: session.id, projectRepositoryId: newLink.id });\n}\nconst factory = await createWorkspaceFactory({ session });","handlingStrategy":"validation","validationCode":"const link = await storage.projectRepositories.get({ orgId: session.orgId, id: session.projectRepositoryId });\nif (!link) throw new Error(`Repository link ${session.projectRepositoryId} missing; recreate it before resolving the session`);","typeGuard":"null","tryCatchPattern":"try {\n  factory = await createWorkspaceFactory({ session, user });\n} catch (e) {\n  if (e.message.includes('Repository link') && e.message.includes('was not found')) {\n    // recreate link + update session, then retry once\n  } else throw e;\n}","preventionTips":["Cascade-delete or reassign sessions when removing project-repository links","Back up and restore projectRepositories rows together with sessions","Verify link existence in the same org before resolving sessions"],"tags":["storage","missing-resource","github"],"backgroundTag":"referenced-record-not-found","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}