Show message delivery state and keep unsent messages across reloads

A sent message is stored on the device until the server accepts it, so a
reload mid-request no longer loses it: on reopening, a stored intent the
history records is cleared as sent, otherwise it shows as Not sent with
Retry (same operation id) and Edit. The operator's bubble shows Sending,
Sent (newest only), Queued or Not sent. A live refusal keeps the old
contract (exact draft and chips back in the composer, unchanged re-send
reuses the id) and marks the bubble Not sent with Retry. On web, leaving
or reloading the page while a prompt is in flight asks first.
This commit is contained in:
2026-10-31 20:03:29 +00:00
parent 4bc99f490b
commit 8d147ac83b
6 changed files with 134 additions and 38 deletions
@@ -206,8 +206,11 @@ void main() {
expect(find.byKey(const Key('chat-composer-error')), findsOneWidget);
expect(find.textContaining('response lost'), findsOneWidget);
expect(gateway.promptBodies.single['operation_id'], 'op/1');
// The refused optimistic turn is withdrawn, not left as a fake message.
expect(find.text('keep me'), findsNothing);
// The refused turn says it was not sent; its text is back in the
// composer, so it offers Retry but not Edit.
expect(find.byKey(const Key('chat-delivery-failed')), findsOneWidget);
expect(find.byKey(const Key('chat-delivery-retry')), findsOneWidget);
expect(find.byKey(const Key('chat-delivery-edit')), findsNothing);
await tester.tap(find.byKey(const Key('chat-composer-primary')));
await tester.pumpAndSettle();
@@ -218,6 +221,7 @@ void main() {
]);
expect(fieldText(tester), '');
expect(find.byKey(const Key('chat-composer-error')), findsNothing);
expect(find.byKey(const Key('chat-delivery-failed')), findsNothing);
await tester.enterText(
find.byKey(const Key('chat-composer-field')),
@@ -102,11 +102,14 @@ void main() {
await controller.send();
expect(controller.error, contains('response lost'));
// The composer is free for the next message; the refused one is kept.
expect(controller.draft, '');
// The exact draft is back in the composer, and the message is Not sent.
expect(controller.draft, ' keep me ');
expect(await drafts.read('s'), ' keep me ');
expect(rejected.single, contains('response lost'));
expect(controller.deliveries.unsent.single.operationId, 'op/1');
expect(drafts.unsentEntries['s'], hasLength(1));
expect(controller.isInComposer('op/1'), isTrue);
// The draft store keeps it across a reload, so no second stored copy.
expect(drafts.unsentEntries, isEmpty);
fail = false;
expect(await controller.retry('op/1'), isTrue);
@@ -118,9 +121,11 @@ void main() {
'msg_op1',
'msg_op1',
]);
expect(controller.draft, '');
expect(controller.error, isNull);
expect(controller.deliveries.unsent, isEmpty);
expect(drafts.unsentEntries, isEmpty);
expect(controller.composerSendsAccepted, 1);
fail = true;
await controller.setDraft('other');
@@ -135,8 +135,9 @@ void main() {
await _pump(tester);
expect(_state('failed'), findsOneWidget);
expect(find.text('Not sent'), findsOneWidget);
expect(find.text('Please refuse'), findsOneWidget);
expect(drafts.unsentEntries['s'], hasLength(1));
// The text is back in the composer, so the bubble offers Retry only.
expect(find.byKey(const Key('chat-delivery-edit')), findsNothing);
expect(drafts.entries['s'], 'Please refuse');
await tester.tap(find.byKey(const Key('chat-delivery-retry')));
await _pump(tester);
@@ -150,7 +151,7 @@ void main() {
gateway.prompts.last.complete({});
await _pump(tester);
expect(_state('sent'), findsOneWidget);
expect(find.text('Please refuse'), findsOneWidget);
expect(drafts.entries, isEmpty);
expect(drafts.unsentEntries, isEmpty);
});
}