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|"), 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 } }