Chat usage limit: Claude Code-style banner with auto-continue; drop the approaching-limit banner

- The usage-limit banner now reads 'Out of usage credits' (or the refusal's
  own 'You've hit your 5-hour limit'), 'Auto-resuming at <time>', an
  'Auto-continue when limits reset' checkbox and Try again, keeping the
  account switcher's 'Switch to <account>'.
- Auto-continue (a shared, persisted preference, on by default) hands the
  server a durable scheduled prompt (send_at = reset + 1 min) with an
  idempotent auto-continue-<refusal id> operation id: the refused operator
  message again, or 'continue' when the refused turn had already worked.
  Unchecking unqueues it; Try again unqueues it and sends now.
- Remove the approaching-limit banner, its SnackBar announcements and their
  marks; the send-button usage ring replaces them.
- Claude Code's rate_limit_event overage fields (overageStatus,
  isUsingOverage/overageInUse, overageDisabledReason, overageResetsAt) reach
  clients as usage.account.extra_usage; the ring tooltip and the banner say
  'Extra usage available' or 'Using extra usage'.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-30 16:13:33 +00:00
parent 7a259d8f60
commit 293474ac3c
22 changed files with 905 additions and 1254 deletions
@@ -123,31 +123,107 @@ void main() {
);
});
test('the last prompt is the newest user text and notes its files', () {
final last = chatLastPrompt([
{
'role': 'user',
'parts': [
{'type': 'text', 'text': 'first'},
],
},
{
'role': 'user',
'parts': [
{'type': 'text', 'text': ' fix the build '},
{'type': 'file', 'name': 'log.txt'},
],
},
{
'role': 'assistant',
'parts': [
{'type': 'text', 'text': 'ok'},
],
},
])!;
expect(last.text, 'fix the build');
expect(last.hadAttachments, isTrue);
expect(chatLastPrompt(const []), 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('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', () {
@@ -129,6 +129,44 @@ void main() {
);
});
test('summary ends with extra usage when the account has it', () {
ChatUsageRing ring(ChatExtraUsage extra) => chatUsageRing(
ChatAccountUsage(
harness: 'Claude Code',
plan: 'max',
asOf: 0,
windows: [_window('five_hour', '5-hour', 42, null)],
unsupported: false,
extraUsage: extra,
),
_now,
)!;
const available = ChatExtraUsage(
status: 'allowed',
inUse: false,
disabledReason: '',
);
expect(
chatUsageRingSummary(ring(available), _now),
'5-hour 42% · Extra usage available',
);
const inUse = ChatExtraUsage(
status: 'allowed_warning',
inUse: true,
disabledReason: '',
);
expect(
chatUsageRingSummary(ring(inUse), _now),
'5-hour 42% · Using extra usage',
);
const off = ChatExtraUsage(
status: 'rejected',
inUse: false,
disabledReason: 'out_of_credits',
);
expect(chatUsageRingSummary(ring(off), _now), '5-hour 42%');
});
test('reset times: today, this week, further out', () {
expect(chatUsageRingResetTime(DateTime(2026, 11, 30, 8, 5), _now), '08:05');
expect(
@@ -1,197 +0,0 @@
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_warning.dart';
final _now = DateTime(2026, 11, 25, 12);
ChatQuotaWindow _window(String id, double percent, Duration? resetIn) =>
ChatQuotaWindow(
id: id,
label: id,
usedPercent: percent,
resetsAt: resetIn == null ? 0 : _now.add(resetIn).millisecondsSinceEpoch,
);
ChatAccountUsage _usage(List<ChatQuotaWindow> windows) => ChatAccountUsage(
harness: 'Claude Code',
plan: 'max',
asOf: 0,
windows: windows,
unsupported: false,
);
const _hour = Duration(hours: 1);
const _day = Duration(days: 1);
void main() {
test('levels are 80, 90 and 95, and past 100% stays at 95', () {
expect(chatQuotaLevel(79.9), 0);
expect(chatQuotaLevel(80), 80);
expect(chatQuotaLevel(89.99), 80);
expect(chatQuotaLevel(90), 90);
expect(chatQuotaLevel(95), 95);
expect(chatQuotaLevel(130), 95);
});
group('chatUsageWarning', () {
test('nothing below 80%, unsupported or without usage', () {
expect(chatUsageWarning(null, _now, const {}), isNull);
expect(
chatUsageWarning(_usage([_window('a', 79, _hour)]), _now, const {}),
isNull,
);
const unsupported = ChatAccountUsage(
harness: 'OpenCode',
plan: '',
asOf: 0,
windows: [],
unsupported: true,
);
expect(chatUsageWarning(unsupported, _now, const {}), isNull);
});
test(
'the highest percentage leads and a second warning window follows',
() {
final warning = chatUsageWarning(
_usage([
_window('five', 84, _hour),
_window('week', 91, _day * 3),
_window('opus', 50, _day),
]),
_now,
const {},
)!;
expect(warning.primary.id, 'week');
expect(warning.second?.id, 'five');
expect(warning.level, 90);
expect(warning.urgent, isFalse);
},
);
test('a second window below 80% is not mentioned', () {
final warning = chatUsageWarning(
_usage([_window('five', 84, _hour), _window('week', 70, _day)]),
_now,
const {},
)!;
expect(warning.second, isNull);
});
test('a tie goes to the soonest reset, an unknown reset last', () {
final warning = chatUsageWarning(
_usage([
_window('unknown', 88, null),
_window('late', 88, _day * 2),
_window('soon', 88, _hour),
]),
_now,
const {},
)!;
expect(warning.primary.id, 'soon');
expect(warning.second?.id, 'late');
});
test('over 100% is urgent and leads', () {
final warning = chatUsageWarning(
_usage([_window('five', 96, _hour), _window('week', 104, null)]),
_now,
const {},
)!;
expect(warning.primary.id, 'week');
expect(warning.urgent, isTrue);
});
test('a window whose reset has passed is stale and left out', () {
expect(
chatUsageWarning(_usage([_window('five', 90, -_hour)]), _now, const {}),
isNull,
);
});
test('a dismissal holds until the next level or a reset', () {
final at84 = _window('five', 84, _hour);
final dismissed = {
chatQuotaKey('Claude Code', at84): chatQuotaMarkOf(at84),
};
expect(chatUsageWarning(_usage([at84]), _now, dismissed), isNull);
// Still within the 80 level.
expect(
chatUsageWarning(_usage([_window('five', 89, _hour)]), _now, dismissed),
isNull,
);
// Crossing 90 brings it back, and so on to 95.
expect(
chatUsageWarning(_usage([_window('five', 90, _hour)]), _now, dismissed),
isNotNull,
);
// A new reset brings it back at the same level.
expect(
chatUsageWarning(
_usage([_window('five', 84, _hour * 5)]),
_now,
dismissed,
),
isNotNull,
);
// Another harness's window with the same id is its own account.
final codex = ChatAccountUsage(
harness: 'Codex',
plan: '',
asOf: 0,
windows: [at84],
unsupported: false,
);
expect(chatUsageWarning(codex, _now, dismissed), isNotNull);
});
test('a dismissed window yields the lead to one still warning', () {
final week = _window('week', 93, _day * 3);
final warning = chatUsageWarning(
_usage([week, _window('five', 82, _hour)]),
_now,
{chatQuotaKey('Claude Code', week): chatQuotaMarkOf(week)},
)!;
expect(warning.primary.id, 'five');
expect(warning.second, isNull);
});
});
test('a level is newly crossed once, and not when dismissed', () {
final at84 = _window('five', 84, _hour);
final usage = _usage([at84]);
expect(chatQuotaNewlyCrossed(usage, _now, const {}, const {}), [at84]);
final announced = {
chatQuotaKey('Claude Code', at84): chatQuotaMarkOf(at84),
};
expect(chatQuotaNewlyCrossed(usage, _now, announced, const {}), isEmpty);
expect(chatQuotaNewlyCrossed(usage, _now, const {}, announced), isEmpty);
final at95 = _window('five', 95.5, _hour);
expect(chatQuotaNewlyCrossed(_usage([at95]), _now, announced, const {}), [
at95,
]);
});
test('texts', () {
final five = _window('5-hour', 84.4, const Duration(hours: 1, minutes: 20));
expect(
chatQuotaWarningTitle('Claude Code', five),
'Claude Code 5-hour limit 84% used',
);
expect(chatQuotaResetsIn(five, _now), 'in 1\u00a0h\u00a020\u00a0m');
expect(chatQuotaResetsIn(_window('w', 90, _hour * 3), _now), 'in 3\u00a0h');
expect(
chatQuotaResetsIn(_window('w', 90, const Duration(minutes: 7)), _now),
'in 7\u00a0m',
);
expect(chatQuotaResetsIn(_window('w', 90, _day), _now), isNull);
expect(chatQuotaResetsIn(_window('w', 90, null), _now), isNull);
expect(
chatQuotaAnnouncement('Claude Code', [
_window('5-hour', 90.2, _hour),
_window('Weekly', 95, _day),
]),
'Claude Code 5-hour limit at 90% · Weekly at 95%',
);
});
}