mastra-ai/mastra · error

Repository ${id} not found in organization ${orgId}

Error message

Repository ${id} not found in organization ${orgId}

What it means

Thrown by the in-memory migrateInstallation when the repository identified by { orgId, id } does not exist. Migration requires an existing repository row before moving it to a new installation.

Source

Thrown at mastracode/factory/src/storage/domains/source-control/inmemory.ts:155

        providerMetadata: input.providerMetadata ?? {},
        createdAt: now,
        updatedAt: now,
      };
      this.repositoriesRows.push(created);
      return created;
    },
    migrateInstallation: async ({
      orgId,
      id,
      newInstallationId,
    }: {
      orgId: string;
      id: string;
      newInstallationId: string;
    }) => {
      const existing = await this.repositories.get({ orgId, id });
      if (!existing) {
        throw new Error(`Repository ${id} not found in organization ${orgId}`);
      }
      if (!(await this.installations.get({ orgId, id: newInstallationId }))) {
        throw new Error('Source-control installation not found');
      }
      // Check if a repository with the same external_id exists under the new installation
      const conflict = this.repositoriesRows.find(
        row => row.installationId === newInstallationId && row.externalId === existing.externalId,
      );
      if (conflict) {
        // Return the existing repository under the new installation
        return conflict;
      }
      // Update the repository's installation
      existing.installationId = newInstallationId;
      existing.updatedAt = new Date();
      // Migrate dependent connections to the new installation
      for (const conn of this.connectionsRows) {
        if (conn.installationId === existing.installationId) {

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Ensure the repository is created in the in-memory store before migrating
  2. Use the id returned by repositories.upsert/get
  3. Confirm orgId matches the repository's org

Example fix

// before
await storage.migrateInstallation({ orgId, id: repoId, newInstallationId });
// after
if (!(await storage.repositories.get({ orgId, id: repoId }))) {
  await storage.repositories.upsert({ orgId, input: { installationId: oldInstId, externalId, name } });
}
await storage.migrateInstallation({ orgId, id: repoId, newInstallationId });
Defensive patterns

Strategy: validation

Validate before calling

if (!(await storage.repositories.get({ orgId, id }))) {
  throw new Error(`Repository ${id} must exist before migration`);
}

Type guard

function isExistingRepo(r: SourceControlRepository | null): r is SourceControlRepository {
  return r !== null && typeof r.externalId === 'string';
}

Try / catch

try {
  await storage.migrateInstallation({ orgId, id, newInstallationId });
} catch (e) {
  if (e instanceof Error && e.message.startsWith('Repository')) {
    throw new Error(`Seed repository ${id} in the in-memory store first`);
  } else throw e;
}

Prevention

When it happens

Trigger: Calling migrateInstallation on the in-memory storage with a deleted or never-created repository id, or with a mismatched orgId.

Common situations: Test state reset between cases; ids fabricated in tests; orgId typo so the lookup misses.

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/222f9380c5e4dcfe. Report an issue: GitHub.