Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

19.6 KB
Raw Blame
import 'dart:async';
import 'dart:convert';
import 'dart:math';

import 'package:flutter/foundation.dart';

import '../core/local_key_value_store.dart';
import 'model.dart';
import 'swarm_chat_notify.dart';
import 'swarm_reader.dart';

/// Sends the reader's read marks to the server (backend `superTabs/readMarks.ts`, #2131), so a chat, or a thread
/// ([K] names which), read on this phone reads as read on every other device. The desktop's
/// `SuperTabReadMarkSender`: one request per chat or thread at a time; a higher mark asked for while one is on its way
/// goes after it, and a lower or equal one is never sent. A failed send is forgotten, so the next mark asked for tries
/// again (after [retryAfter] when it is no higher); meanwhile this phone's own mark keeps it read here.
class SwarmReadMarkSender<K> {
  SwarmReadMarkSender(
    this._send, {
    this.retryAfter = Duration.zero,
    DateTime Function()? now,
  }) : _now = now ?? DateTime.now;

  /// Writes [sequence] as the mark of [conversationId]; completes once the server kept it, or throws.
  final Future<void> Function(K conversationId, int sequence) _send;

  /// How long a mark whose send failed waits before it is sent again: a
  /// thread asks for its mark on every change while it is open, and a server
  /// without thread marks refuses each.
  final Duration retryAfter;
  final DateTime Function() _now;

  /// The highest mark sent and kept, by chat.
  final _sent = <K, int>{};

  /// The mark waiting for the one on its way, by chat.
  final _next = <K, int>{};
  final _sending = <K>{};

  /// The mark on its way, by conversation: one asked for again meanwhile is not sent twice.
  final _inFlight = <K, int>{};

  /// The last mark that failed, and when, by chat.
  final _failed = <K, (int, DateTime)>{};
  bool _disposed = false;

  /// Asks for [sequence] to be [conversationId]'s mark on the server, unless [kept] (what the server already holds)
  /// or a mark sent from here is as high.
  void mark(K conversationId, int sequence, {int kept = 0}) {
    if (_disposed || sequence <= kept) return;
    if (sequence <= (_sent[conversationId] ?? 0)) return;
    if (sequence <= (_next[conversationId] ?? 0)) return;
    if (sequence <= (_inFlight[conversationId] ?? 0)) return;
    if (_failed[conversationId] case (final failed, final at)
        when sequence <= failed && _now().difference(at) < retryAfter) {
      return;
    }
    _next[conversationId] = sequence;
    if (_sending.add(conversationId)) unawaited(_drain(conversationId));
  }

  Future<void> _drain(K conversationId) async {
    while (!_disposed) {
      final sequence = _next.remove(conversationId);
      if (sequence == null) break;
      _inFlight[conversationId] = sequence;
      try {
        await _send(conversationId, sequence);
        _failed.remove(conversationId);
        if (sequence > (_sent[conversationId] ?? 0)) {
          _sent[conversationId] = sequence;
        }
      } catch (_) {
        _failed[conversationId] = (sequence, _now());
        // Offline, or a server without read marks: this phone's mark stands,
        // and the next one asked for is sent afresh.
      }
    }
    _inFlight.remove(conversationId);
    _sending.remove(conversationId);
  }

  void dispose() => _disposed = true;
}

