Files
pipelines/app/test/features/chat/domain/chat_usage_limit_test.dart
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

286 lines
8.5 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/domain/chat_account_usage.dart';
import 'package:pipelines_app/features/chat/domain/chat_usage_limit.dart';
ChatAccountUsage _usage(List<(double, DateTime?)> windows) => ChatAccountUsage(
harness: 'Claude Code',
plan: 'max',
asOf: 0,
unsupported: false,
windows: [
for (final (index, (percent, reset)) in windows.indexed)
ChatQuotaWindow(
id: 'w$index',
label: 'W$index',
usedPercent: percent,
resetsAt: reset?.millisecondsSinceEpoch ?? 0,
),
],
);
void main() {
final now = DateTime(2026, 11, 25, 12);
group('chatUsageLimit', () {
test('an exhausted window with a future reset is a limit', () {
final reset = now.add(const Duration(hours: 3));
final limit = chatUsageLimit(
_usage([(42, now.add(const Duration(days: 2))), (100, reset)]),
now,
)!;
expect(limit.resetsAt, reset);
expect(limit.sendAt, reset.add(const Duration(minutes: 1)));
});
test('below 100% or unknown quota is not a limit', () {
expect(chatUsageLimit(null, now), isNull);
expect(
chatUsageLimit(
_usage([(99.9, now.add(const Duration(hours: 1)))]),
now,
),
isNull,
);
final unsupported = ChatAccountUsage(
harness: 'OpenCode',
plan: '',
asOf: 0,
windows: const [],
unsupported: true,
);
expect(chatUsageLimit(unsupported, now), isNull);
});
test('a reset that has passed, or is unknown, is not a limit', () {
expect(
chatUsageLimit(
_usage([(100, now.subtract(const Duration(minutes: 1)))]),
now,
),
isNull,
);
expect(chatUsageLimit(_usage([(100, null)]), now), isNull);
});
test('several exhausted windows wait for the latest reset', () {
final early = now.add(const Duration(hours: 1));
final late = now.add(const Duration(days: 3));
final limit = chatUsageLimit(
_usage([
(100, early),
(104, late),
(60, now.add(const Duration(days: 9))),
(100, now.subtract(const Duration(hours: 1))),
]),
now,
)!;
expect(limit.resetsAt, late);
});
});
test('Claude Code limit text carries the reset in epoch seconds', () {
final reset = now.add(const Duration(hours: 2));
final seconds = reset.millisecondsSinceEpoch ~/ 1000;
expect(
chatUsageLimitFromText(
'Claude AI usage limit reached|$seconds',
now,
)!.resetsAt,
DateTime.fromMillisecondsSinceEpoch(seconds * 1000),
);
expect(chatUsageLimitFromText('Hello there', now), isNull);
final past = now.subtract(const Duration(hours: 1));
expect(
chatUsageLimitFromText(
'Claude AI usage limit reached|${past.millisecondsSinceEpoch ~/ 1000}',
now,
),
isNull,
);
});
test('the thread limit prefers quota, then the last reply text', () {
final seconds =
now.add(const Duration(hours: 2)).millisecondsSinceEpoch ~/ 1000;
final refused = {
'role': 'assistant',
'parts': [
{'type': 'text', 'text': 'Claude AI usage limit reached|$seconds'},
],
};
expect(chatThreadUsageLimit(null, [refused], now), isNotNull);
expect(
chatThreadUsageLimit(null, [
refused,
{
'role': 'user',
'parts': [
{'type': 'text', 'text': 'continue'},
],
},
], now),
isNull,
);
});
group('chatRefusedTurn', () {
Map<String, Object?> message(
String id,
String role,
String text, {
bool file = false,
int? sendAt,
}) => {
'id': id,
'role': role,
'send_at': ?sendAt,
'parts': [
{'type': 'text', 'text': text},
if (file) {'type': 'file', 'name': 'log.txt'},
],
};
const hit =
"You've hit your 5-hour limit · resets 5:20pm (Europe/London) · "
'out of usage credits';
test('a refused operator message is sent again, without its files', () {
final turn = chatRefusedTurn([
message('u1', 'user', 'first'),
message('a1', 'assistant', 'done'),
message('u2', 'user', ' fix the build ', file: true),
message('r1', 'assistant', hit),
], null)!;
expect(turn.text, 'fix the build');
expect(turn.resends, isTrue);
expect(turn.hadAttachments, isTrue);
expect(turn.wording, "You've hit your 5-hour limit");
expect(turn.autoContinueId, 'auto-continue-r1');
});
test('a refusal after the turn worked continues it', () {
final turn = chatRefusedTurn([
message('u1', 'user', 'fix it'),
message('a1', 'assistant', 'Working on it.'),
message('r1', 'assistant', 'Claude AI usage limit reached|1900000000'),
// A prompt scheduled since is not part of the conversation yet.
message('q1', 'user', 'continue', sendAt: 1900000060000),
], null)!;
expect(turn.text, 'continue');
expect(turn.resends, isFalse);
expect(turn.wording, isNull);
});
test('anything but a refusal last is not a refused turn', () {
expect(chatRefusedTurn(const [], null), isNull);
expect(
chatRefusedTurn([message('a1', 'assistant', 'All done.')], null),
isNull,
);
expect(
chatRefusedTurn([
message('r1', 'assistant', hit),
message('u1', 'user', 'continue'),
], null),
isNull,
);
});
test('notices, reports and activity rows after the refusal do not hide '
'it; a later turn does', () {
final refused = [
message('u1', 'user', 'fix the build'),
message('r1', 'assistant', hit),
for (final id in ['n1', 'n2', 'n3'])
{
...message(id, 'assistant', 'Background task finished: tests'),
'kind': 'notice',
},
{
...message('rep1', 'assistant', 'Subagent report'),
'kind': 'agent_report',
},
{
'id': 'act1',
'role': 'assistant',
'parts': [
{'type': 'compaction', 'status': 'completed'},
],
},
];
final turn = chatRefusedTurn(refused, null)!;
expect(turn.refusalId, 'r1');
expect(turn.text, 'fix the build');
expect(
chatThreadUsageLimit(null, [
message(
'r1',
'assistant',
'Claude AI usage limit reached|1900000000',
),
{...message('n1', 'assistant', 'Background task'), 'kind': 'notice'},
], now),
isNotNull,
);
expect(
chatRefusedTurn([
...refused,
message('a2', 'assistant', 'Picked it back up.'),
], null),
isNull,
);
expect(
chatRefusedTurn([...refused, message('u2', 'user', 'continue')], null),
isNull,
);
});
test('a refusal from before an account switch no longer counts', () {
final refusal = {...message('r1', 'assistant', hit), 'created_at': 10};
final switched = ChatAccountUsage(
harness: 'Claude Code',
plan: 'max',
asOf: 0,
windows: const [],
unsupported: false,
switchedAt: 20,
);
expect(chatRefusedTurn([refusal], switched), isNull);
expect(chatRefusedTurn([refusal], null), isNotNull);
});
});
test('extra usage reads as available or in use, else nothing', () {
ChatAccountUsage? parse(Map<String, Object?> extra) => chatAccountUsage({
'harness': 'Claude Code',
'windows': [
{'id': 'five_hour', 'label': '5-hour', 'used_percent': 10},
],
'extra_usage': extra,
});
expect(
chatExtraUsageNote(parse({'status': 'allowed'})),
'Extra usage available',
);
expect(
chatExtraUsageNote(parse({'status': 'allowed', 'in_use': true})),
'Using extra usage',
);
expect(
chatExtraUsageNote(
parse({'status': 'rejected', 'disabled_reason': 'out_of_credits'}),
),
isNull,
);
expect(chatExtraUsageNote(parse({})), isNull);
expect(chatExtraUsageNote(null), isNull);
});
test('scheduled send_at is a future epoch-ms time only', () {
final later = now.add(const Duration(minutes: 5));
expect(chatScheduledSendAt(later.millisecondsSinceEpoch, now), later);
expect(chatScheduledSendAt(now.millisecondsSinceEpoch - 1, now), isNull);
expect(chatScheduledSendAt(null, now), isNull);
expect(chatScheduledSendAt('x', now), isNull);
});
}