fix(accounts): apply only the renamed account from a rename reply

A rename reply built before a newer select or remove no longer replaces
the whole roster, so it can't undo that change. Local settings are read
first so the row never briefly falls back to the email.

Co-authored-by: Wooseong Kim <innocarpe@gmail.com>
This commit is contained in:
Brennan Benson
2026-11-12 21:22:46 -08:00
co-authored by Wooseong Kim
parent 8cccde8a7b
commit 037daea17b
2 changed files with 70 additions and 12 deletions
@@ -0,0 +1,32 @@
import { describe, expect, it } from 'vitest'
import { withRenamedAccount } from './account-rename-runner'
describe('withRenamedAccount', () => {
it('applies a late rename reply without undoing a newer selection or removal', () => {
// The rename reply was built while A and B existed and B was selected.
const reply = {
activeAccountId: 'b',
accounts: [
{ id: 'a', email: 'a@example.com', displayName: 'Work' },
{ id: 'b', email: 'b@example.com' }
]
}
// Meanwhile C was selected and B removed.
const current = {
activeAccountId: 'c',
accounts: [
{ id: 'a', email: 'a@example.com' },
{ id: 'c', email: 'c@example.com' }
]
}
expect(withRenamedAccount(current, reply, 'a')).toEqual({
activeAccountId: 'c',
accounts: [
{ id: 'a', email: 'a@example.com', displayName: 'Work' },
{ id: 'c', email: 'c@example.com' }
]
})
// A rename of an account removed meanwhile does not bring it back.
expect(withRenamedAccount(current, reply, 'b')).toBe(current)
})
})
@@ -20,6 +20,29 @@ export function renamingAccountKey(target: Pick<RenameAccountTarget, 'provider'
return `${target.provider}:${target.id}`
}
export function withRenamedAccount<
State extends { accounts: readonly { id: string; displayName?: string }[] }
>(
current: State,
reply: { accounts: readonly { id: string; displayName?: string }[] },
accountId: string
): State {
const displayName = reply.accounts.find((account) => account.id === accountId)?.displayName
if (!current.accounts.some((account) => account.id === accountId)) {
return current
}
return {
...current,
accounts: current.accounts.map((account) => {
if (account.id !== accountId) {
return account
}
const { displayName: _previous, ...rest } = account
return displayName ? { ...rest, displayName } : rest
})
}
}
type AccountRenameContext = {
accountOwner: RuntimeClientTarget
hostLabel: string
@@ -51,21 +74,24 @@ export function createAccountRenameRunner(
const rename = { accountId: target.id, displayName }
setPending(key, true)
try {
if (target.provider === 'claude') {
const next = await renameClaudeProviderAccount(accountOwner, rename, hostLabel)
if (isCurrentAccountOwner()) {
context.setClaudeAccounts(next)
}
} else {
const next = await renameCodexProviderAccount(accountOwner, rename, hostLabel)
if (isCurrentAccountOwner()) {
context.setCodexAccounts(next)
}
}
// Local names live in this desktop's settings, which the status bar reads.
const reply =
target.provider === 'claude'
? await renameClaudeProviderAccount(accountOwner, rename, hostLabel)
: await renameCodexProviderAccount(accountOwner, rename, hostLabel)
// Local names live in this desktop's settings; read them first so the pane's
// settings overlay already holds the new name when the row updates.
if (!context.isRemoteAccountScope) {
await context.fetchSettings()
}
// Apply only this account's name: a select or remove may have answered after the
// host built this reply, and its whole roster would undo that newer state.
if (isCurrentAccountOwner()) {
if (target.provider === 'claude') {
context.setClaudeAccounts((current) => withRenamedAccount(current, reply, target.id))
} else {
context.setCodexAccounts((current) => withRenamedAccount(current, reply, target.id))
}
}
} catch (error) {
if (target.provider === 'claude') {
toast.error(