Matthias Nott
10 days ago 90fc31a938afac0f6910c7947c6ecba0adebfea4
lib/providers/providers.dart
....@@ -9,6 +9,7 @@
99 import '../models/server_config.dart';
1010 import '../models/session.dart';
1111 import '../services/message_store.dart';
12
+import '../services/trace_service.dart';
1213 import '../services/mqtt_service.dart' show ConnectionStatus;
1314 import '../services/navigate_notifier.dart';
1415
....@@ -99,6 +100,12 @@
99100
100101 /// Switch to a new session and load its messages.
101102 Future<void> switchSession(String sessionId) async {
103
+ // Log caller for debugging
104
+ final trace = StackTrace.current.toString().split('\n').take(4).join(' | ');
105
+ TraceService.instance.addTrace(
106
+ 'switchSession',
107
+ 'from=${_currentSessionId?.substring(0, 8) ?? "null"}(${state.length}) → ${sessionId.substring(0, 8)} | $trace',
108
+ );
102109 // Write current session DIRECTLY to disk (no debounce — prevents data loss)
103110 if (_currentSessionId != null && state.isNotEmpty) {
104111 await MessageStore.writeDirect(_currentSessionId!, state);
....@@ -113,7 +120,8 @@
113120 void addMessage(Message message) {
114121 state = [...state, message];
115122 if (_currentSessionId != null) {
116
- MessageStore.save(_currentSessionId!, state);
123
+ // Write immediately (not debounced) to prevent race with switchSession's loadAll
124
+ MessageStore.writeDirect(_currentSessionId!, state);
117125 }
118126 }
119127