From 6cbbea9b96db551e5c0ac26f0ace3d4c3d82a276 Mon Sep 17 00:00:00 2001
From: Matthias Nott <mnott@mnsoft.org>
Date: Mon, 06 Apr 2026 15:02:33 +0200
Subject: [PATCH] fix: single pailot/out topic, per-session file locks, merge protection, resume reconnect
---
lib/providers/providers.dart | 20 +++++++++++++++-----
1 files changed, 15 insertions(+), 5 deletions(-)
diff --git a/lib/providers/providers.dart b/lib/providers/providers.dart
index ad4259a..6155189 100644
--- a/lib/providers/providers.dart
+++ b/lib/providers/providers.dart
@@ -100,28 +100,38 @@
/// Switch to a new session and load its messages.
Future<void> switchSession(String sessionId) async {
- // Log caller for debugging
final trace = StackTrace.current.toString().split('\n').take(4).join(' | ');
TraceService.instance.addTrace(
'switchSession',
'from=${_currentSessionId?.substring(0, 8) ?? "null"}(${state.length}) → ${sessionId.substring(0, 8)} | $trace',
);
- // Write current session DIRECTLY to disk (no debounce — prevents data loss)
+ // Write current session to disk
if (_currentSessionId != null && state.isNotEmpty) {
await MessageStore.writeDirect(_currentSessionId!, state);
}
+ // Skip reload if staying on the same session — messages are already in memory
+ if (_currentSessionId == sessionId) {
+ TraceService.instance.addTrace('switchSession SKIP', 'already on ${sessionId.substring(0, 8)}');
+ return;
+ }
+
_currentSessionId = sessionId;
final messages = await MessageStore.loadAll(sessionId);
- state = messages;
+ // Merge: if addMessage ran during loadAll and added messages for THIS session,
+ // they'll be in state but not in the loaded messages. Keep the longer list.
+ if (state.length > messages.length && _currentSessionId == sessionId) {
+ TraceService.instance.addTrace('switchSession MERGE', 'kept ${state.length} (loaded ${messages.length})');
+ } else {
+ state = messages;
+ }
}
/// Add a message to the current session.
void addMessage(Message message) {
state = [...state, message];
if (_currentSessionId != null) {
- // Write immediately (not debounced) to prevent race with switchSession's loadAll
- MessageStore.writeDirect(_currentSessionId!, state);
+ MessageStore.save(_currentSessionId!, state);
}
}
--
Gitblit v1.3.1