#25602·mastra

[BUG] Slack-triggered Factory runs fail with "No usable <provider> credential" after a restart: Slack path never primes tenant credentials

Author: tomasvargaCreated Sep 30, 2026Updated Sep 30, 2026
Labelsstatus: needs triage

Versions

  • @mastra/factory 0.17.2, @mastra/core 1.71.0, @mastra/code-sdk 1.8.3, @chat-adapter/slack 4.41.0
  • Self-hosted Factory with Better Auth; OpenRouter key stored as an organization credential

Expected

A linked Slack user who mentions or DMs the bot gets an answer using the workspace's organization credential, like in the web UI (the behaviour described in #20060: "your agent, your model credentials").

Actual

After the server restarts, the first Slack-triggered run fails until the same user makes any web request:

[CHANNEL] [slack] Error handling message {
  authorId: 'U…',
  error: 'ProviderAuthRequiredError: No usable openrouter credential is configured for this signed-in Factory account. Connect the provider or add an organization credential, then try again.'
}

Once that user opens the web UI, Slack works until the next restart.

Cause

Credentials are resolved from an in-memory, per-tenant snapshot (routes/tenant-credentials.js). Its own docs for primeTenantCredentialsForRequestContext:

Prime the tenant stores for a request context built outside the web layer, such as a webhook-triggered session run. The auth middleware does this for HTTP requests; a run that starts without one would otherwise resolve its model against an empty snapshot and fail closed.

  • The web auth middleware primes on every request.
  • GitHub subscription runs prime explicitly (integrations/subscription-session.js).
  • The Slack path doesn't: gateDispatch in integrations/slack/slack.js sets ctx.requestContext.set("user", { id, organizationId }) for a linked sender and the handler then calls defaultHandler, without priming. The snapshot is only filled if a web request by that user happened since the last restart.

Repro

  1. Store a provider key as an organization credential; link a Slack account.
  2. Restart the server.
  3. Before opening the web UI, mention the bot in Slack → ProviderAuthRequiredError.
  4. Open the web UI, mention it again → works.

Suggested fix

In gateDispatch, after setting the linked user, await primeTenantCredentialsForRequestContext(ctx.requestContext), the same call subscription-session.js makes. Slash-command dispatch skips these handlers and would need it too.

Workaround

A host can wrap the Slack integration's onDirectMessage / onMention / onSubscribedMessage handlers so their defaultHandler first awaits primeTenantCredentialsForRequestContext(ctx.requestContext). We run this in production and it fixes the failure.