Skip to content

Commit ec1b786

Browse files
browstudio
andauthored
fix(mobile): diagnostic reporting follow-ups (#9808)
Follow-ups to the mobile diagnostic reporting work in docs/mobile-diagnostic-reporting.md. ## Changes - **Install ID repair:** if the install ID file holds something that isn't text, the app now replaces it with a new ID, as it already does for an empty file. Before, every report went out without an install ID. A read that fails (for example, no file descriptor free) still keeps the file, so a passing error never replaces a good ID. - **Clock-window refusals are not reported as errors:** the spec treats relay refusals for a timestamp outside the relay's clock window as expected (counting them is still pending). The app only recognized one of the relay's wordings ("timestamp too far from server time"), so refusals for canvas events, relay-admin commands, leave requests and observer frames were reported as invalid requests. The five wordings are matched from the start of the reason, so unrelated `invalid:` reasons that mention a timestamp, such as the feedback timestamp overflow or one that echoes a value, are still reported. - **Diagnostics card:** rows get subtitles, and the divider indent matches the neighbouring Settings cards. - **Comments:** explain why push stop gates its current-identity request on the community key, and correct a test comment about who drops a repeated error object. ## Validation New tests cover: - member attribution on relay sessions, including a missing key and an answer that arrives after the member changed; - push stop reports while signed out; - reporter wiring for hosted admission and the self-hosted probe; - frames the socket can't decode; - a table of relay refusal reasons, each either reported or not, for both `OK` and `CLOSED`; - an install ID file whose reads fail keeps its ID. Full-app run on an iPhone 17 Pro simulator at 61b04f6: startup, channel list, sending and reacting, Settings, the Diagnostics card and Send test report, and install ID repair (a file that isn't valid UTF-8 and an empty file each got a new ID that stayed the same on the next launch). Not covered: a relay clock-window refusal, which the live relay would not produce. ## Diagnostics card | Before | After | | --- | --- | | ![Diagnostics card before](https://github.com/user-attachments/assets/b8fa7100-3efd-4c36-ac4d-006d487f4902) | ![Diagnostics card after, with row subtitles](https://github.com/user-attachments/assets/9ca7e95b-6061-4efa-b476-d7bb4c113a9b) | --------- Signed-off-by: Tom Brow <tomb@block.xyz> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
1 parent 792a9ce commit ec1b786

22 files changed

Lines changed: 917 additions & 113 deletions

‎mobile/lib/features/settings/settings_page/diagnostics_section.dart‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ class _DiagnosticsSection extends ConsumerWidget {
99
Widget build(BuildContext context, WidgetRef ref) {
1010
final reporter = ref.watch(diagnosticReporterProvider);
1111
return AppListCard(
12+
dividerIndent: Grid.xs,
1213
verticalPadding: Grid.twelve,
1314
children: [
1415
AppListRow(
1516
key: const Key('settings-send-test-report'),
1617
title: 'Send test report',
18+
subtitle: 'Send a test error to the reporting destination',
1719
onTap: () {
1820
reporter.sendTestReport();
1921
ScaffoldMessenger.of(context).showFeedbackSnackBar(
@@ -25,6 +27,7 @@ class _DiagnosticsSection extends ConsumerWidget {
2527
AppListRow(
2628
key: const Key('settings-test-crash'),
2729
title: 'Test crash',
30+
subtitle: 'Crash Buzz in native code to check crash reports',
2831
titleColor: context.colors.error,
2932
onTap: () async {
3033
final confirmed = await showDestructiveConfirmation(

‎mobile/lib/shared/diagnostics/diagnostic_reporter.dart‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ abstract interface class DiagnosticReporter {
4141

4242
/// The error a test report carries, so it is easy to tell apart.
4343
///
44-
/// Not const: each report needs its own instance, or the destination's
45-
/// deduplication drops every report after the first.
44+
/// Not const: each report needs its own instance, or the SDK's own
45+
/// deduplication drops a repeat of the last error object.
4646
class DiagnosticTestError implements Exception {
4747
DiagnosticTestError();
4848

‎mobile/lib/shared/diagnostics/diagnostic_startup.dart‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:async';
2+
import 'dart:convert';
23
import 'dart:io';
34

45
import 'package:flutter/foundation.dart';
@@ -60,8 +61,16 @@ Future<String?> diagnosticInstallId({
6061
final dir = await (directory ?? _installIdDirectory)();
6162
final file = File('${dir.path}/$_installIdFile');
6263
if (await file.exists()) {
63-
final saved = (await file.readAsString()).trim();
64-
if (saved.isNotEmpty) return saved;
64+
// A failed read goes to the catch below and keeps the file: replacing it
65+
// after a passing error would split this device's reports.
66+
final bytes = await file.readAsBytes();
67+
try {
68+
final saved = utf8.decode(bytes).trim();
69+
if (saved.isNotEmpty) return saved;
70+
} on FormatException {
71+
// Not text: a damaged file is replaced below, not a reason to send
72+
// reports without an install ID.
73+
}
6574
}
6675
final created = const Uuid().v4();
6776
await dir.create(recursive: true);

‎mobile/lib/shared/hosted/builderlab_account.dart‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,11 @@ class BuilderlabAccountClient {
388388

389389
/// Public identity metadata only. Private-key recovery belongs to the identity
390390
/// storage owner, which must derive and verify the key before any persistence.
391-
Future<Map<String, dynamic>> currentIdentity() =>
392-
post('/v1/buzz/nostr-identities/current', {});
391+
///
392+
/// [signer] is the key a community is asking for, when that may not be the
393+
/// active member's: a bad reply is then reported only while reports name it.
394+
Future<Map<String, dynamic>> currentIdentity({String? signer}) =>
395+
post('/v1/buzz/nostr-identities/current', {}, signer: signer);
393396

394397
/// Saves a successfully admitted destination to the same account, even if
395398
/// another session operation was queued before this request runs.
@@ -425,12 +428,14 @@ class BuilderlabAccountClient {
425428
String path,
426429
Map<String, dynamic> body, {
427430
String? expectedSubject,
428-
}) => _post(path, body, expectedSubject: expectedSubject);
431+
String? signer,
432+
}) => _post(path, body, expectedSubject: expectedSubject, signer: signer);
429433

430434
Future<Map<String, dynamic>> _post(
431435
String path,
432436
Map<String, dynamic> body, {
433437
String? signingKey,
438+
String? signer,
434439
String? expectedSubject,
435440
}) {
436441
if (!RegExp(r'^/v1/buzz(/[a-z0-9_-]+)+$').hasMatch(path) &&
@@ -455,6 +460,7 @@ class BuilderlabAccountClient {
455460
credential: credential,
456461
body: body,
457462
signingKey: signingKey,
463+
signer: signer,
458464
);
459465
} on AccountFailure catch (failure) {
460466
if (failure == AccountFailure.denied &&
@@ -503,6 +509,7 @@ class BuilderlabAccountClient {
503509
}
504510
final pubkey = AccountIdentityStore.publicKeyForNsec(nsec);
505511
// Never creates a binding: the key must already belong to the account.
512+
// Gated on [pubkey]: the community may be removed, not the active one.
506513
final current = await _request(
507514
'/v1/buzz/nostr-identities/current',
508515
credential: signedOut.credential,

‎mobile/lib/shared/hosted/hosted_relay_auth.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ class HostedRelayAuth extends ChangeNotifier {
390390
return _token(value, pubkey).token;
391391
}
392392
final session = account.account!;
393-
final current = await account.currentIdentity();
393+
final current = await account.currentIdentity(signer: pubkey);
394394
if (!identical(session, account.account)) throw AccountFailure.denied;
395395
final identity = current['identity'];
396396
if (current['error'] != null) throw AccountFailure.invalidResponse;

‎mobile/lib/shared/relay/relay_closed_policy.dart‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ class RelayInvalidRequest implements Exception {
5555
String toString() => 'RelayInvalidRequest';
5656
}
5757

58+
/// How the relay begins a refusal for a timestamp outside its clock window.
59+
/// It has one wording per kind of event: ordinary events, canvas events,
60+
/// relay-admin, moderation and identity archive commands (followed by the
61+
/// timing details), leave requests (likewise) and observer frames. Matched from
62+
/// the start of the reason, so a reason about something else that mentions a
63+
/// timestamp, or echoes a value that does, is still reported.
64+
const _clockWindowRefusals = [
65+
'invalid: event timestamp too far from server time',
66+
'invalid: canvas event timestamp too far in the future',
67+
'invalid: event timestamp out of range',
68+
'invalid: leave request timestamp out of range',
69+
'invalid: observer frame timestamp outside',
70+
];
71+
5872
/// Whether [message], a relay `OK` or `CLOSED` reason, says the app sent
5973
/// something invalid. Refusals the app handles as expected outcomes, an
6074
/// archived channel, an Unarchive of a channel that is no longer archived, an
@@ -75,7 +89,7 @@ bool isInvalidRequestRejection(String message) {
7589
'invalid: only members can add people in this channel',
7690
) &&
7791
!normalized.startsWith('invalid: cannot remove the last owner') &&
78-
!normalized.contains('timestamp too far from server time');
92+
!_clockWindowRefusals.any(normalized.startsWith);
7993
}
8094

8195
/// Explicit access refusal; cached private history must not mask this error.

‎mobile/test/features/onboarding/known_communities_provider_test.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class _Account extends BuilderlabAccountClient {
3939
String path,
4040
Map<String, dynamic> body, {
4141
String? expectedSubject,
42+
String? signer,
4243
}) async {
4344
requested.add(path);
4445
if (subject.value == 'first') return firstResponse.future;

‎mobile/test/features/onboarding/known_community_sync_integration_test.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class _Account extends BuilderlabAccountClient {
4747
String path,
4848
Map<String, dynamic> body, {
4949
String? expectedSubject,
50+
String? signer,
5051
}) async {
5152
expectSync(expectedSubject, 'owner');
5253
if (path.endsWith('/list')) {

‎mobile/test/features/onboarding/known_community_sync_test.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class _Account extends BuilderlabAccountClient {
3636
String path,
3737
Map<String, dynamic> body, {
3838
String? expectedSubject,
39+
String? signer,
3940
}) async {
4041
if (account?.subject != expectedSubject) {
4142
throw AccountFailure.differentAccount;

‎mobile/test/helpers/hosted_communities_fixture.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class WidgetDiscoveryAccount extends BuilderlabAccountClient {
6868
String path,
6969
Map<String, dynamic> body, {
7070
String? expectedSubject,
71+
String? signer,
7172
}) async {
7273
fixture.requests.add(path);
7374
if (path.endsWith('/current')) {

0 commit comments

Comments
 (0)