Files
meshcore-open/lib/services/path_history_service.dart
T
zjs81 5ea44d6ce9 Tester feedback: routing, bulk select, notifications, map crash
Routing
- Auto routing picks routes by Thompson sampling over faded delivery
  stats (24 h half-life, capped evidence), weighted by what the radio
  currently hears; retries prefer a different route; last attempt floods.
- Route failures are held until a message's fate is known, so an offline
  contact no longer wrecks its route history; failing routes are only
  deleted after a week without a delivery.
- Direct (zero-hop) path is a rotation candidate (#643) and a new Direct
  mode in the routing sheet; history routes are used for repeater logins.
- Remove the route-weight sliders the new chooser no longer uses.

Chats
- Bulk selection in channel chats (spam / not spam / delete / copy) and
  in the Filtered messages list.
- Tapping a search result jumps to the message.
- Swipe-to-reply ignores vertical scrolls.
- Active conversation follows route lifecycle, so covered chats count
  unread again.

Notifications
- Tapping a notification opens its conversation.
- Per-conversation coalescing and one alert per queued-message sync.
- Monochrome status-bar icon; background service stops when the app is
  swiped away; stale notifications cleared on radio or slot change.

Other
- Online map: opaque links and painted dots for dense areas to avoid an
  Impeller GPU crash; expandable hop list on path maps.
- Contact share: clipboard import of shared contacts, "Add again".
- Android: enableEdgeToEdge and scrolling-screenshot support.
- Translations for all new strings; fix mistranslated routing labels.
2026-11-08 23:19:15 -07:00

635 lines
19 KiB
Dart

import 'dart:async';
import 'dart:math' as math;
import 'package:flutter/foundation.dart';
import '../models/contact.dart';
import '../models/path_history.dart';
import '../models/path_selection.dart';
import '../utils/coalesced_writes.dart';
import 'route_chooser.dart';
import 'storage_service.dart';
class PathHistoryService extends ChangeNotifier {
final StorageService _storage;
final Map<String, ContactPathHistory> _cache = {};
final Map<String, _FloodStats> _floodStats = {};
final Map<String, Future<void>> _pendingLoads = {};
// A path record lands for most received messages, so saves are batched.
final CoalescedWrites _writes = CoalescedWrites(
delay: const Duration(seconds: 5),
);
// LRU cache eviction tracking
static const int _maxCachedContacts = 50;
final List<String> _cacheAccessOrder = [];
static const int _maxHistoryEntries = 100;
/// A failing route is only deleted once it hasn't delivered for this long.
static const Duration _forgetRouteAfter = Duration(days: 7);
int _version = 0;
int get version => _version;
final math.Random _random;
final DateTime Function() _now;
/// [random] and [clock] are for tests: route picks are random by design.
PathHistoryService(
this._storage, {
math.Random? random,
DateTime Function()? clock,
}) : _random = random ?? math.Random(),
_now = clock ?? DateTime.now;
void setDevicePublicKey(String publicKeyHex) {
// Queued saves use the storage key on entry, so they still land under
// the previous radio's key.
unawaited(_writes.flush());
_storage.setPublicKeyHex = publicKeyHex;
_cache.clear();
_cacheAccessOrder.clear();
_floodStats.clear();
_version++;
notifyListeners();
}
Future<void> initialize() async {
// Load cached path histories on startup if needed
}
void handlePathUpdated(Contact contact, {double initialWeight = 1.0}) {
if (contact.pathLength < 0 && contact.path.isEmpty) return;
final hopCount = contact.pathLength < 0
? contact.path.length
: contact.pathLength;
_addPathRecord(
contactPubKeyHex: contact.publicKeyHex,
hopCount: hopCount,
tripTimeMs: 0,
wasFloodDiscovery: true,
pathBytes: contact.path,
successCount: 0,
failureCount: 0,
initialWeight: initialWeight,
timestamp: null,
);
}
void recordPathAttempt(String contactPubKeyHex, PathSelection selection) {
if (selection.useFlood) {
_updateFloodStats(contactPubKeyHex);
return;
}
_addPathRecord(
contactPubKeyHex: contactPubKeyHex,
hopCount: selection.hopCount,
tripTimeMs: 0,
wasFloodDiscovery: false,
pathBytes: selection.pathBytes,
successCount: 0,
failureCount: 0,
timestamp: null,
);
}
/// When a flood message is delivered, credit the contact's current device
/// path so that the route the ACK traveled back through gets a weight boost.
void recordFloodPathAttribution({
required String contactPubKeyHex,
required List<int> pathBytes,
required int hopCount,
int? tripTimeMs,
double successIncrement = 0.5,
double maxWeight = 5.0,
}) {
// A flood answered directly credits the direct (zero-hop) path.
if (hopCount < 0 || (pathBytes.isEmpty && hopCount != 0)) return;
if (!_cache.containsKey(contactPubKeyHex)) {
_ensureLoaded(contactPubKeyHex).then((_) {
if (!_cache.containsKey(contactPubKeyHex)) return;
recordFloodPathAttribution(
contactPubKeyHex: contactPubKeyHex,
pathBytes: pathBytes,
hopCount: hopCount,
tripTimeMs: tripTimeMs,
successIncrement: successIncrement,
maxWeight: maxWeight,
);
});
return;
}
final existing = _findPathRecord(contactPubKeyHex, pathBytes);
final successCount = (existing?.successCount ?? 0) + 1;
final failureCount = existing?.failureCount ?? 0;
final now = _now();
final faded = addRouteResult(existing, success: true, now: now);
final currentWeight = existing?.routeWeight ?? 1.0;
final newWeight = (currentWeight + successIncrement).clamp(0.0, maxWeight);
debugPrint(
'Flood path attribution: crediting path [${pathBytes.map((b) => b.toRadixString(16).padLeft(2, '0')).join(',')}] '
'for $contactPubKeyHex (weight $currentWeight → $newWeight)',
);
_addPathRecord(
contactPubKeyHex: contactPubKeyHex,
hopCount: hopCount,
tripTimeMs: tripTimeMs ?? existing?.tripTimeMs ?? 0,
wasFloodDiscovery: true,
pathBytes: pathBytes,
successCount: successCount,
failureCount: failureCount,
routeWeight: newWeight,
timestamp: now,
recentSuccess: faded.success,
recentFailure: faded.failure,
statsAt: now,
);
}
void recordPathResult(
String contactPubKeyHex,
PathSelection selection, {
required bool success,
int? tripTimeMs,
double successIncrement = 0.5,
double failureDecrement = 0.5,
double maxWeight = 5.0,
}) {
if (selection.useFlood) {
final stats = _floodStats.putIfAbsent(
contactPubKeyHex,
() => _FloodStats(),
);
if (success) {
stats.successCount += 1;
if (tripTimeMs != null) stats.lastTripTimeMs = tripTimeMs;
} else {
stats.failureCount += 1;
}
stats.lastUsed = DateTime.now();
return;
}
if (!_cache.containsKey(contactPubKeyHex)) {
_ensureLoaded(contactPubKeyHex).then((_) {
if (!_cache.containsKey(contactPubKeyHex)) return;
recordPathResult(
contactPubKeyHex,
selection,
success: success,
tripTimeMs: tripTimeMs,
successIncrement: successIncrement,
failureDecrement: failureDecrement,
maxWeight: maxWeight,
);
});
return;
}
final existing = _findPathRecord(contactPubKeyHex, selection.pathBytes);
final successCount = (existing?.successCount ?? 0) + (success ? 1 : 0);
final failureCount = (existing?.failureCount ?? 0) + (success ? 0 : 1);
final now = _now();
final faded = addRouteResult(existing, success: success, now: now);
final currentWeight = existing?.routeWeight ?? 1.0;
double newWeight;
if (success) {
newWeight = (currentWeight + successIncrement).clamp(0.0, maxWeight);
} else {
newWeight = currentWeight - failureDecrement;
// Faded stats already push a failing route out of use, and a repeater
// that is down for a day comes back; only forget a route that keeps
// failing and hasn't delivered in a week. Route weight is bookkeeping
// only now and plays no part in this.
final lastWorked = existing?.timestamp;
final longDead =
lastWorked == null || now.difference(lastWorked) > _forgetRouteAfter;
if (failureCount >= 3 && longDead) {
removePathRecord(contactPubKeyHex, selection.pathBytes);
return;
}
// Keep the record with a small floor weight until we have enough evidence
newWeight = newWeight.clamp(0.1, maxWeight);
}
_addPathRecord(
contactPubKeyHex: contactPubKeyHex,
hopCount: selection.hopCount,
tripTimeMs: success ? (tripTimeMs ?? 0) : (existing?.tripTimeMs ?? 0),
wasFloodDiscovery: existing?.wasFloodDiscovery ?? false,
pathBytes: selection.pathBytes,
successCount: successCount,
failureCount: failureCount,
routeWeight: newWeight,
timestamp: success ? now : existing?.timestamp,
recentSuccess: faded.success,
recentFailure: faded.failure,
statsAt: now,
);
}
/// Route for one send attempt of a message to [contactPubKeyHex].
///
/// The last attempt always floods. Otherwise a known route is drawn by
/// Thompson sampling over faded delivery stats, weighted by how well it
/// fits [context]; routes used for this message's recent attempts score
/// lower so retries usually try something different.
PathSelection selectPathForAttempt(
String contactPubKeyHex, {
required int attemptIndex,
required int maxRetries,
List<PathSelection> recentSelections = const [],
RouteContext context = RouteContext.unknown,
}) {
if (maxRetries <= 0 || attemptIndex >= maxRetries - 1) {
return const PathSelection(pathBytes: [], hopCount: -1, useFlood: true);
}
final ranked = _getRankedPaths(contactPubKeyHex, context: context);
if (ranked.isEmpty) {
return const PathSelection(pathBytes: [], hopCount: -1, useFlood: true);
}
_trackAccess(contactPubKeyHex);
final recentPaths = recentSelections
.where((selection) => !selection.useFlood)
.map((selection) => selection.pathBytes)
.toList();
final selected = pickRoute(
ranked,
random: _random,
now: _now(),
context: context,
triedThisMessage: (path) => recentPaths.any(
(recentPath) => _pathsEqual(path.pathBytes, recentPath),
),
);
return PathSelection(
pathBytes: selected.pathBytes,
hopCount: selected.hopCount,
useFlood: false,
);
}
void _addPathRecord({
required String contactPubKeyHex,
required int hopCount,
required int tripTimeMs,
required bool wasFloodDiscovery,
required List<int> pathBytes,
required int successCount,
required int failureCount,
double? routeWeight,
double initialWeight = 1.0,
DateTime? timestamp,
double? recentSuccess,
double? recentFailure,
DateTime? statsAt,
}) {
void add() => _addPathRecordInternal(
contactPubKeyHex,
hopCount,
tripTimeMs,
wasFloodDiscovery,
pathBytes,
successCount,
failureCount,
routeWeight,
initialWeight,
timestamp,
recentSuccess: recentSuccess,
recentFailure: recentFailure,
statsAt: statsAt,
);
if (_cache.containsKey(contactPubKeyHex)) {
add();
} else {
_ensureLoaded(contactPubKeyHex).then((_) => add());
}
}
Future<void> _ensureLoaded(String contactPubKeyHex) {
if (_cache.containsKey(contactPubKeyHex)) return Future.value();
return _pendingLoads[contactPubKeyHex] ??=
_loadHistoryFromStorage(contactPubKeyHex)
.then((loaded) {
_cache[contactPubKeyHex] ??=
loaded ??
ContactPathHistory(
contactPubKeyHex: contactPubKeyHex,
recentPaths: [],
);
_trackAccess(contactPubKeyHex);
_evictIfNeeded();
})
.whenComplete(() {
_pendingLoads.remove(contactPubKeyHex);
});
}
void _addPathRecordInternal(
String contactPubKeyHex,
int hopCount,
int tripTimeMs,
bool wasFloodDiscovery,
List<int> pathBytes,
int successCount,
int failureCount,
double? routeWeight,
double initialWeight,
DateTime? timestamp, {
double? recentSuccess,
double? recentFailure,
DateTime? statsAt,
}) {
var history = _cache[contactPubKeyHex];
if (history == null) return;
_version++;
final existing = _findPathRecord(contactPubKeyHex, pathBytes);
if (existing != null) {
successCount = successCount == 0 ? existing.successCount : successCount;
failureCount = failureCount == 0 ? existing.failureCount : failureCount;
if (tripTimeMs == 0) {
tripTimeMs = existing.tripTimeMs;
}
wasFloodDiscovery = existing.wasFloodDiscovery || wasFloodDiscovery;
timestamp ??= existing.timestamp;
if (recentSuccess == null) {
// Not a result: keep the faded stats as they were.
recentSuccess = existing.recentSuccess;
recentFailure = existing.recentFailure;
statsAt = existing.statsAt;
}
}
routeWeight ??= existing?.routeWeight ?? initialWeight;
final newRecord = PathRecord(
hopCount: hopCount,
tripTimeMs: tripTimeMs,
timestamp: timestamp,
wasFloodDiscovery: wasFloodDiscovery,
pathBytes: pathBytes,
successCount: successCount,
failureCount: failureCount,
routeWeight: routeWeight,
// A brand-new record starts with no faded results.
recentSuccess: recentSuccess ?? 0,
recentFailure: recentFailure ?? 0,
statsAt: statsAt,
);
final updatedPaths = List<PathRecord>.from(history.recentPaths);
updatedPaths.removeWhere((p) => _pathsEqual(p.pathBytes, pathBytes));
// Make room by dropping the weakest path so new discoveries still land.
if (existing == null && updatedPaths.length >= _maxHistoryEntries) {
updatedPaths.remove(_lowestScoredPath(updatedPaths));
}
updatedPaths.insert(0, newRecord);
final updatedHistory = ContactPathHistory(
contactPubKeyHex: contactPubKeyHex,
recentPaths: updatedPaths,
);
_cache[contactPubKeyHex] = updatedHistory;
_trackAccess(contactPubKeyHex);
_evictIfNeeded();
// The snapshot is written, not the cache entry: the LRU may evict it
// before the batch runs.
_writes.schedule(
contactPubKeyHex,
() => _storage.savePathHistory(contactPubKeyHex, updatedHistory),
);
notifyListeners();
}
/// Whether this contact's history is in the in-memory cache.
bool isCached(String contactPubKeyHex) =>
_cache.containsKey(contactPubKeyHex);
/// Paths already in memory, without loading from storage or changing the
/// cache order. For bulk readers that must not churn the 50-contact cache.
List<PathRecord> peekRecentPaths(String contactPubKeyHex) =>
_cache[contactPubKeyHex]?.recentPaths ?? const [];
/// Reads saved paths straight from storage without adding them to the
/// cache, evicting anything, or notifying listeners.
Future<List<PathRecord>> readStoredPaths(String contactPubKeyHex) async {
final cached = _cache[contactPubKeyHex];
if (cached != null) return cached.recentPaths;
final stored = await _loadHistoryFromStorage(contactPubKeyHex);
return stored?.recentPaths ?? const [];
}
List<PathRecord> getRecentPaths(String contactPubKeyHex) {
final history = _cache[contactPubKeyHex];
if (history != null) {
_trackAccess(contactPubKeyHex);
return history.recentPaths;
}
_ensureLoaded(contactPubKeyHex).then((_) {
if (_cache[contactPubKeyHex]?.recentPaths.isNotEmpty ?? false) {
_version++;
notifyListeners();
}
});
return [];
}
Future<ContactPathHistory?> _loadHistoryFromStorage(
String contactPubKeyHex,
) async {
// An evicted contact may still have a queued save; it reaches the prefs
// cache synchronously, so the load below reads it.
unawaited(_writes.flush());
return await _storage.loadPathHistory(contactPubKeyHex);
}
PathRecord? getFastestPath(String contactPubKeyHex) {
final history = _cache[contactPubKeyHex];
if (history != null) {
_trackAccess(contactPubKeyHex);
}
return history?.fastest;
}
PathRecord? getMostRecentPath(String contactPubKeyHex) {
final history = _cache[contactPubKeyHex];
if (history != null) {
_trackAccess(contactPubKeyHex);
}
return history?.mostRecent;
}
({
int successCount,
int failureCount,
int lastTripTimeMs,
DateTime? lastUsed,
})?
getFloodStats(String contactPubKeyHex) {
final stats = _floodStats[contactPubKeyHex];
if (stats == null) return null;
return (
successCount: stats.successCount,
failureCount: stats.failureCount,
lastTripTimeMs: stats.lastTripTimeMs,
lastUsed: stats.lastUsed,
);
}
Future<void> clearPathHistory(String contactPubKeyHex) async {
_cache.remove(contactPubKeyHex);
_cacheAccessOrder.remove(contactPubKeyHex);
_floodStats.remove(contactPubKeyHex);
_writes.cancel(contactPubKeyHex);
await _storage.clearPathHistory(contactPubKeyHex);
_version++;
notifyListeners();
}
Future<void> removePathRecord(
String contactPubKeyHex,
List<int> pathBytes,
) async {
final history = _cache[contactPubKeyHex];
if (history == null) return;
final updatedPaths = List<PathRecord>.from(history.recentPaths)
..removeWhere((p) => _pathsEqual(p.pathBytes, pathBytes));
_cache[contactPubKeyHex] = ContactPathHistory(
contactPubKeyHex: contactPubKeyHex,
recentPaths: updatedPaths,
);
_writes.cancel(contactPubKeyHex);
await _storage.savePathHistory(contactPubKeyHex, _cache[contactPubKeyHex]!);
_version++;
notifyListeners();
}
PathRecord? _findPathRecord(String contactPubKeyHex, List<int> pathBytes) {
final history = _cache[contactPubKeyHex];
if (history == null) return null;
for (final record in history.recentPaths) {
if (_pathsEqual(record.pathBytes, pathBytes)) {
return record;
}
}
return null;
}
List<PathRecord> _getRankedPaths(
String contactPubKeyHex, {
RouteContext context = RouteContext.unknown,
}) {
final history = _cache[contactPubKeyHex];
if (history == null) return [];
// Records without hop bytes are unusable, except the direct path: zero
// hops has no bytes by definition (issue #643: rotation never tried it).
final ranked = List<PathRecord>.from(history.recentPaths)
..removeWhere((p) => p.pathBytes.isEmpty && p.hopCount != 0);
final now = _now();
final fastest = fastestTripMs(ranked);
final scores = {
for (final p in ranked)
p: routeExpectedScore(
p,
now: now,
fastestTripMs: fastest,
context: context,
),
};
ranked.sort((a, b) {
final scoreCompare = scores[b]!.compareTo(scores[a]!);
if (scoreCompare != 0) return scoreCompare;
final aTrip = a.tripTimeMs == 0 ? 999999 : a.tripTimeMs;
final bTrip = b.tripTimeMs == 0 ? 999999 : b.tripTimeMs;
if (aTrip != bTrip) return aTrip.compareTo(bTrip);
final aTime = a.timestamp ?? DateTime.fromMillisecondsSinceEpoch(0);
final bTime = b.timestamp ?? DateTime.fromMillisecondsSinceEpoch(0);
return bTime.compareTo(aTime);
});
return ranked;
}
PathRecord _lowestScoredPath(List<PathRecord> paths) {
final now = _now();
final fastest = fastestTripMs(paths);
double score(PathRecord p) =>
routeExpectedScore(p, now: now, fastestTripMs: fastest);
return paths.reduce((a, b) => score(b) < score(a) ? b : a);
}
bool _pathsEqual(List<int> a, List<int> b) {
return listEquals(a, b);
}
void _updateFloodStats(String contactPubKeyHex) {
final stats = _floodStats.putIfAbsent(
contactPubKeyHex,
() => _FloodStats(),
);
stats.lastUsed = DateTime.now();
}
void _trackAccess(String contactPubKeyHex) {
_cacheAccessOrder.remove(contactPubKeyHex);
_cacheAccessOrder.add(contactPubKeyHex);
}
void _evictIfNeeded() {
while (_cache.length > _maxCachedContacts && _cacheAccessOrder.isNotEmpty) {
final oldest = _cacheAccessOrder.removeAt(0);
_cache.remove(oldest);
_floodStats.remove(oldest);
}
}
/// Runs queued saves now, e.g. before the app is backgrounded.
Future<void> flushPendingWrites() => _writes.flush();
@override
void dispose() {
unawaited(_writes.flush());
super.dispose();
}
void clearAllHistories() {
_cache.clear();
_cacheAccessOrder.clear();
_floodStats.clear();
_writes.dispose();
_storage.clearAllPathHistories();
_version = 0;
notifyListeners();
}
}
class _FloodStats {
int successCount = 0;
int failureCount = 0;
int lastTripTimeMs = 0;
DateTime? lastUsed;
}