/// How far the person has read each chat of one Swarm ([reader]), as the
/// desktop keeps it (`SuperTabController.unread`): a chat is unread while its
/// newest message in the main stream ([SuperTabConversation.mainSequence]; a
/// reply only in a thread is not) is past the higher of the server's mark
/// ([SuperTabSnapshot.readMarks], #2131) and this phone's own. Unread is drawn
/// as the chat's name in bold (design system: Unread).
///
/// The phone's own marks are what a server without read marks has, and what
/// keeps a chat read here while a send is on its way or failed. They are kept
/// on the phone ([store]) under the account and the Swarm: no message, only
/// each chat's id and a number. The first read of a Swarm on this phone, with
/// no mark yet from the phone or the server, counts what is already there as
/// read (Tony, 2026-10-29), so a new phone does not start with every chat in
/// bold; the Home kept from before is no read. That first mark is no read of
/// a tag, though: a tag already waiting then still counts ([tagsReadThrough]).
/// Nothing is unread until the marks are read back. Access lost takes them off the phone too.
class SwarmReadMarks extends ChangeNotifier {
  SwarmReadMarks(
    this.reader, {
    required this.store,
    this.saveAfter = Duration.zero,
  }) {
    _sender = SwarmReadMarkSender<String>(
      (id, sequence) => reader.request(
        'POST',
        '/api/super-tabs/${Uri.encodeComponent(reader.tabId)}'
            '/conversations/${Uri.encodeComponent(id)}/read',
        body: {'sequence': sequence},
      ),
    );
    _threadSender = SwarmReadMarkSender<(String, String)>(
      (thread, sequence) => reader.request(
        'POST',
        '/api/super-tabs/${Uri.encodeComponent(reader.tabId)}'
            '/conversations/${Uri.encodeComponent(thread.$1)}'
            '/threads/${Uri.encodeComponent(thread.$2)}/read',
        body: {'sequence': sequence},
      ),
      retryAfter: const Duration(minutes: 1),
    );
    reader.addListener(_swarmChanged);
    reader.reads.addListener(_swarmChanged);
    unawaited(_load());
  }

  /// The most chats whose marks the phone keeps per Swarm; the marks of chats
  /// the Swarm's read no longer has go first.
  static const kept = 1000;

  /// The most threads whose marks the phone keeps per Swarm, as the server
  /// does (backend readMarks.ts THREAD_MARK_CAP): those of chats the Swarm's
  /// read no longer has go first, then those of the chat kept longest, its
  /// oldest first (not the oldest overall: no mark keeps a time). A thread
  /// without one reads
  /// as far as the phone's other marks say (Activity's opened mark, the
  /// chat's).
  static const keptThreads = 500;

  final SwarmReader reader;

  /// How long marks wait to be written: at once by default, after what moved
  /// them together (a first read's, a chat read on), which is written once.
  final Duration saveAfter;

  /// Where the phone keeps its marks; null keeps them in memory only.
  final LocalKeyValueStore? store;
  late final SwarmReadMarkSender<String> _sender;
  late final SwarmReadMarkSender<(String, String)> _threadSender;

  /// The phone's own marks, by chat.
  final _marks = <String, int>{};

  /// The marks of [_marks] that only the first read set, by chat: no read by
  /// the person. Kept apart in what the phone keeps too, so a phone before
  /// them reads [_marks] as it always has.
  final _firstRead = <String, int>{};

  /// The phone's own thread marks, by chat then thread root.
  final _threads = <String, Map<String, int>>{};

  /// Marks a Mark all read shows on this phone only, by chat, while its
  /// Undo waits ([holdAllRead]): read marks never move back on the server,
  /// so Undo works by not sending them yet. Never kept on the phone; access
  /// lost drops them unsent.
  final _held = <String, int>{};

  /// The marks were read back from the phone (or there is no store).
  bool _loaded = false;

  /// The marks count: read back from the phone, or started by a first read.
  bool _started = false;
  Timer? _save;
  bool _disposed = false;

  String get _key => 'swarm_read_marks.${reader.accountId}.${reader.tabId}';

  /// The phone's marks were read back. Until then [readThrough] has none to
  /// give, which is not a chat with no mark: what waits on a mark waits on
  /// this, and is told when it moves.
  bool get loaded => _loaded;

  /// Whether [group] has messages in its main stream past what the person
  /// has read.
  bool unread(SuperTabConversation group) {
    if (!_loaded || !_started) return false;
    final server = reader.snapshot?.readMarks[group.id] ?? 0;
    return group.mainSequence >
        max(server, max(_marks[group.id] ?? 0, _held[group.id] ?? 0));
  }

