Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix(desktop): signing out keeps the account's refused and unconfirmed Swarm messages (#2410)

0xken158856cdb9c7fe
diff --git a/desktop/lib/super_tabs/client.dart b/desktop/lib/super_tabs/client.dart
index e7a17904a..b159ac28c 100644
--- a/desktop/lib/super_tabs/client.dart
+++ b/desktop/lib/super_tabs/client.dart
@@ -423,7 +423,12 @@ class SuperTabClient {
_listMentions = true;
discoveryError = null;
for (final controller in _loaded.values) {
- controller.revokeAccess('The account changed. Open this Swarm again.');
+ // Desktop keeps each account's unsent and unconfirmed messages for its
+ // next sign-in; the web forgets them with the rest ([_forgetSignedOut]).
+ controller.revokeAccess(
+ 'The account changed. Open this Swarm again.',
+ keepSaved: !_forgetsWebOnly,
+ );
}
for (final future in _controllers.values) {
unawaited(
diff --git a/desktop/lib/super_tabs/controller.dart b/desktop/lib/super_tabs/controller.dart
index 222b32c6a..b1ccc2592 100644
--- a/desktop/lib/super_tabs/controller.dart
+++ b/desktop/lib/super_tabs/controller.dart
@@ -1981,8 +1981,19 @@ class SuperTabController extends ChangeNotifier {
/// Open dialogs and native panes lose their content immediately, before
/// account cleanup waits for an in-flight read or a draft write.
- void revokeAccess(String message) {
+ ///
+ /// With [keepSaved] (Desktop), what is saved on this computer stays as it
+ /// was: this account's own refused, unsent and unconfirmed messages are
+ /// there for its next sign-in (saved state is kept per account), and an
+ /// unconfirmed one is checked against the backend by its receipt id then.
+ /// The web clears what an account leaves behind, so it keeps nothing.
+ void revokeAccess(String message, {bool keepSaved = false}) {
if (_disposed) return;
+ if (keepSaved) {
+ _pruneSendOrder();
+ _savedAtRevoke ??=
+ jsonDecode(jsonEncode(_localState())) as Map<String, dynamic>;
+ }
accessRevoked = true;
devicesSelected = false;
error = message;
@@ -2066,7 +2077,12 @@ class SuperTabController extends ChangeNotifier {
},
};
- Future<void> _persist() {
+ /// The state saved once access is revoked ([revokeAccess]): every later
+ /// write, an autosave queued before or a send answering after, writes it, so
+ /// the emptied screen never overwrites what this account kept here.
+ Map<String, dynamic>? _savedAtRevoke;
+
+ void _pruneSendOrder() {
final active = {
if (_pending != null) _pending!['operationId'],
..._sent.keys,
@@ -2075,10 +2091,14 @@ class SuperTabController extends ChangeNotifier {
..._queuedSends.map((q) => q.operationId),
};
_sendOrder.removeWhere((id) => !active.contains(id));
+ }
+
+ Future<void> _persist() {
+ if (_savedAtRevoke == null) _pruneSendOrder();
// Serial writes prevent an older autosave from restoring a resolved receipt.
final next = _persisting
.catchError((Object _) {})
- .then((_) => save(_localState()));
+ .then((_) => save(_savedAtRevoke ?? _localState()));
_persisting = next;
return next;
}
diff --git a/desktop/test/super_tab_sign_out_keeps_outgoing_test.dart b/desktop/test/super_tab_sign_out_keeps_outgoing_test.dart
new file mode 100644
index 000000000..22a13b650
--- /dev/null
+++ b/desktop/test/super_tab_sign_out_keeps_outgoing_test.dart
@@ -0,0 +1,200 @@
+import 'dart:async';
+
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness/core/local_key_value_store.dart';
+import 'package:harness/super_tabs/client.dart';
+import 'package:harness/super_tabs/controller.dart';
+import 'package:harness/super_tabs/reading.dart';
+
+import 'support/super_tab_fixture.dart';
+import 'swarm_state_test.dart' show MemoryStore;
+
+const _key = 'super_tab.owner.tab';
+
+/// A browser's storage: it can list its keys.
+class _BrowserStore extends MemoryStore
+ implements KeyListingLocalKeyValueStore {
+ @override
+ Future<List<String>> keys() async => values.keys.toList();
+}
+
+/// The client over [server], reading as Desktop does unless [reading] says.
+SuperTabClient _client(
+ ConversationServer server,
+ MemoryStore store, {
+ Completer<void>? Function()? holdPost,
+ SuperTabReading reading = SuperTabReading.desktop,
+}) => SuperTabClient(
+ transport: (method, path, {body}) async {
+ if (method == 'POST') await holdPost?.call()?.future;
+ return server.request(method, path, body: body);
+ },
+ storage: store,
+ discoveryInterval: Duration.zero,
+)..reading = reading;
+
+List<(String, SuperTabOutgoingState)> _outgoing(SuperTabController c) => [
+ for (final m in c.outgoing('lead')) (m.text, m.state),
+];
+
+/// Signs out and lets the controllers' last writes land.
+Future<void> _signOut(SuperTabClient client) async {
+ client.bind(null);
+ await pumpEventQueue();
+}
+
+void main() {
+ test('a refused send survives signing out and back in as the same account, '
+ 'and another account sees none of it', () async {
+ final server = ConversationServer()..reject = true;
+ final store = MemoryStore();
+ final client = _client(server, store);
+ addTearDown(client.dispose);
+ client.bind('owner');
+ final first = await client.open('tab');
+ await first.refresh();
+ first.setDraft('lead', 'Keep this text');
+ await first.send();
+ expect(_outgoing(first), [
+ ('Keep this text', SuperTabOutgoingState.refused),
+ ]);
+
+ await _signOut(client);
+ expect(first.outgoing('lead'), isEmpty, reason: 'The screen clears.');
+ expect(store.values[_key], contains('Keep this text'));
+
+ client.bind('owner');
+ final again = await client.open('tab');
+ await again.refresh();
+ expect(_outgoing(again), [
+ ('Keep this text', SuperTabOutgoingState.refused),
+ ]);
+
+ client.bind('someone-else');
+ final other = await client.open('tab');
+ await other.refresh();
+ expect(other.outgoing('lead'), isEmpty);
+ expect(
+ store.values['super_tab.someone-else.tab'] ?? '',
+ isNot(contains('Keep this text')),
+ );
+ expect(store.values[_key], contains('Keep this text'));
+ });
+
+ test('an unconfirmed send survives signing out and is checked against the '
+ 'backend by its receipt id after signing back in', () async {
+ final server = ConversationServer()..loseReply = true;
+ final store = MemoryStore();
+ final client = _client(server, store);
+ addTearDown(client.dispose);
+ client.bind('owner');
+ final first = await client.open('tab');
+ await first.refresh();
+ first.setDraft('lead', 'Did this arrive?');
+ await first.send();
+ expect(_outgoing(first), [
+ ('Did this arrive?', SuperTabOutgoingState.unconfirmed),
+ ]);
+
+ await _signOut(client);
+ client.bind('owner');
+ final again = await client.open('tab');
+ await again.refresh();
+ expect(_outgoing(again), [
+ ('Did this arrive?', SuperTabOutgoingState.unconfirmed),
+ ]);
+
+ await again.retryOutgoing(again.outgoing('lead').single);
+ expect(server.posts, hasLength(2));
+ expect(
+ server.posts[1]['operationId'],
+ server.posts[0]['operationId'],
+ reason:
+ 'The same receipt id: the backend answers, nothing is sent twice.',
+ );
+ expect(server.messages, hasLength(1));
+ expect(again.outgoing('lead'), isEmpty);
+ expect(again.messages('lead').map((m) => m.text), ['Did this arrive?']);
+
+ client.bind('someone-else');
+ final other = await client.open('tab');
+ await other.refresh();
+ expect(other.outgoing('lead'), isEmpty);
+ });
+
+ test('a send that answers after signing out cannot erase the kept ones, '
+ 'and its own text stays to be checked', () async {
+ // Messages named as the backend names them, by the send's receipt id.
+ final server = ConversationServer()
+ ..reject = true
+ ..hashedIds = true;
+ final store = MemoryStore();
+ Completer<void>? gate;
+ final client = _client(server, store, holdPost: () => gate);
+ addTearDown(client.dispose);
+ client.bind('owner');
+ final first = await client.open('tab');
+ await first.refresh();
+ first.setDraft('lead', 'Refused one');
+ await first.send();
+ server.reject = false;
+ gate = Completer<void>();
+ first.setDraft('lead', 'In flight');
+ final flying = first.send();
+ await pumpEventQueue();
+
+ await _signOut(client);
+ gate.complete();
+ await flying;
+ await pumpEventQueue();
+ expect(first.outgoing('lead'), isEmpty);
+ expect(store.values[_key], contains('Refused one'));
+ expect(store.values[_key], contains('In flight'));
+
+ client.bind('owner');
+ final again = await client.open('tab');
+ await again.refresh();
+ expect(again.outgoing('lead').map((m) => m.text), contains('Refused one'));
+ // The answer came after sign-out: the backend's transcript or its receipt
+ // says it arrived, and it shows once.
+ expect(
+ [
+ ...again.messages('lead').map((m) => m.text),
+ ...again.outgoing('lead').map((m) => m.text),
+ ].where((t) => t == 'In flight'),
+ hasLength(1),
+ );
+ });
+
+ test(
+ 'the web still forgets what a signed-out account left (#2333)',
+ () async {
+ final server = ConversationServer()..reject = true;
+ final store = _BrowserStore();
+ final client = _client(
+ server,
+ store,
+ reading: const SuperTabReading(drawsSaved: true),
+ );
+ addTearDown(client.dispose);
+ client.bind('owner');
+ final first = await client.open('tab');
+ await first.refresh();
+ first.setDraft('lead', 'Not on a shared browser');
+ await first.send();
+ expect(first.outgoing('lead'), hasLength(1));
+
+ await _signOut(client);
+ await client.forgotten;
+ await pumpEventQueue();
+ expect(
+ store.values[_key] ?? '',
+ isNot(contains('Not on a shared browser')),
+ );
+ client.bind('owner');
+ final again = await client.open('tab');
+ await again.refresh();
+ expect(again.outgoing('lead'), isEmpty);
+ },
+ );
+}