feat(desktop): offer to restart running agents onto the new default account

Merged
KI
3mo ago
+1137-207

Summary

  • Switching the default provider account in Settings → Usage now asks whether to restart that provider's running agent sessions; confirming relaunches each one with its own CLI session id on the new account — same conversation, new login.
  • Host side: terminalAgents.accountRestartCandidates / restartAccountSessions kill each candidate terminal crash-style so the existing auto-resume machinery does the rest.
  • Declining (or having no eligible sessions) keeps today's behavior and toast. The AddAccountDialog "Use for new agents" path routes through the same ask.

Why / Context

The PTY env is frozen at terminal spawn, so a default-account switch only reaches new launches — running agents silently keep the old login (the old toast just said "Relaunch running agents to switch them"). Since agent session state is shared across Claude profiles (session-share.ts) and every agent config carries resumeArgs, we can move a running session to the new account without losing the conversation.

How It Works

  • restartAccountSessions(provider) filters live bindings to the provider's resumable ones (session id captured, past attach, config presetId matches, non-empty resumeArgs), then for each: markTerminalExited before disposeSessionAndWait. terminal-exited (unlike killSession's disposed) satisfies the resume-candidate predicate, and marking first matters because the renderer re-checks candidates on the WS-close transition — the store's change event never reaches it.
  • Open panes auto-resume via the existing TerminalAgentAutoResume (claude --resume <id> etc.); the agent wrapper re-reads the account pointer at launch, which is what lands the session on the new account. Panes in unopened workspaces resume when next viewed, like any dead-terminal candidate.
  • Renderer: after setDefaultAccount succeeds, UsageView counts candidates and shows RestartSessionsDialog (skipped at zero); a failed dispose logs, keeps the candidate, and lets the reaper finish the kill.

Manual QA (done, dev app driven over CDP with two real Claude accounts)

  • Repro of the gap: switched default while an agent ran, declined → agent kept old CLAUDE_CONFIG_DIR, decline toast shown
  • Dialog appears only after a successful switch; singular and plural copy both exercised
  • Confirm: old claude killed, binding terminal-exited → pane auto-resumed claude --resume <session id>; new process env carried the new account's CLAUDE_CONFIG_DIR
  • Conversation continuity across accounts: codeword stored pre-switch was recalled post-resume (demo video below)
  • Deliberate kills unaffected: killSession still yields disposed, never a resume candidate
  • Dialog exit animation keeps its text (last-prompt cache; was flashing "0 running agents")

Demo film of the full flow: https://claude.ai/code/artifact/a35d00ba-980a-4966-8a1f-e2758e699c1f (private artifact — ask Kiet for access)

Testing

  • bun test src/trpc/router/terminal-agents/terminal-agents.test.ts (10 pass; new coverage: candidate filtering, crash-style restart leaves resume candidates, dispose-failure keeps the candidate)
  • bun run typecheck (host-service + desktop)
  • bun run --cwd packages/i18n check (extract + compile --strict green; ja/zh-CN translated)
  • biome on touched dirs

Known Limitations

  • The candidate count includes bindings whose session row is active but whose pty died while the host was down (they're legitimate resume candidates, but "N running agents" can overstate what's visibly running). Daemon-verified counting is a possible follow-up.
  • Not exercised live: the AddAccountDialog "Use for new agents" entry point (compiles, reuses the verified handler) and the ja/zh strings visually.

https://claude.ai/code/session_01H4qeLYFcvDjnNzLVqJu8hY


Summary by cubic

Switching the default provider account in Settings → Usage now offers to restart the provider's running agent sessions. Previously, running agents kept the old login because their environment froze at spawn; confirming now relaunches each session with the same conversation on the new account, while declining preserves the old toast.

  • Host-side accountRestartCandidates and restartAccountSessions identify resumable sessions and kill them crash-style so auto-resume relaunches them via the existing TerminalAgentAutoResume path.
  • The AddAccountDialog "Use for new agents" flow routes through the same restart ask.
  • New i18n strings added for all 16 locales.

Summary by CodeRabbit

  • New Features

    • Switching the default Claude or Codex account now identifies eligible running sessions and offers to restart them with the new account.
    • A confirmation dialog displays the number of sessions that can be restarted.
    • Users can confirm the restart or decline and leave sessions running.
    • Default-account changes now provide consistent confirmation messaging across account settings.
  • Localization

    • Added translated restart-flow messaging across supported languages, including English, Japanese, Chinese, German, French, and others.