  /// The desktop's project-row rule: only a person's main-stream message
  /// past the read mark, never agents' progress or system notices.
  bool unreadForPeople(SuperTabConversation group) =>
      _loaded &&
      _started &&
      !swarmMuted(reader, group) &&
      group.lastPersonSequence > (readThrough(group.id) ?? 0);

  /// How far the person has read [conversationId]: the higher of the
  /// server's mark and this phone's; null until the phone's are read back,
  /// and once access is lost.
  int? readThrough(String conversationId) => _loaded && !reader.accessRevoked
      ? max(
          reader.snapshot?.readMarks[conversationId] ?? 0,
          max(_marks[conversationId] ?? 0, _held[conversationId] ?? 0),
        )
      : null;

  /// How far the person has read [conversationId] for its tags: the higher
  /// of the server's mark and this phone's, leaving out the first read's,
  /// which saw nothing (the desktop's `readMark`). Null as [readThrough] is.
  int? tagsReadThrough(String conversationId) =>
      _loaded && !reader.accessRevoked
      ? max(
          reader.snapshot?.readMarks[conversationId] ?? 0,
          max(switch (_marks[conversationId]) {
            final mark? when mark != _firstRead[conversationId] => mark,
            _ => 0,
          }, _held[conversationId] ?? 0),
        )
      : null;

  /// How far the person has read [root]'s thread in [conversationId]: the
  /// higher of the server's thread mark, from any of their devices, and this
  /// phone's; null until the phone's are read back, and once access is lost.
  /// Zero for a thread with no mark: the chat's own mark is the caller's.
  int? threadThrough(String conversationId, String root) =>
      _loaded && !reader.accessRevoked
      ? max(
          reader.snapshot?.threadMarks[conversationId]?[root] ?? 0,
          _threads[conversationId]?[root] ?? 0,
        )
      : null;

  /// The person saw [root]'s thread in [conversationId] through [sequence]:
  /// sent to the server, and kept on the phone when it moves the phone's mark
  /// on.
  void markThreadRead(String conversationId, String root, int sequence) {
    if (_disposed || reader.accessRevoked || sequence <= 0) return;
    // A chat the Swarm's read no longer has is not the person's to mark.
    if (reader.snapshot?.conversation(conversationId) == null) return;
    _threadSender.mark(
      (conversationId, root),
      sequence,
      kept: reader.snapshot?.threadMarks[conversationId]?[root] ?? 0,
    );
    final held = _threads[conversationId] ??= {};
    if (sequence <= (held[root] ?? 0)) return;
    // Moved to the end: the newest are the ones a trim keeps.
    held.remove(root);
    held[root] = sequence;
    _saveSoon();
    notifyListeners();
  }

  /// The person saw [conversationId] through [sequence]: sent to the server,
  /// and kept on the phone when it moves the phone's mark on.
  void markRead(String conversationId, int sequence) {
    if (_disposed || reader.accessRevoked) return;
    _sender.mark(
      conversationId,
      sequence,
      kept: reader.snapshot?.readMarks[conversationId] ?? 0,
    );
    // Seen through the first read's mark, or through the chat's newest
    // message in its main stream when replies only in a thread came after
    // it: the mark is the person's own now.
    final first = _firstRead[conversationId];
    final main = reader.snapshot?.conversation(conversationId)?.mainSequence;
    final seen =
        first != null &&
        (sequence >= first || (main != null && sequence >= main));
    if (seen) _firstRead.remove(conversationId);
    final moved = sequence > (_marks[conversationId] ?? 0);
    if (!moved && !seen) return;
    if (moved) _marks[conversationId] = sequence;
    _saveSoon();
    notifyListeners();
  }

