Skip to content

The agent that grows with you

fix(desktop): reconnect goes by account id, failed sign-ins retry, add errors show

Siddharth Balyandf728fc7b4a0
diff --git a/apps/desktop/src/app/capabilities/connectors/accounts-section.test.tsx b/apps/desktop/src/app/capabilities/connectors/accounts-section.test.tsx
index fce188ae671..e11d638e10a 100644
--- a/apps/desktop/src/app/capabilities/connectors/accounts-section.test.tsx
+++ b/apps/desktop/src/app/capabilities/connectors/accounts-section.test.tsx
@@ -11,7 +11,7 @@ import { setPrimaryGateway } from '@/store/gateway'
import { type AccountEdit, AccountsSection } from './accounts-section'
import { connectorsAccountsQueryKey } from './data/keys'
-import { useRenameAccount } from './data/mutations'
+import { useRenameAccount, type WriteOutcome } from './data/mutations'
const SCOPE = 'default'
@@ -40,7 +40,15 @@ const ACCOUNTS: ConnectorAccountsResult = {
]
}
-function Harness() {
+function Harness({
+ connecting = false,
+ onAdd = async () => ({ ok: true }),
+ retryable = null
+}: {
+ connecting?: boolean
+ onAdd?: (alias: string) => Promise<WriteOutcome>
+ retryable?: null | string
+}) {
// A refetch never answers, so only the rename's own reconcile or rollback can change the row.
const accounts = useQuery({
queryFn: () => new Promise<ConnectorAccountsResult>(() => undefined),
@@ -54,26 +62,27 @@ function Harness() {
return (
<AccountsSection
accounts={accounts.data?.accounts ?? []}
- connecting={false}
+ connecting={connecting}
edit={edit}
- onAdd={async () => ({ ok: true })}
+ onAdd={onAdd}
onEditChange={setEdit}
onReconnect={() => undefined}
onRemove={() => undefined}
onRename={renamer.rename}
reconnecting={false}
retired={[]}
+ retryable={retryable}
/>
)
}
-function renderSection() {
- queryClient.setQueryData(connectorsAccountsQueryKey(SCOPE), ACCOUNTS)
+function renderSection(props: Parameters<typeof Harness>[0] = {}, accounts: ConnectorAccountsResult = ACCOUNTS) {
+ queryClient.setQueryData(connectorsAccountsQueryKey(SCOPE), accounts)
return render(
<QueryClientProvider client={queryClient}>
<I18nProvider>
- <Harness />
+ <Harness {...props} />
</I18nProvider>
</QueryClientProvider>
)
@@ -191,3 +200,41 @@ describe('AccountsSection rename', () => {
expect(renameCalls(request)).toEqual([])
})
})
+
+describe('AccountsSection add', () => {
+ it('shows why the gateway refused a typed name until the user changes it', async () => {
+ renderSection({
+ onAdd: async () => ({
+ error: { message: 'Sign-in could not start.', reason: 'CONNECTOR_REQUEST_FAILED' } as never,
+ ok: false
+ })
+ })
+
+ fireEvent.click(screen.getByRole('button', { name: 'Add another account' }))
+ const field = screen.getByRole('textbox', { name: copyLabel() })
+ fireEvent.change(field, { target: { value: 'desk' } })
+ fireEvent.click(screen.getByRole('button', { name: 'Connect' }))
+
+ await waitFor(() => expect(screen.getByRole('alert')).toBeTruthy())
+
+ fireEvent.change(screen.getByRole('textbox', { name: copyLabel() }), { target: { value: 'desk2' } })
+ expect(screen.queryByRole('alert')).toBeNull()
+ })
+})
+
+describe('AccountsSection reconnect while a sign-in is open', () => {
+ const broken: ConnectorAccountsResult = {
+ accounts: ACCOUNTS.accounts.map(row => ({ ...row, active: false, status: 'expired' as const }))
+ }
+
+ it('lets the failed account retry and keeps every other account blocked', () => {
+ renderSection({ connecting: true, retryable: 'ca_work' }, broken)
+
+ const [work, home] = screen.getAllByRole('button', { name: 'Reconnect' }) as HTMLButtonElement[]
+
+ expect(work.disabled).toBe(false)
+ expect(home.disabled).toBe(true)
+ })
+})
+
+const copyLabel = () => 'Account name'
diff --git a/apps/desktop/src/app/capabilities/connectors/accounts-section.tsx b/apps/desktop/src/app/capabilities/connectors/accounts-section.tsx
index 1f51708cc1f..9d05686ea07 100644
--- a/apps/desktop/src/app/capabilities/connectors/accounts-section.tsx
+++ b/apps/desktop/src/app/capabilities/connectors/accounts-section.tsx
@@ -47,6 +47,8 @@ export interface AccountsSectionProps {
onRename: (connectionId: string, alias: string) => Promise<WriteOutcome>
reconnecting: boolean
retired: readonly AccountRow[]
+ /** The account whose failed sign-in the open operation may retry while `connecting` blocks the others. */
+ retryable?: null | string
}
export function AccountsSection({
@@ -59,7 +61,8 @@ export function AccountsSection({
onRemove,
onRename,
reconnecting,
- retired
+ retired,
+ retryable = null
}: AccountsSectionProps) {
const { t } = useI18n()
const copy = t.connectorsPage.accounts
@@ -114,6 +117,7 @@ export function AccountsSection({
label={copy.renameLabel(accountName(account))}
onCancel={() => onEditChange(null)}
onSubmit={alias => void rename(account.connection_id, alias)}
+ refusedValue={edit.draft}
self={account.connection_id}
submitLabel={t.common.save}
unchanged={account.alias ?? ''}
@@ -125,7 +129,7 @@ export function AccountsSection({
<>
{account.status !== 'active' && STATUS_VIEW[account.status].reconnect ? (
<Button
- disabled={reconnecting || connecting}
+ disabled={reconnecting || (connecting && account.connection_id !== retryable)}
onClick={() => onReconnect(account)}
size="xs"
variant="secondary"
@@ -163,6 +167,7 @@ export function AccountsSection({
label={copy.aliasLabel}
onCancel={() => onEditChange(null)}
onSubmit={alias => void add(alias)}
+ refusedValue={edit.draft}
submitLabel={copy.add}
/>
) : accounts.length > 0 ? (
@@ -246,6 +251,8 @@ interface AliasEditorProps {
label: string
onCancel: () => void
onSubmit: (alias: string) => void
+ /** The value `error` refused; the error shows while the field still holds it. */
+ refusedValue?: string
self?: string
submitLabel: string
/** Submitting this value closes the editor without a write. */
@@ -261,6 +268,7 @@ function AliasEditor({
label,
onCancel,
onSubmit,
+ refusedValue,
self,
submitLabel,
unchanged
@@ -269,8 +277,7 @@ function AliasEditor({
const copy = t.connectorsPage.accounts
const [value, setValue] = useState(initial)
const [invalid, setInvalid] = useState<null | string>(null)
- const [edited, setEdited] = useState(false)
- const error = invalid ?? (edited ? undefined : refused)
+ const error = invalid ?? (refusedValue !== undefined && value.trim() === refusedValue ? refused : undefined)
const submit = () => {
const alias = value.trim()
@@ -310,7 +317,6 @@ function AliasEditor({
onChange={event => {
setValue(event.target.value.toLowerCase())
setInvalid(null)
- setEdited(true)
}}
placeholder={label}
size="sm"
diff --git a/apps/desktop/src/app/capabilities/connectors/connect-element.tsx b/apps/desktop/src/app/capabilities/connectors/connect-element.tsx
index 1fa197b220f..89f039e9e19 100644
--- a/apps/desktop/src/app/capabilities/connectors/connect-element.tsx
+++ b/apps/desktop/src/app/capabilities/connectors/connect-element.tsx
@@ -8,10 +8,12 @@ import { type AccountOperation, syncAccountOperation } from './data/account-oper
export interface ConnectElementProps {
operation: AccountOperation
+ /** Restarts the failed sign-in of the account the operation is for; absent while one is still waiting. */
+ onRetry?: () => void
onStopWaiting: () => void
}
-export function ConnectElement({ operation, onStopWaiting }: ConnectElementProps) {
+export function ConnectElement({ operation, onRetry, onStopWaiting }: ConnectElementProps) {
const { t } = useI18n()
const copy = t.connectorsPage
const link = operation.targets.find(target => target.connectUrl)?.connectUrl ?? null
@@ -40,6 +42,12 @@ export function ConnectElement({ operation, onStopWaiting }: ConnectElementProps
</Button>
) : null}
+ {onRetry ? (
+ <Button onClick={onRetry} size="xs" variant="secondary">
+ {copy.card.verb.tryAgain}
+ </Button>
+ ) : null}
+
<Button onClick={onStopWaiting} size="xs" variant="text">
{copy.card.verb.stopWaiting}
</Button>
diff --git a/apps/desktop/src/app/capabilities/connectors/connectors-tab.tsx b/apps/desktop/src/app/capabilities/connectors/connectors-tab.tsx
index 27a2a404bc0..c495458a506 100644
--- a/apps/desktop/src/app/capabilities/connectors/connectors-tab.tsx
+++ b/apps/desktop/src/app/capabilities/connectors/connectors-tab.tsx
@@ -141,11 +141,11 @@ export function ConnectorsTab({ gateway, profile }: ConnectorsTabProps) {
return outcome
}
- // A row passes its own account, named or not. An app-level reconnect or Try again (no account) names the
- // account the summary speaks for, or nothing, so it still matches the sign-in that is already open.
+ // A row passes its own account. An app-level reconnect or Try again (no account) names the account the
+ // summary speaks for, or nothing; either goes by id, so it still matches the sign-in that is already open.
const startConnect = async (card: ConnectorCardModel, reconnect: boolean, account?: AccountRow) => {
- const fallback = reconnect ? pickAccount(hosted.accounts, card.slug)?.alias : undefined
- const address = account ? accountAddress(account) : { alias: fallback ?? undefined }
+ const target = account ?? (reconnect ? pickAccount(hosted.accounts, card.slug) : undefined)
+ const address = target ? accountAddress(target) : {}
const outcome = await connectAccount(card, reconnect, address)
if (!outcome.ok) {
@@ -299,6 +299,15 @@ export function ConnectorsTab({ gateway, profile }: ConnectorsTabProps) {
pendingRename,
reconnect: (account: AccountRow) => void startConnect(openCard, true, account),
reconnecting: connector.pending === openCard.slug,
+ retry: operation => {
+ const { reconnect, ...address } = operation.request
+
+ void connectAccount(openCard, reconnect, address).then(outcome => {
+ if (!outcome.ok) {
+ notifyError(outcome.error, t.connectors.connectErrorFor(openCard.name))
+ }
+ })
+ },
remove: (account: AccountRow) => setDisconnecting({ account, card: openCard }),
rename: renamer.rename,
renameOpened
diff --git a/apps/desktop/src/app/capabilities/connectors/data/account-operations.test.ts b/apps/desktop/src/app/capabilities/connectors/data/account-operations.test.ts
index af2ccbe7787..24a93eba5f9 100644
--- a/apps/desktop/src/app/capabilities/connectors/data/account-operations.test.ts
+++ b/apps/desktop/src/app/capabilities/connectors/data/account-operations.test.ts
@@ -1,11 +1,14 @@
import { describe, expect, it } from 'vitest'
-import { type AccountOperation, accountOperationFor } from './account-operations'
+import type { ConnectionTarget } from '@/store/connection-request'
+
+import { type AccountOperation, accountOperationFor, retryableAccount } from './account-operations'
const operation = (opId: string, scope: AccountOperation['scope'], settled = false): AccountOperation => ({
connectors: ['gmail'],
deadlineAt: 1_800_000_000,
opId,
+ request: { reconnect: false },
scope,
seq: 0,
settled,
@@ -31,3 +34,36 @@ describe('accountOperationFor', () => {
expect(accountOperationFor(operations, 'gmail', { connectionId: 'local', profile: 'home' })?.opId).toBe('a')
})
})
+
+describe('retryableAccount', () => {
+ const target = (state: ConnectionTarget['state']): ConnectionTarget => ({
+ action: 'reconnect',
+ connectUrl: null,
+ connectionId: 'ca_new',
+ detail: '',
+ discoveryError: null,
+ instructions: null,
+ kind: 'connector',
+ name: 'gmail',
+ requiredEnv: [],
+ state,
+ tools: []
+ })
+
+ const repair = (state: ConnectionTarget['state']): AccountOperation => ({
+ ...operation('a', 'home'),
+ request: { connectionId: 'ca_work', reconnect: true },
+ targets: [target(state)]
+ })
+
+ it('names the account a failed or expired sign-in repairs, so only that row may retry', () => {
+ expect(retryableAccount(repair('failed'))).toBe('ca_work')
+ expect(retryableAccount(repair('expired'))).toBe('ca_work')
+ })
+
+ it('names nothing while a sign-in still waits, or for an add', () => {
+ expect(retryableAccount(repair('initiated'))).toBeNull()
+ expect(retryableAccount({ ...repair('failed'), request: { alias: 'work', reconnect: false } })).toBeNull()
+ expect(retryableAccount(null)).toBeNull()
+ })
+})
diff --git a/apps/desktop/src/app/capabilities/connectors/data/account-operations.ts b/apps/desktop/src/app/capabilities/connectors/data/account-operations.ts
index da09ccfe3d1..5f84e859f54 100644
--- a/apps/desktop/src/app/capabilities/connectors/data/account-operations.ts
+++ b/apps/desktop/src/app/capabilities/connectors/data/account-operations.ts
@@ -14,10 +14,18 @@ import { normalizeProfileKey } from '@/store/profile'
import { invalidateConnectors } from './keys'
import { accountOperationStatus } from './rpc'
+/** What started an operation, so a retry sends the same request and the backend reuses that operation. */
+export interface AccountOperationRequest {
+ alias?: string
+ connectionId?: string
+ reconnect: boolean
+}
+
export interface AccountOperation {
connectors: string[]
deadlineAt: number
opId: string
+ request: AccountOperationRequest
scope: ProfileScope
seq: number
settled: boolean
@@ -73,15 +81,33 @@ export function accountOperationFor(
return mine.find(operation => !operation.settled) ?? mine[mine.length - 1] ?? null
}
+const RETRYABLE: readonly ConnectionTarget['state'][] = ['failed', 'expired']
+
+/**
+ * An open operation whose every target failed or expired: nothing waits on a browser, and the backend keeps it
+ * open so the same request can mint a fresh link. Only that request may run again; another account may not start.
+ */
+export const retryable = (operation: AccountOperation | null): operation is AccountOperation =>
+ operation !== null &&
+ !operation.settled &&
+ operation.targets.length > 0 &&
+ operation.targets.every(target => RETRYABLE.includes(target.state))
+
+/** The account a retryable operation repairs, by the id its request named; null for an add or an app-level start. */
+export const retryableAccount = (operation: AccountOperation | null): null | string =>
+ retryable(operation) ? (operation.request.connectionId ?? null) : null
+
export function startAccountOperation(
scope: ProfileScope,
connectors: readonly string[],
- snapshot: ConnectorsConnectResult
+ snapshot: ConnectorsConnectResult,
+ request: AccountOperationRequest = { reconnect: false }
): AccountOperation {
const operation: AccountOperation = {
connectors: [...connectors],
deadlineAt: snapshot.deadline_at,
opId: snapshot.op_id,
+ request,
scope,
seq: snapshot.seq,
settled: snapshot.settled,
diff --git a/apps/desktop/src/app/capabilities/connectors/data/connect-address.test.ts b/apps/desktop/src/app/capabilities/connectors/data/connect-address.test.ts
index 9ba9e1a8db8..43fef1cf371 100644
--- a/apps/desktop/src/app/capabilities/connectors/data/connect-address.test.ts
+++ b/apps/desktop/src/app/capabilities/connectors/data/connect-address.test.ts
@@ -35,8 +35,8 @@ function serveConnect() {
afterEach(() => setPrimaryGateway(null))
describe('reconnect addresses the account the user picked', () => {
- it('names a named account by its name', () => {
- expect(accountAddress(account({ alias: 'work', connection_id: 'ca_work' }))).toEqual({ alias: 'work' })
+ it('names a named account by its id, so an unconfirmed rename cannot point the repair at another account', () => {
+ expect(accountAddress(account({ alias: 'office', connection_id: 'ca_work' }))).toEqual({ connectionId: 'ca_work' })
})
it('names an unnamed account by its id, the only address it has', () => {
diff --git a/apps/desktop/src/app/capabilities/connectors/data/join.ts b/apps/desktop/src/app/capabilities/connectors/data/join.ts
index 87ec14f305c..d5f7ddb9b93 100644
--- a/apps/desktop/src/app/capabilities/connectors/data/join.ts
+++ b/apps/desktop/src/app/capabilities/connectors/data/join.ts
@@ -162,10 +162,10 @@ export function retiredAccountsFor(accounts: readonly AccountRow[], slug: string
/**
* How a reconnect names the account it repairs, so the gateway mints a replacement for that one and deletes
- * nothing: a named account goes by its name, an unnamed one by its id (it has nothing else).
+ * nothing: always its id. The name can be an optimistic rename the server has not confirmed, and another
+ * session may hold that name, so it never addresses a repair.
*/
-export const accountAddress = (account: AccountRow): ConnectAddress =>
- account.alias ? { alias: account.alias } : { connectionId: account.connection_id }
+export const accountAddress = (account: AccountRow): ConnectAddress => ({ connectionId: account.connection_id })
/** The one account the app's summary speaks for. */
export function pickAccount(accounts: readonly AccountRow[], slug: string): AccountRow | undefined {
diff --git a/apps/desktop/src/app/capabilities/connectors/data/mutations.ts b/apps/desktop/src/app/capabilities/connectors/data/mutations.ts
index 37e59504d0c..7ff5a12b719 100644
--- a/apps/desktop/src/app/capabilities/connectors/data/mutations.ts
+++ b/apps/desktop/src/app/capabilities/connectors/data/mutations.ts
@@ -271,12 +271,14 @@ export function useConnectConnector(scope: ProfileScope): ConnectorConnect {
setPending(slug)
try {
- const snapshot = await connectAccountConnectors(scope, [slug], options?.reconnect ?? false, {
+ const request = {
alias: options?.alias,
- connectionId: options?.connectionId
- })
+ connectionId: options?.connectionId,
+ reconnect: options?.reconnect ?? false
+ }
+ const snapshot = await connectAccountConnectors(scope, [slug], request.reconnect, request)
- return { ok: true, operation: startAccountOperation(scope, [slug], snapshot) }
+ return { ok: true, operation: startAccountOperation(scope, [slug], snapshot, request) }
} catch (error) {
return { error: asConnectorError(error), ok: false }
} finally {
diff --git a/apps/desktop/src/app/capabilities/connectors/hosted-dialog.tsx b/apps/desktop/src/app/capabilities/connectors/hosted-dialog.tsx
index b20c285f9fd..d6317b433bf 100644
--- a/apps/desktop/src/app/capabilities/connectors/hosted-dialog.tsx
+++ b/apps/desktop/src/app/capabilities/connectors/hosted-dialog.tsx
@@ -13,7 +13,9 @@ import {
$accountOperations,
type AccountOperation,
accountOperationFor,
- clearAccountOperation
+ clearAccountOperation,
+ retryable,
+ retryableAccount
} from './data/account-operations'
import type { WriteOutcome } from './data/mutations'
import { openConnectorsAdmin } from './data/portal'
@@ -58,6 +60,8 @@ export interface HostedAccountActions {
pendingRename: null | PendingRename
reconnect: (account: AccountRow) => void
reconnecting: boolean
+ /** Sends a failed operation's own request again, so the backend mints a fresh link on that operation. */
+ retry: (operation: AccountOperation) => void
remove: (account: AccountRow) => void
rename: (connectionId: string, alias: string) => Promise<WriteOutcome>
renameOpened: () => void
@@ -94,7 +98,13 @@ export function HostedConnectorDialog({
}, [inUse])
const accounts = card.ways.hosted?.accounts ?? []
- const accountList = useAccountList(card, accountActions, operation !== null && !operation.settled)
+
+ const accountList = useAccountList(
+ card,
+ accountActions,
+ operation !== null && !operation.settled,
+ retryableAccount(operation)
+ )
const local = card.ways.local
const serverName = localServerName(card)
@@ -116,7 +126,11 @@ export function HostedConnectorDialog({
)
const element = stillOpen(operation) ? (
- <ConnectElement onStopWaiting={() => onGiveUp(operation.opId)} operation={operation} />
+ <ConnectElement
+ onRetry={retryable(operation) ? () => accountActions.retry(operation) : undefined}
+ onStopWaiting={() => onGiveUp(operation.opId)}
+ operation={operation}
+ />
) : undefined
return (
@@ -194,7 +208,12 @@ function menuReconnect(
/** The account list's props and its one piece of local state: which name is being edited. A deep link's pending
* rename opens that account's editor once this app lists it. */
-function useAccountList(card: ConnectorCardModel, actions: HostedAccountActions, connecting: boolean) {
+function useAccountList(
+ card: ConnectorCardModel,
+ actions: HostedAccountActions,
+ connecting: boolean,
+ retryable: null | string
+) {
const [edit, setEdit] = useState<AccountEdit | null>(null)
const accounts = card.ways.hosted?.accounts ?? []
const { pendingRename, renameOpened } = actions
@@ -219,7 +238,8 @@ function useAccountList(card: ConnectorCardModel, actions: HostedAccountActions,
onRemove: actions.remove,
onRename: actions.rename,
reconnecting: actions.reconnecting,
- retired: card.ways.hosted?.retiredAccounts ?? []
+ retired: card.ways.hosted?.retiredAccounts ?? [],
+ retryable
}
// Escape cancels an open name edit and reports it, so the dialog stays open.