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): preserve verified remote status across stale inventory (#687)

Dee8251c0e9f3cf
diff --git a/desktop/lib/state/app_state.dart b/desktop/lib/state/app_state.dart
index 2deb95827..6566edacf 100644
--- a/desktop/lib/state/app_state.dart
+++ b/desktop/lib/state/app_state.dart
@@ -386,6 +386,12 @@ class MachineState {
// distinct from `connectionStatus`, which only reflects OUR websocket to
// the backend. null = not seen yet (initial connect).
bool? nodeOnline;
+ // Captured only after machine selection succeeds, including the remote
+ // handshake. Merely opening the socket to the local service is not enough.
+ WsConn? _selectedConnection;
+ bool get _selectedConnectionReady =>
+ connectionStatus == ConnectionStatus.connected &&
+ _selectedConnection?.isReady == true;
// The agent the user selected while the Harness adapter was offline. Keep
// this separate from activeAgentId so the UI can show a join guide without
// opening a terminal stream against an unavailable node.
@@ -442,22 +448,16 @@ class MachineState {
/// Whether this machine can be given work right now.
///
- /// Two witnesses, and neither is always right. [nodeOnline] is what has been
- /// heard ABOUT the far end — but it is also set hopefully, the moment our
- /// own socket connects (`_onMachineConnected`), and that socket only reaches
- /// the LOCAL daemon: it proves nothing about a computer on the other side of
- /// the relay. The machine list is the backend's own view, and a computer it
- /// calls `offline` is one that stopped heartbeating to it.
- ///
- /// So the list's `offline` counts even against a hopeful `nodeOnline`, and
- /// the one machine exempt is this one — the computer the app is running on
- /// is not offline, whatever a stale row says about it. Erring this way is
- /// deliberate: the cost of being wrong is a machine that looks unavailable
- /// for a few seconds longer, against starting work on a computer that is
- /// not there and finding out a minute later.
+ /// An explicit offline report always wins. A saved or hopeful [nodeOnline]
+ /// value cannot override the inventory either. A successfully selected,
+ /// still-ready connection is stronger evidence: the local relay acknowledges
+ /// selection only after its handshake with the remote machine succeeds.
+ /// This computer also remains usable when its backend inventory is stale.
bool get isOffline =>
nodeOnline == false ||
- (!isLocalMachine && machine.reportedOnline == false);
+ (!isLocalMachine &&
+ machine.reportedOnline == false &&
+ !(nodeOnline == true && _selectedConnectionReady));
AgentProject? projectOf(Agent agent) =>
agent.displayProject ??
@@ -3315,6 +3315,8 @@ class AppNotifier extends ChangeNotifier {
/// What a machine hears the moment its socket is up — first connect, or a
/// reconnect after its daemon restarted, which has forgotten all of it.
void _onMachineConnected(String machineId, MachineState machine) {
+ machine._selectedConnection =
+ connectionForTest?.call(machineId) ?? _pool?[machineId];
_rereadDevicesAfterDaemonReconnect(machineId);
// A viewer build keeps its own trust group (a CLI build's daemon keeps one for it): any session
// that comes up is the moment to compare it with this machine.
@@ -6094,7 +6096,7 @@ class AppNotifier extends ChangeNotifier {
if (!state.isLocalMachine &&
reportedOnline != null &&
(state.nodeOnline == null || state.nodeOnline != reportedOnline)) {
- unawaited(_applyNodeStatus(state, reportedOnline));
+ unawaited(_applyInventoryNodeStatus(state, reportedOnline));
}
}
// A list that arrived but does not name this computer (a stale copy from before this computer
@@ -7271,7 +7273,7 @@ class AppNotifier extends ChangeNotifier {
final reportedOnline = latest.first.reportedOnline;
if (reportedOnline == null) return;
machine.machine = latest.first;
- await _applyNodeStatus(machine, reportedOnline);
+ await _applyInventoryNodeStatus(machine, reportedOnline);
} catch (error) {
debugPrint('offline node retry failed: $machineId: $error');
} finally {
@@ -12239,6 +12241,21 @@ class AppNotifier extends ChangeNotifier {
return true;
}
+ /// An inventory snapshot can predate a successful remote selection. Keep its
+ /// offline status from taking a live connection down; socket failures and
+ /// explicit node_status pushes still go through _applyNodeStatus directly.
+ Future<void> _applyInventoryNodeStatus(
+ MachineState machine,
+ bool online,
+ ) async {
+ if (!online &&
+ machine.nodeOnline == true &&
+ machine._selectedConnectionReady) {
+ return;
+ }
+ await _applyNodeStatus(machine, online, why: 'machine inventory');
+ }
+
Future<void> _applyNodeStatus(
MachineState machine,
bool online, {
diff --git a/desktop/test/machine_list_outage_test.dart b/desktop/test/machine_list_outage_test.dart
index 8c53f182f..383c6b9c9 100644
--- a/desktop/test/machine_list_outage_test.dart
+++ b/desktop/test/machine_list_outage_test.dart
@@ -487,4 +487,113 @@ void main() {
expect(local.nodeOnline, isFalse);
});
+
+ test(
+ 'a delayed offline inventory cannot override a remote reconnect',
+ () async {
+ const remote = Machine(
+ machineId: 'remote-machine',
+ name: 'Remote test computer',
+ computerId: 'remote-computer',
+ authMode: MachineAuthMode.remote,
+ status: 'offline',
+ );
+ app.machines.add(remote);
+ final state = MachineState(remote)..nodeOnline = false;
+ app.machineStates[remote.machineId] = state;
+
+ final refresh = app.refreshMachines();
+ await _tick();
+ connection.ready = true;
+ app.onMachineConnectedForTest(remote.machineId);
+ await _tick();
+ expect(state.nodeOnline, isTrue);
+
+ api.lists.single.complete([_localMachine, remote]);
+ await refresh;
+ await _tick();
+ expect(
+ state.nodeOnline,
+ isTrue,
+ reason: 'the selected remote socket is newer than the inventory',
+ );
+ expect(
+ state.isOffline,
+ isFalse,
+ reason: 'the New Harness picker must agree',
+ );
+ connection.ready = false;
+ expect(
+ state.isOffline,
+ isTrue,
+ reason: 'a closed socket cannot override inventory',
+ );
+ },
+ );
+
+ for (final cached in [false, true]) {
+ test(
+ 'offline inventory still applies without a live remote socket (cached=$cached)',
+ () async {
+ const remote = Machine(
+ machineId: 'remote-machine',
+ name: 'Remote test computer',
+ authMode: MachineAuthMode.remote,
+ status: 'offline',
+ );
+ app.machines.add(remote);
+ final state = MachineState(remote)
+ ..nodeOnline = true
+ ..connectionStatus = ConnectionStatus.connected;
+ app.machineStates[remote.machineId] = state;
+ connection.ready = true;
+ app.onMachineConnectedForTest(remote.machineId);
+ await _tick();
+ connection.ready = false;
+ api.nextIsStale = cached;
+ final refresh = app.refreshMachines();
+ await _tick();
+ api.lists.single.complete([_localMachine, remote]);
+ await refresh;
+ expect(state.nodeOnline, isFalse);
+ },
+ );
+ }
+
+ test(
+ 'a remote offline push wins even before its ready socket closes',
+ () async {
+ const remote = Machine(
+ machineId: 'remote-machine',
+ name: 'Remote test computer',
+ authMode: MachineAuthMode.remote,
+ status: 'offline',
+ );
+ app.machines.add(remote);
+ final state = MachineState(remote)
+ ..nodeOnline = true
+ ..connectionStatus = ConnectionStatus.connected;
+ app.machineStates[remote.machineId] = state;
+ connection.ready = true;
+ app.onMachineConnectedForTest(remote.machineId);
+ await _tick();
+ expect(state.isOffline, isFalse);
+ await app.handleEventForTest(remote.machineId, {
+ 'type': 'node_status',
+ 'payload': {'online': false},
+ });
+ expect(state.nodeOnline, isFalse);
+ expect(connection.isReady, isTrue);
+ expect(state.isOffline, isTrue);
+ final refresh = app.refreshMachines();
+ await _tick();
+ api.lists.single.complete([_localMachine, remote]);
+ await refresh;
+ expect(
+ state.nodeOnline,
+ isFalse,
+ reason: 'an inventory must not resurrect an explicit offline report',
+ );
+ },
+ );
}