Claude account switcher: save and switch a chat's Claude login in place

Saved accounts live under <CLAUDE_CONFIG_DIR>/pipelines-accounts/<slug>/
(0700 dir, 0600 files). Switching changes what /login changes: the
claudeAiOauth entry of .credentials.json and oauthAccount of .claude.json
(atomic temp+rename, mode and other keys kept) and the org caches /login
drops, under Claude Code's own refresh, storage and config locks, so
running CLIs continue on the new account without a restart. The active
login's rotated tokens are written back to its slot first.

Server: claude_accounts / claude_account_save / _use / _remove, chat-scoped
and Claude-only; a switch moves the home's open chats to the new account's
quota (usage.account.switched_at), clearing a reached limit.
CLI: pipelines claude-account list|save|use|remove [--dir].
App: Claude account in the chat menu, and Switch to <account> on the
usage-limit and usage-warning banners, with a confirmation snack.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-30 15:10:08 +00:00
parent 1772100af2
commit c490f71042
33 changed files with 2958 additions and 4 deletions
+39
View File
@@ -520,6 +520,45 @@ five minutes) and merges `account/rateLimits/updated` notifications.
OpenCode has no quota API, so it sends `{"harness":"OpenCode","unsupported":true}`.
A usage frame with only `account` updates the quota and leaves token figures as
they were. No quota is estimated and no model call is made to obtain one.
`switched_at` (epoch ms) marks a report sent after the operator switched the
chat's Claude account: it replaces the previous account's report even with no
windows yet, and a usage-limit refusal received before it no longer applies.
### Claude account switching
A chat whose directory selects its own Claude Code home (`CLAUDE_CONFIG_DIR`,
for example from a direnv `.envrc`) can keep several subscription logins and
switch between them. Saved accounts live in
`<config dir>/pipelines-accounts/<slug>/` (0700): `credentials.json` (that
account's `claudeAiOauth`), `account.json` (its `oauthAccount`) and `slot.json`
(its name), all 0600. Switching changes exactly what `/login` changes, in
place: the `claudeAiOauth` entry of `.credentials.json` (other entries such as
MCP OAuth are kept) and the `oauthAccount` object of `.claude.json` (every other
key kept, in order), each written to a temporary file and renamed, keeping the
file's mode; it also deletes the organization caches `/login` deletes
(`policy-limits.json`, `remote-settings.json` and their stamp, signature and
synced-at files). It holds Claude Code's own `.oauth_refresh.lock`,
`.storage-write.lock` and `.claude.json.lock` meanwhile, so a token refresh or a
config save cannot write the old account back. Running Claude Code processes notice the credentials file's new mtime and
continue on the new account without a restart, as after a terminal `/login`.
Before switching, the active login's current (possibly refreshed) tokens are
written back to its own slot; an unsaved login is saved under its email first,
so a switch never loses one. Accounts are compared by account uuid (else email
and organization), never by token, and no reply or log carries a token.
Requests (body `claude_account: {"session_id", "name"}`; reply
`claude_accounts: {"config_dir", "active", "accounts": [{"name", "email",
"organization", "plan", "active", "saved"}], "usage"}`):
`claude_accounts` lists; `claude_account_save` snapshots the active login (name
defaults to its email); `claude_account_use` switches to a saved account and
pushes the new account's quota (`switched_at`) to every open chat on that
Claude home, which clears a usage limit the old account had reached;
`claude_account_remove` deletes a saved account. They are refused for chats
that are not Claude Code's and for directories without `CLAUDE_CONFIG_DIR`.
The same operations run locally with
`pipelines claude-account list | save [name] | use <name> | remove <name>
[--dir DIR] [--json]`, which resolves DIR's (default: the current directory's)
environment the way the server does.
The brokered OpenCode chat exposes the operator's own sessions, separate from the
sessions pipeline runs create. The protocol types are: