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
@@ -0,0 +1,106 @@
// `?claudeAccounts=N` (with `?chats=1&harness=claude`): the chat's Claude
// home has N saved accounts (alex@caplens.example, sam@caplens.example,
// ops@caplens.example), the first one active, and `claude_account_use`
// switches between them. `?claudeUsage=limit` pushes the active account's
// quota at 100% on subscribe (the usage-limit banner); `?claudeUsage=warn`
// pushes it at 88% (the usage warning banner).
import 'package:pipelines_app/core/api_gateway.dart';
Map<String, String> _params = const {};
int _active = 0;
const _emails = [
'alex@caplens.example',
'sam@caplens.example',
'ops@caplens.example',
];
void configureChatClaudeAccountsFixture(Map<String, String> params) {
_params = params;
}
int get _count =>
(int.tryParse(_params['claudeAccounts'] ?? '') ?? 0).clamp(0, 3);
JsonMap _account(int i) => {
'name': _emails[i],
'email': _emails[i],
'organization': 'Caplens',
'plan': i == 2 ? 'pro' : 'max',
'active': i == _active,
'saved': true,
};
JsonMap _list({JsonMap? usage}) => {
'config_dir': '/home/alex/caplens/.claude-profile',
'active': _account(_active),
'accounts': [for (var i = 0; i < _count; i++) _account(i)],
'usage': ?usage,
};
/// Replies to the claude_account* requests, or null for any other request.
JsonMap? chatClaudeAccountsReply(JsonMap request) {
final type = '${request['type']}';
if (_count == 0 || !type.startsWith('claude_account')) return null;
final body = request['claude_account'];
final name = body is Map ? '${body['name'] ?? ''}' : '';
JsonMap? usage;
if (type == 'claude_account_use') {
final index = _emails.indexOf(name);
if (index < 0 || index >= _count) {
return {
'id': request['id'],
'type': 'error',
'error': 'no saved Claude account "$name"',
};
}
_active = index;
final now = DateTime.now().millisecondsSinceEpoch;
usage = {
'harness': 'Claude Code',
'plan': 'max',
'as_of': now,
'switched_at': now,
};
}
return {
'id': request['id'],
'type': type,
'claude_accounts': _list(usage: usage),
};
}
/// The active account's quota frame for `?claudeUsage=`, or null.
JsonMap? chatClaudeUsageFrame(String sessionId) {
final percent = switch (_params['claudeUsage']) {
'limit' => 100.0,
'warn' => 88.0,
_ => null,
};
if (percent == null) return null;
final now = DateTime.now();
return {
'type': 'chat_event',
'chat_event': {
'session_id': sessionId,
'kind': 'usage',
'usage': {
'account': {
'harness': 'Claude Code',
'plan': 'max',
'as_of': now.millisecondsSinceEpoch,
'windows': [
{
'id': 'five_hour',
'label': '5-hour',
'used_percent': percent,
'resets_at': now
.add(const Duration(hours: 2, minutes: 10))
.millisecondsSinceEpoch,
},
],
},
},
},
};
}
+4
View File
@@ -26,6 +26,7 @@
// (tool/perf/chat_new_session_fixture.dart).
import 'package:pipelines_app/core/api_gateway.dart';
import 'chat_claude_accounts_fixture.dart';
import 'chat_effort_fixture.dart';
import 'chat_catalog_fixture.dart';
import 'chat_new_session_fixture.dart';
@@ -47,6 +48,7 @@ void configurePerfFixture(Map<String, String> params) {
);
configureChatScrollFixture(params);
configureChatShownPush(params);
configureChatClaudeAccountsFixture(params);
configureChatQuestionFixture(params);
configureChatShellFixture(params);
configureTerminalFixture(params);
@@ -281,6 +283,8 @@ JsonMap? perfChatReply(JsonMap request) {
final terminal = terminalFixtureReply(request);
if (terminal != null) return terminal;
if (_chats == 0) return null;
final accounts = chatClaudeAccountsReply(request);
if (accounts != null) return accounts;
final question = chatQuestionReply(request);
if (question != null) return question;
final shell = chatShellReply(request);
@@ -42,6 +42,7 @@ import 'package:web_socket_channel/web_socket_channel.dart';
import 'fixture_host/fixture_host.dart';
import 'perf/chat_search_fixture.dart';
import 'perf/chat_shown_fixture.dart';
import 'perf/chat_claude_accounts_fixture.dart';
import 'perf/chat_question_fixture.dart';
import 'capture/window_probe.dart';
import 'perf/chat_scroll_fixture.dart';
@@ -824,6 +825,13 @@ class _FixtureChannel implements WebSocketChannel {
chatScrollStream('${subscribe['session_id'] ?? ''}', emit);
chatQuestionStream('${subscribe['session_id'] ?? ''}', emit);
chatShownPush('${subscribe['session_id'] ?? ''}', emit);
final usage = chatClaudeUsageFrame('${subscribe['session_id'] ?? ''}');
if (usage != null) {
Future<void>.delayed(
const Duration(milliseconds: 300),
() => emit(usage),
);
}
}
if (reply == null && followUps.isEmpty) return;
Future<void>.delayed(const Duration(milliseconds: 120), () {