forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemories_provider.dart
More file actions
1058 lines (944 loc) · 40 KB
/
Copy pathmemories_provider.dart
File metadata and controls
1058 lines (944 loc) · 40 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import 'dart:async';
import 'package:omi/utils/platform/platform_manager.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:tuple/tuple.dart';
import 'package:uuid/uuid.dart';
import 'package:omi/services/client_device_service.dart';
import 'package:omi/backend/http/api/memories.dart';
import 'package:omi/backend/preferences.dart';
import 'package:omi/backend/schema/memory.dart';
import 'package:omi/providers/connectivity_provider.dart';
import 'package:omi/utils/logger.dart';
import 'package:omi/widgets/extensions/string.dart';
typedef FetchMemoriesRequest = Future<GetMemoriesResult> Function({int limit, int offset, bool thisDeviceOnly});
typedef FetchLedgerHistoryRequest = Future<GetLedgerHistoryResult> Function({int limit, int offset});
typedef ReviewMemoryRequest = Future<bool> Function(String memoryId, bool value);
typedef EditMemoryRequest = Future<EditMemoryResult> Function(String memoryId, String value);
typedef RevertMemoryRequest = Future<RevertMemoryResult> Function(String memoryId, String operationId);
Future<GetLedgerHistoryResult> _noLedgerHistory({int limit = 500, int offset = 0}) async =>
const GetLedgerHistoryResult([], supported: false);
class MemoriesProvider extends ChangeNotifier {
List<Memory> _memories = [];
bool _loading = true;
String _searchQuery = '';
Set<MemoryCategory> _selectedCategories = {};
bool _showOnlyManual = false;
bool _filterThisDeviceOnly = false;
bool _deviceScopeSupported = true;
bool _ledgerHistorySupported = false;
bool _ledgerHistoryTruncated = false;
bool _loadFailed = false;
bool _hasLoaded = false;
Future<void>? _clientDeviceInitialization;
List<Tuple2<MemoryCategory, int>> categories = [];
MemoryCategory? selectedCategory;
// Connectivity handling for offline sync
ConnectivityProvider? _connectivityProvider;
bool _isSyncing = false;
int _sessionGeneration = 0;
int _loadSequence = 0;
int _ledgerProjectionRevision = 0;
final FetchMemoriesRequest _fetchMemoriesRequest;
final FetchLedgerHistoryRequest _fetchLedgerHistoryRequest;
final Future<bool> Function(String) _deleteMemoryRequest;
final ReviewMemoryRequest _reviewMemoryRequest;
final EditMemoryRequest _editMemoryRequest;
final RevertMemoryRequest _revertMemoryRequest;
final Set<String> _revertingMemoryIds = {};
final Map<String, String> _revertOperationIds = {};
/// Verdicts persisted this session for ids the loaded list does not resolve
/// (cold provider, truncated bulk list, or rows GET /v3/memories filters
/// out). A live row always wins; this only keeps recap rows honest while
/// nothing answers for the id.
final Map<String, bool> _settledUnresolvedReviews = {};
/// Hydration asks review cards have made, per id. Instance state so a card
/// State rebuilt by scrolling does not re-count, and `clearUserData()` can
/// reset the budget when the account changes.
final Map<String, int> _externalHydrateAttempts = {};
static const int _maxExternalHydrateAttempts = 2;
/// The load currently in flight, with the parameters it was started with.
/// Concurrent same-parameter callers join it instead of starting races the
/// sequence guard would discard.
Future<void>? _inFlightLoad;
int _inFlightLoadLimit = 100;
bool _inFlightLoadDeviceScoped = false;
MemoriesProvider({
FetchMemoriesRequest? fetchMemoriesRequest,
FetchLedgerHistoryRequest? fetchLedgerHistoryRequest,
Future<bool> Function(String)? deleteMemoryRequest,
ReviewMemoryRequest? reviewMemoryRequest,
EditMemoryRequest? editMemoryRequest,
RevertMemoryRequest? revertMemoryRequest,
}) : _fetchMemoriesRequest = fetchMemoriesRequest ?? getMemoriesResult,
_fetchLedgerHistoryRequest =
fetchLedgerHistoryRequest ?? (fetchMemoriesRequest == null ? getLedgerHistory : _noLedgerHistory),
_deleteMemoryRequest = deleteMemoryRequest ?? deleteMemoryServer,
_reviewMemoryRequest = reviewMemoryRequest ?? reviewMemoryServer,
_editMemoryRequest = editMemoryRequest ?? editMemoryServer,
_revertMemoryRequest = revertMemoryRequest ?? revertMemoryServer;
List<Memory> get memories => _memories;
bool get loading => _loading;
String get searchQuery => _searchQuery;
Set<MemoryCategory> get selectedCategories => _selectedCategories;
bool get showOnlyManual => _showOnlyManual;
bool get filterThisDeviceOnly => _filterThisDeviceOnly;
bool get hasPendingMemories => SharedPreferencesUtil().pendingMemories.isNotEmpty;
int get pendingMemoriesCount => SharedPreferencesUtil().pendingMemories.length;
bool get ledgerHistorySupported => _ledgerHistorySupported;
bool get ledgerHistoryTruncated => _ledgerHistoryTruncated;
bool get loadFailed => _loadFailed;
/// Whether a load attempt has already completed in this session (success or
/// failure). `_loading` starts `true` before anything was ever fetched, so
/// callers that need "a fetch is actually in flight" must check
/// `loading && hasLoaded`.
bool get hasLoaded => _hasLoaded;
bool get showLoadError => _loadFailed && _memories.isEmpty;
/// The verdict this session persisted for [memoryId] when no live row
/// resolves the id, or null when no verdict has been recorded. A live row's
/// `userReview` always wins over this.
bool? settledReviewFor(String memoryId) => _settledUnresolvedReviews[memoryId];
/// Consume one hydration ask for [memoryId]: true while the id is still
/// eligible (first ask free, then retries only while loads keep failing).
/// Instance-scoped so `clearUserData()` restores eligibility for a new
/// account session.
bool consumeHydrationAsk(String memoryId) {
final attempts = _externalHydrateAttempts[memoryId] ?? 0;
final allowed = attempts == 0 || (attempts < _maxExternalHydrateAttempts && _loadFailed);
if (allowed) {
_externalHydrateAttempts[memoryId] = attempts + 1;
}
return allowed;
}
bool isRevertingMemory(String memoryId) => _revertingMemoryIds.contains(memoryId);
bool canRevertSupersededFact(Memory memory) {
if (!_isEligibleSupersededFact(memory)) return false;
final alreadyRestored = _memories.any(
(candidate) =>
candidate.isCurrentKnowledgeLedgerRow &&
candidate.evidence.any(
(evidence) => evidence['source_type'] == 'explicit_user_revert' && evidence['source_id'] == memory.id,
),
);
if (alreadyRestored) return false;
final currentTail = _matchingCurrentTail(memory);
return currentTail == null || currentTail.content.trim() != memory.content.trim();
}
static bool _isEligibleSupersededFact(Memory memory) {
return memory.ledgerSchemaVersion == 'knowledge_ledger.v1' &&
memory.ledgerKind == KnowledgeLedgerKind.fact &&
memory.intentBacked &&
!memory.deleted &&
!memory.isLocked &&
memory.userReview != false &&
memory.invalidAt != null &&
(memory.supersededBy ?? '').trim().isNotEmpty;
}
List<Memory> get currentLedgerFacts => _memories
.where((memory) => memory.isCurrentKnowledgeLedgerRow && memory.ledgerKind == KnowledgeLedgerKind.fact)
.toList(growable: false)
..sort(_ledgerOrder);
List<Memory> get currentLedgerPlaybooks =>
_memories.where((memory) => memory.isCurrentKnowledgeLedgerRow && memory.isLedgerPlaybook).toList(growable: false)
..sort(_ledgerOrder);
List<Memory> get currentLedgerTriggers =>
_memories.where((memory) => memory.isCurrentKnowledgeLedgerRow && memory.isLedgerTrigger).toList(growable: false)
..sort(_ledgerOrder);
List<Memory> get historicalLedgerRows =>
_memories.where((memory) => memory.isHistoricalKnowledgeLedgerRow).toList(growable: false)
..sort((a, b) => b.updatedAt.compareTo(a.updatedAt));
static int _ledgerOrder(Memory a, Memory b) {
final weight = b.curationWeight.compareTo(a.curationWeight);
if (weight != 0) return weight;
final slot = (a.ledgerSlot ?? '').compareTo(b.ledgerSlot ?? '');
if (slot != 0) return slot;
// Match the canonical backend/macOS renderer exactly. Recency authority
// between concurrently open same-slot rows remains a ratification gate;
// clients must not silently invent a different winner meanwhile.
final validAt = (a.validAt ?? a.updatedAt).compareTo(b.validAt ?? b.updatedAt);
if (validAt != 0) return validAt;
return a.id.compareTo(b.id);
}
List<Memory> get filteredMemories {
return _memories.where((memory) {
// Apply search filter
final matchesSearch =
_searchQuery.isEmpty || memory.content.decodeString.toLowerCase().contains(_searchQuery.toLowerCase());
// Apply category filter or exclusion logic
bool categoryMatch;
if (_showOnlyManual) {
// Show only manual memories (exclude system and interesting)
categoryMatch = memory.category == MemoryCategory.manual;
} else if (_selectedCategories.isNotEmpty) {
// Show only selected categories
categoryMatch = _selectedCategories.contains(memory.category);
} else {
// Show all categories if no filter is applied
categoryMatch = true;
}
// When the server does not support device_scope, legacy memories have no
// primary_capture_device/capture_device_ids. Skip the local device filter
// in that case to avoid hiding all legacy rows on the "This device" view.
final deviceMatch = !_filterThisDeviceOnly ||
!_deviceScopeSupported ||
ClientDeviceService.instance.memoryMatchesThisDevice(
primaryCaptureDevice: memory.primaryCaptureDevice,
captureDeviceIds: memory.captureDeviceIds,
);
return matchesSearch && categoryMatch && deviceMatch;
}).toList()
..sort((a, b) => b.createdAt.compareTo(a.createdAt));
}
void setFilterThisDeviceOnly(bool enabled) {
_filterThisDeviceOnly = enabled;
notifyListeners();
loadMemories();
}
Future<void> _ensureClientDeviceInitialized() {
if (ClientDeviceService.instance.deviceIdHash.isNotEmpty) {
return Future.value();
}
_clientDeviceInitialization ??= ClientDeviceService.instance.initialize();
return _clientDeviceInitialization!;
}
void setShowOnlyManual(bool showOnly) {
_showOnlyManual = showOnly;
notifyListeners();
}
void setCategory(MemoryCategory? category) {
selectedCategory = category;
notifyListeners();
}
void setSearchQuery(String query) {
_searchQuery = query.toLowerCase();
notifyListeners();
}
void toggleCategoryFilter(MemoryCategory category) async {
if (_selectedCategories.contains(category)) {
_selectedCategories.remove(category);
} else {
_selectedCategories.add(category);
}
_showOnlyManual = false; // Reset manual-only filter when setting a category filter
notifyListeners();
final prefs = await SharedPreferences.getInstance();
await prefs.setStringList('memories_filter_categories', _selectedCategories.map((e) => e.name).toList());
}
void clearCategoryFilter() async {
_selectedCategories.clear();
_showOnlyManual = false;
notifyListeners();
final prefs = await SharedPreferences.getInstance();
await prefs.remove('memories_filter_categories');
// Clear old single filter key as well to be clean
await prefs.remove('memories_filter');
}
void clearUserData() {
_sessionGeneration++;
_memories = [];
_selectedCategories = {};
_showOnlyManual = false;
_searchQuery = '';
_filterThisDeviceOnly = false;
_ledgerHistorySupported = false;
_ledgerHistoryTruncated = false;
categories = [];
selectedCategory = null;
_loading = false;
_hasLoaded = false;
_loadFailed = false;
_isSyncing = false;
_revertingMemoryIds.clear();
_revertOperationIds.clear();
_settledUnresolvedReviews.clear();
_externalHydrateAttempts.clear();
_cancelDeletionTimer();
_lastDeletedMemory = null;
_pendingDeletionId = null;
notifyListeners();
}
// Deprecated/Modified: kept as alias if needed but unused internally now
void setCategoryFilter(MemoryCategory? category) {
// Do nothing or migrate logic if called from legacy code?
// Assuming we are updating all call sites.
}
void _setCategories() {
categories = MemoryCategory.values.map((category) {
final count = memories.where((memory) => memory.category == category).length;
return Tuple2(category, count);
}).toList();
notifyListeners();
}
Future<void> init() async {
final generation = _sessionGeneration;
await _ensureClientDeviceInitialized();
if (generation != _sessionGeneration) return;
await _loadFilter();
if (generation != _sessionGeneration) return;
await loadMemories();
if (generation != _sessionGeneration) return;
// Try to sync any pending memories on init
await syncPendingMemories();
}
/// Set the connectivity provider to listen for connection changes
void setConnectivityProvider(ConnectivityProvider provider) {
if (identical(_connectivityProvider, provider)) return;
_connectivityProvider?.removeListener(_onConnectivityChanged);
_connectivityProvider = provider;
_connectivityProvider?.addListener(_onConnectivityChanged);
}
void _onConnectivityChanged() {
if (_connectivityProvider?.isConnected == true) {
// Connection restored, try to sync pending memories
syncPendingMemories();
}
}
@override
void dispose() {
_connectivityProvider?.removeListener(_onConnectivityChanged);
super.dispose();
}
Future<void> _loadFilter() async {
final prefs = await SharedPreferences.getInstance();
final filterList = prefs.getStringList('memories_filter_categories');
if (filterList == null) {
_selectedCategories = {
MemoryCategory.system,
MemoryCategory.interesting,
MemoryCategory.manual,
MemoryCategory.workflow,
};
} else {
_selectedCategories = filterList
.map((e) => MemoryCategory.values.firstWhere((c) => c.name == e, orElse: () => MemoryCategory.system))
.toSet();
}
notifyListeners();
}
Future<void> loadMemories({int limit = 100}) async {
// Coalesce concurrent callers: several review cards can mount while the
// first fetch is still in flight (before `hasLoaded` flips), and racing
// loads would be discarded one after another by the sequence guard. A
// caller with different parameters gets its own load, which supersedes the
// in-flight one via the sequence guard as before.
final inFlight = _inFlightLoad;
if (inFlight != null && _inFlightLoadLimit == limit && _inFlightLoadDeviceScoped == _filterThisDeviceOnly) {
try {
await inFlight;
} catch (_) {}
return;
}
final loadFuture = _loadMemoriesInternal(limit: limit);
_inFlightLoad = loadFuture;
_inFlightLoadLimit = limit;
_inFlightLoadDeviceScoped = _filterThisDeviceOnly;
try {
await loadFuture;
} finally {
if (identical(_inFlightLoad, loadFuture)) {
_inFlightLoad = null;
}
}
}
Future<void> _loadMemoriesInternal({int limit = 100}) async {
final generation = _sessionGeneration;
final loadSequence = ++_loadSequence;
final ledgerProjectionRevision = _ledgerProjectionRevision;
// Snapshot the pending-deletion ID before any await: a refresh that
// started during the undo window must still suppress the deleted item
// even if _finalizeDeletion() clears the field while the fetch is in
// flight.
final tombstoneId = _pendingDeletionId;
_loading = true;
notifyListeners();
if (_filterThisDeviceOnly) {
await _ensureClientDeviceInitialized();
if (generation != _sessionGeneration || loadSequence != _loadSequence) {
return;
}
}
// Page until a short page: backend no longer expands the first page to 5000
// (prod GET /v3/memories 504s). Cap total fetch so a huge account cannot hang the UI.
const maxPages = 20;
final all = <Memory>[];
var offset = 0;
var deviceScopeSupported = true;
var ledgerHistorySupported = false;
var ledgerHistoryTruncated = false;
for (var page = 0; page < maxPages; page++) {
final result = await _fetchMemoriesRequest(limit: limit, offset: offset, thisDeviceOnly: _filterThisDeviceOnly);
if (generation != _sessionGeneration || loadSequence != _loadSequence) {
return;
}
if (!result.ok) {
_loadFailed = true;
_loading = false;
_hasLoaded = true;
notifyListeners();
return;
}
deviceScopeSupported = result.deviceScopeSupported;
all.addAll(result.memories);
// A truncated page is an honest partial response with no resumable cursor;
// stop loading instead of continuing with an unstable offset.
if (result.truncated || result.memories.length < limit) {
if (result.truncated) {
Logger.warning('MemoriesProvider: server returned a truncated list; stopping at $offset rows');
}
break;
}
offset += result.memories.length;
}
// History is an additive owner-scoped projection, fetched independently
// from the current list because GET /v3/memories intentionally filters
// rejected and closed rows. Device-scoped history has no ratified server
// contract, so the "This device" view remains current-only.
if (!_filterThisDeviceOnly) {
final seen = all.map((memory) => memory.id).toSet();
const historyPageSize = 500;
const maxHistoryPages = 10;
var historyOffset = 0;
var historyRowsLoaded = 0;
for (var page = 0; page < maxHistoryPages; page++) {
final result = await _fetchLedgerHistoryRequest(limit: historyPageSize, offset: historyOffset);
if (generation != _sessionGeneration || loadSequence != _loadSequence) return;
ledgerHistorySupported = result.supported;
if (!result.supported) break;
all.addAll(result.memories.where((memory) => seen.add(memory.id)));
historyRowsLoaded += result.memories.length;
if (result.truncated || result.memories.length < historyPageSize) {
ledgerHistoryTruncated = result.truncated;
break;
}
historyOffset += result.memories.length;
if (page == maxHistoryPages - 1) ledgerHistoryTruncated = true;
}
if (ledgerHistoryTruncated) {
Logger.warning('MemoriesProvider: ledger history is partial; loaded $historyRowsLoaded rows');
}
}
if (generation != _sessionGeneration ||
loadSequence != _loadSequence ||
ledgerProjectionRevision != _ledgerProjectionRevision) {
if (generation == _sessionGeneration && loadSequence == _loadSequence) {
_loading = false;
_hasLoaded = true;
notifyListeners();
}
return;
}
// Keep an optimistic delete hidden throughout its undo window. Use the
// snapshot taken before the fetch so a concurrent finalization that
// clears _pendingDeletionId mid-fetch cannot reinsert the row.
// Re-check _pendingDeletionId at apply time: if the user deleted a memory
// after loadMemories() started (tombstoneId was null at snapshot), the
// stale response still contains it and would reinsert the row.
final currentTombstoneId = _pendingDeletionId;
final effectiveTombstoneId = currentTombstoneId ?? tombstoneId;
_memories = effectiveTombstoneId != null ? all.where((memory) => memory.id != effectiveTombstoneId).toList() : all;
_deviceScopeSupported = deviceScopeSupported;
_ledgerHistorySupported = ledgerHistorySupported;
_ledgerHistoryTruncated = ledgerHistoryTruncated;
_loadFailed = false;
// Merge pending memories that haven't synced yet
final pendingMemories = SharedPreferencesUtil().pendingMemories;
for (var pending in pendingMemories) {
if (pending.id != effectiveTombstoneId && !_memories.any((m) => m.id == pending.id)) {
_memories.add(pending);
}
}
_revertOperationIds.removeWhere(
(memoryId, _) => !_memories.any((memory) => memory.id == memoryId && canRevertSupersededFact(memory)),
);
_loading = false;
_hasLoaded = true;
_setCategories();
}
/// Sync pending memories to server when online
Future<void> syncPendingMemories() async {
if (_isSyncing) return;
final generation = _sessionGeneration;
final ownerUid = SharedPreferencesUtil().uid;
if (ownerUid.isEmpty) return;
final pendingMemories = SharedPreferencesUtil().pendingMemories.where((memory) => memory.uid == ownerUid).toList();
if (pendingMemories.isEmpty) return;
_isSyncing = true;
Logger.debug('MemoriesProvider: Syncing ${pendingMemories.length} pending memories...');
for (var memory in List.from(pendingMemories)) {
if (generation != _sessionGeneration) return;
try {
final serverMemory = await createMemoryServer(memory.content, memory.visibility.name, memory.category.name);
if (serverMemory != null) {
SharedPreferencesUtil().removePendingMemory(memory.id, ownerUid: ownerUid);
if (generation != _sessionGeneration) return;
final idx = _memories.indexWhere((m) => m.id == memory.id);
if (idx != -1) {
_memories[idx].id = serverMemory.id;
}
}
if (generation != _sessionGeneration) return;
} catch (e) {
Logger.debug('MemoriesProvider: Failed to sync memory ${memory.id}: $e');
// Keep in pending list for next sync attempt
}
}
if (generation == _sessionGeneration) {
_isSyncing = false;
notifyListeners();
}
}
/// Apply an explicit user review through canonical backend authority.
///
/// The local change is optimistic so the control responds immediately, but
/// it is rolled back if the server rejects or cannot persist the decision.
///
/// A memory that is not in the loaded list (cold provider, truncated bulk
/// list, or a recap referencing an id this client never paged in) is still
/// reviewed by id: the request is id-addressed and the server stays the
/// authority, so refusing to send it would strand the recap controls.
Future<bool> reviewMemory(Memory memory, bool value) async {
// Locked rows are immutable everywhere, including cache misses: the flag
// travels on the memory itself, so a row absent from the loaded list is
// still refused before any request is sent.
if (memory.isLocked) return false;
final index = _memories.indexWhere((candidate) => candidate.id == memory.id);
if (index == -1) {
final generation = _sessionGeneration;
try {
final persisted = await _reviewMemoryRequest(memory.id, value);
if (generation != _sessionGeneration || !persisted) return false;
// No live row will ever answer for this id from the loaded list, so
// remember the persisted verdict for recap rows reading this id. A
// live row (a later refresh, another surface) still wins: this map is
// only consulted when the id does not resolve.
_settledUnresolvedReviews[memory.id] = value;
notifyListeners();
return true;
} catch (error) {
Logger.warning('MemoriesProvider: review persistence failed for ${memory.id}: $error');
return false;
}
}
final generation = _sessionGeneration;
final previousReview = memory.userReview;
final previousReviewed = memory.reviewed;
memory.userReview = value;
memory.reviewed = true;
notifyListeners();
bool persisted;
try {
persisted = await _reviewMemoryRequest(memory.id, value);
} catch (error) {
Logger.warning('MemoriesProvider: review persistence failed for ${memory.id}: $error');
persisted = false;
}
if (generation != _sessionGeneration) return false;
if (!persisted) {
memory.userReview = previousReview;
memory.reviewed = previousReviewed;
notifyListeners();
return false;
}
return true;
}
/// Append an authoritative current replacement for one superseded v1 fact.
///
/// This is deliberately non-optimistic: the historical row remains
/// untouched and no replacement becomes visible until the backend returns a
/// fully validated canonical row. A session change discards the late result.
Future<bool> revertSupersededFact(Memory memory) async {
final sourceIndex = _memories.indexWhere((candidate) => candidate.id == memory.id);
if (sourceIndex == -1 || !canRevertSupersededFact(memory) || isRevertingMemory(memory.id)) return false;
final generation = _sessionGeneration;
if (!_revertingMemoryIds.add(memory.id)) return false;
// Retain one idempotency key across all ambiguous failures. A transport
// error or lost response may follow a committed append; rotating the key
// would let a user retry append the same historical value again.
final operationId = _revertOperationIds.putIfAbsent(memory.id, () => const Uuid().v4());
notifyListeners();
try {
RevertMemoryResult result;
try {
result = await _revertMemoryRequest(memory.id, operationId);
} catch (error) {
Logger.warning('MemoriesProvider: fact revert failed for ${memory.id}: $error');
return false;
}
if (generation != _sessionGeneration || !result.persisted) return false;
final currentSourceIndex = _memories.indexWhere((candidate) => candidate.id == memory.id);
if (currentSourceIndex == -1 ||
!_isEligibleSupersededFact(_memories[currentSourceIndex]) ||
!_sameRevertSource(memory, _memories[currentSourceIndex])) {
return false;
}
final currentSource = _memories[currentSourceIndex];
final replacement = result.authoritativeMemory;
final currentTail = _matchingCurrentTail(currentSource);
if (replacement == null ||
!_isAuthoritativeRevertReplacement(currentSource, replacement, expectedVisibility: currentTail?.visibility)) {
return false;
}
final existingReplacementIndex = _memories.indexWhere((candidate) => candidate.id == replacement.id);
if (existingReplacementIndex != -1 &&
!_sameAuthoritativeReplacement(_memories[existingReplacementIndex], replacement)) {
return false;
}
final staleCurrentTail = currentTail?.id == replacement.id ? null : currentTail;
_ledgerProjectionRevision++;
// The backend atomically closes the current tail when it appends the
// restored row. Remove that known-stale current projection before
// exposing the replacement; do not forge lifecycle fields locally.
if (staleCurrentTail != null) {
_memories.removeWhere((candidate) => candidate.id == staleCurrentTail.id);
}
if (existingReplacementIndex == -1) {
_memories.add(replacement);
}
_setCategories();
await _refreshLedgerHistoryAfterRevert(
generation,
closedTailId: staleCurrentTail?.id,
replacementId: replacement.id,
);
_revertOperationIds.remove(memory.id);
return true;
} finally {
final removed = _revertingMemoryIds.remove(memory.id);
if (removed && generation == _sessionGeneration) notifyListeners();
}
}
Future<void> _refreshLedgerHistoryAfterRevert(
int generation, {
required String? closedTailId,
required String replacementId,
}) async {
if (_filterThisDeviceOnly || closedTailId == null || generation != _sessionGeneration) return;
try {
const historyPageSize = 500;
const maxHistoryPages = 10;
var historyOffset = 0;
final refreshedHistory = <String, Memory>{};
for (var page = 0; page < maxHistoryPages; page++) {
final result = await _fetchLedgerHistoryRequest(limit: historyPageSize, offset: historyOffset);
if (generation != _sessionGeneration || !result.supported) return;
for (final row in result.memories) {
if (row.id != replacementId && row.isHistoricalKnowledgeLedgerRow) {
refreshedHistory[row.id] = row;
}
}
if (result.truncated || result.memories.length < historyPageSize) break;
historyOffset += result.memories.length;
}
if (generation != _sessionGeneration) return;
for (final row in refreshedHistory.values) {
final index = _memories.indexWhere((candidate) => candidate.id == row.id);
if (index == -1) {
_memories.add(row);
} else {
_memories[index] = row;
}
}
_setCategories();
} catch (error) {
Logger.warning('MemoriesProvider: ledger history refresh failed after fact revert: $error');
}
}
static bool _sameRevertSource(Memory requested, Memory current) {
return requested.id == current.id &&
requested.uid == current.uid &&
requested.content == current.content &&
requested.ledgerSchemaVersion == current.ledgerSchemaVersion &&
requested.ledgerKind == current.ledgerKind &&
requested.ledgerSlot == current.ledgerSlot &&
requested.subjectScope == current.subjectScope &&
requested.subjectEntityId == current.subjectEntityId &&
requested.supersededBy == current.supersededBy &&
requested.invalidAt == current.invalidAt &&
requested.curationWeight == current.curationWeight &&
requested.userReview == current.userReview;
}
Memory? _matchingCurrentTail(Memory source) {
final seen = <String>{source.id};
var successorId = (source.supersededBy ?? '').trim();
while (successorId.isNotEmpty && seen.add(successorId)) {
final matches = _memories.where((candidate) => candidate.id == successorId).toList(growable: false);
if (matches.length != 1) break;
final successor = matches.single;
if (successor.isCurrentKnowledgeLedgerRow && successor.ledgerKind == KnowledgeLedgerKind.fact) {
return successor;
}
successorId = (successor.supersededBy ?? '').trim();
}
// A bounded history page may omit an intermediate link. Never guess the
// tail from slot/subject identity: active-row uniqueness is not a client
// invariant, and removing a guessed row could hide unrelated knowledge.
return null;
}
static bool _isAuthoritativeRevertReplacement(
Memory source,
Memory replacement, {
MemoryVisibility? expectedVisibility,
}) {
return replacement.id.trim().isNotEmpty &&
replacement.id != source.id &&
replacement.uid == source.uid &&
replacement.ledgerSchemaVersion == 'knowledge_ledger.v1' &&
replacement.ledgerKind == KnowledgeLedgerKind.fact &&
replacement.intentBacked &&
replacement.writeReason == 'direct_user_statement' &&
!replacement.deleted &&
!replacement.isLocked &&
replacement.userReview != false &&
replacement.validAt != null &&
replacement.invalidAt == null &&
(replacement.supersededBy ?? '').trim().isEmpty &&
replacement.content.trim() == source.content.trim() &&
replacement.ledgerSlot == source.ledgerSlot &&
replacement.subjectScope == source.subjectScope &&
replacement.subjectEntityId == source.subjectEntityId &&
replacement.curationWeight == source.curationWeight &&
replacement.evidence.any(
(evidence) => evidence['source_type'] == 'explicit_user_revert' && evidence['source_id'] == source.id,
) &&
(expectedVisibility == null || replacement.visibility == expectedVisibility);
}
static bool _sameAuthoritativeReplacement(Memory current, Memory returned) {
return current.id == returned.id &&
current.uid == returned.uid &&
current.content == returned.content &&
current.ledgerSchemaVersion == returned.ledgerSchemaVersion &&
current.ledgerKind == returned.ledgerKind &&
current.ledgerSlot == returned.ledgerSlot &&
current.subjectScope == returned.subjectScope &&
current.subjectEntityId == returned.subjectEntityId &&
current.curationWeight == returned.curationWeight &&
current.visibility == returned.visibility &&
current.validAt == returned.validAt &&
current.supersededBy == returned.supersededBy &&
current.invalidAt == returned.invalidAt &&
current.intentBacked == returned.intentBacked &&
current.writeReason == returned.writeReason &&
current.userReview == returned.userReview;
}
Memory? _lastDeletedMemory;
Timer? _deletionTimer;
String? _pendingDeletionId;
Memory? get lastDeletedMemory => _lastDeletedMemory;
void deleteMemory(Memory memory) {
_cancelDeletionTimer();
_lastDeletedMemory = memory;
_pendingDeletionId = memory.id;
_memories.remove(memory);
_setCategories();
notifyListeners();
_startDeletionTimer();
}
void _cancelDeletionTimer() {
if (_deletionTimer != null && _deletionTimer!.isActive) {
_deletionTimer!.cancel();
_deletionTimer = null;
}
}
void _startDeletionTimer() {
_deletionTimer = Timer(const Duration(seconds: 4), () async {
await _finalizeDeletion();
});
}
Future<void> _finalizeDeletion() async {
if (_pendingDeletionId == null) {
_lastDeletedMemory = null;
return;
}
final id = _pendingDeletionId!;
final deletedMemory = _lastDeletedMemory;
var deleteSucceeded = true;
// If memory was created offline and not yet synced
if (SharedPreferencesUtil().pendingMemories.any((m) => m.id == id)) {
SharedPreferencesUtil().removePendingMemory(id);
} else {
// Memory exists on server
try {
deleteSucceeded = await _deleteMemoryRequest(id);
} catch (e) {
Logger.debug('MemoriesProvider: Failed to delete memory $id: $e');
deleteSucceeded = false;
}
}
if (!deleteSucceeded && _pendingDeletionId == id && deletedMemory?.id == id) {
if (!_memories.any((memory) => memory.id == id)) {
_memories.add(deletedMemory!);
}
_setCategories();
notifyListeners();
}
if (_pendingDeletionId == id) {
_pendingDeletionId = null;
_lastDeletedMemory = null;
}
}
Future<void> confirmPendingDeletion() async {
_cancelDeletionTimer();
await _finalizeDeletion();
}
// Restore the last deleted memory
Future<bool> restoreLastDeletedMemory() async {
if (_lastDeletedMemory == null) return false;
_cancelDeletionTimer();
_pendingDeletionId = null;
_memories.add(_lastDeletedMemory!);
_lastDeletedMemory = null;
_setCategories();
notifyListeners();
return true;
}
void deleteAllMemories() async {
final int countBeforeDeletion = _memories.length;
await deleteAllMemoriesServer();
_memories.clear();
if (countBeforeDeletion > 0) {
PlatformManager.instance.analytics.memoriesAllDeleted(countBeforeDeletion);
}
_setCategories();
}
/// Create a memory - works offline by saving locally first, then syncing
Future<bool> createMemory(
String content, [
MemoryVisibility visibility = MemoryVisibility.public,
MemoryCategory category = MemoryCategory.manual,
]) async {
final generation = _sessionGeneration;
final ownerUid = SharedPreferencesUtil().uid;
if (ownerUid.isEmpty) return false;
// Create the memory object first
final newMemory = Memory(
id: const Uuid().v4(),
uid: ownerUid,
content: content,
category: category,
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
conversationId: null,
reviewed: false,
manuallyAdded: true,
visibility: visibility,
);
// Add to local list immediately (optimistic update)
_memories.add(newMemory);
_setCategories();
notifyListeners();
// Save to pending memories for persistence across app restarts
SharedPreferencesUtil().addPendingMemory(newMemory);
// Try to sync to server immediately
final serverMemory = await createMemoryServer(content, visibility.name, category.name);
if (serverMemory != null) {
// Remove from the original account's pending queue even if the visible
// session changed while the request was in flight.
SharedPreferencesUtil().removePendingMemory(newMemory.id, ownerUid: ownerUid);
if (generation != _sessionGeneration) return true;
final idx = _memories.indexWhere((m) => m.id == newMemory.id);
if (idx != -1) {
_memories[idx].id = serverMemory.id;
}
}
if (generation != _sessionGeneration) return true;
// Return true since memory is saved locally regardless of server sync
return true;
}
Future<void> updateMemoryVisibility(Memory memory, MemoryVisibility visibility) async {
await updateMemoryVisibilityServer(memory.id, visibility.name);
final idx = _memories.indexWhere((m) => m.id == memory.id);
if (idx != -1) {
Memory memoryToUpdate = _memories[idx];
memoryToUpdate.visibility = visibility;
_memories[idx] = memoryToUpdate;
PlatformManager.instance.analytics.memoryVisibilityChanged(memoryToUpdate, visibility);
_setCategories();
}
}
Future<bool> toggleMemoryBaseline(Memory memory, bool isBaseline) async {
final success = await updateMemoryBaselineServer(memory.id, isBaseline);
if (success) {
final idx = _memories.indexWhere((m) => m.id == memory.id);
if (idx != -1) {
_memories[idx].isBaseline = isBaseline;
notifyListeners();
_setCategories();
}
}
return success;
}
Future<bool> editMemory(Memory memory, String value, [MemoryCategory? category]) async {
if (memory.isKnowledgeLedger &&
(memory.deleted ||
memory.invalidAt != null ||
(memory.supersededBy ?? '').trim().isNotEmpty ||
memory.ledgerKind != KnowledgeLedgerKind.fact ||
memory.isLocked)) {
return false;
}
final result = await _editMemoryRequest(memory.id, value);
if (result.persisted) {
final idx = _memories.indexWhere((m) => m.id == memory.id);
if (idx != -1) {
if (memory.isKnowledgeLedger) {
final replacement = result.authoritativeMemory;
if (replacement == null ||
!replacement.isKnowledgeLedger ||
replacement.uid != memory.uid ||
replacement.id == memory.id ||
replacement.content.trim() != value.trim() ||
replacement.deleted ||
replacement.invalidAt != null ||