fix(mobile): keep Active activity unknown after deletions the gap hides (#9951)

Follow-up to #9832, which derives the Active filter's activity from the
channel list's own reads. Three cases could leave a channel marked known
with activity that is too old or too new. Each now leaves it unknown, so
it stays visible until the next list read re-reads it (the filter's
existing fail-open path).

- **A latest post deleted while offline, after the read marker moved
past the deletion on another device.** The catch-up asked for deletions
only after the read marker, so it never saw this one, and the channel
kept the deleted post's time. The gap read, which already runs for a
channel an Active section shows that has an activity gap, now also asks
for deletions of each such channel's current latest post (`#e` plus
`#h`, since the gap's start) and forgets a head they removed. With up to
64 such channels, those deletions are read in the gap read's own
request. Past that, the gap posts are read and applied first, and the
deletions are then read only for the channels whose latest post no gap
post replaced, so a failed deletion read leaves only those channels
unknown. When the read marker sits on the latest post, so the gap read
is moot, the catch-up's deletion filter for that channel starts one
second earlier instead, so a deletion in that post's second is seen. A
deletion from that extra second only updates the Active filter's
activity. ([review
comment](https://github.com/block/buzz/pull/9832#issuecomment-7053648925))
- **A gap read whose one post is already deleted.** The gap read asks
for the newest post in the gap (`limit: 1`). When the catch-up has just
found that post deleted, an older post in the gap may survive, but the
channel was marked covered. It now stays unknown. ([review
comment](https://github.com/block/buzz/pull/9832#discussion_r4688526643))
- **A gap read whose signature checks throw.** The channels were marked
covered with nothing recorded. They now stay unknown, as the catch-up
already does when its own checks throw. ([review
comment](https://github.com/block/buzz/pull/9832#discussion_r4688526649))

### Validation

- Added three steps to the journey test [Active filter activity survives
rebuilds, read gaps, failed catch-ups and
deletions](bc664ae8a9/mobile/test/features/channels/channels_provider_test.dart),
one per case, plus a step that another post's deletion in the extra
second leaves a page being saved current. Added [gap posts settle Active
activity even when the head deletion read
fails](55144f79be/mobile/test/features/channels/channels_provider_test.dart)
for 65 gap channels, with every gap channel holding a cached head (two
requests) and with two empty ones (one request).
- Full-app run on an iOS 27 simulator signed in to a Builderlab
community: channel list on launch, Active filter (7 days) collapsing
each section to its active channels, opening a channel and back,
background and resume, pull to refresh, and a cold relaunch with Active
kept. All behaved as on main, with no error notice. The three changed
paths need staged deletions and failures a live community can't produce,
so only the journey test covers them.

### Regressions

- **Network:** no new requests while a gap read's posts and the head ids
fit in one request (128 channel values, so always up to 64 such
channels). Past that, one more optional request per 128 channels, sent
after the gap posts settle and only for the channels whose latest post
no gap post replaced. The optional gap read, sent after the unread
badges and only for channels an Active section shows with an activity
gap, names their latest posts by id and returns only deletions of those
posts. The unread catch-up's filters are unchanged, except that a shown
channel whose latest post sits at the read marker asks for deletions
from one second earlier.
- **Unread state and saved pages:** unchanged for `h`-tagged deletions,
including those from the extra second, which only update the Active
filter's activity. A deletion without an `h` tag stamped in that extra
second takes the ordinary unread path, as main already does for one a
second later. For a reaction removal (what agents send when a turn
ends), that republishes the list once, with no saved page or badge
change. An untagged deletion of a post already observed would also reset
that channel's unread catch-up cursor, so the next reconnect fetches its
backlog again, but no shipped client sends one: desktop, mobile and the
SDK all tag `h` on post deletions.
- **Activity:** each change only turns a channel unknown, which keeps it
visible in Active, never hidden.

buzz-review-completed

---------

Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
This commit is contained in:
Tom Brow
2026-11-25 13:53:55 -08:00
committed by GitHub
parent 5e11b5380f
commit 2c7e67fcc1
3 changed files with 422 additions and 19 deletions

View File

@@ -2849,11 +2849,23 @@ void main() {
recentMessages: [old],
enforceHistoryLimits: true,
);
final savedDir = await Directory.systemTemp.createTemp('active-saved');
addTearDown(() => savedDir.delete(recursive: true));
final container = _buildContainer(
session: session,
overrides: [
readStateProvider.overrideWith(_ActivityReadState.new),
_testPubkeyProvider.overrideWith(_ActivityPubkey.new),
savedMessageScopeProvider.overrideWithValue((
communityId: 'community',
pubkey: _activityPk,
)),
savedMessageStoreProvider.overrideWith(
(ref) => SavedMessageStore(
root: () async => savedDir,
diagnostics: const NoopDiagnosticReporter(),
),
),
],
);
addTearDown(container.dispose);
@@ -3022,11 +3034,12 @@ void main() {
// Deleting the latest post forgets the channel's activity until the next
// list read re-reads it.
NostrEvent deletionOf(NostrEvent post) => NostrEvent.fromJson(
NostrEvent deletionOf(NostrEvent post, {int? at}) => NostrEvent.fromJson(
signNostrEvent(
kind: EventKind.deletion,
content: '',
secretKey: _activityKey,
createdAt: at,
tags: [
['h', _channelA],
['e', post.id],
@@ -3052,6 +3065,131 @@ void main() {
await reconnect();
expect(notifier.activityFor(_channelA), (known: true, at: old.createdAt));
// A gap read answers with its newest post. When the catch-up found that
// post deleted, an older one in the gap may survive, so the channel stays
// unknown.
final kept = message('kept', now - 8);
session.emit(kept);
await _waitUntil(
() => notifier.activityFor(_channelA).at == kept.createdAt,
);
final older = message('older', now - 7);
final deletedInGap = message('deletedInGap', now - 6);
session.recentMessages = [
old,
kept,
older,
deletedInGap,
deletionOf(deletedInGap),
];
(container.read(readStateProvider.notifier) as _ActivityReadState)
.markReadThrough(_channelA, now - 4);
await reconnect();
expect(notifier.activityFor(_channelA).known, isFalse);
session.recentMessages = [old, kept, older];
await reconnect();
expect(notifier.activityFor(_channelA), (known: true, at: older.createdAt));
// So does a gap read whose signature checks fail to run.
final unchecked = message('unchecked', now - 5);
session.recentMessages = [old, kept, older, unchecked];
final checks = debugRunActivityChecks;
debugRunActivityChecks = (run) async {
final verified = await checks(run);
if (verified.contains(unchecked.id)) throw StateError('checks failed');
return verified;
};
try {
await reconnect();
expect(notifier.activityFor(_channelA).known, isFalse);
} finally {
debugRunActivityChecks = checks;
}
await reconnect();
expect(notifier.activityFor(_channelA), (
known: true,
at: unchecked.createdAt,
));
// A deletion made while offline counts even when the read marker has
// since moved past it on another device.
final gone2 = message('gone2', now - 4);
session.emit(gone2);
await _waitUntil(
() => notifier.activityFor(_channelA).at == gone2.createdAt,
);
session.recentMessages = [old, unchecked, deletionOf(gone2, at: now - 3)];
(container.read(readStateProvider.notifier) as _ActivityReadState)
.markReadThrough(_channelA, now - 2);
await reconnect();
expect(notifier.activityFor(_channelA).known, isFalse);
session.recentMessages = [old, unchecked];
await reconnect();
expect(notifier.activityFor(_channelA), (
known: true,
at: unchecked.createdAt,
));
// A newer post in the gap replaces the deleted one, so the channel stays
// known.
final gone3 = message('gone3', now - 4);
session.emit(gone3);
await _waitUntil(
() => notifier.activityFor(_channelA).at == gone3.createdAt,
);
final after = message('after', now - 3);
session.recentMessages = [
old,
unchecked,
after,
deletionOf(gone3, at: now - 3),
];
await reconnect();
expect(notifier.activityFor(_channelA), (known: true, at: after.createdAt));
// So does a deletion in the same second as the latest post the read
// marker sits on.
final boundary = message('boundary', now - 1);
session.emit(boundary);
await _waitUntil(
() => notifier.activityFor(_channelA).at == boundary.createdAt,
);
session.recentMessages = [
old,
unchecked,
after,
deletionOf(boundary, at: now - 1),
];
(container.read(readStateProvider.notifier) as _ActivityReadState)
.markReadThrough(_channelA, now - 1);
// The earlier second it reads only serves the Active head: another post's
// deletion there is not new, so a page being saved meanwhile still is.
final regular = session.recentMessages;
session.recentMessages = [
old,
unchecked,
after,
deletionOf(older, at: now - 1),
];
final store = container.read(savedMessageStoreProvider);
final page = (channelId: _channelA, rootId: '');
final ticket = store.capture(
container.read(savedMessageScopeProvider)!,
page,
);
await reconnect();
expect(store.isCurrent(ticket), isTrue);
expect(notifier.activityFor(_channelA), (
known: true,
at: boundary.createdAt,
));
session.recentMessages = regular;
await reconnect();
expect(notifier.activityFor(_channelA).known, isFalse);
session.recentMessages = [old, unchecked, after];
await reconnect();
expect(notifier.activityFor(_channelA), (known: true, at: after.createdAt));
// A channel no Active section shows skips its gap read, and reads as
// unknown until a catch-up while it is shown.
shown.close();
@@ -3224,6 +3362,107 @@ void main() {
);
}
// Past one request's channel values, the head deletions go out on their
// own after the gap posts; below it, with them.
for (final empty in [0, 2]) {
test('gap posts settle Active activity even when the head deletion read '
'fails ($empty channels without a head)', () async {
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
final ids = [
for (var i = 0; i <= kMaxExplicitChannelValues ~/ 2; i++) 'gap-$i',
];
final heads = {
for (final id in ids.take(ids.length - empty))
id: _signedPost(now - 100, channel: id, content: id),
};
final newer = _signedPost(now - 30, channel: ids[0], content: 'newer');
final deletion = NostrEvent.fromJson(
signNostrEvent(
kind: EventKind.deletion,
content: '',
secretKey: _activityKey,
createdAt: now - 50,
tags: [
['h', ids[1]],
['e', heads[ids[1]]!.id],
],
).toMap(),
);
final session = _FakeRelaySession(
memberships: [for (final id in ids) _membership(id, _activityPk)],
metadata: [for (final id in ids) _meta(id: id, name: id)],
recentMessages: heads.values.toList(),
enforceHistoryLimits: true,
);
final container = _buildContainer(
session: session,
overrides: [
readStateProvider.overrideWith(_ActivityReadState.new),
_testPubkeyProvider.overrideWith(_ActivityPubkey.new),
],
);
addTearDown(container.dispose);
await container.read(channelsProvider.future);
final notifier = container.read(channelsProvider.notifier);
await notifier.waitForUnreadCatchUp();
for (final id in ids) {
final shown = container.listen(
channelFilterActivityProvider(id),
(_, _) {},
);
addTearDown(shown.close);
}
Future<void> reconnect() async {
final batches = session.queryBatches.length;
session.setStatus(SessionStatus.reconnecting);
session.setStatus(SessionStatus.connected);
await _waitUntil(
() => session.queryBatches
.skip(batches)
.any((batch) => batch.every((filter) => filter.since != null)),
);
await notifier.waitForUnreadCatchUp();
await _settle();
}
// Read on another device while disconnected, so every channel has a gap.
session.recentMessages = [...heads.values, newer, deletion];
for (final id in ids) {
(container.read(readStateProvider.notifier) as _ActivityReadState)
.markReadThrough(id, now - 15);
}
session.headDeletionBatchError = RelayException(
503,
'{"error":"query timed out"}',
);
final before = session.queryBatches.length;
await reconnect();
expect(
session.queryBatches
.skip(before)
.where(
(batch) =>
_isGapBatch(batch) ||
batch.every((filter) => filter.tags.containsKey('#e')),
),
hasLength(empty == 0 ? 2 : 1),
);
expect(notifier.activityFor(ids[0]), (known: true, at: newer.createdAt));
expect(notifier.activityFor(ids[1]).known, isFalse);
// Only a separate deletion read can fail on its own.
expect(notifier.activityFor(ids[2]).known, empty > 0);
// Once the deletions are read, only the deleted head stays unknown.
session.headDeletionBatchError = null;
await reconnect();
expect(notifier.activityFor(ids[1]).known, isFalse);
expect(notifier.activityFor(ids[2]), (
known: true,
at: heads[ids[2]]!.createdAt,
));
});
}
test('live channel events update channel lastMessageAt', () async {
final session = _FakeRelaySession(
memberships: [_membership(_channelA, myPk)],
@@ -4697,8 +4936,7 @@ NostrEvent _signedPost(
);
bool _isGapBatch(List<NostrFilter> batch) =>
batch.isNotEmpty &&
batch.every((filter) => filter.until != null && filter.limit == 1);
batch.any((filter) => filter.until != null && filter.limit == 1);
/// Drains pending microtasks so provider rebuilds and awaited writes land.
Future<void> _settle() async {
@@ -4787,6 +5025,8 @@ class _FakeRelaySession extends RelaySessionNotifier {
Object? messageBatchError;
// Fails batches made only of the catch-up's activity gap reads.
Object? gapBatchError;
// Fails a batch that only reads deletions of given posts.
Object? headDeletionBatchError;
// Holds the next batch of activity gap reads.
Completer<void>? gapBatchGate;
Object? latestBatchError;
@@ -5180,8 +5420,16 @@ class _FakeRelaySession extends RelaySessionNotifier {
throw RelayException(400, '{"error":"too many explicit channels"}');
}
if (messageBatchError case final error?) throw error;
if (filters.isNotEmpty &&
filters.every((f) => f.until != null && f.limit == 1)) {
if (headDeletionBatchError case final error?
when filters.isNotEmpty &&
filters.every(
(filter) =>
filter.tags.containsKey('#e') &&
filter.kinds.contains(EventKind.deletion),
)) {
throw error;
}
if (_isGapBatch(filters)) {
if (gapBatchError case final error?) throw error;
final gate = gapBatchGate;
gapBatchGate = null;