Files
pipelines/app/lib/features/chat/domain/chat_usage_warning.dart
T
alan b6d06219cd Chat: warn before the harness account's usage limit runs out
A compact banner above the composer when any quota window of the chat's
harness account reaches 80% (error colours from 95%), naming the most
constrained window and a second one also at 80%+. Dismissing it holds per
harness+window until the next level (80/90/95) or a reset, across chats and
reloads; a newly crossed level is announced once with a root SnackBar. The
usage-limit banner wins. Fixture: quota=warn80|warn95.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 12:34:01 +00:00

179 lines
6.0 KiB
Dart

/// The early warning before a harness account runs out of quota: which
/// windows of the reported account usage (chat_account_usage.dart) are
/// nearly used up, which of them the operator has already been told about or
/// has dismissed, and the text that says so.
///
/// A window warns at 80%, again at 90% and again at 95%. A dismissal or an
/// announcement holds for the level it saw until the window reaches the next
/// level or resets (its reset time changes).
library;
import 'chat_account_usage.dart';
/// The percentages at which a window warns, lowest first.
const List<int> chatQuotaWarningLevels = [80, 90, 95];
/// The level from which the warning is drawn as urgent.
const int chatQuotaUrgentLevel = 95;
/// The highest warning level [percent] has reached, or 0 below the first.
/// Past 100% stays at the top level.
int chatQuotaLevel(double percent) {
var level = 0;
for (final threshold in chatQuotaWarningLevels) {
if (percent >= threshold) level = threshold;
}
return level;
}
/// Identifies one window of one harness account across chats.
String chatQuotaKey(String harness, ChatQuotaWindow window) =>
'$harness|${window.id}';
/// The level a window was at when it was dismissed or announced, and the
/// reset it had then.
class ChatQuotaMark {
const ChatQuotaMark({required this.level, required this.resetsAt});
final int level;
final int resetsAt;
/// Whether this mark still covers [window]: the same reset and no higher
/// level since.
bool covers(ChatQuotaWindow window) =>
resetsAt == window.resetsAt &&
level >= chatQuotaLevel(window.usedPercent);
@override
bool operator ==(Object other) =>
other is ChatQuotaMark &&
other.level == level &&
other.resetsAt == resetsAt;
@override
int get hashCode => Object.hash(level, resetsAt);
}
/// A warning to show: the most constrained window and, when another is also
/// at a warning level, the next one.
class ChatUsageWarning {
const ChatUsageWarning({
required this.harness,
required this.primary,
this.second,
});
final String harness;
final ChatQuotaWindow primary;
final ChatQuotaWindow? second;
int get level => chatQuotaLevel(primary.usedPercent);
bool get urgent => level >= chatQuotaUrgentLevel;
/// The windows this warning shows.
List<ChatQuotaWindow> get windows => [primary, ?second];
}
/// The windows of [usage] at a warning level whose reset is still ahead (or
/// unknown), most constrained first: the highest percentage, then the
/// soonest reset, an unknown reset last.
List<ChatQuotaWindow> chatQuotaWarningWindows(
ChatAccountUsage? usage,
DateTime now,
) {
if (usage == null || usage.unsupported) return const [];
final at = now.millisecondsSinceEpoch;
final windows = [
for (final window in usage.windows)
if (chatQuotaLevel(window.usedPercent) > 0 &&
(window.resetsAt <= 0 || window.resetsAt > at))
window,
];
int reset(ChatQuotaWindow window) =>
window.resetsAt > 0 ? window.resetsAt : 1 << 62;
windows.sort((a, b) {
final percent = b.usedPercent.compareTo(a.usedPercent);
return percent != 0 ? percent : reset(a).compareTo(reset(b));
});
return windows;
}
/// The warning for [usage] at [now], leaving out windows whose dismissal in
/// [dismissed] (by [chatQuotaKey]) still holds; null when nothing is left.
ChatUsageWarning? chatUsageWarning(
ChatAccountUsage? usage,
DateTime now,
Map<String, ChatQuotaMark> dismissed,
) {
final windows = [
for (final window in chatQuotaWarningWindows(usage, now))
if (!(dismissed[chatQuotaKey(usage!.harness, window)]?.covers(window) ??
false))
window,
];
if (windows.isEmpty) return null;
return ChatUsageWarning(
harness: usage!.harness,
primary: windows.first,
second: windows.length > 1 ? windows[1] : null,
);
}
/// The windows of [usage] that reached a level the operator has not been
/// told about: neither announced (in [announced]) nor dismissed at that level
/// since their last reset. Most constrained first.
List<ChatQuotaWindow> chatQuotaNewlyCrossed(
ChatAccountUsage? usage,
DateTime now,
Map<String, ChatQuotaMark> announced,
Map<String, ChatQuotaMark> dismissed,
) => [
for (final window in chatQuotaWarningWindows(usage, now))
if (!(announced[chatQuotaKey(usage!.harness, window)]?.covers(window) ??
false) &&
!(dismissed[chatQuotaKey(usage.harness, window)]?.covers(window) ??
false))
window,
];
/// The mark recording [window] at its current level.
ChatQuotaMark chatQuotaMarkOf(ChatQuotaWindow window) => ChatQuotaMark(
level: chatQuotaLevel(window.usedPercent),
resetsAt: window.resetsAt,
);
/// "Claude Code 5-hour limit at 90%", announcing newly crossed [windows].
String chatQuotaAnnouncement(String harness, List<ChatQuotaWindow> windows) {
final first = windows.first;
final name = harness.isEmpty ? '' : '$harness ';
return [
'$name${first.label} limit at ${first.usedPercent.round()}%',
for (final other in windows.skip(1))
'${other.label} at ${other.usedPercent.round()}%',
].join(' · ');
}
/// "Claude Code 5-hour limit 84% used".
String chatQuotaWarningTitle(String harness, ChatQuotaWindow window) {
final name = harness.isEmpty ? '' : '$harness ';
return '$name${window.label} limit ${window.usedPercent.round()}% used';
}
/// "in 1 h 20 m" (no-break spaces) for a reset within a day of [now]; null for an unknown
/// reset or one a day or more away (shown as a local time instead).
String? chatQuotaResetsIn(ChatQuotaWindow window, DateTime now) {
if (window.resetsAt <= 0) return null;
final left = DateTime.fromMillisecondsSinceEpoch(
window.resetsAt,
).difference(now);
if (left >= const Duration(days: 1)) return null;
if (left.inMinutes < 1) return 'in under a minute';
final hours = left.inHours;
final minutes = left.inMinutes % 60;
// No-break spaces keep a wrapped line from splitting the duration.
if (hours == 0) return 'in $minutes\u00a0m';
return minutes == 0
? 'in $hours\u00a0h'
: 'in $hours\u00a0h\u00a0$minutes\u00a0m';
}