  /// Mark all read (the desktop's `SuperTabController.markAllRead`): every
  /// chat of this Swarm shown unread — not dormant, not muted, as the person
  /// set it on this phone a moment ago too ([swarmMuted]) — reads as read on
  /// this phone at once, through its newest message in its main stream,
  /// and nothing is sent. Returns those marks by chat, for [sendHeld] once
  /// the Undo window passes or [dropHeld] on Undo; empty with nothing unread,
  /// before the marks are back, and once access is lost.
  Map<String, int> holdAllRead() {
    final snapshot = reader.snapshot;
    if (_disposed || reader.accessRevoked || snapshot == null) return const {};
    final through = <String, int>{};
    for (final group in snapshot.conversations) {
      if (group.dormant || swarmMuted(reader, group) || !unread(group)) {
        continue;
      }
      through[group.id] = _held[group.id] = group.mainSequence;
    }
    if (through.isNotEmpty) notifyListeners();
    return through;
  }

  /// The Undo window of a Mark all read passed: each of [through] it still
  /// holds goes to the server, and is kept on the phone, as a chat read is
  /// ([markRead]). A chat the Swarm's read no longer has sends nothing.
  void sendHeld(Map<String, int> through) => _sendHeld(through, tell: true);

  void _sendHeld(Map<String, int> through, {required bool tell}) {
    if (_disposed || reader.accessRevoked) return;
    var moved = false;
    for (final MapEntry(key: id, value: sequence) in through.entries) {
      if (_held[id] != sequence) continue;
      _held.remove(id);
      if (reader.snapshot?.conversation(id) == null) continue;
      _sender.mark(id, sequence, kept: reader.snapshot?.readMarks[id] ?? 0);
      if (sequence > (_marks[id] ?? 0)) {
        _marks[id] = sequence;
        moved = true;
      }
    }
    if (moved) _saveSoon();
    if (tell) notifyListeners();
  }

  /// Undo of a Mark all read: each of [through] it still holds reads as it
  /// did before; a chat read since keeps what it read. Nothing was sent.
  void dropHeld(Map<String, int> through) {
    if (_disposed) return;
    var dropped = false;
    for (final MapEntry(key: id, value: sequence) in through.entries) {
      if (_held[id] != sequence) continue;
      _held.remove(id);
      dropped = true;
    }
    if (dropped) notifyListeners();
  }

  Future<void> _load() async {
    final held = <String, int>{};
    final first = <String, int>{};
    final threads = <String, Map<String, int>>{};
    var found = false;
    try {
      if (await store?.read(_key) case final raw?) {
        final value = jsonDecode(raw);
        if (value case {'marks': final Map marks}) {
          for (final MapEntry(:key, :value) in marks.entries) {
            if (key is String && value is num) held[key] = value.toInt();
          }
          found = true;
        }
        // Absent from what a phone before them wrote.
        if (value case {'firstRead': final Map marks}) {
          for (final MapEntry(:key, :value) in marks.entries) {
            if (key is String && value is num) first[key] = value.toInt();
          }
        }
        // Absent from what a phone before thread marks wrote.
        if (value case {'threads': final Map kept}) {
          for (final MapEntry(key: id, value: roots) in kept.entries) {
            if (id is! String || roots is! Map) continue;
            for (final MapEntry(:key, :value) in roots.entries) {
              if (key is String && value is num) {
                (threads[id] ??= {})[key] = value.toInt();
              }
            }
          }
        }
      }
    } catch (_) {
      // An unreadable value is no marks: the next first read starts afresh.
    }
    if (_disposed || reader.accessRevoked) return;
    final marked = _marks.isNotEmpty || _threads.isNotEmpty;
    for (final MapEntry(:key, :value) in held.entries) {
      if (value > (_marks[key] ?? 0)) _marks[key] = value;
    }
    // A first read's mark stays one while nothing read moved it on.
    for (final MapEntry(:key, :value) in first.entries) {
      if (_marks[key] == value) _firstRead[key] = value;
    }
    // A thread read before the marks were back keeps the higher of the two.
    for (final MapEntry(key: id, value: roots) in threads.entries) {
      final now = _threads[id] ??= {};
      for (final MapEntry(:key, :value) in roots.entries) {
        if (value > (now[key] ?? 0)) now[key] = value;
      }
    }
    _loaded = true;
    _started |= found;
    // A chat read before the marks were back is kept with them.
    if (marked) _saveSoon();
    _swarmChanged();
    notifyListeners();
  }

