Files
pipelines/internal/cli/claude_account.go
T
alan ed9072b79a Claude accounts: switch a chat refused for usage to another saved account and continue it
When Claude Code refuses an operator chat's turn for a usage limit, the
server marks the refused account until its reset and, if the chat's Claude
home allows it (claude_account_auto_switch / `pipelines claude-account auto
on|off`, on by default, kept in pipelines-accounts/settings.json), switches
the home to the saved account with the most usage left through the
claude_account_use path. Switches of one home are serialized, so chats
refused together switch once. Each refused chat gets one queued prompt,
marked as sent automatically, that asks the agent to check it is on track
before continuing; the app's auto-continue-at-reset prompt for the refusal is
removed (and refused if it arrives later), and the operator gets a
low-priority notification.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 00:23:43 +00:00

124 lines
3.6 KiB
Go

package cli
import (
"context"
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"git.a-wan.net/alan/pipelines/internal/claude"
"git.a-wan.net/alan/pipelines/internal/claudeaccounts"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/workenv"
)
const claudeAccountUsage = "usage: pipelines claude-account list | save [name] | use <name> | remove <name> | auto on|off [--dir DIR] [--json]"
// claudeAccount saves and switches the Claude login of the Claude home a
// directory selects (its CLAUDE_CONFIG_DIR after direnv/mise), locally and
// without a server. It changes the same files /login does, so running Claude
// Code processes there continue on the new account. `auto on|off` sets whether
// the server switches a chat that runs out of usage to another saved account
// and continues it (kept in the Claude home, so the server reads the same
// choice).
func (a App) claudeAccount(ctx context.Context, args []string) error {
f := flag.NewFlagSet("claude-account", flag.ContinueOnError)
f.SetOutput(a.Err)
dir := f.String("dir", "", "Directory whose environment selects the Claude home (default: current directory)")
jsonMode := f.Bool("json", false, "Machine-readable JSON")
if err := parse(f, args); err != nil {
if errors.Is(err, flag.ErrHelp) {
return nil
}
return err
}
pos := f.Args()
if len(pos) == 0 {
return errors.New(claudeAccountUsage)
}
directory := *dir
if directory == "" {
wd, err := os.Getwd()
if err != nil {
return err
}
directory = wd
}
directory, err := filepath.Abs(directory)
if err != nil {
return err
}
configDir, err := claude.AccountConfigDir(ctx, workenv.Default, directory, os.Environ())
if err != nil {
return err
}
store := claudeaccounts.Store{ConfigDir: configDir}
var list protocol.ClaudeAccountList
switch {
case pos[0] == "list" && len(pos) == 1:
list, err = store.List()
case pos[0] == "save" && len(pos) <= 2:
list, err = store.Save(strings.Join(pos[1:], ""))
case pos[0] == "use" && len(pos) == 2:
list, err = store.Use(pos[1])
case pos[0] == "remove" && len(pos) == 2:
list, err = store.Remove(pos[1])
case pos[0] == "auto" && len(pos) == 2 && (pos[1] == "on" || pos[1] == "off"):
list, err = store.SetAutoSwitch(pos[1] == "on")
default:
return errors.New(claudeAccountUsage)
}
if err != nil {
return err
}
if *jsonMode {
return a.output(list)
}
fmt.Fprintf(a.Out, "Claude home: %s\n", list.ConfigDir)
if len(list.Accounts) == 0 {
fmt.Fprintln(a.Out, "No saved accounts.")
}
for _, account := range list.Accounts {
mark := " "
if account.Active {
mark = "*"
}
fmt.Fprintf(a.Out, "%s %s%s\n", mark, account.Name, accountDetail(account))
}
if list.Active != nil && !list.Active.Saved {
fmt.Fprintf(a.Out, "* %s (signed in, not saved; pipelines claude-account save)\n", list.Active.Email)
}
if list.Active == nil {
fmt.Fprintln(a.Out, "Signed out.")
}
auto := "off"
if list.AutoSwitch {
auto = "on"
}
fmt.Fprintf(a.Out, "Switch automatically when usage runs out: %s\n", auto)
if pos[0] == "use" && list.Active != nil {
fmt.Fprintf(a.Out, "Switched to %s. Running Claude Code sessions continue on it.\n", list.Active.Email)
}
return nil
}
func accountDetail(account protocol.ClaudeAccount) string {
var parts []string
if account.Email != "" && account.Email != account.Name {
parts = append(parts, account.Email)
}
if account.Organization != "" {
parts = append(parts, account.Organization)
}
if account.Plan != "" {
parts = append(parts, account.Plan)
}
if len(parts) == 0 {
return ""
}
return " (" + strings.Join(parts, ", ") + ")"
}