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): sign-out and account switch forget the previous account's remote usage rows (#1848)

0xken156ddb3028f2c8
diff --git a/desktop/lib/state/app_state.dart b/desktop/lib/state/app_state.dart
index 639fb3ac2..e0882b87e 100644
--- a/desktop/lib/state/app_state.dart
+++ b/desktop/lib/state/app_state.dart
@@ -7484,6 +7484,8 @@ class AppNotifier extends ChangeNotifier {
sessionTails.clear();
shareStatus.clear();
gridPictures.clear();
+ // Pickers keep listening to this menu; only the old account's rows go.
+ _modelsMenu?.reset();
_stopWakeFollowers();
expandedMachines.clear();
selectedMachineId = null;
diff --git a/desktop/lib/usage/models_menu_controller.dart b/desktop/lib/usage/models_menu_controller.dart
index 546d771a2..f7ed9b120 100644
--- a/desktop/lib/usage/models_menu_controller.dart
+++ b/desktop/lib/usage/models_menu_controller.dart
@@ -45,6 +45,9 @@ class ModelsMenuController extends ChangeNotifier {
bool _failed = false;
bool _disposed = false;
+ /// Advanced by [reset], so a read the previous account started is told apart from the next's.
+ int _account = 0;
+
List<Map<String, Object?>> get rows => [
for (final account in _usage.accounts)
_row(account, _now(), failed: _failed, refreshing: _pending != null),
@@ -98,18 +101,30 @@ class ModelsMenuController extends ChangeNotifier {
}
Future<void> _refresh() async {
+ final account = _account;
try {
await _usage.refresh();
} catch (_) {
// A credential/source failure must never escape into a menu callback.
// Never include a raw exception: it could contain authentication data.
- _failed = true;
+ if (account == _account) _failed = true;
} finally {
- _pending = null;
+ // A signed-out account's read neither ends nor fails the next one's.
+ if (account == _account) _pending = null;
_changed();
}
}
+ /// Forgets the signed-out account: its other computers' rows go, and the next account reads at
+ /// once instead of waiting out the old one's minute, or joining its read still on the way.
+ void reset() {
+ if (_disposed) return;
+ _account++;
+ _pending = null;
+ _lastAttempt = null;
+ _usage.forgetRemote();
+ }
+
void _changed() {
if (!_disposed) notifyListeners();
}
diff --git a/desktop/lib/usage/usage_controller.dart b/desktop/lib/usage/usage_controller.dart
index c97030f36..2d1a18d2f 100644
--- a/desktop/lib/usage/usage_controller.dart
+++ b/desktop/lib/usage/usage_controller.dart
@@ -133,6 +133,16 @@ class UsageController extends ChangeNotifier {
notifyListeners();
}
+ /// Drops every remote machine's figures: they belonged to the account that
+ /// just signed out, and its emails must not show under the next one. A read
+ /// still on its way lands nowhere, like any older answer.
+ void forgetRemote() {
+ if (_disposed) return;
+ _remoteRequest++;
+ _remote = const [];
+ notifyListeners();
+ }
+
Future<void> _refreshLocal() async {
_started = true;
final request = ++_request;
diff --git a/desktop/test/models_menu_test.dart b/desktop/test/models_menu_test.dart
index 030790c2c..61bc4c775 100644
--- a/desktop/test/models_menu_test.dart
+++ b/desktop/test/models_menu_test.dart
@@ -10,6 +10,7 @@ import 'package:harness/core/dsh_catalog.dart';
import 'package:harness/widgets/agent_picker.dart';
import 'package:harness/core/config.dart';
import 'package:harness/auth/auth_session.dart';
+import 'package:harness/auth/cli_login.dart';
import 'package:harness/screens/swarm_screen.dart';
import 'package:harness/state/app_state.dart';
import 'package:harness/state/harness_placement.dart';
@@ -109,6 +110,14 @@ _GridApp _gridApp({
return app;
}
+class _NoopLogin extends CliLogin {
+ int logoutCalls = 0;
+ @override
+ Future<void> logout() async {
+ logoutCalls++;
+ }
+}
+
class _Source implements UsageSource {
_Source(this.provider, this.answer);
@override
@@ -212,6 +221,139 @@ void main() {
},
);
+ group('sign-out', () {
+ List<MachineUsage> otherAccount() => [
+ MachineUsage(
+ machineName: 'Other Mac',
+ readings: [
+ reading(account: '1122334455667788', email: '[email protected]'),
+ ],
+ ),
+ ];
+ bool remoteRow(Map<String, Object?> row) => row['local'] == false;
+
+ test('sign-out forgets remote usage rows', () async {
+ final usage = UsageController(
+ sources: [_Source(UsageProvider.claude, () async => reading())],
+ autoStart: false,
+ remote: () async => otherAccount(),
+ );
+ final menu = ModelsMenuController(usage: usage, now: () => instant);
+ addTearDown(usage.dispose);
+ addTearDown(menu.dispose);
+ await menu.refresh();
+ expect(menu.rows.where(remoteRow), hasLength(1));
+
+ menu.reset();
+
+ expect(menu.rows.where(remoteRow), isEmpty);
+ expect(menu.rows.single['account'], '[email protected]');
+ });
+
+ test('a remote read the old account started lands nowhere', () async {
+ final answers = <Completer<List<MachineUsage>>>[];
+ final source = _Source(UsageProvider.claude, () async => reading());
+ final usage = UsageController(
+ sources: [source],
+ autoStart: false,
+ remote: () {
+ answers.add(Completer());
+ return answers.last.future;
+ },
+ );
+ final menu = ModelsMenuController(usage: usage, now: () => instant);
+ addTearDown(usage.dispose);
+ addTearDown(menu.dispose);
+ final asked = menu.refresh();
+
+ menu.reset();
+ answers.single.complete(otherAccount());
+ await asked;
+
+ expect(menu.rows.where(remoteRow), isEmpty);
+ // The next account reads at once, not a minute after the old one did.
+ final next = menu.refresh();
+ expect(source.calls, 2);
+ answers.last.complete(const []);
+ await next;
+ });
+
+ test('the next account reads while the old one\'s read is held', () async {
+ final locals = <Completer<ProviderUsage>>[];
+ final remotes = <Completer<List<MachineUsage>>>[];
+ final source = _Source(UsageProvider.claude, () {
+ locals.add(Completer());
+ return locals.last.future;
+ });
+ final usage = UsageController(
+ sources: [source],
+ autoStart: false,
+ remote: () {
+ remotes.add(Completer());
+ return remotes.last.future;
+ },
+ );
+ var clock = instant;
+ final menu = ModelsMenuController(usage: usage, now: () => clock);
+ addTearDown(usage.dispose);
+ addTearDown(menu.dispose);
+ final old = menu.refresh();
+
+ menu.reset();
+ final next = menu.refresh();
+ expect(source.calls, 2);
+
+ // The old account's read lands late, and fails: it must neither end
+ // nor fail the next account's read.
+ locals.first.completeError(StateError('old account'));
+ remotes.first.complete(otherAccount());
+ await old;
+ clock = instant.add(const Duration(minutes: 2));
+ final reopened = menu.refresh();
+ expect(source.calls, 2);
+
+ locals.last.complete(reading());
+ remotes.last.complete([
+ MachineUsage(
+ machineName: 'Studio',
+ readings: [reading(account: '99aa', email: '[email protected]')],
+ ),
+ ]);
+ await Future.wait([next, reopened]);
+
+ final remote = menu.rows.where(remoteRow).single;
+ expect(remote['account'], '[email protected]');
+ expect(remote['status'], '15% remaining');
+ });
+
+ for (final expires in [false, true]) {
+ test('${expires ? 'expiry' : 'sign-out'} resets the window\'s Models '
+ 'menu', () async {
+ // Never the real CLI: its logout would sign this computer out.
+ final login = _NoopLogin();
+ final app = AppNotifier(
+ config: AppConfig.dev,
+ authSession: AuthSession(),
+ configStore: null,
+ cliLogin: login,
+ );
+ addTearDown(app.dispose);
+ final menu = app.modelsMenu;
+ var told = 0;
+ menu.addListener(() => told++);
+ if (expires) {
+ app.expireSessionForTest('Session expired.');
+ } else {
+ await app.logout();
+ }
+ expect(login.logoutCalls, expires ? 0 : 1);
+ expect(told, greaterThan(0));
+ expect(app.modelsMenu, same(menu), reason: 'pickers keep listening');
+ expect(menu.rows.where(remoteRow), isEmpty);
+ });
+ }
+ });
+
test(
'readings without email do not expose their internal account key',
() async {