Let entry cap remove spent outgoing images; clarify channel row arrival ordering

Outgoing image entries were excluded from the maxEntries cap, and age
eviction only marks them evicted, so frequent senders accumulated entries
and sidecars without bound. Outgoing entries with nothing left on disk (or
failed) are now eligible, oldest first.

Rename _ChannelChatRow.timestamp to receivedAt: it already returned the
local arrival time used for sorting and day separators.
This commit is contained in:
zjs81 committed 2026-10-11 01:37:36 -07:00
1 parent 25f197c979
commit 616c433922
3 files changed
+45 -8

No files matched your search

+6 -6
View File
@@ -458,13 +458,13 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
);
// Images are not ChannelMessages: they arrive as GRP_DATA
// chunks and are owned by ReceivedImageStore, so the two
// sources are merged here in timestamp order and the list
// sources are merged here in arrival (receivedAt) order and the list
// below indexes rows, not messages.
final rows = <_ChannelChatRow>[
for (final message in messages)
_ChannelChatRow(message: message),
...imageRows,
]..sort((a, b) => a.timestamp.compareTo(b.timestamp));
]..sort((a, b) => a.receivedAt.compareTo(b.receivedAt));
// Reverse rows so newest appear at bottom with reverse: true
final reversedRows = rows.reversed.toList();
@@ -556,15 +556,15 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
final startsDay =
index == reversedRows.length - 1 ||
!isSameChatDay(
reversedRows[index + 1].timestamp,
row.timestamp,
reversedRows[index + 1].receivedAt,
row.receivedAt,
);
if (isUnreadAnchor || startsDay) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
if (startsDay)
ChatDaySeparator(day: row.timestamp),
ChatDaySeparator(day: row.receivedAt),
if (isUnreadAnchor)
const UnreadDivider(),
bubble,
@@ -2951,7 +2951,7 @@ class _ChannelChatRow {
/// Local arrival time, used for ordering and day separators so a sender
/// clock running ahead cannot pin a row to the bottom (#584).
DateTime get timestamp => message?.receivedAt ?? image!.firstSeen;
DateTime get receivedAt => message?.receivedAt ?? image!.firstSeen;
/// Stable identity for the scroll-to-message [GlobalKey] map. The `aeic:`
/// prefix keeps a stream id from ever colliding with a message id.
+10 -2
View File
@@ -1556,7 +1556,8 @@ class ReceivedImageStore extends ChangeNotifier {
/// Brings the entry count inside [maxEntries], oldest first: failures, then
/// entries with nothing left on disk, then undecoded incoming images. Never
/// touches outgoing images, a running decode or a stream still receiving.
/// touches an outgoing image that still has data on disk, a running decode
/// or a stream still receiving.
Future<List<String>> _enforceEntryCap({String? protect}) async {
final removed = <String>[];
if (_entries.length <= maxEntries) return removed;
@@ -1573,7 +1574,14 @@ class ReceivedImageStore extends ChangeNotifier {
.where(
(e) =>
e.streamId != protect &&
!e.isOutgoing &&
// An outgoing image may go once nothing of it is left on
// disk (age eviction only marks it evicted), or it failed;
// otherwise frequent senders grow sidecars without bound.
(!e.isOutgoing ||
e.state.isFailure ||
(e.state == ReceivedImageState.evicted &&
!e.pngStored &&
!e.bitstreamStored)) &&
(e.state.isFailure ||
e.state == ReceivedImageState.evicted ||
e.state == ReceivedImageState.reassembled ||
@@ -777,6 +777,35 @@ void main() {
expect(await h.store.ensurePng(entry.streamId), isNotNull);
expect(h.decoder.calls, 0);
});
test('spent outgoing entries count toward maxEntries', () async {
final h = _build(maxEntries: 2, maxAge: const Duration(minutes: 10));
final ids = <String>[];
for (var i = 0; i < 4; i++) {
final entry = await h.store.registerOutgoing(
channelIndex: 1,
senderPrefix: kSelf,
imgId: 60 + i,
previewPng: Uint8List.fromList(List<int>.filled(64, i)),
rate: AeicRatePoint.ft32,
chunkCount: 2,
);
ids.add(entry.streamId);
h.advance(const Duration(seconds: 2));
}
// Fresh outgoing images keep their data and are never capped.
await h.store.evictToBudget();
expect(h.store.entries.length, 4);
// Past maxAge they are evicted (nothing left on disk), and the cap may
// then drop the oldest so sidecars cannot grow without bound.
h.advance(const Duration(minutes: 11));
await h.store.evictToBudget();
expect(h.store.entries.length, 2);
expect(h.store.entryFor(ids[0]), isNull);
expect(h.store.entryFor(ids[1]), isNull);
expect(h.store.entryFor(ids[3]), isNotNull);
});
});
group('processAutomatically (tap to process)', () {