Chat: the server continues a usage-refused Claude chat at the reset

When Claude Code refuses an operator chat's turn for a usage limit and the
server does not continue it on another account, the server now queues a
durable prompt (auto-continue-<refusal id>, sent as Pipelines (automatic))
for a minute after the reset: the refused operator message when the turn
did no work, else "continue". The reset comes from the refusal's text,
else the rejected rate_limit_event. Refusals of turns Claude Code started
itself are handled too, with no client attached; a restarted server or a
device scheduling the same refusal adds nothing. The choice is kept per
Claude home beside auto-switch (or by the server for chats without one),
read and set with chat_auto_continue, on by default.

The app's limit banner now finds the refusal as the latest turn, ignoring
notices, agent reports and activity rows after it, reads and writes the
server's choice, and only schedules its own prompt for older servers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-02 19:52:03 +00:00
parent c441400f36
commit 633b5953bb
23 changed files with 1380 additions and 71 deletions
+126 -5
View File
@@ -2,6 +2,7 @@ package claude
import (
"regexp"
"strconv"
"strings"
"time"
)
@@ -27,6 +28,15 @@ type UsageRefusal struct {
MessageID string
// Email is the account the refused turn started on ("" when unknown).
Email string
// ResetsAt is when the limit resets: the time the refusal's own text
// names, else the reset of the turn's rejected rate_limit_event; zero
// when neither said.
ResetsAt time.Time
// Prompt is the operator's message the refusal answered when the
// refused turn was started by it and did no work before the refusal;
// "" for a turn that worked, or one Claude Code started itself (a
// background-task notification).
Prompt string
}
// OnUsageRefused registers the receiver of usage-limit refusals; each is
@@ -50,8 +60,19 @@ func (c *Chat) reportUsageRefusal(refusal UsageRefusal) {
// refusal of the previous turn. The caller holds s.mu.
func (s *session) beginTurnLocked() {
s.turnEmail = s.account.email
s.clearRefusalLocked()
}
// clearRefusalLocked forgets the running turn's refusal record and prompt.
// The caller holds s.mu.
func (s *session) clearRefusalLocked() {
s.limitMessageID = ""
s.limitByError = false
s.limitText = ""
s.limitAt = time.Time{}
s.limitReset = time.Time{}
s.turnPrompt = ""
s.turnWorked = false
}
// noteUsageRefusalLocked remembers an assistant message that is a
@@ -65,9 +86,23 @@ func (s *session) noteUsageRefusalLocked(envelope lineEnvelope, body messageBody
break
}
}
if byError || byText {
s.limitMessageID = messageID
s.limitByError = s.limitByError || byError
if !byError && !byText {
if messageID != s.limitMessageID {
s.turnWorked = true
}
return
}
s.limitMessageID = messageID
s.limitByError = s.limitByError || byError
s.limitText = ""
for _, block := range blocks {
if block.Type == "text" {
s.limitText += block.Text + "\n"
}
}
s.limitAt = time.Now()
if at, err := time.Parse(time.RFC3339Nano, envelope.Timestamp); err == nil {
s.limitAt = at
}
}
@@ -84,11 +119,97 @@ func (s *session) takeUsageRefusalLocked(envelope lineEnvelope) (UsageRefusal, b
if refusal.TurnID == "" {
refusal.TurnID = "result-" + strings.TrimSpace(envelope.UUID)
}
s.limitMessageID = ""
s.limitByError = false
at := s.limitAt
if at.IsZero() {
at = time.Now()
}
if reset, ok := usageLimitReset(s.limitText+envelope.Result, at); ok {
refusal.ResetsAt = reset
} else if !s.limitReset.IsZero() {
refusal.ResetsAt = s.limitReset
}
if !s.turnWorked {
refusal.Prompt = s.turnPrompt
}
s.clearRefusalLocked()
return refusal, refused
}
// noteRejectedResetLocked records when a rejected rate_limit_event the
// running turn received says the limit resets. The caller holds s.mu.
func (s *session) noteRejectedResetLocked(until time.Time) {
if until.After(s.limitReset) {
s.limitReset = until
}
}
var (
// usageLimitEpoch is the older CLI's "usage limit reached|<epoch>".
usageLimitEpoch = regexp.MustCompile(`usage limit reached\|(\d{9,11})\b`)
// usageLimitResets is the current CLI's "resets 5:20pm (Europe/London)",
// or with a date for a longer window, "resets Dec 9, 5pm (Europe/London)".
usageLimitResets = regexp.MustCompile(`(?i)\bresets\s+(?:([a-z]{3})[a-z]*\.?\s+(\d{1,2})(?:,|\s+at)?\s+)?(\d{1,2})(?::(\d{2}))?\s*([ap]m)\s*\(([^)\s]+)\)`)
)
// usageLimitReset is the reset a refusal's text names, the first such time
// after at (when the refusal was written).
func usageLimitReset(text string, at time.Time) (time.Time, bool) {
if match := usageLimitEpoch.FindStringSubmatch(text); match != nil {
seconds, err := strconv.ParseInt(match[1], 10, 64)
if err != nil || seconds <= 0 {
return time.Time{}, false
}
return time.Unix(seconds, 0), true
}
match := usageLimitResets.FindStringSubmatch(text)
if match == nil {
return time.Time{}, false
}
location, err := time.LoadLocation(match[6])
if err != nil {
return time.Time{}, false
}
hour, _ := strconv.Atoi(match[3])
minute := 0
if match[4] != "" {
minute, _ = strconv.Atoi(match[4])
}
if hour < 1 || hour > 12 || minute > 59 {
return time.Time{}, false
}
hour %= 12
if strings.EqualFold(match[5], "pm") {
hour += 12
}
local := at.In(location)
if match[1] != "" {
month, ok := monthOf(match[1])
day, _ := strconv.Atoi(match[2])
if !ok || day < 1 || day > 31 {
return time.Time{}, false
}
reset := time.Date(local.Year(), month, day, hour, minute, 0, 0, location)
if reset.Before(at.Add(-time.Minute)) {
reset = time.Date(local.Year()+1, month, day, hour, minute, 0, 0, location)
}
return reset, true
}
reset := time.Date(local.Year(), local.Month(), local.Day(), hour, minute, 0, 0, location)
if reset.Before(at.Add(-time.Minute)) {
reset = time.Date(local.Year(), local.Month(), local.Day()+1, hour, minute, 0, 0, location)
}
return reset, true
}
func monthOf(name string) (time.Month, bool) {
for month := time.January; month <= time.December; month++ {
if strings.EqualFold(month.String()[:3], name) {
return month, true
}
}
return 0, false
}
// noteRejectedLocked records when a rejected account's limit ends. The
// caller holds c.quotaMu.
func (c *Chat) noteRejectedLocked(email string, until time.Time) {