Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

feat(mobile): count app_start, connect_user/auto and resume_recovery

tonyfb5802004744
diff --git a/mobile/lib/app_shell.dart b/mobile/lib/app_shell.dart
index 2cf6c0a82..19b9f370c 100644
--- a/mobile/lib/app_shell.dart
+++ b/mobile/lib/app_shell.dart
@@ -1,3 +1,4 @@
+import 'measurement/launch_measurement.dart';
import 'measurement/setup_usage_choice.dart';
import 'package:flutter/material.dart';
@@ -52,6 +53,8 @@ Future<void> startHarness({
// is an offset into THIS launch. Touched before any awaited work so the origin
// is the entrypoint rather than whatever step happens to mark itself first.
StartupTrace.mark('startHarness');
+ // The same origin for the launch the account's `app_start` counts.
+ LaunchMeasurement.launched();
// ⚠️ **No keymap.** The desktop loads its keyboard configuration here, a file
// and its watchers, beside the appearance. Nothing on the phone reads a keymap
// — no key is ever matched against one — so the file a touchscreen could not
diff --git a/mobile/lib/measurement/launch_measurement.dart b/mobile/lib/measurement/launch_measurement.dart
new file mode 100644
index 000000000..21c4cb9e0
--- /dev/null
+++ b/mobile/lib/measurement/launch_measurement.dart
@@ -0,0 +1,121 @@
+import 'package:flutter/widgets.dart';
+
+import 'product_measurement.dart';
+
+/// `app_start` on the phone: a launch that came back signed in, from `startHarness` to the first
+/// computer answering — the wait a returning person sits through before there is anything to use.
+///
+/// The desktop counts its launch to a connected workspace the same way
+/// (`desktop/lib/measurement/product_runtime_health.dart`). A launch that signs in first is not one:
+/// that is the onboarding, which its own flows measure, and its wait for a computer can run hours.
+///
+/// Outcomes: `ok` when a computer answered; `unknown` when the app left the screen before one did
+/// (whatever the wait took from there was not the launch's). A launch whose computers never answer
+/// stays started, which is itself the signal.
+class LaunchMeasurement {
+ static final _launchClock = Stopwatch();
+
+ /// Starts the launch clock. Called once, from `startHarness`, before anything is awaited.
+ static void launched() {
+ if (!_launchClock.isRunning) _launchClock.start();
+ }
+
+ LaunchMeasurement(
+ this.measurement, {
+ Duration Function()? sinceLaunch,
+ bool observeLifecycle = true,
+ }) : _sinceLaunch = sinceLaunch ?? (() => _launchClock.elapsed) {
+ measurement.addListener(_choiceChanged);
+ if (observeLifecycle) {
+ final state = WidgetsBinding.instance.lifecycleState;
+ if (state != null && !_onScreen(state)) foreground(false);
+ _lifecycle = AppLifecycleListener(
+ onStateChange: (state) => foreground(_onScreen(state)),
+ );
+ }
+ }
+
+ final ProductMeasurement measurement;
+ final Duration Function() _sinceLaunch;
+ AppLifecycleListener? _lifecycle;
+ bool _disposed = false;
+
+ bool _signedIn = false, _accountSeen = false;
+
+ /// Whether this launch is one `app_start` describes; decided by the first account it sees.
+ bool _wanted = false;
+ bool _begun = false;
+
+ /// A launch starts on screen.
+ bool _foreground = true;
+
+ /// The launch left the screen before a computer answered.
+ bool _left = false;
+ Duration? _readyAt;
+ void Function(ProductOutcome)? _open;
+
+ static bool _onScreen(AppLifecycleState state) =>
+ state == AppLifecycleState.resumed || state == AppLifecycleState.inactive;
+
+ /// Signed in or not. [restored]: the launch came back signed in rather than signing in itself.
+ void account({required bool signedIn, bool restored = false}) {
+ if (signedIn && !_accountSeen) {
+ _accountSeen = true;
+ _wanted = restored;
+ }
+ if (_signedIn == signedIn) return;
+ _signedIn = signedIn;
+ if (!signedIn) {
+ // A sign-out ends the launch's story; its queue goes with the account.
+ _wanted = false;
+ _open = null;
+ }
+ _choiceChanged();
+ }
+
+ void _choiceChanged() {
+ if (_disposed || !_signedIn || !measurement.sharing) return;
+ if (!_wanted || _begun) return;
+ _begun = true;
+ // Sharing is confirmed only once the account's choice is read — often after a computer has
+ // already answered: the launch is then counted with the time it really took.
+ final readyAt = _readyAt;
+ if (_left) {
+ measurement.begin(ProductFlow.appStart, elapsed: _sinceLaunch())(
+ ProductOutcome.unknown,
+ );
+ } else if (readyAt != null) {
+ measurement.begin(ProductFlow.appStart, elapsed: readyAt)(
+ ProductOutcome.ok,
+ );
+ } else {
+ _open = measurement.begin(ProductFlow.appStart, elapsed: _sinceLaunch());
+ }
+ }
+
+ /// A computer answered. Only the first in this process ends the launch.
+ void ready() {
+ if (_readyAt != null || _left) return;
+ _readyAt = _sinceLaunch();
+ _open?.call(ProductOutcome.ok);
+ _open = null;
+ }
+
+ /// The app came on screen ([value]) or left it.
+ void foreground(bool value) {
+ if (_foreground == value) return;
+ _foreground = value;
+ if (value || _readyAt != null || _left) return;
+ _left = true;
+ _open?.call(ProductOutcome.unknown);
+ _open = null;
+ }
+
+ void dispose() {
+ if (_disposed) return;
+ _disposed = true;
+ _lifecycle?.dispose();
+ measurement.removeListener(_choiceChanged);
+ _open = null;
+ }
+}
diff --git a/mobile/lib/measurement/product_connections.dart b/mobile/lib/measurement/product_connections.dart
new file mode 100644
index 000000000..e9b883af8
--- /dev/null
+++ b/mobile/lib/measurement/product_connections.dart
@@ -0,0 +1,119 @@
+import 'dart:async';
+
+import 'product_measurement.dart';
+
+/// The phone's dials as the desktop counts them (`desktop/lib/measurement/product_connections.dart`):
+/// `connect_user` for one the person asked for ("Try again"), `connect_auto` for every other — the
+/// launch's, a dropped socket coming back. Beside `remote_connect`, which still counts every dial
+/// as before, so its figures carry on unbroken.
+///
+/// Connection keys stay in these bounded RAM maps and never reach a counter. Why a dial failed is
+/// told by its outcome alone — `failed` when the phone knows (the computer is locked or offline),
+/// `unknown` when it does not: the reasons' own counters (`connect_error_*`) are the desktop's
+/// health vocabulary, which the backend does not take from a phone yet.
+class ProductConnections {
+ ProductConnections(
+ this.measurement, {
+ this.manualDeadline = const Duration(seconds: 12),
+ }) {
+ measurement.addListener(_choiceChanged);
+ }
+
+ final ProductMeasurement measurement;
+
+ /// How long an asked-for dial may take before it counts as failed — the desktop's Connect timeout.
+ final Duration manualDeadline;
+
+ final _pending = <String, void Function(ProductOutcome)>{};
+ final _manual =
+ <String, ({void Function(ProductOutcome) finish, Timer deadline})>{};
+ int _generation = -1;
+
+ void _choiceChanged() {
+ if (!measurement.sharing ||
+ _generation != measurement.observationGeneration) {
+ clear();
+ }
+ _generation = measurement.observationGeneration;
+ }
+
+ /// The person asked to reach [key] again. [connected]: it already is, so the ask is answered.
+ /// Otherwise it ends with the first status that settles it ([status]), or fails at
+ /// [manualDeadline]. A dial already under way for [key] is the person's from here.
+ void manual(String key, {bool connected = false}) {
+ _choiceChanged();
+ if (!measurement.sharing) return;
+ if (_manual.length >= 64 && !_manual.containsKey(key)) return;
+ _pending.remove(key)?.call(ProductOutcome.cancelled);
+ _endManual(key, ProductOutcome.cancelled);
+ final finish = measurement.begin(ProductFlow.connectUser);
+ if (connected) return finish(ProductOutcome.ok);
+ late final Timer deadline;
+ deadline = Timer(manualDeadline, () {
+ if (_manual[key]?.deadline == deadline) {
+ _endManual(key, ProductOutcome.failed);
+ }
+ });
+ _manual[key] = (finish: finish, deadline: deadline);
+ }
+
+ /// [key]'s socket moved: [connecting] (a re-dial included), [connected], or neither — dropped.
+ /// [failed]: the phone knows why it is not up (locked, offline).
+ void status(
+ String key, {
+ required bool connecting,
+ required bool connected,
+ bool failed = false,
+ }) {
+ _choiceChanged();
+ if (!measurement.sharing) return;
+ if (_manual.containsKey(key)) {
+ // Asked for: up is the answer, down is a failed ask. A socket backing off between dials is
+ // still on its way, until the deadline says otherwise.
+ if (connected) {
+ _endManual(key, ProductOutcome.ok);
+ } else if (!connecting) {
+ _endManual(key, ProductOutcome.failed);
+ }
+ return;
+ }
+ if (connecting) {
+ if (_pending.length >= 64 && !_pending.containsKey(key)) return;
+ _pending.putIfAbsent(
+ key,
+ () => measurement.begin(ProductFlow.connectAuto),
+ );
+ return;
+ }
+ _pending
+ .remove(key)
+ ?.call(
+ connected
+ ? ProductOutcome.ok
+ : failed
+ ? ProductOutcome.failed
+ : ProductOutcome.unknown,
+ );
+ }
+
+ void _endManual(String key, ProductOutcome outcome) {
+ final entry = _manual.remove(key);
+ if (entry == null) return;
+ entry.deadline.cancel();
+ entry.finish(outcome);
+ }
+
+ /// Drops every open observation uncounted: a sign-out, another account, a sharing change.
+ void clear() {
+ _pending.clear();
+ for (final entry in _manual.values) {
+ entry.deadline.cancel();
+ }
+ _manual.clear();
+ }
+
+ void dispose() {
+ measurement.removeListener(_choiceChanged);
+ clear();
+ }
+}
diff --git a/mobile/lib/measurement/product_measurement.dart b/mobile/lib/measurement/product_measurement.dart
index 69aa4d2a6..64bca271a 100644
--- a/mobile/lib/measurement/product_measurement.dart
+++ b/mobile/lib/measurement/product_measurement.dart
@@ -21,6 +21,11 @@ enum ProductFlow {
viewerOpen('viewer_open'),
updateCheck('update_check'),
updateStage('update_stage'),
+ // From the backend's `HEALTH_FLOWS` (productHealthContract.ts), counted as the desktop counts them.
+ appStart('app_start'),
+ connectUser('connect_user'),
+ connectAuto('connect_auto'),
+ resumeRecovery('resume_recovery'),
computerWait('computer_wait'),
sessionOpen('session_open'),
firstMessage('first_message'),
@@ -88,6 +93,9 @@ class ProductMeasurement extends ChangeNotifier {
bool get sharing => _account != null && _enabled == true;
+ /// Moves with every account or choice change: an observation begun before one never counts after.
+ int get observationGeneration => _accountEpoch;
+
/// The account's choice has read off: what waits to be counted for it never is.
bool get declined => _account != null && _enabled == false;
bool get _setupAllowed =>
diff --git a/mobile/lib/measurement/product_resumes.dart b/mobile/lib/measurement/product_resumes.dart
new file mode 100644
index 000000000..97a73c9c0
--- /dev/null
+++ b/mobile/lib/measurement/product_resumes.dart
@@ -0,0 +1,49 @@
+import 'product_measurement.dart';
+
+/// `resume_recovery` as the desktop counts it (`desktop/lib/state/app_state.dart`, its restart
+/// attempt): from a stopped harness's resume being sent to the first turn it starts afterwards —
+/// back at work, not merely relaunched. A refusal, or a failed launch, is `failed`.
+///
+/// Keys stay in this bounded RAM map and never reach a counter.
+class ProductResumes {
+ ProductResumes(this.measurement) {
+ measurement.addListener(_choiceChanged);
+ }
+
+ final ProductMeasurement measurement;
+ final _open = <String, void Function(ProductOutcome)>{};
+ int _generation = -1;
+
+ void _choiceChanged() {
+ if (!measurement.sharing ||
+ _generation != measurement.observationGeneration) {
+ _open.clear();
+ }
+ _generation = measurement.observationGeneration;
+ }
+
+ /// A resume of [key] went to its machine. One already open for [key] was given up on.
+ void sent(String key) {
+ _choiceChanged();
+ if (!measurement.sharing) return;
+ _open.remove(key)?.call(ProductOutcome.cancelled);
+ // The oldest gives way, as on the desktop: one never heard of again is not a failure.
+ if (_open.length >= 64) {
+ _open.remove(_open.keys.first)?.call(ProductOutcome.unknown);
+ }
+ _open[key] = measurement.begin(ProductFlow.resumeRecovery);
+ }
+
+ /// The machine refused [key]'s resume, or its launch failed.
+ void refused(String key) => _open.remove(key)?.call(ProductOutcome.failed);
+
+ /// [key] started a turn — live, not a replay. Only a resume still open is ended by it.
+ void worked(String key) => _open.remove(key)?.call(ProductOutcome.ok);
+
+ void clear() => _open.clear();
+
+ void dispose() {
+ measurement.removeListener(_choiceChanged);
+ _open.clear();
+ }
+}
diff --git a/mobile/lib/state/app_state.dart b/mobile/lib/state/app_state.dart
index ea8b0275d..ba17865ec 100644
--- a/mobile/lib/state/app_state.dart
+++ b/mobile/lib/state/app_state.dart
@@ -1,7 +1,10 @@
import '../core/app_version.dart';
import '../core/test_run.dart';
+import '../measurement/launch_measurement.dart';
import '../measurement/onboarding_measurement.dart';
+import '../measurement/product_connections.dart';
import '../measurement/product_measurement.dart';
+import '../measurement/product_resumes.dart';
import '../measurement/setup_transport.dart';
import 'dart:async';
@@ -285,6 +288,15 @@ class AppNotifier extends ChangeNotifier {
late final onboardingMeasurement = OnboardingMeasurement(productMeasurement);
final _measurementConnections = <String, void Function(ProductOutcome)>{};
+ /// Each machine's dials, the person's (`connect_user`) apart from the app's own (`connect_auto`).
+ late final productConnections = ProductConnections(productMeasurement);
+
+ /// A stopped harness resumed from here, until it works again (`resume_recovery`).
+ late final productResumes = ProductResumes(productMeasurement);
+
+ /// This launch, to its first computer (`app_start`). The app's alone: none in a test or the sample.
+ LaunchMeasurement? launchMeasurement;
+
/// Says whether this device is signed in, and signs it out: [ViewerServices.login], the app's own
/// session. The name is the desktop's, where the harness CLI holds the session instead.
late final SignInClient cliLogin;
@@ -1126,6 +1138,11 @@ class AppNotifier extends ChangeNotifier {
sendSetup: (report) =>
sendSetupMeasurement(this.config.apiBaseUrl, report),
);
+ // On the stores' terms above: no layout store is a test or the sample (`demo/`), and neither is
+ // a launch of this app.
+ if (!kUnderTest && paneLayoutStore != null) {
+ launchMeasurement = LaunchMeasurement(this.productMeasurement);
+ }
addListener(_syncProductMeasurement);
unawaited(_measurementContext());
_initDeviceLog();
@@ -1168,7 +1185,15 @@ class AppNotifier extends ChangeNotifier {
choice: () => api.productUsageSettings(),
send: api.productUsageReport,
);
- if (scope == null) _measurementConnections.clear();
+ launchMeasurement?.account(
+ signedIn: scope != null,
+ restored: !signedInThisRun,
+ );
+ if (scope == null) {
+ _measurementConnections.clear();
+ productConnections.clear();
+ productResumes.clear();
+ }
}
// Connection keys already belong to the app. Only fixed outcomes reach measurement, never the key.
@@ -2335,6 +2360,15 @@ class AppNotifier extends ChangeNotifier {
settling ? ConnectionStatus.connecting : nextStatus,
ProductFlow.remoteConnect,
);
+ productConnections.status(
+ machineId,
+ connecting:
+ settling ||
+ nextStatus == ConnectionStatus.connecting ||
+ nextStatus == ConnectionStatus.reconnecting,
+ connected: nextStatus == ConnectionStatus.connected,
+ failed: machine.needsLink || machine.nodeOnline == false,
+ );
machine.connectionStatus = settling
? ConnectionStatus.connecting
: nextStatus;
@@ -2342,6 +2376,8 @@ class AppNotifier extends ChangeNotifier {
typingEvent('link ${_shortId(machineId)} → ${nextStatus.name}');
}
if (nextStatus == ConnectionStatus.connected) {
+ // The first computer to answer ends the launch.
+ launchMeasurement?.ready();
_refreshDeviceLogAfterReconnect();
machine.needsLink = false;
// Let in: a later refusal is a new story ([_redialOnceVouched], [_settleTrust]).
@@ -6738,6 +6774,11 @@ class AppNotifier extends ChangeNotifier {
// No transport — the same guard [reachAllMachines] keeps: [_conn] would build one out of a
// null pool.
if (_pool == null && connectionForTest == null) return failure;
+ // Before the dial, whose `connecting` may be reported at once: this one is the person's.
+ productConnections.manual(
+ machineId,
+ connected: state.connectionStatus == ConnectionStatus.connected,
+ );
_connectMachine(state);
_redialNow(machineId);
await _loadMachineData(state, force: true);
@@ -6932,8 +6973,12 @@ class AppNotifier extends ChangeNotifier {
final checking = _agentResumes[key];
final receipt = checking ?? _newReceiptId();
_agentResumes[key] = receipt;
- RestartAgentResult settle(String error) {
+ // Counted from the resume being sent — the desktop's rule — not from a status check on one.
+ final measured = _turnActivityKey(machine.machine.machineId, stopped.id);
+ if (checking == null) productResumes.sent(measured);
+ RestartAgentResult settle(String error, {bool refused = true}) {
_agentResumes.remove(key);
+ if (refused) productResumes.refused(measured);
return RestartAgentResult(error: error);
}
@@ -6985,7 +7030,11 @@ class AppNotifier extends ChangeNotifier {
error: 'The machine is still resuming this harness. Tap it again in a moment.',
);
case 'unavailable':
- return settle('That harness is no longer available. Search again.');
+ // Gone rather than refused: the desktop leaves its resume open for this one too.
+ return settle(
+ 'That harness is no longer available. Search again.',
+ refused: false,
+ );
case 'failed':
final failure = result['failure'];
if (failure is! Map || failure['code'] is! String) return unconfirmed;
@@ -8717,6 +8766,12 @@ class AppNotifier extends ChangeNotifier {
stats.onTurnStarted(
_turnActivityKey(machine.machine.machineId, agentId),
);
+ // Live work, not a reconnect's replay: a harness resumed from here is back.
+ if (event['replay'] != true) {
+ productResumes.worked(
+ _turnActivityKey(machine.machine.machineId, agentId),
+ );
+ }
}
} else {
final sessionId = _eventSessionId(event, payload);
@@ -9147,6 +9202,9 @@ class AppNotifier extends ChangeNotifier {
@override
void dispose() {
+ launchMeasurement?.dispose();
+ productConnections.dispose();
+ productResumes.dispose();
onboardingMeasurement.dispose();
productMeasurement.dispose();
_measurementConnections.clear();
diff --git a/mobile/test/app_state_flow_measurement_test.dart b/mobile/test/app_state_flow_measurement_test.dart
new file mode 100644
index 000000000..831bd5738
--- /dev/null
+++ b/mobile/test/app_state_flow_measurement_test.dart
@@ -0,0 +1,172 @@
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness_mobile/core/models.dart';
+import 'package:harness_mobile/measurement/product_measurement.dart';
+
+import 'viewer_app_fixture.dart';
+
+/// The flows the phone counts as the desktop does — its dials, a stopped harness resumed — as the
+/// app itself reports them, through the account's own sharing.
+void main() {
+ late ProductMeasurement usage;
+
+ setUp(() {
+ usage = ProductMeasurement(
+ surface: 'phone',
+ automatic: false,
+ sendSetup: (_) async => {'accepted': true},
+ );
+ });
+
+ /// A signed-in phone whose account shares product usage.
+ Future<ViewerRig> sharing(Map<String, List<String>> agents) async {
+ final rig = await signedInWith(agents, productMeasurement: usage);
+ addTearDown(rig.app.dispose);
+ await usage.flush();
+ expect(usage.sharing, isTrue);
+ return rig;
+ }
+
+ /// The [flow] counters the rig's account was sent, summed across reports.
+ Future<Map<String, int>> sent(ViewerRig rig, String flow) async {
+ await usage.flush();
+ await usage.flush();
+ final total = <String, int>{};
+ for (final report in rig.api.usageReports) {
+ (report['counters'] as Map).forEach((metric, value) {
+ if ('$metric'.startsWith('ui_phone_${flow}_')) {
+ total[metric as String] = (total[metric] ?? 0) + (value as int);
+ }
+ });
+ }
+ return total;
+ }
+
+ group('dials', () {
+ test('a socket coming back by itself is the app\'s own', () async {
+ final rig = await sharing({'m': []});
+ rig.app.connectionStatusForTest('m', ConnectionStatus.reconnecting);
+ rig.app.connectionStatusForTest('m', ConnectionStatus.connecting);
+ rig.app.connectionStatusForTest('m', ConnectionStatus.connected);
+
+ expect(
+ await sent(rig, 'connect_auto'),
+ allOf([
+ containsPair('ui_phone_connect_auto_started', 1),
+ containsPair('ui_phone_connect_auto_ok', 1),
+ ]),
+ );
+ expect(await sent(rig, 'connect_user'), isEmpty);
+ // Still counted as before, beside the split.
+ expect(
+ await sent(rig, 'remote_connect'),
+ containsPair('ui_phone_remote_connect_ok', 1),
+ );
+ });
+
+ test('Try again on a dropped computer is the person\'s', () async {
+ final rig = await sharing({'m': []});
+ rig.app.connectionStatusForTest('m', ConnectionStatus.disconnected);
+
+ final retry = rig.app.retryMachine('m');
+ await settle();
+ rig.app.connectionStatusForTest('m', ConnectionStatus.connecting);
+ rig.app.connectionStatusForTest('m', ConnectionStatus.connected);
+ await retry;
+
+ expect(
+ await sent(rig, 'connect_user'),
+ allOf([
+ containsPair('ui_phone_connect_user_started', 1),
+ containsPair('ui_phone_connect_user_ok', 1),
+ ]),
+ );
+ expect(await sent(rig, 'connect_auto'), isEmpty);
+ });
+
+ test('Try again on a computer already up is answered at once', () async {
+ final rig = await sharing({'m': []});
+ await rig.app.retryMachine('m');
+
+ expect(
+ await sent(rig, 'connect_user'),
+ containsPair('ui_phone_connect_user_ok', 1),
+ );
+ });
+ });
+
+ group('resuming a stopped harness', () {
+ Map<String, dynamic> stopped(String id) =>
+ agentJson(id, status: 'stopped', terminal: false, sessionId: 's-$id');
+
+ Future<ViewerRig> withStopped() async {
+ final rig = await sharing({'m': []});
+ await push(rig, 'm', 'agent_synced', {'agent': stopped('a')});
+ return rig;
+ }
+
+ test(
+ 'is recovered by its first live turn, not by a replayed one',
+ () async {
+ final rig = await withStopped();
+ rig.conn('m').answers['agent_resume'] = (p) => {
+ 'creationId': p['creationId'],
+ 'state': 'created',
+ 'agent': agentJson('a', sessionId: 's-a'),
+ };
+
+ expect((await rig.app.resumeAgent('m', 'a')).error, isNull);
+ await push(
+ rig,
+ 'm',
+ 'turn_started',
+ {'agentId': 'a'},
+ {'replay': true},
+ );
+ expect(await sent(rig, 'resume_recovery'), {
+ 'ui_phone_resume_recovery_started': 1,
+ });
+
+ await push(rig, 'm', 'turn_started', {'agentId': 'a'});
+ expect(
+ await sent(rig, 'resume_recovery'),
+ containsPair('ui_phone_resume_recovery_ok', 1),
+ );
+ },
+ );
+
+ test(
+ 'a refusal is failed, and a status check is no second resume',
+ () async {
+ final rig = await withStopped();
+ rig.conn('m').answers['agent_resume'] = (_) =>
+ throw refusal('AGENT_BUSY');
+ await rig.app.resumeAgent('m', 'a');
+
+ expect(
+ await sent(rig, 'resume_recovery'),
+ allOf([
+ containsPair('ui_phone_resume_recovery_started', 1),
+ containsPair('ui_phone_resume_recovery_failed', 1),
+ ]),
+ );
+
+ // A lost reply is checked on the next tap, under the same receipt: not counted again.
+ rig.conn('m').answers['agent_resume'] = (_) =>
+ throw refusal('INTERNAL');
+ await rig.app.resumeAgent('m', 'a');
+ rig.conn('m').answers['agent_create_status'] = (p) => {
+ 'creationId': p['creationId'],
+ 'state': 'pending',
+ };
+ await rig.app.resumeAgent('m', 'a');
+ expect(
+ (await sent(
+ rig,
+ 'resume_recovery',
+ ))['ui_phone_resume_recovery_started'],
+ 2,
+ );
+ },
+ );
+ });
+}
diff --git a/mobile/test/launch_measurement_test.dart b/mobile/test/launch_measurement_test.dart
new file mode 100644
index 000000000..146f2f14a
--- /dev/null
+++ b/mobile/test/launch_measurement_test.dart
@@ -0,0 +1,130 @@
+import 'dart:convert';
+
+import 'package:flutter_test/flutter_test.dart';
+
+import 'package:harness_mobile/measurement/launch_measurement.dart';
+import 'package:harness_mobile/measurement/product_measurement.dart';
+
+void main() {
+ late ProductMeasurement usage;
+ late LaunchMeasurement launch;
+ late List<Map<String, dynamic>> reports;
+ late Duration clock;
+
+ setUp(() {
+ reports = [];
+ clock = Duration.zero;
+ usage = ProductMeasurement(
+ surface: 'phone',
+ automatic: false,
+ now: () => DateTime.utc(2026, 10, 16, 12),
+ sendSetup: (_) async => {'accepted': true},
+ );
+ launch = LaunchMeasurement(
+ usage,
+ observeLifecycle: false,
+ sinceLaunch: () => clock,
+ );
+ });
+ tearDown(() {
+ launch.dispose();
+ usage.dispose();
+ });
+
+ /// Signed in; the account's sharing choice is read, which is when counting can start.
+ Future<void> signIn({bool restored = true}) async {
+ usage.account(
+ 'PRIVATE_ACCOUNT',
+ choice: () async => {'enabled': true, 'revision': 0},
+ send: (r) async {
+ reports.add(jsonDecode(jsonEncode(r)));
+ return {'accepted': true};
+ },
+ );
+ launch.account(signedIn: true, restored: restored);
+ await usage.flush();
+ }
+
+ Future<Map<String, int>> sent() async {
+ await usage.flush();
+ await usage.flush();
+ final total = <String, int>{};
+ for (final report in reports) {
+ (report['counters'] as Map).forEach((metric, value) {
+ total[metric as String] = (total[metric] ?? 0) + (value as int);
+ });
+ }
+ return total;
+ }
+
+ test('a computer answering before sharing is read is counted with the time it took', () async {
+ clock = const Duration(seconds: 4);
+ launch.ready();
+ clock = const Duration(seconds: 30);
+ launch.ready();
+ await signIn();
+ expect(await sent(), {
+ 'ui_phone_app_start_started': 1,
+ 'ui_phone_app_start_ok': 1,
+ 'ui_phone_app_start_1s_to_10s': 1,
+ });
+ });
+
+ test(
+ 'a computer answering after sharing is read ends the launch once',
+ () async {
+ clock = const Duration(seconds: 2);
+ await signIn();
+ launch.ready();
+ launch.ready();
+ final counted = await sent();
+ expect(counted['ui_phone_app_start_started'], 1);
+ expect(counted['ui_phone_app_start_ok'], 1);
+ // Begun two seconds into the launch: the bucket counts from the launch, not from sharing.
+ expect(counted['ui_phone_app_start_1s_to_10s'], 1);
+ },
+ );
+
+ test('leaving the screen before a computer answered is unknown, whenever it is counted', () async {
+ launch.foreground(false);
+ launch.foreground(true);
+ launch.ready();
+ await signIn();
+ final counted = await sent();
+ expect(counted['ui_phone_app_start_unknown'], 1);
+ expect(counted, isNot(contains('ui_phone_app_start_ok')));
+ });
+
+ test(
+ 'leaving the screen while waiting ends the open launch as unknown',
+ () async {
+ await signIn();
+ launch.foreground(false);
+ launch.ready();
+ final counted = await sent();
+ expect(counted['ui_phone_app_start_started'], 1);
+ expect(counted['ui_phone_app_start_unknown'], 1);
+ expect(counted, isNot(contains('ui_phone_app_start_ok')));
+ },
+ );
+
+ test('a launch that signs in is the onboarding\'s, not one', () async {
+ launch.ready();
+ await signIn(restored: false);
+ expect(
+ (await sent()).keys.where((metric) => metric.contains('app_start')),
+ isEmpty,
+ );
+ });
+
+ test('a sign-out before sharing is read leaves no launch to count', () async {
+ launch.account(signedIn: true, restored: true);
+ launch.account(signedIn: false);
+ launch.ready();
+ await signIn(restored: false);
+ expect(
+ (await sent()).keys.where((metric) => metric.contains('app_start')),
+ isEmpty,
+ );
+ });
+}
diff --git a/mobile/test/onboarding_measurement_test.dart b/mobile/test/onboarding_measurement_test.dart
index 79790cfdc..30912953c 100644
--- a/mobile/test/onboarding_measurement_test.dart
+++ b/mobile/test/onboarding_measurement_test.dart
@@ -352,8 +352,12 @@ void main() {
});
test('every flow is in the backend vocabulary', () {
- final vocabulary = File('../backend/src/types/productUsage.ts')
- .readAsStringSync();
+ // `PRODUCT_USAGE_FLOWS` spreads the health contract's `HEALTH_FLOWS` (app_start,
+ // connect_user, connect_auto, resume_recovery) into its own.
+ final vocabulary = [
+ '../backend/src/types/productUsage.ts',
+ '../backend/src/types/productHealthContract.ts',
+ ].map((path) => File(path).readAsStringSync()).join('\n');
for (final flow in ProductFlow.values) {
expect(vocabulary, contains("'${flow.wire}'"), reason: flow.wire);
}
diff --git a/mobile/test/product_connections_test.dart b/mobile/test/product_connections_test.dart
new file mode 100644
index 000000000..c53654277
--- /dev/null
+++ b/mobile/test/product_connections_test.dart
@@ -0,0 +1,180 @@
+import 'dart:convert';
+
+import 'package:flutter_test/flutter_test.dart';
+
+import 'package:harness_mobile/measurement/product_connections.dart';
+import 'package:harness_mobile/measurement/product_measurement.dart';
+
+void main() {
+ late ProductMeasurement usage;
+ late ProductConnections connections;
+ late List<Map<String, dynamic>> reports;
+ late Map<String, dynamic> choice;
+
+ setUp(() {
+ reports = [];
+ choice = {'enabled': true, 'revision': 0};
+ usage = ProductMeasurement(
+ surface: 'phone',
+ automatic: false,
+ now: () => DateTime.utc(2026, 10, 16, 12),
+ sendSetup: (_) async => {'accepted': true},
+ );
+ connections = ProductConnections(
+ usage,
+ manualDeadline: const Duration(milliseconds: 1),
+ );
+ });
+ tearDown(() {
+ connections.dispose();
+ usage.dispose();
+ });
+
+ Future<void> signIn() async {
+ usage.account(
+ 'PRIVATE_ACCOUNT',
+ choice: () async => choice,
+ send: (r) async {
+ reports.add(jsonDecode(jsonEncode(r)));
+ return {'accepted': true};
+ },
+ );
+ await usage.flush();
+ }
+
+ /// The connect_* counters sent, summed across reports.
+ Future<Map<String, int>> sent() async {
+ await usage.flush();
+ await usage.flush();
+ final total = <String, int>{};
+ for (final report in reports) {
+ (report['counters'] as Map).forEach((metric, value) {
+ if ('$metric'.contains('_connect_')) {
+ total[metric as String] = (total[metric] ?? 0) + (value as int);
+ }
+ });
+ }
+ return total;
+ }
+
+ void dial(
+ String key, {
+ bool up = false,
+ bool down = false,
+ bool failed = false,
+ }) => connections.status(
+ key,
+ connecting: !up && !down,
+ connected: up,
+ failed: failed,
+ );
+
+ test(
+ 'the app\'s own dial is one episode, however many re-dials it takes',
+ () async {
+ await signIn();
+ dial('m');
+ dial('m');
+ dial('m');
+ dial('m', up: true);
+ expect(await sent(), {
+ 'ui_phone_connect_auto_started': 1,
+ 'ui_phone_connect_auto_ok': 1,
+ 'ui_phone_connect_auto_under_1s': 1,
+ });
+ },
+ );
+
+ test(
+ 'a dropped dial is failed when the phone knows why, unknown when not',
+ () async {
+ await signIn();
+ dial('locked');
+ dial('locked', down: true, failed: true);
+ dial('lost');
+ dial('lost', down: true);
+ final counted = await sent();
+ expect(counted['ui_phone_connect_auto_started'], 2);
+ expect(counted['ui_phone_connect_auto_failed'], 1);
+ expect(counted['ui_phone_connect_auto_unknown'], 1);
+ },
+ );
+
+ test('Try again is the person\'s, and takes over a dial under way', () async {
+ await signIn();
+ dial('m');
+ connections.manual('m');
+ dial('m');
+ dial('m', up: true);
+ final counted = await sent();
+ expect(counted['ui_phone_connect_auto_cancelled'], 1);
+ expect(counted['ui_phone_connect_user_started'], 1);
+ expect(counted['ui_phone_connect_user_ok'], 1);
+ expect(counted, isNot(contains('ui_phone_connect_auto_ok')));
+ });
+
+ test('Try again on a computer already up is answered at once', () async {
+ await signIn();
+ connections.manual('m', connected: true);
+ expect(await sent(), {
+ 'ui_phone_connect_user_started': 1,
+ 'ui_phone_connect_user_ok': 1,
+ 'ui_phone_connect_user_under_1s': 1,
+ });
+ });
+
+ test('Try again that drops, or never answers, is failed', () async {
+ await signIn();
+ connections.manual('dropped');
+ dial('dropped', down: true);
+ connections.manual('silent');
+ dial('silent');
+ await Future<void>.delayed(const Duration(milliseconds: 20));
+ // Settled by its deadline: the late answer is no second outcome.
+ dial('silent', up: true);
+ final counted = await sent();
+ expect(counted['ui_phone_connect_user_started'], 2);
+ expect(counted['ui_phone_connect_user_failed'], 2);
+ expect(counted, isNot(contains('ui_phone_connect_user_ok')));
+ });
+
+ test('a second Try again ends the first as cancelled', () async {
+ await signIn();
+ connections.manual('m');
+ connections.manual('m');
+ dial('m', up: true);
+ final counted = await sent();
+ expect(counted['ui_phone_connect_user_started'], 2);
+ expect(counted['ui_phone_connect_user_cancelled'], 1);
+ expect(counted['ui_phone_connect_user_ok'], 1);
+ });
+
+ test(
+ 'nothing is counted unshared, and a choice change drops what was open',
+ () async {
+ dial('m');
+ connections.manual('n');
+ await signIn();
+ dial('m', up: true);
+ expect(await sent(), isEmpty);
+
+ dial('m');
+ choice = {'enabled': true, 'revision': 1};
+ usage.savedChoice(choice);
+ dial('m', up: true);
+ expect(await sent(), isEmpty, reason: 'begun under the old choice');
+
+ dial('m');
+ dial('m', up: true);
+ expect(await sent(), containsPair('ui_phone_connect_auto_ok', 1));
+ },
+ );
+
+ test('no connection key ever reaches a report', () async {
+ await signIn();
+ dial('PRIVATE_MACHINE');
+ dial('PRIVATE_MACHINE', up: true);
+ await sent();
+ expect(jsonEncode(reports), isNot(contains('PRIVATE')));
+ });
+}
diff --git a/mobile/test/product_resumes_test.dart b/mobile/test/product_resumes_test.dart
new file mode 100644
index 000000000..8b2d49e26
--- /dev/null
+++ b/mobile/test/product_resumes_test.dart
@@ -0,0 +1,110 @@
+import 'dart:convert';
+
+import 'package:flutter_test/flutter_test.dart';
+
+import 'package:harness_mobile/measurement/product_measurement.dart';
+import 'package:harness_mobile/measurement/product_resumes.dart';
+
+void main() {
+ late ProductMeasurement usage;
+ late ProductResumes resumes;
+ late List<Map<String, dynamic>> reports;
+ late Map<String, dynamic> choice;
+
+ setUp(() {
+ reports = [];
+ choice = {'enabled': true, 'revision': 0};
+ usage = ProductMeasurement(
+ surface: 'phone',
+ automatic: false,
+ now: () => DateTime.utc(2026, 10, 16, 12),
+ sendSetup: (_) async => {'accepted': true},
+ );
+ resumes = ProductResumes(usage);
+ });
+ tearDown(() {
+ resumes.dispose();
+ usage.dispose();
+ });
+
+ Future<void> signIn() async {
+ usage.account(
+ 'PRIVATE_ACCOUNT',
+ choice: () async => choice,
+ send: (r) async {
+ reports.add(jsonDecode(jsonEncode(r)));
+ return {'accepted': true};
+ },
+ );
+ await usage.flush();
+ }
+
+ Future<Map<String, int>> sent() async {
+ await usage.flush();
+ await usage.flush();
+ final total = <String, int>{};
+ for (final report in reports) {
+ (report['counters'] as Map).forEach((metric, value) {
+ total[metric as String] = (total[metric] ?? 0) + (value as int);
+ });
+ }
+ return total;
+ }
+
+ test('a resume is recovered by the first turn it starts', () async {
+ await signIn();
+ resumes.sent('m/a');
+ resumes.worked('m/b');
+ resumes.worked('m/a');
+ resumes.worked('m/a');
+ expect(await sent(), {
+ 'ui_phone_resume_recovery_started': 1,
+ 'ui_phone_resume_recovery_ok': 1,
+ 'ui_phone_resume_recovery_under_1s': 1,
+ });
+ });
+
+ test(
+ 'a refused resume is failed; sending again gives up the first',
+ () async {
+ await signIn();
+ resumes.sent('m/a');
+ resumes.refused('m/a');
+ resumes.sent('m/b');
+ resumes.sent('m/b');
+ resumes.worked('m/b');
+ final counted = await sent();
+ expect(counted['ui_phone_resume_recovery_started'], 3);
+ expect(counted['ui_phone_resume_recovery_failed'], 1);
+ expect(counted['ui_phone_resume_recovery_cancelled'], 1);
+ expect(counted['ui_phone_resume_recovery_ok'], 1);
+ },
+ );
+
+ test('the oldest of too many open resumes gives way as unknown', () async {
+ await signIn();
+ for (var i = 0; i < 65; i++) {
+ resumes.sent('m/$i');
+ }
+ final counted = await sent();
+ expect(counted['ui_phone_resume_recovery_unknown'], 1);
+ resumes.worked('m/0');
+ expect(
+ (await sent())['ui_phone_resume_recovery_ok'],
+ isNull,
+ reason: 'the one that gave way is not open any more',
+ );
+ });
+
+ test('nothing is counted unshared, nor across a sign-out', () async {
+ resumes.sent('m/a');
+ await signIn();
+ resumes.worked('m/a');
+ expect(await sent(), isEmpty);
+
+ resumes.sent('m/a');
+ usage.account(null);
+ resumes.worked('m/a');
+ expect(reports, isEmpty);
+ });
+}
diff --git a/mobile/test/viewer_app_fixture.dart b/mobile/test/viewer_app_fixture.dart
index e896140f6..fb150d9b0 100644
--- a/mobile/test/viewer_app_fixture.dart
+++ b/mobile/test/viewer_app_fixture.dart
@@ -13,6 +13,7 @@ import 'package:harness_mobile/core/local_key_value_store.dart';
import 'package:harness_mobile/core/machine_cache.dart';
import 'package:harness_mobile/core/models.dart';
import 'package:harness_mobile/core/snapshot_store.dart';
+import 'package:harness_mobile/measurement/product_measurement.dart';
import 'package:harness_mobile/notify/system_notices.dart';
import 'package:harness_mobile/state/app_state.dart';
import 'package:harness_mobile/state/desk_sync.dart';
@@ -151,6 +152,23 @@ class FakeApi extends ApiClient {
@override
Future<bool> postDeviceAck(DevAck ack) async => false;
+ /// The account's product-usage choice, and every report sent under it — what a test that counts
+ /// usage reads back. Never the network.
+ Map<String, dynamic> usageChoice = const {'enabled': true, 'revision': 0};
+ final List<Map<String, dynamic>> usageReports = [];
+
+ @override
+ Future<Map<String, dynamic>> productUsageSettings({bool? enabled}) async =>
+ Map.of(usageChoice);
+
+ @override
+ Future<Map<String, dynamic>> productUsageReport(
+ Map<String, dynamic> report,
+ ) async {
+ usageReports.add(jsonDecode(jsonEncode(report)) as Map<String, dynamic>);
+ return {'accepted': true};
+ }
+
/// Answers `/api/auth/me` in place of [profile] when set — held open, or
/// throwing.
Future<Map<String, dynamic>?> Function()? onProfile;
@@ -440,6 +458,7 @@ ViewerRig viewerApp({
MachineCache? machineCache,
LocalKeyValueStore? storage,
Duration turnActivityTimeout = const Duration(seconds: 12),
+ ProductMeasurement? productMeasurement,
}) {
final session = AuthSession(storage: MemoryKeyValueStore());
final viewer = FakeViewer(session);
@@ -459,6 +478,7 @@ ViewerRig viewerApp({
machineCache ??
(storage == null ? null : MachineCache(store: MemorySnapshotStore())),
turnActivityTimeout: turnActivityTimeout,
+ productMeasurement: productMeasurement,
connectionForTest: (machineId) =>
conns.putIfAbsent(machineId, ScriptedConn.new),
);
@@ -488,10 +508,12 @@ Future<ViewerRig> signedInWith(
bool noTakeover = true,
LocalKeyValueStore? storage,
Duration turnActivityTimeout = const Duration(seconds: 12),
+ ProductMeasurement? productMeasurement,
}) async {
final rig = viewerApp(
storage: storage,
turnActivityTimeout: turnActivityTimeout,
+ productMeasurement: productMeasurement,
);
rig.api.onMachines = () async => [
for (final id in agents.keys) remoteMachine(id),