  /// The Swarm was read, changed or not: its first read on this phone starts
  /// the marks.
  void _swarmChanged() {
    if (_disposed) return;
    if (reader.accessRevoked) {
      _forget();
      return;
    }
    final snapshot = reader.snapshot;
    // The Home kept holds no first load ([SwarmReader.heldLoad]).
    if (!_loaded || _started || snapshot == null || reader.heldLoad == 0) {
      return;
    }
    _started = true;
    if (snapshot.readMarks.isEmpty) {
      for (final group in snapshot.conversations) {
        if (group.lastMessageSequence > (_marks[group.id] ?? 0)) {
          _marks[group.id] = group.lastMessageSequence;
          _firstRead[group.id] = group.lastMessageSequence;
        }
      }
    }
    _saveSoon();
    notifyListeners();
  }

  void _forget() {
    _save?.cancel();
    _save = null;
    final had =
        _marks.isNotEmpty ||
        _threads.isNotEmpty ||
        _held.isNotEmpty ||
        _started;
    // A held Mark all read belongs to the access that is gone: never sent.
    _held.clear();
    _marks.clear();
    _firstRead.clear();
    _threads.clear();
    _started = false;
    unawaited(store?.delete(_key).catchError((Object _) {}));
    if (had) notifyListeners();
  }

  void _saveSoon() {
    if (!_loaded || store == null || _disposed) return;
    _save?.cancel();
    _save = Timer(saveAfter, _write);
  }

  void _write() {
    _save = null;
    _trim();
    final value = jsonEncode({
      'marks': _marks,
      if (_firstRead.isNotEmpty) 'firstRead': _firstRead,
      if (_threads.isNotEmpty) 'threads': _threads,
    });
    unawaited(store?.write(_key, value).catchError((Object _) {}));
  }

  /// At most [kept] marks: those of chats the Swarm's read no longer has go
  /// first, then the oldest kept.
  void _trim() {
    _trimThreads();
    _firstRead.removeWhere((id, first) => _marks[id] != first);
    if (_marks.length <= kept) return;
    final read = {
      for (final group in reader.snapshot?.conversations ?? const []) group.id,
    };
    for (final id in [
      for (final id in _marks.keys)
        if (!read.contains(id)) id,
    ]) {
      if (_marks.length <= kept) return;
      _marks.remove(id);
    }
    while (_marks.length > kept) {
      _marks.remove(_marks.keys.first);
    }
    _firstRead.removeWhere((id, _) => !_marks.containsKey(id));
  }

  /// At most [keptThreads] thread marks: those of chats the Swarm's read no
  /// longer has go first, then those of the chat kept longest, its oldest
  /// first.
  void _trimThreads() {
    int count() => _threads.values.fold(0, (n, roots) => n + roots.length);
    if (count() <= keptThreads) return;
    final read = {
      for (final group in reader.snapshot?.conversations ?? const []) group.id,
    };
    _threads.removeWhere((id, _) => !read.contains(id));
    // Then the chat kept longest (the first key), its oldest mark first.
    while (count() > keptThreads) {
      final id = _threads.keys.first;
      final roots = _threads[id]!..remove(_threads[id]!.keys.first);
      if (roots.isEmpty) _threads.remove(id);
    }
  }

  @override
  void dispose() {
    // Closing the Swarm or the app is no Undo: a held Mark all read goes now.
    if (_held.isNotEmpty) _sendHeld({..._held}, tell: false);
    _disposed = true;
    // What waited to be written is written now.
    if (_save case final save?) {
      save.cancel();
      _write();
    }
    reader.removeListener(_swarmChanged);
    reader.reads.removeListener(_swarmChanged);
    _sender.dispose();
    _threadSender.dispose();
    super.dispose();
  }
}