WIP: message delivery status and refresh safety; fix reconcile listener recursion

The transcript listener read the lazily built _controller while _attach was
still building it (load() notifies synchronously), so it built another
controller, recursively, until the test process ran out of memory.
This commit is contained in:
2026-10-31 19:54:48 +00:00
parent 24956266d8
commit 4bc99f490b
15 changed files with 1133 additions and 177 deletions
@@ -1,3 +1,5 @@
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import '../../../core/servers/server_scope.dart';
@@ -15,12 +17,23 @@ abstract interface class ChatDraftStore {
/// Stores [text] exactly. A blank draft removes the entry so the store does
/// not grow without bound. Returns false when nothing was stored.
Future<bool> write(String sessionId, String text);
/// The messages sent from this device that the server has not yet accepted,
/// as stored JSON intents in send order. Empty when there are none.
Future<List<Object?>> readUnsent(String sessionId);
/// Replaces the stored unsent intents; an empty list removes the entry.
/// Returns false when nothing was stored.
Future<bool> writeUnsent(String sessionId, List<Object?> intents);
}
/// The app's per-session draft keys. Independent of the legacy web/Android
/// storage: no old draft is imported or written under an old key.
const String chatDraftKeyPrefix = 'pipelines.next.chatDraft.v1.';
/// The per-session key for messages still awaiting the server's acceptance.
const String chatUnsentKeyPrefix = 'pipelines.next.chatUnsent.v1.';
/// `shared_preferences`-backed draft storage.
///
/// Reads are tolerant and a write reports failure instead of throwing, so the
@@ -62,14 +75,52 @@ class SharedPreferencesChatDraftStore implements ChatDraftStore {
return false;
}
}
String _unsentKey(String sessionId) =>
scopedServerKey('$chatUnsentKeyPrefix$sessionId', serverId);
@override
Future<List<Object?>> readUnsent(String sessionId) async {
final preferences = await _preferences();
if (preferences == null) return const [];
try {
final stored = preferences.getString(_unsentKey(sessionId));
if (stored == null || stored.isEmpty) return const [];
final decoded = jsonDecode(stored);
return decoded is List ? decoded : const [];
} catch (_) {
return const [];
}
}
@override
Future<bool> writeUnsent(String sessionId, List<Object?> intents) async {
final preferences = await _preferences();
if (preferences == null) return false;
try {
final key = _unsentKey(sessionId);
if (intents.isEmpty) return await preferences.remove(key);
return await preferences.setString(key, jsonEncode(intents));
} catch (_) {
return false;
}
}
}
/// An in-memory store for tests and previews. It never touches the platform
/// store, so no production preference can be mutated by a fixture.
class MemoryChatDraftStore implements ChatDraftStore {
MemoryChatDraftStore({Map<String, String>? drafts}) : _drafts = {...?drafts};
MemoryChatDraftStore({
Map<String, String>? drafts,
Map<String, List<Object?>>? unsent,
}) : _drafts = {...?drafts},
_unsent = {...?unsent};
final Map<String, String> _drafts;
final Map<String, List<Object?>> _unsent;
/// A snapshot of the stored unsent intents per session.
Map<String, List<Object?>> get unsentEntries => Map.unmodifiable(_unsent);
bool failReads = false;
bool failWrites = false;
@@ -93,4 +144,22 @@ class MemoryChatDraftStore implements ChatDraftStore {
}
return true;
}
@override
Future<List<Object?>> readUnsent(String sessionId) async {
if (failReads) throw StateError('Draft storage is unavailable.');
// A round trip through JSON, exactly as the device store keeps it.
return jsonDecode(jsonEncode(_unsent[sessionId] ?? const [])) as List;
}
@override
Future<bool> writeUnsent(String sessionId, List<Object?> intents) async {
if (failWrites) return false;
if (intents.isEmpty) {
_unsent.remove(sessionId);
} else {
_unsent[sessionId] = jsonDecode(jsonEncode(intents)) as List;
}
return true;
}
}