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.

fix(desktop): See your phones opens Machines; no red code mismatch

tony506409a4db5e
diff --git a/desktop/lib/screens/swarm_screen.dart b/desktop/lib/screens/swarm_screen.dart
index 642e9b2c5..77ae3f721 100644
--- a/desktop/lib/screens/swarm_screen.dart
+++ b/desktop/lib/screens/swarm_screen.dart
@@ -3004,10 +3004,6 @@ class _SwarmScreenState extends State<SwarmScreen> {
// (its Continue with…) must not bring the card back once it closes, so
// the offer is dropped on the way out too.
app.dropPhoneOffer();
- // "See your phones" pops the dialog and asks for Machines, but this
- // [_dialog] is still open until the pop lands — a picker opened from
- // the callback would be refused. So it is remembered, and opened after.
- var manageDevices = false;
try {
await _dialog(
() => showAddPhoneDialog(
@@ -3015,15 +3011,17 @@ class _SwarmScreenState extends State<SwarmScreen> {
app,
keymap: _keymap,
onConnectMachine: () => unawaited(_openMachines()),
- onManageDevices: () => manageDevices = true,
+ // "See your phones": Machines, at its phones, once the dialog has
+ // gone. ⚠️ Asked for, not opened here: right after the pop this
+ // route still reads as not current until its next frame, and the
+ // picker refused to open — the link did nothing. The request waits
+ // for the route ([_maybeOpenMachinesRequest]).
+ onManageDevices: () => app.requestMachines(phones: true),
),
);
} finally {
app.dropPhoneOffer();
}
- if (manageDevices && mounted) {
- await _openMachines(phones: true, toggle: false);
- }
}
/// Settings, by section, as rows of the box: `> usage` goes straight to
diff --git a/desktop/lib/widgets/add_phone_dialog.dart b/desktop/lib/widgets/add_phone_dialog.dart
index b47614b34..b10b40ae5 100644
--- a/desktop/lib/widgets/add_phone_dialog.dart
+++ b/desktop/lib/widgets/add_phone_dialog.dart
@@ -522,10 +522,20 @@ class _AddPhoneDialogState extends State<AddPhoneDialog> {
if (!_sticky && _message != null) setState(() => _message = null);
wait = _poll;
case 'CODE_MISMATCH':
- // The phone holds the old code and the daemon has spent it. A new
- // one — and a new QR — is the only way forward.
- // Under the code, which is the new one by now: "above".
- _say("That code didn't work. Scan the new one above.", sticky: true);
+ // A phone holding another code than this one, and the daemon has
+ // spent this one: a new code — and a new QR — goes up, QUIETLY.
+ //
+ // ⚠️ That phone is almost always one already in. A scan signs the
+ // phone in by its sign-in code, and the account takes it at once
+ // (the dialog turns to "is ready." on [_deviceJoined]); the phone
+ // then also pairs by the QR's code, which reaches the daemon after
+ // that dialog stopped answering, and the daemon keeps it a minute.
+ // The next Add Phone opened in that minute hands it a fresh code,
+ // and the handshake fails. Its red "That code didn't work. Scan the
+ // new one above." read as the phone just added having failed
+ // (2026-10-13). A phone that truly scanned an old code is told so
+ // on its own screen, and joins by signing in all the same.
+ if (_message != null) setState(() => _message = null);
setState(() => _code = newPhonePairCode());
wait = _poll;
case 'RATE_LIMITED':
diff --git a/desktop/test/add_phone_dialog_test.dart b/desktop/test/add_phone_dialog_test.dart
index 97e3f2498..80e9a1eb5 100644
--- a/desktop/test/add_phone_dialog_test.dart
+++ b/desktop/test/add_phone_dialog_test.dart
@@ -694,7 +694,7 @@ void main() {
expect(find.byType(AddPhoneDialog), findsNothing);
});
- testWidgets('a code that did not match is replaced, QR and all', (
+ testWidgets('a code that did not match is replaced quietly, QR and all', (
tester,
) async {
final app = _signedInApp();
@@ -708,7 +708,10 @@ void main() {
await _codeUp(tester);
await tester.pump();
final first = daemon.codes.single;
- expect(_status(tester), "That code didn't work. Scan the new one above.");
+ // Most often a phone already in, pairing late by an earlier dialog's
+ // code: nothing for the person to do here, so nothing in red.
+ expect(find.byKey(const ValueKey('add-phone-status')), findsNothing);
+ expect(find.textContaining("didn't work"), findsNothing);
expect(_qrData(tester), isNot(contains('c=$first')));
await tester.pump(const Duration(milliseconds: 1500));
@@ -716,9 +719,8 @@ void main() {
final second = daemon.codes.last;
expect(second, isNot(first));
expect(_qrData(tester), contains('c=$second&'));
- // An instruction outlives the "no phone yet" that follows it.
await tester.pump();
- expect(_status(tester), "That code didn't work. Scan the new one above.");
+ expect(find.byKey(const ValueKey('add-phone-status')), findsNothing);
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.pump();
@@ -728,6 +730,29 @@ void main() {
expect(daemon.codes, hasLength(2), reason: 'nothing asks after closing');
});
+ testWidgets('a mismatch also takes away what an earlier answer said', (
+ tester,
+ ) async {
+ final app = _signedInApp();
+ addTearDown(app.dispose);
+ final daemon = _FakeDaemon([
+ _failed('TIMEOUT'),
+ _failed('CODE_MISMATCH'),
+ _failed('NO_INTENT'),
+ ]);
+ await _open(tester, app, daemon.call);
+ await _codeUp(tester);
+ await tester.pump();
+ expect(_status(tester), 'Your phone stopped answering. Scan again.');
+
+ await tester.pump(const Duration(milliseconds: 1500));
+ await tester.pump();
+ expect(daemon.codes, hasLength(2));
+ expect(find.byKey(const ValueKey('add-phone-status')), findsNothing);
+ await tester.sendKeyEvent(LogicalKeyboardKey.escape);
+ await tester.pump();
+ });
+
testWidgets('rate limited waits a minute before asking again', (
tester,
) async {
diff --git a/desktop/test/machine_devices_picker_test.dart b/desktop/test/machine_devices_picker_test.dart
index b9e565180..3bc6b849a 100644
--- a/desktop/test/machine_devices_picker_test.dart
+++ b/desktop/test/machine_devices_picker_test.dart
@@ -2,8 +2,12 @@
// this computer's code, and a signed-in computer that has no machine record), then PHONES & APPS.
// These moved here from Settings ▸ Your devices and Settings ▸ Profiles. The device list and every
// write are canned (support/devices_app.dart), so nothing reaches a daemon.
+import 'package:dio/dio.dart' show CancelToken;
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
+import 'package:harness/api/api_client.dart';
+import 'package:harness/auth/auth_session.dart';
+import 'package:harness/core/config.dart';
import 'package:harness/core/models.dart';
import 'package:harness/screens/swarm_screen.dart';
import 'package:harness/shared/theme/app_theme.dart' as grid;
@@ -14,12 +18,14 @@ import 'package:harness/state/machine_devices.dart';
import 'package:harness/state/swarm_search.dart' show MachineSearchSection;
import 'package:harness/viewer/device_history.dart';
import 'package:harness/widgets/desktop_chrome.dart';
+import 'package:harness/widgets/add_phone_dialog.dart' show AddPhoneDialog;
import 'package:harness/widgets/machine_picker_extras.dart';
import 'package:harness/widgets/new_device_notice.dart' show openMachines;
import 'support/devices_app.dart';
import 'support/resource_picker.dart';
import 'support/workspace_tools.dart';
+import 'swarm_screen_test.dart' show terminal;
const _localId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
const _remoteId = 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb';
@@ -56,6 +62,25 @@ class _App extends FakeDevicesApp {
}
}
+/// Add Phone's daemon and backend, canned so nothing reaches a network: no
+/// phone waiting to pair, a sign-in code for the QR, and no pending list.
+class _AddPhoneApi extends ApiClient {
+ _AddPhoneApi() : super(config: AppConfig.dev, session: AuthSession());
+
+ @override
+ Future<({int status, Map<String, dynamic> body})> pair(
+ String code, {
+ CancelToken? cancelToken,
+ }) async => (status: 409, body: <String, dynamic>{'error': 'NO_INTENT'});
+
+ @override
+ Future<({String code, Duration ttl})?> phoneSignInCode() async =>
+ (code: 'h1', ttl: const Duration(seconds: 90));
+
+ @override
+ Future<Map<String, dynamic>?> daemonDevices() async => null;
+}
+
AccountDevice _device(
String pub, {
required String label,
@@ -469,6 +494,60 @@ void main() {
expect(resourceSearch(tester).selected?.devicePub, 'phone');
});
+ testWidgets('See your phones, once Add Phone has its phone, opens Machines '
+ 'at its phones', (tester) async {
+ app
+ ..api = _AddPhoneApi()
+ ..signedIn = true
+ ..currentUser = const CurrentUserProfile(email: '[email protected]');
+ // A terminal open, as on a desk in use: closing the dialog hands focus
+ // back to it, and only then did the link fail.
+ app.add('m', 'Test host');
+ app.machineStates['m']!.agents = [
+ const Agent(
+ id: 'a0',
+ name: 'Agent 0',
+ engine: 'codex',
+ terminalAvailable: true,
+ ),
+ ];
+ app.adoptSessionForTest(terminal('a0', []));
+ await mount(tester);
+ expect(app.panes, isNotEmpty);
+ // In by Machines' own Add phone: the account has a phone already, so the
+ // tab row has no Add Phone of its own.
+ await openPicker(tester);
+ await tester.tap(find.byKey(const Key('machine-picker-add-phone')));
+ await tester.pumpAndSettle();
+ expect(find.byType(AddPhoneDialog), findsOneWidget);
+ expect(resourceScope('@'), findsNothing);
+
+ app.ownDaemonMachineIdForTest(_localId);
+ await tester.runAsync(
+ () => app.handleEventForTest(_localId, {
+ 'type': 'device_key_added',
+ 'payload': {
+ 'pub': 'new-phone',
+ 'label': 'Phat’s S23',
+ 'kind': 'viewer',
+ },
+ }),
+ );
+ await tester.pump();
+ expect(find.text('Phat’s S23 is ready.'), findsOneWidget);
+
+ // Right after the dialog's pop the workspace's route is not current yet:
+ // opening Machines then was refused, and the link did nothing.
+ await tester.tap(find.byKey(const ValueKey('add-phone-see-phones')));
+ await tester.pumpAndSettle();
+ expect(find.byType(AddPhoneDialog), findsNothing);
+ expect(resourceScope('@'), findsOneWidget);
+ expect(
+ resourceSearch(tester).selected?.devicePub,
+ isIn(['phone', 'new-phone']),
+ );
+ });
+
test('a picker that opened on its own marks nothing seen', () async {
final devices = MachineDevices(app)..markSeen = false;
app.newDevices.add(