Files
pipelines/internal/claude/refusal.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

106 lines
3.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package claude
import (
"regexp"
"strings"
"time"
)
// A turn Claude Code refuses for a usage limit ends with a synthetic
// assistant message carrying the API error ("rate_limit") and the CLI's own
// text, "You've hit your 5-hour limit · resets 5:20pm (Europe/London) · out of
// usage credits" (older CLIs: "Claude AI usage limit reached|<epoch>"), and a
// result line marked is_error that repeats it. The app recognises the same
// text (app domain/chat_usage_limit.dart).
var usageLimitText = regexp.MustCompile(`(?m)^\s*You['’]ve hit your [^·\n]*limit|usage limit reached\|\d{9,11}\b`)
// UsageRefusal is one live turn Claude Code refused because the account ran
// out of usage.
type UsageRefusal struct {
SessionID string
// Directory is the chat's working directory, which selects its Claude
// home (CLAUDE_CONFIG_DIR).
Directory string
TurnID string
// MessageID is the refusal message's id, as transcripts name it ("" when
// only the result reported it).
MessageID string
// Email is the account the refused turn started on ("" when unknown).
Email string
}
// OnUsageRefused registers the receiver of usage-limit refusals; each is
// delivered once, on its own goroutine, after the turn's end was published.
func (c *Chat) OnUsageRefused(handler func(UsageRefusal)) {
c.refusalMu.Lock()
c.onRefusal = handler
c.refusalMu.Unlock()
}
func (c *Chat) reportUsageRefusal(refusal UsageRefusal) {
c.refusalMu.Lock()
handler := c.onRefusal
c.refusalMu.Unlock()
if handler != nil {
go handler(refusal)
}
}
// beginTurnLocked records the account a new turn starts on and forgets any
// refusal of the previous turn. The caller holds s.mu.
func (s *session) beginTurnLocked() {
s.turnEmail = s.account.email
s.limitMessageID = ""
s.limitByError = false
}
// noteUsageRefusalLocked remembers an assistant message that is a
// usage-limit refusal. The caller holds s.mu.
func (s *session) noteUsageRefusalLocked(envelope lineEnvelope, body messageBody, blocks []contentBlock, messageID string) {
byError := envelope.Error == "rate_limit" || body.Error == "rate_limit"
byText := false
for _, block := range blocks {
if block.Type == "text" && usageLimitText.MatchString(block.Text) {
byText = true
break
}
}
if byError || byText {
s.limitMessageID = messageID
s.limitByError = s.limitByError || byError
}
}
// takeUsageRefusalLocked reports whether the turn a result line ends was
// refused for a usage limit, and clears the record. The caller holds s.mu.
func (s *session) takeUsageRefusalLocked(envelope lineEnvelope) (UsageRefusal, bool) {
refused := (s.limitMessageID != "" && (s.limitByError || envelope.IsError)) ||
(envelope.IsError && usageLimitText.MatchString(envelope.Result))
email := s.turnEmail
if email == "" {
email = s.account.email
}
refusal := UsageRefusal{SessionID: s.id, Directory: s.directory, TurnID: s.turnID, MessageID: s.limitMessageID, Email: email}
if refusal.TurnID == "" {
refusal.TurnID = "result-" + strings.TrimSpace(envelope.UUID)
}
s.limitMessageID = ""
s.limitByError = false
return refusal, refused
}
// noteRejectedLocked records when a rejected account's limit ends. The
// caller holds c.quotaMu.
func (c *Chat) noteRejectedLocked(email string, until time.Time) {
email = strings.ToLower(strings.TrimSpace(email))
if email == "" || until.IsZero() {
return
}
if c.rejectedUntil == nil {
c.rejectedUntil = map[string]time.Time{}
}
if until.After(c.rejectedUntil[email]) {
c.rejectedUntil[email] = until
}
}