From 90fc31a938afac0f6910c7947c6ecba0adebfea4 Mon Sep 17 00:00:00 2001
From: Matthias Nott <mnott@mnsoft.org>
Date: Mon, 06 Apr 2026 14:12:23 +0200
Subject: [PATCH] fix: immediate disk writes, notification tap skip same session, catch_up trace logging, resume simplification

---
 lib/providers/providers.dart |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)

diff --git a/lib/providers/providers.dart b/lib/providers/providers.dart
index 63cf5a4..ad4259a 100644
--- a/lib/providers/providers.dart
+++ b/lib/providers/providers.dart
@@ -9,6 +9,7 @@
 import '../models/server_config.dart';
 import '../models/session.dart';
 import '../services/message_store.dart';
+import '../services/trace_service.dart';
 import '../services/mqtt_service.dart' show ConnectionStatus;
 import '../services/navigate_notifier.dart';
 
@@ -99,6 +100,12 @@
 
   /// 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)
     if (_currentSessionId != null && state.isNotEmpty) {
       await MessageStore.writeDirect(_currentSessionId!, state);
@@ -113,7 +120,8 @@
   void addMessage(Message message) {
     state = [...state, message];
     if (_currentSessionId != null) {
-      MessageStore.save(_currentSessionId!, state);
+      // Write immediately (not debounced) to prevent race with switchSession's loadAll
+      MessageStore.writeDirect(_currentSessionId!, state);
     }
   }
 

--
Gitblit v1.3.1