-
Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(knowledge): unschedule a connector whose credential the source rejected and prompt to reconnect #8158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
waleedlatif1
merged 2 commits into
staging
from
fix/connector-reconnect-on-revoked-credential
Sep 22, 2026
Merged
fix(knowledge): unschedule a connector whose credential the source rejected and prompt to reconnect #8158
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
apps/sim/lib/knowledge/connectors/credential-recovery.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { dbChainMockFns, queueTableRows, resetDbChainMock, schemaMock } from '@sim/testing' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { resumeConnectorsAfterCredentialReconnect } from '@/lib/knowledge/connectors/credential-recovery' | ||
| import { CREDENTIAL_REVOKED_SYNC_ERROR } from '@/lib/knowledge/connectors/sync-limits' | ||
|
|
||
| const RESUMED = { | ||
| status: 'active', | ||
| lastSyncError: null, | ||
| consecutiveFailures: 0, | ||
| } | ||
|
|
||
| describe('resumeConnectorsAfterCredentialReconnect', () => { | ||
| const now = new Date('2026-09-22T20:00:00.000Z') | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| resetDbChainMock() | ||
| }) | ||
|
|
||
| it('resumes the connectors of every credential on the reconnected account', async () => { | ||
| queueTableRows(schemaMock.account, [ | ||
| { providerId: 'confluence', providerAccountId: 'subject-1' }, | ||
| ]) | ||
| await resumeConnectorsAfterCredentialReconnect('account-1', now) | ||
| expect(dbChainMockFns.set).toHaveBeenCalledWith({ ...RESUMED, nextSyncAt: now, updatedAt: now }) | ||
| const guard = JSON.stringify(dbChainMockFns.where.mock.calls.at(-1)) | ||
| expect(guard).toContain('knowledgeConnector.credentialId') | ||
| expect(guard).toContain(CREDENTIAL_REVOKED_SYNC_ERROR) | ||
| const credentials = JSON.stringify(dbChainMockFns.where.mock.calls) | ||
| expect(credentials).toContain('"left":"credential.accountId","right":"account-1"') | ||
| }) | ||
|
|
||
| it('resumes across the Slack installation, whose sibling accounts share the repaired chain', async () => { | ||
| queueTableRows(schemaMock.account, [ | ||
| { providerId: 'slack', providerAccountId: 'TEXAMPLE-usr_U1' }, | ||
| ]) | ||
| await resumeConnectorsAfterCredentialReconnect('account-1', now) | ||
| expect(dbChainMockFns.set).toHaveBeenCalledWith({ ...RESUMED, nextSyncAt: now, updatedAt: now }) | ||
| const conditions = JSON.stringify(dbChainMockFns.where.mock.calls) | ||
| expect(conditions).toContain('"pattern":"TEXAMPLE-%"') | ||
| expect(conditions).not.toContain('"left":"credential.accountId","right":"account-1"') | ||
| }) | ||
|
|
||
| it('does nothing for an account that no longer exists', async () => { | ||
| queueTableRows(schemaMock.account, []) | ||
| await resumeConnectorsAfterCredentialReconnect('account-gone', now) | ||
| expect(dbChainMockFns.update).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { db } from '@sim/db' | ||
| import { account, credential, knowledgeConnector } from '@sim/db/schema' | ||
| import { and, eq, inArray } from 'drizzle-orm' | ||
| import { CREDENTIAL_REVOKED_SYNC_ERROR } from '@/lib/knowledge/connectors/sync-limits' | ||
| import { extractSlackTeamId, installationFilter, isSlackProvider } from '@/lib/oauth/slack' | ||
|
|
||
| /** | ||
| * Puts the connectors a reconnected account had unscheduled back on their schedule. | ||
| * | ||
| * A sync that finds its credential rejected by the source leaves the connector unscheduled | ||
| * with {@link CREDENTIAL_REVOKED_SYNC_ERROR}, since retrying cannot help until someone | ||
| * authorizes again. Reauthorizing the account is that moment: every connector on a credential | ||
| * of that account still carrying the error is due now, with its failure count cleared. A Slack | ||
| * reauthorization repairs the installation's shared token chain, so the connectors on every | ||
| * credential of the installation's sibling accounts are due as well. Connectors paused or | ||
| * disabled for another reason keep their state, and a connector that already moved on is left | ||
| * alone. | ||
| */ | ||
| export async function resumeConnectorsAfterCredentialReconnect( | ||
| accountId: string, | ||
| now: Date | ||
| ): Promise<void> { | ||
| const [reconnected] = await db | ||
| .select({ providerId: account.providerId, providerAccountId: account.accountId }) | ||
| .from(account) | ||
| .where(eq(account.id, accountId)) | ||
| .limit(1) | ||
| if (!reconnected) return | ||
| const slackTeamId = isSlackProvider(reconnected.providerId) | ||
| ? extractSlackTeamId(reconnected.providerAccountId) | ||
| : null | ||
| const repairedAccounts = slackTeamId | ||
| ? inArray( | ||
| credential.accountId, | ||
| db.select({ id: account.id }).from(account).where(installationFilter(slackTeamId)) | ||
| ) | ||
| : eq(credential.accountId, accountId) | ||
| await db | ||
| .update(knowledgeConnector) | ||
| .set({ | ||
| status: 'active', | ||
| lastSyncError: null, | ||
| consecutiveFailures: 0, | ||
| nextSyncAt: now, | ||
| updatedAt: now, | ||
| }) | ||
| .where( | ||
| and( | ||
| inArray( | ||
| knowledgeConnector.credentialId, | ||
| db.select({ id: credential.id }).from(credential).where(repairedAccounts) | ||
| ), | ||
| eq(knowledgeConnector.status, 'error'), | ||
| eq(knowledgeConnector.lastSyncError, CREDENTIAL_REVOKED_SYNC_ERROR) | ||
| ) | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.