different-ai/openwork · error · PluginArchRouteFailure

organization_not_found

organization_not_found

Error message

Organization not found for connector instance.

What it means

Thrown in the connector-instance resolution path of the plugin-system store when the OrganizationTable lookup for `input.connectorInstance.organizationId` returns no rows. The connector instance record references an organization id that no longer exists in the database. This is a 404 PluginArchRouteFailure raised before member/session resolution proceeds.

Source

Thrown at ee/apps/den-api/src/routes/org/plugin-system/store.ts:5857

    branch,
    connectorAccount,
    connectorInstance,
    connectorTarget,
    installationId,
    ref,
    repositoryFullName,
  }
}

async function buildConnectorAutomationContext(input: { connectorInstance: ConnectorInstanceRow }) {
  const organizationRows = await db
    .select()
    .from(OrganizationTable)
    .where(eq(OrganizationTable.id, input.connectorInstance.organizationId))
    .limit(1)
  const organization = organizationRows[0] as OrganizationRow | undefined
  if (!organization) {
    throw new PluginArchRouteFailure(404, "organization_not_found", "Organization not found for connector instance.")
  }

  const memberRows = await db
    .select()
    .from(MemberTable)
    .where(and(
      eq(MemberTable.organizationId, input.connectorInstance.organizationId),
      eq(MemberTable.id, input.connectorInstance.createdByOrgMembershipId),
      isNull(MemberTable.removedAt),
    ))
    .limit(1)
  const member = memberRows[0] as MemberRow | undefined
  if (!member) {
    throw new PluginArchRouteFailure(404, "member_not_found", "Connector creator member not found.")
  }

  if (!member.userId) {
    throw new PluginArchRouteFailure(404, "member_not_joined", "Connector creator member has not joined the organization.")

View on GitHub (pinned to 2b7df46e8a)

Solutions

  1. Verify the connector instance's organizationId exists: SELECT * FROM organization WHERE id = '<orgId>'.
  2. Delete or re-parent the orphaned connector instance row, or restore the missing organization.
  3. If triggered by a test, ensure fixtures create the organization before creating connector instances and clean up in dependency order.

Example fix

// before
const instance = await getConnectorInstance(instanceId) // row exists, org deleted
await resolveConnectorSession(instance) // 404 organization_not_found
// after
const org = await db.select().from(OrganizationTable).where(eq(OrganizationTable.id, instance.organizationId)).limit(1)
if (!org[0]) await deleteConnectorInstance(instance.id) // clean orphan before resolving
await resolveConnectorSession(instance)
Defensive patterns

Strategy: validation

Validate before calling

const org = await db.select().from(OrganizationTable).where(eq(OrganizationTable.id, instance.organizationId)).limit(1)
if (!org[0]) throw new Error(`Org ${instance.organizationId} missing; clean up connector instance ${instance.id} first`)

Type guard

function orgExists(row: OrganizationRow | undefined): row is OrganizationRow { return row !== undefined }

Try / catch

try {
  await resolveConnectorInstance(instanceId)
} catch (e) {
  if (e instanceof PluginArchRouteFailure && e.code === 'organization_not_found') {
    await deleteConnectorInstance(instanceId) // remove orphan
  } else throw e
}

Prevention

When it happens

Trigger: Resolving a connector instance (e.g. GitHub connector automation/session resolution) whose `organizationId` points to a deleted or nonexistent row in the OrganizationTable; querying with a stale connectorInstance payload from another environment or database snapshot.

Common situations: Organization was hard-deleted (or cleaned up by a test fixture) while connector instance rows were left orphaned; cross-environment data restore where instances were copied without their parent orgs; hand-crafted API calls referencing a fabricated connector instance id.

Related errors


AI-assisted analysis of different-ai/openwork@2b7df46e8a (2026-09-01). Data as JSON: /api/errors/c1519c517ac7da39. Report an issue: GitHub.