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
@@ -3,6 +3,8 @@ import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../../core/api_gateway.dart';
const chatAutoContinuePreferenceKey = 'pipelines.chatAutoContinue.v1';
/// The usage-limit banner's "Auto-continue when limits reset" choice: one
@@ -68,3 +70,85 @@ class ChatAutoContinue extends ChangeNotifier {
}
}
}
/// The "Auto-continue when limits reset" choice the server keeps for one
/// chat (`chat_auto_continue`): per Claude home, or the server's own for a
/// chat without one. A server that keeps it also schedules the continue
/// prompt itself, so the app only shows it. An older server does not answer
/// with a choice; then [unsupported] is set and the per-device
/// [ChatAutoContinue] preference, and the app's own scheduling, apply as
/// before.
class ChatServerAutoContinue extends ChangeNotifier {
ChatServerAutoContinue({required this.gateway, required this.sessionId});
final ApiGateway gateway;
final String sessionId;
bool? _enabled;
bool _unsupported = false;
bool _disposed = false;
Future<void>? _loading;
/// Whether the server answered with its choice.
bool get supported => _enabled != null && !_unsupported;
/// Whether the server does not keep the choice (an older server).
bool get unsupported => _unsupported;
/// Whether the server's answer (or refusal) arrived.
bool get loaded => supported || _unsupported;
/// The server's choice; on until it says otherwise.
bool get enabled => _enabled ?? true;
/// Reads the server's choice once.
Future<void> load() => _loading ??= _read();
Future<void> _read() async {
try {
_enabled = _choice(await _call(null));
_unsupported = _enabled == null;
} on Object {
// An older server does not know the request.
_unsupported = true;
}
_notify();
}
/// Records the operator's choice on the server; the shown choice goes
/// back when the server does not record it.
Future<void> set(bool on) async {
final previous = _enabled;
_enabled = on;
_notify();
try {
final recorded = _choice(await _call(on));
if (recorded == null) throw StateError('no auto_continue in the reply');
_enabled = recorded;
} on Object catch (error) {
debugPrint('Auto-continue: the server did not record it: $error');
_enabled = previous;
}
_notify();
}
Future<JsonMap> _call(bool? on) => gateway.call('chat_auto_continue', {
'chat_auto_continue': {'session_id': sessionId, 'auto_continue': ?on},
});
static bool? _choice(JsonMap reply) {
final body = reply['chat_auto_continue'];
final value = body is Map ? body['auto_continue'] : null;
return value is bool ? value : null;
}
void _notify() {
if (!_disposed) notifyListeners();
}
@override
void dispose() {
_disposed = true;
super.dispose();
}
}
@@ -18,12 +18,16 @@ import 'chat_thread_controller.dart';
/// "Auto-resuming at 15:41", an
/// "Auto-continue when limits reset" checkbox and Try again.
///
/// With auto-continue on (a preference shared by every chat, on by default,
/// [ChatAutoContinue]) and the transcript ending on the limit's refusal, the
/// banner hands the server a prompt to send a minute after the reset: the
/// refused operator message again, or "continue" when the refused turn had
/// already worked on it. The server holds it durably, so it is sent with the
/// app closed or the server restarted. Unchecking takes it back out of the
/// The chat counts as refused while its latest turn is the limit's refusal,
/// whatever notices or agent reports arrived after it. With auto-continue on,
/// the server queues a prompt to send a minute after the reset (the refused
/// operator message again, or "continue" when the refused turn had already
/// worked on it), with no app involved; the choice is the server's
/// ([ChatServerAutoContinue], per Claude home, on by default), and the
/// banner shows the prompt it scheduled. An older server that keeps no such
/// choice falls back to the per-device [ChatAutoContinue] preference, and the
/// banner hands that server the prompt itself, under the same operation id
/// so a server copy is never doubled. Unchecking takes it back out of the
/// server's queue; Try again takes it out and sends now. The banner hides
/// once the reset passes, or once the account is switched (by the operator
/// or by the server's automatic switch, whose queued continue prompt also
@@ -35,6 +39,7 @@ class ChatUsageLimitBanner extends StatefulWidget {
required this.composer,
this.accounts,
ChatAutoContinue? autoContinue,
this.serverAutoContinue,
this.now = DateTime.now,
}) : autoContinue = autoContinue ?? ChatAutoContinue.shared;
@@ -45,6 +50,10 @@ class ChatUsageLimitBanner extends StatefulWidget {
/// null for a chat that is not Claude Code's.
final ChatClaudeAccountsController? accounts;
final ChatAutoContinue autoContinue;
/// The server's choice for this chat; null builds one on [thread]'s
/// gateway.
final ChatServerAutoContinue? serverAutoContinue;
final DateTime Function() now;
@override
@@ -59,18 +68,48 @@ class _ChatUsageLimitBannerState extends State<ChatUsageLimitBanner> {
/// refused request is not repeated on every rebuild.
final Set<String> _requested = {};
late ChatServerAutoContinue _server;
bool _ownsServer = false;
@override
void initState() {
super.initState();
unawaited(widget.autoContinue.load());
_server = _serverFor(widget);
}
ChatServerAutoContinue _serverFor(ChatUsageLimitBanner widget) {
_ownsServer = widget.serverAutoContinue == null;
return widget.serverAutoContinue ??
ChatServerAutoContinue(
gateway: widget.thread.gateway,
sessionId: widget.thread.sessionId,
);
}
@override
void didUpdateWidget(ChatUsageLimitBanner old) {
super.didUpdateWidget(old);
if (widget.serverAutoContinue != old.serverAutoContinue ||
widget.thread.sessionId != old.thread.sessionId) {
if (_ownsServer) _server.dispose();
_server = _serverFor(widget);
_requested.clear();
}
}
@override
void dispose() {
_reset?.cancel();
if (_ownsServer) _server.dispose();
super.dispose();
}
/// Whether auto-continue is on: the server's choice, or this device's for
/// an older server.
bool get _enabled =>
_server.supported ? _server.enabled : widget.autoContinue.enabled;
/// Rebuilds when the reset passes, so the banner leaves on time.
void _watchReset(DateTime? at, DateTime now) {
if (at == _resetFor) return;
@@ -124,11 +163,27 @@ class _ChatUsageLimitBannerState extends State<ChatUsageLimitBanner> {
}
}
Future<void> _toggle(bool on, List<JsonMap> pending) async {
Future<void> _toggle(
bool on,
List<JsonMap> pending,
ChatRefusedTurn? refused,
ChatUsageLimit limit,
) async {
// Rechecking may schedule the same refusal again.
_requested.clear();
await widget.autoContinue.set(on);
if (!on) await _cancel(pending);
if (!_server.supported) {
await widget.autoContinue.set(on);
if (!on) await _cancel(pending);
return;
}
await _server.set(on);
if (!on) {
await _cancel(pending);
} else if (refused != null && pending.isEmpty && mounted) {
// The server considered this refusal while it was off; ask for its
// prompt now, under the id the server would have used.
_arm(refused, limit);
}
}
Future<void> _tryAgain(ChatRefusedTurn refused, List<JsonMap> pending) async {
@@ -144,6 +199,7 @@ class _ChatUsageLimitBannerState extends State<ChatUsageLimitBanner> {
widget.thread,
widget.composer,
widget.autoContinue,
_server,
]),
builder: (context, _) {
final now = widget.now();
@@ -158,9 +214,13 @@ class _ChatUsageLimitBannerState extends State<ChatUsageLimitBanner> {
final refused = chatRefusedTurn(thread.messages, thread.accountUsage);
final pending = _pending(now);
final auto = widget.autoContinue;
unawaited(_server.load());
// The server already switched accounts and is continuing the chat.
final switching = chatAutoSwitchPending(thread.queuedPrompts);
if (auto.loaded &&
// A server that keeps the choice schedules the prompt itself; only
// for an older one does this device ask for it.
if (_server.unsupported &&
auto.loaded &&
auto.enabled &&
refused != null &&
pending.isEmpty &&
@@ -183,7 +243,7 @@ class _ChatUsageLimitBannerState extends State<ChatUsageLimitBanner> {
final theme = Theme.of(context);
final scheme = theme.colorScheme;
final text = theme.textTheme;
final enabled = widget.autoContinue.enabled;
final enabled = _enabled;
String time(DateTime at) => chatScheduleTime(context, at, now: () => now);
final resumesAt = pending.isEmpty
? limit.sendAt
@@ -253,7 +313,7 @@ class _ChatUsageLimitBannerState extends State<ChatUsageLimitBanner> {
spacing: 8,
runSpacing: 4,
children: [
_checkbox(context, enabled, pending),
_checkbox(context, enabled, pending, refused, limit),
Wrap(
spacing: 8,
runSpacing: 4,
@@ -283,8 +343,15 @@ class _ChatUsageLimitBannerState extends State<ChatUsageLimitBanner> {
);
}
Widget _checkbox(BuildContext context, bool enabled, List<JsonMap> pending) {
void toggle(bool? on) => unawaited(_toggle(on ?? false, pending));
Widget _checkbox(
BuildContext context,
bool enabled,
List<JsonMap> pending,
ChatRefusedTurn? refused,
ChatUsageLimit limit,
) {
void toggle(bool? on) =>
unawaited(_toggle(on ?? false, pending, refused, limit));
// A labelled checkbox: the label toggles it too, as one control.
return MergeSemantics(
child: InkWell(