ed9072b79a
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>
65 lines
1.7 KiB
Go
65 lines
1.7 KiB
Go
package claudeaccounts
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// settingsFile holds the Claude home's account-switching preferences, beside
|
|
// the saved slots (a file, so slots() never reads it as an account).
|
|
const settingsFile = "settings.json"
|
|
|
|
type settings struct {
|
|
// AutoSwitch is nil until the operator chooses; the default is on.
|
|
AutoSwitch *bool `json:"auto_switch,omitempty"`
|
|
}
|
|
|
|
func (s Store) readSettings() settings {
|
|
var out settings
|
|
data, err := os.ReadFile(filepath.Join(s.slotsDir(), settingsFile))
|
|
if err == nil {
|
|
_ = json.Unmarshal(data, &out)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// AutoSwitch reports whether a chat on this Claude home that runs out of
|
|
// usage is switched to another saved account automatically (on by default).
|
|
func (s Store) AutoSwitch() bool {
|
|
if on := s.readSettings().AutoSwitch; on != nil {
|
|
return *on
|
|
}
|
|
return true
|
|
}
|
|
|
|
// SetAutoSwitch records the automatic-switch preference and returns the
|
|
// accounts list, which reports it.
|
|
func (s Store) SetAutoSwitch(on bool) (protocol.ClaudeAccountList, error) {
|
|
if err := s.check(); err != nil {
|
|
return protocol.ClaudeAccountList{}, err
|
|
}
|
|
dir := s.slotsDir()
|
|
if err := os.Mkdir(dir, 0o700); err != nil && !errors.Is(err, fs.ErrExist) {
|
|
return protocol.ClaudeAccountList{}, err
|
|
}
|
|
current := s.readSettings()
|
|
current.AutoSwitch = &on
|
|
data, err := marshal(current)
|
|
if err != nil {
|
|
return protocol.ClaudeAccountList{}, err
|
|
}
|
|
path := filepath.Join(dir, settingsFile)
|
|
if err := writeAtomic(path, data, 0o600); err != nil {
|
|
return protocol.ClaudeAccountList{}, err
|
|
}
|
|
if err := os.Chmod(path, 0o600); err != nil {
|
|
return protocol.ClaudeAccountList{}, err
|
|
}
|
|
return s.List()
|
|
}
|