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.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
fix(mobile): sign in first when scanning a computer's sign-in QR
tonyd9318afa37ff
diff --git a/mobile/lib/phone/approve_sign_in.dart b/mobile/lib/phone/approve_sign_in.dartindex 2d0908f66..ffcce24da 100644--- a/mobile/lib/phone/approve_sign_in.dart+++ b/mobile/lib/phone/approve_sign_in.dart@@ -28,17 +28,34 @@ Future<void> signInAComputer(BuildContext context, AppNotifier notifier, {Widget}/// The prompt and the answer for one scanned [code].-Future<void> approveComputerSignIn(BuildContext context, AppNotifier notifier, SignInCode code) async {+///+/// [scanAgain], when given, follows the reason a code could not be read: where to scan a fresh one.+/// For a code scanned a while before it is answered — on the welcome screen, before the sign-in this+/// phone needed first — when the camera that read it is long gone.+Future<void> approveComputerSignIn(+ BuildContext context,+ AppNotifier notifier,+ SignInCode code, {+ String? scanAgain,+}) async {final Map<String, dynamic> asking;try {asking = await notifier.api.signInLookup(code.code);} catch (error) {- if (context.mounted) await _tell(context, "Couldn't read that code", '$error');+ if (context.mounted) {+ await _tell(context, "Couldn't read that code", scanAgain == null ? '$error' : '$error $scanAgain');+ }return;}if (!context.mounted) return;if (asking['status'] != 'pending') {- await _tell(context, 'Already answered', 'That code was already answered. Scan the new one.');+ await _tell(+ context,+ 'Already answered',+ scanAgain == null+ ? 'That code was already answered. Scan the new one.'+ : 'That code was already answered. $scanAgain',+ );return;}final answer = await showAppDialog<_Answer>(diff --git a/mobile/lib/phone/phone_shell.dart b/mobile/lib/phone/phone_shell.dartindex 099cbfd30..5ac40df23 100644--- a/mobile/lib/phone/phone_shell.dart+++ b/mobile/lib/phone/phone_shell.dart@@ -6,10 +6,12 @@ import 'package:harness_mobile/state/app_state.dart';import '../p2p/phone_terminal_p2p.dart';import 'agent_home.dart';+import 'approve_sign_in.dart';import 'devices_page.dart';import 'exit_app.dart';import 'new_device_banner.dart';import 'phone_shell_scope.dart';+import 'welcome/connect_code.dart';/// The signed-in phone app: one page stack, rooted in [AgentHome] — the terminal the phone opens/// on and stays on. Machines and Settings are pages pushed onto it, from the terminal's menu and@@ -54,6 +56,13 @@ class _PhoneShellState extends State<PhoneShell> with WidgetsBindingObserver {_openNoticedDevice();});}+ // A computer's sign-in QR read before this phone was signed in: the sign-in was the way to+ // answer it, and this shell is the first thing up after it — see [_approveHeldComputerSignIn].+ if (widget.notifier.pendingComputerSignIn != null) {+ WidgetsBinding.instance.addPostFrameCallback(+ (_) => _approveHeldComputerSignIn(),+ );+ }// ⚠️ Not asked here any more. Signed in with nothing on screen, "Harness would like to send you// notifications" is a question with no reason attached. It is asked the first time a harness// is on screen — see `FocusHints.onDone` in `terminal_page.dart`.@@ -128,6 +137,27 @@ class _PhoneShellState extends State<PhoneShell> with WidgetsBindingObserver {setState(() => _canPop = false);}+ /// The computer whose "Scan with your phone" QR the welcome screen read before this phone was+ /// signed in ([AppNotifier.pendingComputerSignIn]): the approval it asked for, now that it can be+ /// given. Spent here whatever the answer — a code is one computer's sign-in, once.+ ///+ /// From this shell's context and not a page's: the home page under it is still settling (the+ /// machines loading, "Waiting for your computer…" swapping with "Looking for your machines…") and+ /// a page swapped out mid-answer would drop it. The dialog stands on the root navigator either way.+ void _approveHeldComputerSignIn() {+ final code = widget.notifier.pendingComputerSignIn;+ if (!mounted || code == null) return;+ widget.notifier.pendingComputerSignIn = null;+ unawaited(+ approveComputerSignIn(+ context,+ widget.notifier,+ SignInCode(code),+ scanAgain: 'Show it again on the computer, then scan it from Settings ▸ Sign in a computer.',+ ),+ );+ }+/// A tapped "agent finished" notice: that agent, as the home screen — the/// dial's drawer row opening its agent.void _openNoticedAgent() {diff --git a/mobile/lib/phone/welcome/phone_welcome.dart b/mobile/lib/phone/welcome/phone_welcome.dartindex 5d980898b..6a4516005 100644--- a/mobile/lib/phone/welcome/phone_welcome.dart+++ b/mobile/lib/phone/welcome/phone_welcome.dart@@ -85,7 +85,8 @@ class PhoneWelcome extends StatefulWidget {State<PhoneWelcome> createState() => _PhoneWelcomeState();}-enum _Step { hello, setUp, scan, email, code }+/// [signInFirst]: a computer's own sign-in QR was scanned — see [_SignInFirst].+enum _Step { hello, setUp, scan, signInFirst, email, code }class _PhoneWelcomeState extends State<PhoneWelcome> {_Step _step = _Step.hello;@@ -99,23 +100,12 @@ class _PhoneWelcomeState extends State<PhoneWelcome> {/// Between a scan and the session it signs in: the scan page says so.bool _signingInWithScan = false;- /// The scan read a computer's own sign-in QR — "Scan with your phone" on its sign-in sheet, the- /// button someone adding a phone is most likely to press there. It needs a phone that is signed in- /// already, so the scan page says what to do instead. Gone when the step is left.- bool _readComputerSignIn = false;-/// Under the scan page's hint. A desktop app opens signed out (its guest mode), and its Add Phone/// then shows no code, only "Sign in to add your phone." — with no way to sign in from there.static const _signInThereFirst ='Does it say “Sign in to add your phone”? Sign in on the computer first ''(Settings ▸ Account, with Google or Apple), then open Add Phone… again.';- /// In its place once a computer's sign-in QR is read ([_readComputerSignIn]).- static const _notThatCode =- 'That code signs a computer in from a phone that is already signed in. '- 'On the computer, choose Continue with Google or Apple instead, then scan the code in '- 'Add Phone….';-String? _error;int _resendIn = 0;Timer? _resendTimer;@@ -181,12 +171,18 @@ class _PhoneWelcomeState extends State<PhoneWelcome> {void _go(_Step step) {// Leaving for another way in takes the account's page with it: whatever it came back with- // would sign the phone in behind the step the person chose instead.- if (step != _Step.hello) widget.notifier.cancelProviderSignIn();+ // would sign the phone in behind the step the person chose instead. Any step that is left — two+ // of them offer the accounts now, the first screen and [_SignInFirst].+ if (step != _step) widget.notifier.cancelProviderSignIn();+ // A computer's sign-in code is held only on the way to the sign-in it asked for — its own step,+ // and the email and code it may send the person to. Back at the start, at set-up or at the+ // camera, it is let go: a sign-in from there must not end in a computer to approve.+ if (step == _Step.hello || step == _Step.setUp || step == _Step.scan) {+ widget.notifier.pendingComputerSignIn = null;+ }setState(() {_step = step;_error = null;- _readComputerSignIn = false;});if (step == _Step.email) _emailFocus.requestFocus();if (step == _Step.code) _codeFocus.requestFocus();@@ -257,11 +253,29 @@ class _PhoneWelcomeState extends State<PhoneWelcome> {});}+ /// Where back goes from [step] — the system's back and each page's own `‹ Back` alike.+ _Step _backFrom(_Step step) => switch (step) {+ _Step.code => _Step.email,+ _Step.email when _forComputer => _Step.signInFirst,+ _Step.signInFirst => _Step.scan,+ _ => _Step.hello,+ };++ /// A computer's sign-in code is waiting on this sign-in ([AppNotifier.pendingComputerSignIn]).+ bool get _forComputer => widget.notifier.pendingComputerSignIn != null;+@overrideWidget build(BuildContext context) {AppTheme.watch(context);final tty = Tty.of(context);final signingIn = widget.notifier.signingIn;+ // Past the page and the exchange, the session is saved and the machines are on their way:+ // nothing is waited on in the browser any more, and nothing to cancel.+ final entering =+ signingIn && widget.notifier.status == AppStatus.bootstrapping;+ final onProvider = signingIn+ ? null+ : (SignInProvider provider) => unawaited(_signInWith(provider));return PopScope(// The first screen lets the press go to the system — except on Android, which asks before// the app is left ([confirmExitApp]) and so keeps it here too.@@ -272,7 +286,7 @@ class _PhoneWelcomeState extends State<PhoneWelcome> {unawaited(confirmExitApp(context));return;}- _go(_step == _Step.code ? _Step.email : _Step.hello);+ _go(_backFrom(_step));},child: Scaffold(backgroundColor: tty.ground,@@ -284,16 +298,10 @@ class _PhoneWelcomeState extends State<PhoneWelcome> {onSample: widget.onTrySample == null? null: () => unawaited(_trySample()),- onProvider: signingIn- ? null- : (provider) => unawaited(_signInWith(provider)),+ onProvider: onProvider,waitingOn: widget.notifier.signInProvider,signedOutReason: widget.notifier.signedOutReason,- // Past the page and the exchange, the session is saved and the machines are on- // their way: nothing is waited on in the browser any more, and nothing to cancel.- entering:- signingIn &&- widget.notifier.status == AppStatus.bootstrapping,+ entering: entering,onCancel: widget.notifier.cancelProviderSignIn,error: _error,),@@ -308,21 +316,37 @@ class _PhoneWelcomeState extends State<PhoneWelcome> {_Step.scan => ScanToConnectPage(camera: widget.scanCamera,onCode: (code) => unawaited(_onScanned(code)),- // Read, not ignored: a phone pointed at a computer's sign-in QR is a person who chose- // the wrong button there, and nothing happening was all they used to get.- onSignInCode: (_) => setState(() => _readComputerSignIn = true),- signInCodeEndsScan: false,- note: _readComputerSignIn ? _notThatCode : _signInThereFirst,- noteWarns: _readComputerSignIn,+ // Taken, not ignored: a phone pointed at a computer's sign-in QR has a person behind it+ // who chose "Scan with your phone" there — nothing happening was all they used to get.+ // Held for the sign-in this phone needs before it can approve that computer.+ onSignInCode: (code) {+ widget.notifier.pendingComputerSignIn = code.code;+ _go(_Step.signInFirst);+ },+ note: _signInThereFirst,signingIn: _signingInWithScan,onUseEmail: () => _go(_Step.email),onBack: () => _go(_Step.hello),),+ _Step.signInFirst => _SignInFirst(+ onBack: () => _go(_backFrom(_Step.signInFirst)),+ onProvider: onProvider,+ waitingOn: widget.notifier.signInProvider,+ entering: entering,+ onCancel: widget.notifier.cancelProviderSignIn,+ onUseEmail: () => _go(_Step.email),+ error: _error,+ ),_Step.email => _Form(- onBack: () => _go(_Step.hello),+ onBack: () => _go(_backFrom(_Step.email)),title: 'Your email',- lines: const [- 'The one Harness on your computer is signed in with. We’ll send you a 4-digit code.',+ lines: [+ _forComputer+ // The computer is not signed in yet: the account is chosen here, for both.+ ? 'The account to sign this phone and your computer in with. We’ll send you a '+ '4-digit code.'+ : 'The one Harness on your computer is signed in with. We’ll send you a '+ '4-digit code.',],field: TtyField(key: const Key('welcome-email'),@@ -493,38 +517,13 @@ class _Hello extends StatelessWidget {const SizedBox(height: 18),const _OrDivider(label: 'or sign in with'),const SizedBox(height: 14),- for (final provider in SignInProvider.values) ...[- if (provider != SignInProvider.values.first)- const SizedBox(height: 10),- SignInProviderButton(- provider: provider,- onPressed: onProvider == null- ? null- : () => onProvider!(provider),- busyLabel: provider == waitingOn- ? (entering- ? 'Signing in…'- : 'Waiting for ${provider.label}…')- : null,- ),- ],- if (error case final error?)- Padding(- padding: const EdgeInsets.only(top: 10),- child: Text(- '✗ $error',- style: tty.style(size: TtySize.meta, color: tty.red),- ),- ),- if (waitingOn != null && !entering) ...[- const SizedBox(height: 4),- Center(- child: TtyTextButton(- label: 'Cancel',- onPressed: onCancel,- ),- ),- ],+ _ProviderSignIn(+ onProvider: onProvider,+ waitingOn: waitingOn,+ entering: entering,+ onCancel: onCancel,+ error: error,+ ),if (onSample != null) ...[const SizedBox(height: 8),TtyTextButton(label: 'Try the sample', onPressed: onSample),@@ -539,6 +538,164 @@ class _Hello extends StatelessWidget {}}+/// The two accounts the desktop and web sign in with ([SignInProviderButton]), and what goes with+/// them while one is waited on: its button saying so, Cancel under it, and why the last one did not+/// finish. On the first screen ([_Hello]) and on [_SignInFirst].+class _ProviderSignIn extends StatelessWidget {+ const _ProviderSignIn({+ required this.onProvider,+ required this.onCancel,+ this.waitingOn,+ this.entering = false,+ this.error,+ });++ /// "Continue with …" pressed. Null while any sign-in is in flight, which both buttons wait out.+ final ValueChanged<SignInProvider>? onProvider;++ /// The account whose page is up — its button says so, and [onCancel] is offered under it.+ final SignInProvider? waitingOn;++ /// The page is done with and the phone is going in: the button says so, and there is nothing+ /// left to cancel.+ final bool entering;+ final VoidCallback onCancel;++ /// Why the last account's sign-in did not finish.+ final String? error;++ @override+ Widget build(BuildContext context) {+ final tty = Tty.of(context);+ return Column(+ mainAxisSize: MainAxisSize.min,+ crossAxisAlignment: CrossAxisAlignment.stretch,+ children: [+ for (final provider in SignInProvider.values) ...[+ if (provider != SignInProvider.values.first)+ const SizedBox(height: 10),+ SignInProviderButton(+ provider: provider,+ onPressed: onProvider == null ? null : () => onProvider!(provider),+ busyLabel: provider == waitingOn+ ? (entering ? 'Signing in…' : 'Waiting for ${provider.label}…')+ : null,+ ),+ ],+ if (error case final error?)+ Padding(+ padding: const EdgeInsets.only(top: 10),+ child: Text(+ '✗ $error',+ style: tty.style(size: TtySize.meta, color: tty.red),+ ),+ ),+ if (waitingOn != null && !entering) ...[+ const SizedBox(height: 4),+ Center(+ child: TtyTextButton(label: 'Cancel', onPressed: onCancel),+ ),+ ],+ ],+ );+ }+}++/// A computer's own sign-in QR, read by a phone that is not signed in: "Scan with your phone" on the+/// desktop app's sign-in sheet — the button someone adding a phone is most likely to press there,+/// and where a new desktop app's Add Phone sends them, since it opens signed out and its Add Phone+/// only says "Sign in to add your phone.".+///+/// Only a signed-in phone can approve that code, so this one signs in first. The code is held+/// meanwhile ([AppNotifier.pendingComputerSignIn]); once in, the phone asks to approve the computer+/// (`phone_shell.dart`), and the computer then asks its person to confirm the account — this one.+/// Both end up on one account, which is all it takes for them to trust each other.+///+/// ```+/// ‹ Back+/// Sign in on this phone first+/// That code lets a signed-in phone sign your computer in. …+///+/// [G Continue with Google]+/// [ Continue with Apple ]+/// Use email instead+/// ```+class _SignInFirst extends StatelessWidget {+ const _SignInFirst({+ required this.onBack,+ required this.onProvider,+ required this.onCancel,+ required this.onUseEmail,+ this.waitingOn,+ this.entering = false,+ this.error,+ });++ final VoidCallback onBack;+ final ValueChanged<SignInProvider>? onProvider;+ final VoidCallback onCancel;+ final VoidCallback onUseEmail;+ final SignInProvider? waitingOn;+ final bool entering;+ final String? error;++ @override+ Widget build(BuildContext context) {+ final tty = Tty.of(context);+ final faint = tty.style(size: TtySize.row, color: tty.faint);+ return Column(+ crossAxisAlignment: CrossAxisAlignment.stretch,+ children: [+ Align(+ alignment: Alignment.centerLeft,+ child: TtyBackButton(onPressed: onBack),+ ),+ Expanded(+ child: ListView(+ padding: const EdgeInsets.fromLTRB(Tty.origin, 12, Tty.origin, 16),+ children: [+ // A Text and not a TtyText: at a large size the title wraps rather than being cut off.+ Text(+ 'Sign in on this phone first',+ key: const ValueKey('welcome-sign-in-first'),+ style: tty.style(size: 24, weight: FontWeight.w600),+ ),+ const SizedBox(height: 12),+ Text(+ 'That code lets a signed-in phone sign your computer in. Sign in here, and the '+ 'phone asks you to approve the computer next.',+ style: faint,+ ),+ const SizedBox(height: 6),+ Text(+ 'Leave the code on the computer’s screen until then.',+ style: faint,+ ),+ const SizedBox(height: 24),+ _ProviderSignIn(+ onProvider: onProvider,+ waitingOn: waitingOn,+ entering: entering,+ onCancel: onCancel,+ error: error,+ ),+ const SizedBox(height: 8),+ Center(+ child: TtyTextButton(+ label: 'Use email instead',+ color: tty.faint,+ // Not while an account's page is up: the email would sign in behind it.+ onPressed: onProvider == null ? null : onUseEmail,+ ),+ ),+ ],+ ),+ ),+ ],+ );+ }+}+/// One of the question's two answers: a raised row the width of the screen, its words and a `›`./// Equal weight — neither is the "primary" — because which one is right depends on the person.class _Answer extends StatelessWidget {diff --git a/mobile/lib/phone/welcome/scan_to_connect.dart b/mobile/lib/phone/welcome/scan_to_connect.dartindex e405154c3..e49875024 100644--- a/mobile/lib/phone/welcome/scan_to_connect.dart+++ b/mobile/lib/phone/welcome/scan_to_connect.dart@@ -47,8 +47,6 @@ class ScanToConnectPage extends StatefulWidget {this.title = 'Scan the code on your computer',this.hint = 'On your Mac: Harness ▸ Add Phone…',this.note,- this.noteWarns = false,- this.signInCodeEndsScan = true,});final ValueChanged<ConnectCode> onCode;@@ -56,17 +54,9 @@ class ScanToConnectPage extends StatefulWidget {/// A computer's sign-in QR ([SignInCode]) was read — offered only where the page takes one.final ValueChanged<SignInCode>? onSignInCode;- /// Whether reading a sign-in QR ends the scan, as it does where the page is pushed to fetch one.- /// False keeps the camera reading after [onSignInCode] — for the first screen, which can only say- /// why that code is not the one, and then has to read the right one. The same code still in frame- /// is answered once, not every frame.- final bool signInCodeEndsScan;-/// A line or two under the hint, in words that wrap: what to do when the computer cannot show the- /// code yet, or why the code just read is not the one. [noteWarns] draws it in the terminal's- /// yellow, for the second.+ /// code yet.final String? note;- final bool noteWarns;/// Whether an Add Phone QR ([ConnectCode]) is one this page takes.final bool acceptConnectCodes;@@ -95,10 +85,6 @@ class _ScanToConnectPageState extends State<ScanToConnectPage>/// Set once a code of ours is read: the camera keeps reporting it every frame.bool _done = false;- /// The sign-in QR last answered while the scan went on ([ScanToConnectPage.signInCodeEndsScan]- /// false) — still in frame, it is reported every frame and must not be answered every frame.- String? _answeredSignIn;-/// The camera, held here rather than left to [MobileScanner] — null when [ScanToConnectPage.camera]/// stands in for it.///@@ -186,13 +172,6 @@ class _ScanToConnectPageState extends State<ScanToConnectPage>for (final barcode in capture.barcodes) {final raw = barcode.rawValue ?? '';final signIn = widget.onSignInCode == null ? null : SignInCode.parse(raw);- if (signIn != null && !widget.signInCodeEndsScan) {- if (raw == _answeredSignIn) continue;- _answeredSignIn = raw;- HapticFeedback.mediumImpact();- widget.onSignInCode!(signIn);- continue;- }if (signIn != null) {_done = true;HapticFeedback.mediumImpact();@@ -288,18 +267,11 @@ class _ScanToConnectPageState extends State<ScanToConnectPage>const SizedBox(height: 12),Padding(padding: const EdgeInsets.symmetric(horizontal: Tty.origin),- // Wraps, unlike the hint above it: a sentence of what to do, not a label. Announced when- // it changes — it is the page's only answer to a code it will not take.- child: Semantics(- liveRegion: true,- child: Text(- note,- key: const ValueKey('scan-note'),- style: tty.style(- size: TtySize.meta,- color: widget.noteWarns ? tty.yellow : tty.faint,- ),- ),+ // Wraps, unlike the hint above it: a sentence of what to do, not a label.+ child: Text(+ note,+ key: const ValueKey('scan-note'),+ style: tty.style(size: TtySize.meta, color: tty.faint),),),],diff --git a/mobile/lib/state/app_state.dart b/mobile/lib/state/app_state.dartindex d24f06c15..286b2d3d6 100644--- a/mobile/lib/state/app_state.dart+++ b/mobile/lib/state/app_state.dart@@ -1590,6 +1590,7 @@ class AppNotifier extends ChangeNotifier {signInProvider = null;// The code was scanned into the session that just ended — see [logout].pendingPairing = null;+ pendingComputerSignIn = null;_clearDeviceNotices();_desk.reset();zoo.reset();@@ -1787,8 +1788,10 @@ class AppNotifier extends ChangeNotifier {// the next sign-in — perhaps another account's — would spend it on the first// locked machine of that id and show a pairing error where its password form// belongs. Not in [_invalidateAuthWork]: signing IN starts there too, and the- // code is set before that sign-in on purpose (`phone_welcome.dart`).+ // code is set before that sign-in on purpose (`phone_welcome.dart`). The same for a computer's+ // sign-in code: held for the sign-in it asked for, not the one after this.pendingPairing = null;+ pendingComputerSignIn = null;_closedHistory.clear();// Best-effort and fire-and-forget: local state is cleared below regardless, but the saved// session goes too, so the NEXT launch doesn't silently sign back in without ever showing the@@ -2851,6 +2854,16 @@ class AppNotifier extends ChangeNotifier {/// See `phone/welcome/connect_code.dart`. Null the rest of the time.({String machineId, String code})? pendingPairing;+ /// A computer's own sign-in QR (the bare `hnq_…` code, [SignInCode] in+ /// `phone/welcome/connect_code.dart`) read on the welcome screen by a phone not yet signed in —+ /// the desktop app's "Scan with your phone". Only a signed-in phone can approve it, so the welcome+ /// signs this one in first and holds the code across that sign-in; the signed-in shell then asks+ /// to approve it (`phone/phone_shell.dart`), and spends it. Null the rest of the time.+ ///+ /// Like [pendingPairing], set before the sign-in on purpose and so not cleared by+ /// [_invalidateAuthWork]: a sign-out drops it instead.+ String? pendingComputerSignIn;+/// The scanned code is spent (it failed, or the person chose the password): the computer's/// password form is what the home screen shows next.void dropPendingPairing() {