mastra-ai/mastra · error

GitHub subscriptions require an authenticated repository ses

Error message

GitHub subscriptions require an authenticated repository session with an active thread.

What it means

resolveSessionTarget extracts the agent controller request context, thread id, project repository id, org id, and user id from the incoming request session. If any of these is missing, the request cannot be tied to an authenticated repository session and this error is thrown. It ensures GitHub subscriptions only happen inside a valid, authenticated repo session with an active thread.

Source

Thrown at mastracode/factory/src/integrations/github/session-subscriptions.ts:105

 * throwing, for passive callers that should no-op instead of erroring.
 */
function isGithubProjectSession(requestContext: RequestContext): boolean {
  const context = requestContext.get('controller') as AgentControllerRequestContext<RepositorySessionState> | undefined;
  return Boolean(
    context?.threadId &&
    context.getState().projectRepositoryId &&
    sessionOrgId(requestContext) &&
    sessionUserId(requestContext),
  );
}

async function resolveSessionTarget(requestContext: RequestContext, github: GithubIntegration): Promise<SessionTarget> {
  const context = requestContext.get('controller') as AgentControllerRequestContext<RepositorySessionState> | undefined;
  const orgId = sessionOrgId(requestContext);
  const userId = sessionUserId(requestContext);
  const projectRepositoryId = context?.getState().projectRepositoryId;
  if (!context || !context.threadId || !projectRepositoryId || !orgId || !userId) {
    throw new Error('GitHub subscriptions require an authenticated repository session with an active thread.');
  }

  const projectRepository = await github.sourceControlStorage.projectRepositories.get({
    orgId,
    id: projectRepositoryId,
  });
  if (!projectRepository) throw new Error('Project repository not found for this organization.');
  const connection = await github.sourceControlStorage.connections.get({ orgId, id: projectRepository.connectionId });
  if (!connection) throw new Error('Source-control connection not found for this organization.');
  const repository = await github.sourceControlStorage.repositories.get({ orgId, id: projectRepository.repositoryId });
  if (!repository) throw new Error('Repository not found for this organization.');
  const installation = await github.sourceControlStorage.installations.get({ orgId, id: connection.installationId });
  if (!installation) throw new Error('Source-control installation not found for this organization.');
  return { context, projectRepository, connection, installation, repository, orgId, userId };
}

async function verifyPullRequest(target: SessionTarget, pullRequest: number, github: GithubIntegration) {
  const [owner, repo] = target.repository.slug.split('/');

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Authenticate the request so orgId and userId are present in the session
  2. Start an agent session thread before invoking subscription endpoints
  3. Set projectRepositoryId in the repository session state before subscribing
  4. Ensure the request flows through the agent controller that populates RequestContext 'controller'
Defensive patterns

Strategy: validation

Validate before calling

const state = controller?.getState();
const ready = !!(controller?.threadId && state?.projectRepositoryId && sessionOrgId(req) && sessionUserId(req));
if (!ready) throw new Error('subscription requires an authenticated repo session with an active thread');

Try / catch

try {
  await subscribe(...);
} catch (e) {
  if (e.message.includes('authenticated repository session')) {
    // redirect user to authenticate and start a repository session
  }
}

Prevention

When it happens

Trigger: Calling the subscriptions target resolver without an agent controller context, with no active threadId, without a projectRepositoryId in session state, or without orgId/userId session values.

Common situations: Calling the API outside of a repository session (e.g., unauthenticated HTTP request), session state not initialized with a project repository, or anonymous/expired sessions lacking org/user identifiers.

Understand the failure class

Related errors


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