[BUG] Slack-triggered Factory runs fail with "No usable <provider> credential" after a restart: Slack path never primes tenant credentials
Versions
@mastra/factory0.17.2,@mastra/core1.71.0,@mastra/code-sdk1.8.3,@chat-adapter/slack4.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:
gateDispatchinintegrations/slack/slack.jssetsctx.requestContext.set("user", { id, organizationId })for a linked sender and the handler then callsdefaultHandler, without priming. The snapshot is only filled if a web request by that user happened since the last restart.
Repro
- Store a provider key as an organization credential; link a Slack account.
- Restart the server.
- Before opening the web UI, mention the bot in Slack →
ProviderAuthRequiredError. - 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.
Source: mastra-ai/mastra