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(team): offer the Harness Team account at each daemon start, and say why it was not offered (#1790)

0xken153d4b3ba0d044
diff --git a/cli/src/team/autoSetup.spec.ts b/cli/src/team/autoSetup.spec.ts
index c04b8a897..670c9a271 100644
--- a/cli/src/team/autoSetup.spec.ts
+++ b/cli/src/team/autoSetup.spec.ts
@@ -38,6 +38,25 @@ describe('the Harness Team account, set up on its own', () => {
expect(f.log.join('\n')).toContain('the Harness Team account is set up')
})
+ it('says in its log why it asked nothing, one line per answer, so a Mac that never saw the prompt can tell why', async () => {
+ const cases: Array<[Partial<AutoSetupDeps>, string]> = [
+ [{ platform: 'linux' }, 'unsupported'], [{ session: () => null }, 'signed-out'], [{ session: () => ({ ...session, kind: 'team' }) }, 'dedicated'],
+ [{ accountExists: vi.fn(async () => true) }, 'exists'],
+ ]
+ for (const [over, state] of cases) {
+ const f = fake(over)
+ await f.auto.offer('startup', async () => true)
+ expect(f.log, state).toEqual([`the Harness Team account was not offered (startup): ${state}`])
+ }
+ const declined = fake()
+ declined.setDecision({ state: 'declined', at: 1 })
+ await declined.auto.offer('join')
+ expect(declined.log).toEqual(['the Harness Team account was not offered (join): declined'])
+ const alone = fake()
+ await alone.auto.offer('startup', async () => false)
+ expect(alone.log).toEqual(['the Harness Team account was not offered (startup): not-shared'])
+ })
+
it('asks nothing off a Mac, signed out, on a computer that runs team work only, or where the account exists', async () => {
const cases: Array<[Partial<AutoSetupDeps>, string]> = [
[{ platform: 'linux' }, 'unsupported'], [{ platform: 'win32' }, 'unsupported'], [{ session: () => null }, 'signed-out'],
diff --git a/cli/src/team/autoSetup.ts b/cli/src/team/autoSetup.ts
index ba9155afc..27729fd57 100644
--- a/cli/src/team/autoSetup.ts
+++ b/cli/src/team/autoSetup.ts
@@ -86,23 +86,31 @@ export class TeamAutoSetup {
* With [check] it starts nothing and answers `wanted` instead, for Desktop to explain the account first.
*/
async offer(reason: AutoSetupReason, sharedSwarm: () => Promise<boolean> = async () => false, check = false): Promise<{ state: AutoSetupState }> {
+ const state = await this.decide(reason, sharedSwarm, check)
+ // Every answer is said, not only a setup that starts: Macs in a Swarm went without the prompt and nothing on them said
+ // why (2026-10-27). A check that finds it wanted starts nothing: that is Desktop's question, not an answer.
+ if (state !== 'started' && state !== 'wanted') this.deps.log(`the Harness Team account was not offered (${reason}): ${state}`)
+ return { state }
+ }
+
+ private async decide(reason: AutoSetupReason, sharedSwarm: () => Promise<boolean>, check: boolean): Promise<AutoSetupState> {
const { deps } = this
- if (deps.platform !== 'darwin') return { state: 'unsupported' }
+ if (deps.platform !== 'darwin') return 'unsupported'
const session = deps.session()
- if (!session) return { state: 'signed-out' }
- if (session.kind === 'team') return { state: 'dedicated' }
- if (this.running) return { state: 'running' }
- if (deps.readConfig() || await deps.accountExists(TEAM_USER)) return { state: 'exists' }
+ if (!session) return 'signed-out'
+ if (session.kind === 'team') return 'dedicated'
+ if (this.running) return 'running'
+ if (deps.readConfig() || await deps.accountExists(TEAM_USER)) return 'exists'
if (reason !== 'manual') {
const decision = deps.readDecision()
- if (decision?.state === 'declined') return { state: 'declined' }
- if (decision?.state === 'failed' && deps.now() - decision.at < AUTO_SETUP_RETRY_MS && deps.now() >= decision.at) return { state: 'waiting' }
- if (reason === 'startup' && !await sharedSwarm().catch(() => false)) return { state: 'not-shared' }
+ if (decision?.state === 'declined') return 'declined'
+ if (decision?.state === 'failed' && deps.now() - decision.at < AUTO_SETUP_RETRY_MS && deps.now() >= decision.at) return 'waiting'
+ if (reason === 'startup' && !await sharedSwarm().catch(() => false)) return 'not-shared'
}
- if (check) return { state: 'wanted' }
- if (this.running) return { state: 'running' }
+ if (check) return 'wanted'
+ if (this.running) return 'running'
this.running = this.run().finally(() => { this.running = null })
- return { state: 'started' }
+ return 'started'
}
/** "Not now" in Desktop's explanation: not offered again on its own, as a cancelled prompt is not. */
diff --git a/desktop/lib/state/app_state.dart b/desktop/lib/state/app_state.dart
index 07e064f7e..d6485d3e0 100644
--- a/desktop/lib/state/app_state.dart
+++ b/desktop/lib/state/app_state.dart
@@ -1937,7 +1937,41 @@ class AppNotifier extends ChangeNotifier {
/// under test, on a Mac too, unless a test sets it.
@visibleForTesting
bool? macOSForTest;
- bool _teamOfferedAtStart = false;
+
+ /// How long before asking again a daemon that could not answer the start
+ /// offer; each further try waits one more step.
+ @visibleForTesting
+ Duration teamOfferRetryForTest = const Duration(seconds: 30);
+ static const _teamOfferTries = 5;
+
+ /// Which daemon process answers this Mac, for a spec; the daemon's own
+ /// status otherwise.
+ @visibleForTesting
+ Future<String?> Function()? daemonIdentityForTest;
+ bool _teamOfferBusy = false;
+ Timer? _teamOfferRetry;
+
+ /// The daemon process the start offer's budget is for, its tries, and
+ /// whether it answered. A socket reconnect to the same process keeps them;
+ /// only a new process starts over (review of #1790).
+ bool _teamOfferStarted = false;
+ String? _teamOfferDaemon;
+ int _teamOfferTried = 0;
+ bool _teamOfferAnswered = false;
+ bool _teamStartExplained = false;
+
+ /// This Mac's daemon process as its own status says it: its pid and
+ /// version, so a restart or an update reads as a new one. Null when it
+ /// cannot say.
+ Future<String?> _daemonIdentity() async {
+ if (daemonIdentityForTest case final read?) return read();
+ try {
+ final probe = await _discovery.probe();
+ return probe.pid == null ? null : '${probe.pid}:${probe.version ?? ''}';
+ } catch (_) {
+ return null;
+ }
+ }
/// The Harness Team account on this Mac (cli/src/team/autoSetup.ts),
/// offered while the person is here: as they join a Swarm ([reason]
@@ -1948,18 +1982,82 @@ class AppNotifier extends ChangeNotifier {
/// ([teamSetupAsks]): macOS's prompt is titled "osascript", which nobody
/// recognised as Harness (2026-10-27). Nothing on Windows or Linux, and never
/// for another machine, where nobody may be at it.
- Future<void> offerTeamAccount(String reason) async {
+ ///
+ /// True when the daemon answered, whatever it decided; false when it was not
+ /// asked or could not say.
+ Future<bool> offerTeamAccount(String reason) async {
final here = _teamAccountHere();
- if (here == null) return;
+ if (here == null) return false;
try {
final answer = await superTabMachineRequest(here, {
'action': 'teamSetupCheck',
'reason': reason,
});
- if (answer['state'] == 'wanted') _teamSetupAsks.add(reason);
+ appLog.info('super-tabs', 'team account offer ($reason): ${answer['state']}');
+ if (answer['state'] == 'wanted') {
+ // The start check runs once for each daemon process, so a restart
+ // with the explanation still open must not show it again: once a
+ // launch at start. A join, a contribution or the machine row's Set up
+ // is the person asking, and always explains.
+ if (reason != 'startup' || !_teamStartExplained) _teamSetupAsks.add(reason);
+ if (reason == 'startup') _teamStartExplained = true;
+ }
+ return true;
} catch (error) {
// An older daemon, or one that cannot say: nothing is asked.
- appLog.debug('super-tabs', 'team account not offered: $error');
+ appLog.info('super-tabs', 'team account not offered ($reason): $error');
+ return false;
+ }
+ }
+
+ /// The start offer, once for each process of this Mac's daemon: Macs
+ /// already in a Swarm meet no join or contribute moment, and one ask a
+ /// launch went to whichever daemon answered first, one still starting or the
+ /// version from before an update, so nobody saw the prompt (2026-10-27).
+ /// The daemon decides, and remembers a cancel. One ask at a time; a daemon
+ /// that could not say is asked again a few times, with a growing wait, and
+ /// a reconnect to it neither restarts the count nor cuts the wait short.
+ Future<void> _offerTeamAccountAtStart() async {
+ if (_disposed || _teamOfferBusy) return;
+ if (!(macOSForTest ?? (Platform.isMacOS && !kUnderTest))) return;
+ _teamOfferBusy = true;
+ try {
+ final daemon = await _daemonIdentity();
+ if (_disposed) return;
+ // Only a confirmed process other than the last confirmed one starts
+ // over. A status read that could not say (a timeout) is no evidence of
+ // a restart, and counting it as one asked without end (re-check of
+ // #1790); the first identity read after an unknown one is taken as the
+ // same process.
+ final restarted = daemon != null && _teamOfferDaemon != null && daemon != _teamOfferDaemon;
+ if (!_teamOfferStarted || restarted) {
+ _teamOfferRetry?.cancel();
+ _teamOfferRetry = null;
+ _teamOfferStarted = true;
+ _teamOfferTried = 0;
+ _teamOfferAnswered = false;
+ }
+ _teamOfferDaemon = daemon ?? _teamOfferDaemon;
+ if (_teamOfferAnswered ||
+ _teamOfferTried >= _teamOfferTries ||
+ _teamOfferRetry != null) {
+ return;
+ }
+ final answered = await offerTeamAccount('startup');
+ // A request still out at dispose must not schedule another.
+ if (_disposed) return;
+ if (answered) {
+ _teamOfferAnswered = true;
+ return;
+ }
+ _teamOfferTried++;
+ if (_teamOfferTried >= _teamOfferTries) return;
+ _teamOfferRetry = Timer(teamOfferRetryForTest * _teamOfferTried, () {
+ _teamOfferRetry = null;
+ if (!_disposed) unawaited(_offerTeamAccountAtStart());
+ });
+ } finally {
+ _teamOfferBusy = false;
}
}
@@ -5270,12 +5368,11 @@ class AppNotifier extends ChangeNotifier {
// the offline poll (the one thing that would have re-probed) stopped, and the tiles sat on
// "Offline" over a live terminal with nothing left to restore them. Measured 2026-09-18 21:50.
machine.localEndpoint ??= _cliEndpoint;
- // Once a launch, with the person here: a Mac that joined a Swarm before
- // teammates' work went only to Team machines never met the join or
- // contribute moment.
- if (!_teamOfferedAtStart && !machine.machine.isSharedMachine) {
- _teamOfferedAtStart = true;
- unawaited(offerTeamAccount('startup'));
+ // With the person here, at each start of this Mac's daemon: a Mac that
+ // joined a Swarm before teammates' work went only to Team machines never
+ // met the join or contribute moment.
+ if (!machine.machine.isSharedMachine) {
+ unawaited(_offerTeamAccountAtStart());
}
} else {
machine.transportMode = MachineTransportMode.cloudE2ee;
@@ -21562,6 +21659,7 @@ class AppNotifier extends ChangeNotifier {
@override
void dispose() {
+ _teamOfferRetry?.cancel();
if (identical(CrashLog.healthError, _measurementError)) {
CrashLog.healthError = null;
}
diff --git a/desktop/test/team_account_offer_test.dart b/desktop/test/team_account_offer_test.dart
index f7d9ea79f..2f0f21933 100644
--- a/desktop/test/team_account_offer_test.dart
+++ b/desktop/test/team_account_offer_test.dart
@@ -1,3 +1,5 @@
+import 'dart:async';
+
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:harness/state/app_state.dart';
@@ -10,6 +12,8 @@ import 'swarm_state_test.dart' show createApp;
class _Daemon extends RenameConnection {
final asked = <Map<String, dynamic>>[];
Map<String, dynamic> answer = {'ok': true, 'state': 'wanted'};
+ /// Answers in turn before [answer]; a Completer's future is awaited.
+ final queued = <Object>[];
@override
Future<Map<String, dynamic>> request(
@@ -20,21 +24,49 @@ class _Daemon extends RenameConnection {
if (type == 'super_tabs' &&
'${payload['action']}'.startsWith('teamSetup')) {
asked.add(Map.of(payload));
+ if (queued.isNotEmpty) {
+ final next = queued.removeAt(0);
+ return next is Completer<Map<String, dynamic>>
+ ? next.future
+ : Future.value(next as Map<String, dynamic>);
+ }
return Future.value(answer);
}
return super.request(type, payload: payload, timeout: timeout);
}
}
+/// Waits until [ok] holds, failing after two seconds.
+Future<void> until(bool Function() ok) async {
+ final deadline = DateTime.now().add(const Duration(seconds: 2));
+ while (!ok()) {
+ if (DateTime.now().isAfter(deadline)) fail('timed out waiting');
+ await Future<void>.delayed(const Duration(milliseconds: 1));
+ }
+}
+
+const startCheck = {'action': 'teamSetupCheck', 'reason': 'startup'};
+
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
late _Daemon daemon;
late AppNotifier app;
- setUp(() {
- daemon = _Daemon();
- app = createApp(connectionForTest: (_) => daemon, connected: true);
+ /// Which daemon process answers this Mac: a new one after a restart, null
+ /// when its status could not be read.
+ String? running = 'pid-1';
+ AppNotifier make(_Daemon daemon) {
+ final app = createApp(connectionForTest: (_) => daemon, connected: true);
app.machineStates['m']!.localOnly = true;
app.macOSForTest = true;
+ app.daemonIdentityForTest = () async => running;
+ app.teamOfferRetryForTest = const Duration(milliseconds: 1);
+ return app;
+ }
+
+ setUp(() {
+ running = 'pid-1';
+ daemon = _Daemon();
+ app = make(daemon);
});
tearDown(() => app.dispose());
@@ -90,13 +122,131 @@ void main() {
expect(daemon.asked, hasLength(1));
});
- test('offers once a launch, as this Mac\'s daemon first connects', () async {
+ test('checks once for each process of this Mac\'s daemon, as it connects', () async {
app.onMachineConnectedForTest('m');
app.onMachineConnectedForTest('m');
+ await until(() => daemon.asked.isNotEmpty);
await pumpEventQueue();
- expect(daemon.asked, [
- {'action': 'teamSetupCheck', 'reason': 'startup'},
+ expect(daemon.asked, [startCheck]);
+ });
+
+ // Macs already in a Swarm met no join or contribute moment, and the one ask
+ // a launch went to whichever daemon answered first: one still starting, or
+ // the version from before an update, so nobody saw the prompt (2026-10-27).
+ test('checks again when this Mac\'s daemon has restarted, which decides', () async {
+ app.onMachineConnectedForTest('m');
+ await until(() => daemon.asked.length == 1);
+ await pumpEventQueue();
+ // A new daemon process, after an update for instance: asked again.
+ running = 'pid-2';
+ app.onMachineConnectedForTest('m');
+ await until(() => daemon.asked.length == 2);
+ expect(daemon.asked, everyElement(startCheck));
+ });
+
+ // A start check runs for each daemon process, so a restart while the
+ // explanation is still open must not show it a second time.
+ test('explains at start once a launch, though a restarted daemon still wants it', () async {
+ final explained = <String>[];
+ final listening = app.teamSetupAsks.listen(explained.add);
+ app.onMachineConnectedForTest('m');
+ await until(() => daemon.asked.length == 1);
+ await pumpEventQueue();
+ running = 'pid-2';
+ app.onMachineConnectedForTest('m');
+ await until(() => daemon.asked.length == 2);
+ await pumpEventQueue();
+ expect(explained, ['startup']);
+ // The person asking, by joining, is always explained.
+ await app.offerTeamAccount('join');
+ await pumpEventQueue();
+ expect(explained, ['startup', 'join']);
+ await listening.cancel();
+ });
+
+ test('checks one at a time', () async {
+ final pending = Completer<Map<String, dynamic>>();
+ daemon.queued.add(pending);
+ app.onMachineConnectedForTest('m');
+ app.onMachineConnectedForTest('m');
+ await until(() => daemon.asked.isNotEmpty);
+ await pumpEventQueue();
+ expect(daemon.asked, hasLength(1));
+ pending.complete({'ok': true, 'state': 'not-shared'});
+ await pumpEventQueue();
+ expect(daemon.asked, hasLength(1));
+ });
+
+ // Review of #1790: every socket reconnect started a fresh budget, so a
+ // daemon that could not answer was asked at the reconnect rate for good.
+ test('keeps its budget across reconnects to the same daemon, and asks nothing more once it answered', () async {
+ daemon.answer = {'error': 'SERVICE_UNAVAILABLE'};
+ for (var i = 0; i < 20; i++) {
+ app.onMachineConnectedForTest('m');
+ await Future<void>.delayed(const Duration(milliseconds: 2));
+ }
+ await until(() => daemon.asked.length == 5);
+ await Future<void>.delayed(const Duration(milliseconds: 30));
+ // Five tries for one daemon, however often its socket reconnected.
+ expect(daemon.asked, hasLength(5));
+ // The same daemon, once it answered, is not asked on a reconnect.
+ running = 'pid-2';
+ daemon.answer = {'ok': true, 'state': 'not-shared'};
+ app.onMachineConnectedForTest('m');
+ await until(() => daemon.asked.length == 6);
+ for (var i = 0; i < 5; i++) {
+ app.onMachineConnectedForTest('m');
+ await pumpEventQueue();
+ }
+ await Future<void>.delayed(const Duration(milliseconds: 20));
+ expect(daemon.asked, hasLength(6));
+ });
+
+ // Re-check of #1790: a status read that timed out has no identity, which
+ // counted as a new process, so a daemon whose status read now and then
+ // timed out was asked without end.
+ test('keeps its budget when the daemon\'s identity cannot be read', () async {
+ daemon.answer = {'error': 'SERVICE_UNAVAILABLE'};
+ for (var i = 0; i < 20; i++) {
+ running = i.isEven ? 'pid-1' : null;
+ app.onMachineConnectedForTest('m');
+ await Future<void>.delayed(const Duration(milliseconds: 2));
+ }
+ await until(() => daemon.asked.length == 5);
+ await Future<void>.delayed(const Duration(milliseconds: 30));
+ // Five tries for the one daemon whose status read timed out in between.
+ expect(daemon.asked, hasLength(5));
+ // Only a different confirmed process starts over.
+ running = 'pid-2';
+ app.onMachineConnectedForTest('m');
+ await until(() => daemon.asked.length == 6);
+ });
+
+ test('checks again, a few times, a daemon that could not answer yet', () async {
+ daemon.queued.addAll([
+ {'error': 'SERVICE_UNAVAILABLE'},
+ {'error': 'SERVICE_UNAVAILABLE'},
]);
+ app.onMachineConnectedForTest('m');
+ // Two that could not say, then an answer.
+ await until(() => daemon.asked.length == 3);
+ await Future<void>.delayed(const Duration(milliseconds: 20));
+ expect(daemon.asked, hasLength(3));
+ });
+
+ // Review of #1790: a request still out at dispose resumed and scheduled a
+ // retry on a disposed app.
+ test('schedules nothing after it is disposed, though a request was out', () async {
+ final own = _Daemon();
+ final other = make(own);
+ final pending = Completer<Map<String, dynamic>>();
+ own.queued.add(pending);
+ other.onMachineConnectedForTest('m');
+ await until(() => own.asked.isNotEmpty);
+ other.dispose();
+ pending.complete({'error': 'ACCOUNT_CHANGED'});
+ await Future<void>.delayed(const Duration(milliseconds: 30));
+ expect(own.asked, hasLength(1));
});
Future<void> open(WidgetTester tester, {bool asked = false}) async {