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>
This commit is contained in:
2026-12-01 00:23:43 +00:00
parent 1995382e8c
commit ed9072b79a
30 changed files with 1362 additions and 21 deletions
+12 -2
View File
@@ -15,12 +15,15 @@ import (
"git.a-wan.net/alan/pipelines/internal/workenv"
)
const claudeAccountUsage = "usage: pipelines claude-account list | save [name] | use <name> | remove <name> [--dir DIR] [--json]"
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.
// 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)
@@ -63,6 +66,8 @@ func (a App) claudeAccount(ctx context.Context, args []string) error {
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)
}
@@ -89,6 +94,11 @@ func (a App) claudeAccount(ctx context.Context, args []string) error {
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)
}
+34
View File
@@ -76,3 +76,37 @@ func TestClaudeAccountCommandSavesAndSwitches(t *testing.T) {
t.Fatalf("no Claude home: %v", err)
}
}
func TestClaudeAccountAutoCommandTogglesAutomaticSwitching(t *testing.T) {
profile := t.TempDir()
t.Setenv("CLAUDE_CONFIG_DIR", profile)
t.Setenv(workenv.DisableEnvKey, "off")
dir := t.TempDir()
run := func(args ...string) (string, error) {
var out, errs bytes.Buffer
err := App{Out: &out, Err: &errs}.Run(context.Background(), append([]string{"claude-account"}, args...))
return out.String() + errs.String(), err
}
cliLogin(t, profile, "a@example.invalid", "uuid-a", "a1")
if out, err := run("list", "--dir", dir); err != nil || !strings.Contains(out, "Switch automatically when usage runs out: on") {
t.Fatalf("default: %v\n%s", err, out)
}
if out, err := run("auto", "off", "--dir", dir); err != nil || !strings.Contains(out, "Switch automatically when usage runs out: off") {
t.Fatalf("auto off: %v\n%s", err, out)
}
out, err := run("list", "--dir", dir, "--json")
var list protocol.ClaudeAccountList
if err != nil || json.Unmarshal([]byte(out), &list) != nil || list.AutoSwitch {
t.Fatalf("list json: %v\n%s", err, out)
}
info, err := os.Stat(filepath.Join(profile, "pipelines-accounts", "settings.json"))
if err != nil || info.Mode().Perm() != 0o600 {
t.Fatalf("settings file: %v %v", info, err)
}
if out, err := run("auto", "on", "--dir", dir); err != nil || !strings.Contains(out, "runs out: on") {
t.Fatalf("auto on: %v\n%s", err, out)
}
if _, err := run("auto", "maybe", "--dir", dir); err == nil {
t.Fatal("auto maybe was accepted")
}
}
+1 -1
View File
@@ -38,7 +38,7 @@ var usage = map[string]string{
"stats": "stats [--json]",
"rpc": "rpc <request.json|->",
"retention": "retention report|apply [--older-than DURATION] [--keep N] [--db FILE] [--artifacts DIR] [--json] [--yes]",
"claude-account": "claude-account list | save [name] | use <name> | remove <name> [--dir DIR] [--json]",
"claude-account": "claude-account list | save [name] | use <name> | remove <name> | auto on|off [--dir DIR] [--json]",
"tui": "tui",
}
+1 -1
View File
@@ -56,7 +56,7 @@ SERVER / INTERFACE
harness setup <claude|codex|opencode|pi>
Install check, sign-in, models and verify for one harness
harness list Installed / signed-in / configured / default for all four
claude-account list|save|use|remove
claude-account list|save|use|remove|auto
Save and switch the Claude login of this directory's
CLAUDE_CONFIG_DIR (--dir DIR); running sessions follow
tui Open the dashboard (default with no command)