From 616c433922c50b72a3e8c654731abe1bd619616a Mon Sep 17 00:00:00 2001 From: zjs81 Date: Sun, 11 Oct 2026 01:37:36 -0700 Subject: [PATCH] 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. --- lib/screens/channel_chat_screen.dart | 12 ++++---- lib/services/received_image_store.dart | 12 ++++++-- test/services/received_image_store_test.dart | 29 ++++++++++++++++++++ 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/lib/screens/channel_chat_screen.dart b/lib/screens/channel_chat_screen.dart index e3dfc03a..b2198605 100644 --- a/lib/screens/channel_chat_screen.dart +++ b/lib/screens/channel_chat_screen.dart @@ -458,13 +458,13 @@ class _ChannelChatScreenState extends State { ); // 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 { 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. diff --git a/lib/services/received_image_store.dart b/lib/services/received_image_store.dart index 88aca87e..e7d07339 100644 --- a/lib/services/received_image_store.dart +++ b/lib/services/received_image_store.dart @@ -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> _enforceEntryCap({String? protect}) async { final removed = []; 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 || diff --git a/test/services/received_image_store_test.dart b/test/services/received_image_store_test.dart index c7819203..6c7b2587 100644 --- a/test/services/received_image_store_test.dart +++ b/test/services/received_image_store_test.dart @@ -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 = []; + for (var i = 0; i < 4; i++) { + final entry = await h.store.registerOutgoing( + channelIndex: 1, + senderPrefix: kSelf, + imgId: 60 + i, + previewPng: Uint8List.fromList(List.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)', () {