Skip to content

Commit 5fc82e4

Browse files
committed
Bound terminal typing recovery and reconcile pending chat saves
Signed-off-by: kenny lopez <[email protected]>
1 parent a2cfb8a commit 5fc82e4

4 files changed

Lines changed: 86 additions & 8 deletions

File tree

‎mobile/lib/features/channels/channel_typing_provider.dart‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class ChannelTypingNotifier extends Notifier<List<TypingEntry>> {
3131
void Function()? _unsubscribe;
3232
Timer? _pruneTimer;
3333
Timer? _retryTimer;
34+
Timer? _healthyTimer;
3435
int _generation = 0;
3536
final _activity = <(String, String?), _TypingActivity>{};
3637

@@ -51,6 +52,8 @@ class ChannelTypingNotifier extends Notifier<List<TypingEntry>> {
5152
_pruneTimer = null;
5253
_retryTimer?.cancel();
5354
_retryTimer = null;
55+
_healthyTimer?.cancel();
56+
_healthyTimer = null;
5457
});
5558

5659
if (sessionState.status == SessionStatus.connected) {
@@ -72,6 +75,8 @@ class ChannelTypingNotifier extends Notifier<List<TypingEntry>> {
7275
if (generation != _generation) return;
7376
_unsubscribe?.call();
7477
_unsubscribe = null;
78+
_healthyTimer?.cancel();
79+
_healthyTimer = null;
7580
_activity.clear();
7681
state = [];
7782
_pruneTimer?.cancel();
@@ -109,7 +114,12 @@ class ChannelTypingNotifier extends Notifier<List<TypingEntry>> {
109114
if (closed || generation != _generation) {
110115
unsubscribe();
111116
} else {
112-
failures = 0;
117+
// Admission alone is not health: repeated terminal rejections may
118+
// arrive after subscribe's readiness timeout. Reset only after a full
119+
// typing TTL of uninterrupted subscription health.
120+
_healthyTimer = Timer(const Duration(milliseconds: _ttlMs), () {
121+
if (!closed && generation == _generation) failures = 0;
122+
});
113123
_unsubscribe = unsubscribe;
114124
}
115125
} catch (_) {

‎mobile/lib/shared/me/me_chat_creation.dart‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ class MeChatCreation {
100100
sync.check();
101101
final placed = await isPlaced();
102102
sync.check();
103-
if (!placed) await place();
103+
// Placement may be confirmed while its inner journal cleanup failed.
104+
if (!placed || sync.prefs?.getString(sync.pendingKey) != null) {
105+
await place();
106+
}
104107
sync.check();
105108
await clearMeJournal(
106109
sync.prefs!,

‎mobile/test/features/channels/channel_typing_provider_test.dart‎

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void main() {
9191
},
9292
);
9393
test(
94-
'healthy typing recovery resets the consecutive failure budget',
94+
'terminal closure loops stay bounded until sustained recovery',
9595
() async {
9696
final relay = _Relay();
9797
final container = ProviderContainer(
@@ -100,13 +100,37 @@ void main() {
100100
addTearDown(container.dispose);
101101
container.listen(channelTypingProvider('chat'), (_, _) {});
102102
await container.pump();
103-
for (var i = 0; i < 5; i++) {
104-
relay.close!('closed after healthy subscription');
103+
for (final delay in [1, 2, 4]) {
104+
final calls = relay.calls;
105+
relay.close!('rejected after admission');
105106
await container.pump();
106-
await Future<void>.delayed(const Duration(milliseconds: 1100));
107-
expect(relay.calls, i + 2);
108-
expect(relay.active, 1);
107+
if (delay > 1) {
108+
await Future<void>.delayed(const Duration(milliseconds: 1100));
109+
expect(relay.calls, calls);
110+
}
111+
await Future<void>.delayed(
112+
Duration(milliseconds: delay * 1000 + 50 - (delay > 1 ? 1100 : 0)),
113+
);
114+
expect(relay.calls, calls + 1);
109115
}
116+
relay.close!('fourth rejection');
117+
await container.pump();
118+
await Future<void>.delayed(const Duration(milliseconds: 1100));
119+
expect(relay.calls, 4);
120+
relay.setStatus(SessionStatus.disconnected);
121+
await container.pump();
122+
relay.setStatus(SessionStatus.connected);
123+
await container.pump();
124+
relay.close!('first rejection after reconnect');
125+
await container.pump();
126+
await Future<void>.delayed(const Duration(milliseconds: 1100));
127+
final recoveredCalls = relay.calls;
128+
await Future<void>.delayed(const Duration(milliseconds: 8100));
129+
relay.close!('closure after sustained health');
130+
await container.pump();
131+
await Future<void>.delayed(const Duration(milliseconds: 1100));
132+
expect(relay.calls, recoveredCalls + 1);
133+
expect(relay.active, 1);
110134
},
111135
);
112136
test('transport rebuilds release each typing subscription', () async {

‎mobile/test/shared/me/me_sync_test.dart‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:shared_preferences/shared_preferences.dart';
77
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
88
import 'package:buzz/shared/crypto/nip44.dart';
99
import 'package:buzz/shared/me/me_groups.dart';
10+
import 'package:buzz/shared/me/me_chat_creation.dart';
1011
import 'package:buzz/shared/me/me_assistants.dart';
1112
import 'package:buzz/shared/me/me_sync.dart';
1213
import 'package:buzz/shared/me/me_recovery.dart';
@@ -128,6 +129,46 @@ void main() {
128129
verify: false,
129130
).toMap(),
130131
);
132+
for (final failure in ['false', 'throw']) {
133+
test(
134+
'chat recovery reconciles its confirmed Solo journal after $failure cleanup',
135+
() async {
136+
SharedPreferences.setMockInitialValues({});
137+
final store = _FailingCacheStore()..removeFailure = failure;
138+
SharedPreferencesStorePlatform.instance = store;
139+
final prefs = await SharedPreferences.getInstance();
140+
final relay = FakeRelay();
141+
final sync = MeSync(
142+
relay,
143+
RelayConfig(baseUrl: origin, nsec: nsec),
144+
prefs: prefs,
145+
);
146+
const id = '11111111-1111-4111-8111-111111111111';
147+
const intent = MeChatCreation(id, 'Chat', 'agent');
148+
Future<String> complete() => intent.complete(
149+
sync,
150+
findChannel: () async => id,
151+
createChannel: () async => id,
152+
hasAgent: () async => true,
153+
addAgent: () async {},
154+
isPlaced: () async =>
155+
relay.events.isNotEmpty &&
156+
sync.decode(relay.events.single).channels.contains(id),
157+
place: () => sync.update((current) => current.place(id)),
158+
);
159+
await expectLater(complete(), throwsStateError);
160+
expect(prefs.getString(sync.pendingKey), isNotNull);
161+
expect(MeChatCreation.pending(sync), isNotNull);
162+
store.removeFailure = null;
163+
await expectLater(complete(), throwsStateError);
164+
expect(prefs.getString(sync.pendingKey), isNull);
165+
expect(MeChatCreation.pending(sync), isNotNull);
166+
expect(await complete(), id);
167+
expect(MeChatCreation.pending(sync), isNull);
168+
expect(relay.writes, 1);
169+
},
170+
);
171+
}
131172
for (final failure in ['false', 'throw']) {
132173
test(
133174
'pending-save discard retains recovery after $failure removal',

0 commit comments

Comments
 (0)