Matthias Nott
9 days ago 06bb73662d32d65d1e775a4dd35f67d82d673e40
lib/providers/providers.dart
....@@ -98,73 +98,48 @@
9898
9999 String? get currentSessionId => _currentSessionId;
100100
101
- /// Switch to a new session and load its messages.
102
- Future<void> switchSession(String sessionId) async {
103
- final trace = StackTrace.current.toString().split('\n').take(4).join(' | ');
104
- TraceService.instance.addTrace(
105
- 'switchSession',
106
- 'from=${_currentSessionId?.substring(0, 8) ?? "null"}(${state.length}) → ${sessionId.substring(0, 8)} | $trace',
107
- );
108
- // Write current session to disk
109
- if (_currentSessionId != null && state.isNotEmpty) {
110
- await MessageStore.writeDirect(_currentSessionId!, state);
111
- }
112
-
113
- // Skip reload if staying on the same session — messages are already in memory
101
+ /// Switch to a session. SYNCHRONOUS — no async gap, no race with incoming
102
+ /// messages. MessageStoreV2.loadSession reads from the in-memory index.
103
+ void switchSession(String sessionId) {
114104 if (_currentSessionId == sessionId) {
115
- TraceService.instance.addTrace('switchSession SKIP', 'already on ${sessionId.substring(0, 8)}');
105
+ TraceService.instance.addTrace(
106
+ 'switchSession SKIP', 'already on ${sessionId.substring(0, 8)}');
116107 return;
117108 }
118
-
109
+ TraceService.instance.addTrace(
110
+ 'switchSession',
111
+ 'from=${_currentSessionId?.substring(0, 8) ?? "null"}(${state.length}) → ${sessionId.substring(0, 8)}',
112
+ );
119113 _currentSessionId = sessionId;
120
- final messages = await MessageStore.loadAll(sessionId);
121
- // Merge: if addMessage ran during loadAll and added messages for THIS session,
122
- // they'll be in state but not in the loaded messages. Keep the longer list.
123
- if (state.length > messages.length && _currentSessionId == sessionId) {
124
- TraceService.instance.addTrace('switchSession MERGE', 'kept ${state.length} (loaded ${messages.length})');
125
- } else {
126
- state = messages;
127
- }
114
+ state = MessageStoreV2.loadSession(sessionId);
128115 }
129116
130
- /// Add a message to the current session.
117
+ /// Add a message to the current session (display + append-only persist).
131118 void addMessage(Message message) {
132119 state = [...state, message];
133120 if (_currentSessionId != null) {
134
- MessageStore.save(_currentSessionId!, state);
121
+ MessageStoreV2.append(_currentSessionId!, message);
135122 }
136123 }
137124
138
- /// Update a message by ID.
125
+ /// Update a message by ID (in-memory only — patch is not persisted to log).
139126 void updateMessage(String id, Message Function(Message) updater) {
140127 state = state.map((m) => m.id == id ? updater(m) : m).toList();
141
- if (_currentSessionId != null) {
142
- MessageStore.save(_currentSessionId!, state);
143
- }
144128 }
145129
146
- /// Remove a message by ID.
130
+ /// Remove a message by ID (in-memory only).
147131 void removeMessage(String id) {
148132 state = state.where((m) => m.id != id).toList();
149
- if (_currentSessionId != null) {
150
- MessageStore.save(_currentSessionId!, state);
151
- }
152133 }
153134
154
- /// Remove all messages matching a predicate.
135
+ /// Remove all messages matching a predicate (in-memory only).
155136 void removeWhere(bool Function(Message) test) {
156137 state = state.where((m) => !test(m)).toList();
157
- if (_currentSessionId != null) {
158
- MessageStore.save(_currentSessionId!, state);
159
- }
160138 }
161139
162
- /// Clear all messages for the current session.
140
+ /// Clear all messages for the current session (in-memory only).
163141 void clearMessages() {
164142 state = [];
165
- if (_currentSessionId != null) {
166
- MessageStore.save(_currentSessionId!, state);
167
- }
168143 }
169144
170145 void updateContent(String messageId, String content) {
....@@ -185,22 +160,6 @@
185160 else
186161 m,
187162 ];
188
- if (_currentSessionId != null) {
189
- MessageStore.save(_currentSessionId!, state);
190
- }
191
- }
192
-
193
- /// Load more (older) messages for pagination.
194
- Future<void> loadMore() async {
195
- if (_currentSessionId == null) return;
196
- final older = await MessageStore.load(
197
- _currentSessionId!,
198
- offset: state.length,
199
- limit: 50,
200
- );
201
- if (older.isNotEmpty) {
202
- state = [...older, ...state];
203
- }
204163 }
205164 }
206165