Stop background tasks separately from chat turns

Keep Esc and /stop scoped to the foreground turn while exposing an explicit stop-tasks operation for supported chat backends. Add the protocol, server routing, app command, and coverage for task stopping and unsupported harnesses.
This commit is contained in:
2026-11-13 16:47:41 +00:00
parent c3c2c1bbc9
commit 2a182d7f17
19 changed files with 331 additions and 2 deletions
@@ -596,6 +596,9 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
} else {
_showCommandNotice('No turn is running in this chat.', failed: true);
}
} else if (command.name == chatCommandStopTasks) {
_consumeCommandDraft();
unawaited(_stopTasksFromCommand());
} else if (command.name == chatCommandHelp) {
_consumeCommandDraft();
_showCommandNotice(chatCommandHelpText());
@@ -624,6 +627,16 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
_showCommandNotice(chatStopCommandNotice);
}
Future<void> _stopTasksFromCommand() async {
try {
if (mounted) {
_showCommandNotice(await widget.composer.stopTasks());
}
} catch (error) {
if (mounted) _showCommandNotice('$error', failed: true);
}
}
void _consumeCommandDraft() {
_text.clear();
unawaited(widget.composer.setDraft(''));
@@ -373,6 +373,17 @@ class ChatComposerController extends ChangeNotifier {
}
}
/// Stops background tasks without interrupting the current turn.
Future<String> stopTasks() async {
final reply = await _repository.stopTasks(sessionId);
final result = reply['chat_stop_tasks'];
if (result is Map) {
final message = '${result['message'] ?? ''}'.trim();
if (message.isNotEmpty) return message;
}
return 'Background tasks stop request accepted.';
}
/// Follows the server's queue report: a prompt it lists is the server's
/// (any stored device copy is dropped) and shows "Queued"; one that left
/// the queue shows what happened to it.
@@ -272,6 +272,11 @@ class ChatRepository {
'chat_query': {'session_id': sessionId},
});
Future<JsonMap> stopTasks(String sessionId) =>
gateway.call('chat_stop_tasks', {
'chat_query': {'session_id': sessionId},
});
/// Removes one prompt the server still holds in the session's queue. The
/// reply's `chat_queue` is the queue afterwards; a prompt already handed to
/// the harness is refused.
@@ -25,6 +25,7 @@ const String chatCommandNew = 'new';
const String chatCommandClose = 'close';
const String chatCommandRename = 'rename';
const String chatCommandStop = 'stop';
const String chatCommandStopTasks = 'stop_tasks';
const String chatCommandHelp = 'help';
/// The recorded command list, in the order the palette shows it.
@@ -50,6 +51,11 @@ const List<ChatCommand> chatCommands = <ChatCommand>[
summary: 'Stop the turn that is running',
usage: '/stop',
),
ChatCommand(
name: chatCommandStopTasks,
summary: 'Stop all background tasks in this chat',
usage: '/stop_tasks',
),
ChatCommand(
name: chatCommandHelp,
summary: 'List every command',
@@ -249,6 +249,7 @@ void main() {
'/close — Finish this session for every client, keeping its history\n'
'/rename <title> — Rename this session\n'
'/stop — Stop the turn that is running\n'
'/stop_tasks — Stop all background tasks in this chat\n'
'/help — List every command\n'
'!<command> — Run a shell command in this chat’s folder; '
'the agent does not see it unless you send it',
@@ -294,6 +295,34 @@ void main() {
variant: _desktop,
);
testWidgets(
'/stop_tasks stops background tasks without interrupting the turn',
(tester) async {
gateway.stopTasks = () async => {
'chat_stop_tasks': {
'stopped': 2,
'message': 'Stopped background tasks.',
},
};
await pumpComposer(tester);
await tester.enterText(
find.byKey(const Key('chat-composer-field')),
'/stop_tasks',
);
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pumpAndSettle();
expect(gateway.stopTasksBodies, [
<String, dynamic>{'session_id': 's'},
]);
expect(gateway.interruptBodies, isEmpty);
expect(gateway.promptBodies, isEmpty);
expect(find.text('Stopped background tasks.'), findsOneWidget);
},
variant: _desktop,
);
testWidgets('/stop interrupts the current session through the composer', (
tester,
) async {
@@ -16,6 +16,7 @@ class ComposerBarGateway implements ApiGateway {
JsonMap page = chatPage(const []);
Future<JsonMap> Function(String text, String operationId)? prompt;
Future<JsonMap> Function()? interrupt;
Future<JsonMap> Function()? stopTasks;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
@@ -32,6 +33,8 @@ class ComposerBarGateway implements ApiGateway {
: prompt!('${body['text'] ?? ''}', '${body['operation_id'] ?? ''}');
case 'chat_interrupt':
return interrupt == null ? <String, dynamic>{} : interrupt!();
case 'chat_stop_tasks':
return stopTasks == null ? <String, dynamic>{} : stopTasks!();
}
return <String, dynamic>{};
}
@@ -49,6 +52,11 @@ class ComposerBarGateway implements ApiGateway {
.map((call) => call.fields['chat_query'] as JsonMap)
.toList();
List<JsonMap> get stopTasksBodies => calls
.where((call) => call.type == 'chat_stop_tasks')
.map((call) => call.fields['chat_query'] as JsonMap)
.toList();
void emitEvent(JsonMap body) =>
_frames.add({'type': 'chat_event', 'chat_event': body});
@@ -8,6 +8,7 @@ void main() {
'close',
'rename',
'stop',
'stop_tasks',
'help',
]);
expect(chatCommands.map((command) => command.usage), [
@@ -15,6 +16,7 @@ void main() {
'/close',
'/rename <title>',
'/stop',
'/stop_tasks',
'/help',
]);
expect(chatCommands.map((command) => command.summary), [
@@ -22,6 +24,7 @@ void main() {
'Finish this session for every client, keeping its history',
'Rename this session',
'Stop the turn that is running',
'Stop all background tasks in this chat',
'List every command',
]);
expect(