Files
pipelines/internal/claude/refusal_test.go
T
alan 633b5953bb 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>
2026-12-02 19:52:03 +00:00

122 lines
4.7 KiB
Go

package claude
import (
"context"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func TestUsageLimitResetIsReadFromTheRefusal(t *testing.T) {
london, err := time.LoadLocation("Europe/London")
if err != nil {
t.Skip("no time zone data:", err)
}
at := time.Date(2026, 12, 2, 17, 21, 0, 0, london)
for _, tc := range []struct {
text string
want time.Time
}{
{"You've hit your 5-hour limit · resets 7:20pm (Europe/London) · out of usage credits", time.Date(2026, 12, 2, 19, 20, 0, 0, london)},
{"You've hit your limit · resets 5pm (Europe/London)", time.Date(2026, 12, 3, 17, 0, 0, 0, london)},
{"You've hit your 5-hour limit · resets 12:30am (Europe/London)", time.Date(2026, 12, 3, 0, 30, 0, 0, london)},
{"You've hit your weekly limit · resets Dec 9, 5pm (Europe/London)", time.Date(2026, 12, 9, 17, 0, 0, 0, london)},
{"You've hit your weekly limit · resets Jan 2 at 9am (Europe/London)", time.Date(2027, 1, 2, 9, 0, 0, 0, london)},
{"Claude AI usage limit reached|1900000000", time.Unix(1900000000, 0)},
} {
got, ok := usageLimitReset(tc.text, at)
if !ok || !got.Equal(tc.want) {
t.Errorf("%q: %v %v, want %v", tc.text, got, ok, tc.want)
}
}
for _, text := range []string{"You've hit your limit", "resets 7:20pm (Nowhere/Land)", "resets 13pm (Europe/London)"} {
if got, ok := usageLimitReset(text, at); ok {
t.Errorf("%q read as %v", text, got)
}
}
}
// The refused turn's operator message is sent again only when the turn did
// nothing else; the reset falls back to the rejected rate_limit_event.
func TestUsageRefusalCarriesItsPromptAndReset(t *testing.T) {
limit := []contentBlock{{Type: "text", Text: "You've hit your limit"}}
until := time.Now().Add(time.Hour)
s := &session{id: "s", turnID: "t1"}
s.beginTurnLocked()
s.turnPrompt = "Fix the build"
s.noteRejectedResetLocked(until)
s.noteUsageRefusalLocked(lineEnvelope{Error: "rate_limit"}, messageBody{}, limit, "r1")
refusal, refused := s.takeUsageRefusalLocked(lineEnvelope{IsError: true})
if !refused || refusal.Prompt != "Fix the build" || !refusal.ResetsAt.Equal(until) {
t.Fatalf("refusal = %+v %v", refusal, refused)
}
s.turnID = "t2"
s.beginTurnLocked()
s.turnPrompt = "Fix the build"
s.noteUsageRefusalLocked(lineEnvelope{}, messageBody{}, []contentBlock{{Type: "text", Text: "Looking at it"}}, "m2")
s.noteUsageRefusalLocked(lineEnvelope{Error: "rate_limit"}, messageBody{}, limit, "r2")
refusal, refused = s.takeUsageRefusalLocked(lineEnvelope{IsError: true})
if !refused || refusal.Prompt != "" || !refusal.ResetsAt.IsZero() {
t.Fatalf("worked refusal = %+v %v", refusal, refused)
}
}
func receiveRefusal(t *testing.T, refusals <-chan UsageRefusal) UsageRefusal {
t.Helper()
select {
case refusal := <-refusals:
return refusal
case <-time.After(5 * time.Second):
t.Fatal("the refusal was not reported")
}
return UsageRefusal{}
}
// A live refusal of an operator prompt carries the prompt and the reset its
// text names.
func TestLiveUsageRefusalNamesItsResetAndPrompt(t *testing.T) {
london, err := time.LoadLocation("Europe/London")
if err != nil {
t.Skip("no time zone data:", err)
}
chat := testChat(t)
refusals := make(chan UsageRefusal, 4)
chat.OnUsageRefused(func(refusal UsageRefusal) { refusals <- refusal })
created := newCreatedChat(t, chat)
before := time.Now()
if err := chat.Prompt(context.Background(), created.ID, "usage-limit please"); err != nil {
t.Fatal(err)
}
refusal := receiveRefusal(t, refusals)
local := before.In(london)
want := time.Date(local.Year(), local.Month(), local.Day(), 17, 20, 0, 0, london)
if want.Before(before.Add(-time.Minute)) {
want = time.Date(local.Year(), local.Month(), local.Day()+1, 17, 20, 0, 0, london)
}
if refusal.Prompt != "usage-limit please" || !refusal.ResetsAt.Equal(want) {
t.Fatalf("refusal = %+v, want reset %v", refusal, want)
}
}
// A turn Claude Code started on its own (a background task's notification)
// that the limit refuses is reported too, with nothing to send again.
func TestRefusalOfATurnClaudeCodeStartedIsReported(t *testing.T) {
chat := testChat(t)
refusals := make(chan UsageRefusal, 4)
chat.OnUsageRefused(func(refusal UsageRefusal) { refusals <- refusal })
created := newCreatedChat(t, chat)
frames := subscribe(t, chat, created.ID)
if err := chat.Prompt(context.Background(), created.ID, "cli-refusal"); err != nil {
t.Fatal(err)
}
waitFrame(t, frames, "the operator turn's end", func(frame protocol.ChatEventFrame) bool {
return frame.Kind == protocol.ChatEventStep && frame.Step != nil && frame.Step.Status == protocol.ChatStepEnded
})
refusal := receiveRefusal(t, refusals)
if refusal.SessionID != created.ID || refusal.MessageID != "refusal-2" || refusal.Prompt != "" || refusal.ResetsAt.IsZero() {
t.Fatalf("refusal = %+v", refusal)
}
}