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 agent switching with older daemon replies (#1672)

Deefce41ee148d8
diff --git a/desktop/lib/state/app_state.dart b/desktop/lib/state/app_state.dart
index d9b994d9a..727bac1f7 100644
--- a/desktop/lib/state/app_state.dart
+++ b/desktop/lib/state/app_state.dart
@@ -15908,6 +15908,25 @@ class AppNotifier extends ChangeNotifier {
).whenComplete(() => change.pending = null);
}
+ /// Keep explicit protocol refusals available to the compatibility and hold
+ /// handling below. WsConn throws them; transport failures must still throw.
+ Future<Map<String, dynamic>> _agentHandoffRequest(
+ String machineId,
+ String type, {
+ required Map<String, dynamic> payload,
+ required Duration timeout,
+ }) async {
+ try {
+ return await _conn(machineId).request(
+ type,
+ payload: payload,
+ timeout: timeout,
+ );
+ } on WsRequestFailure catch (failure) {
+ return {...failure.payload, 'error': failure.code};
+ }
+ }
+
/// Fill [change].handoff for the new agent, or return why the switch must not go on.
///
/// First choice: the machine writes a record of the whole conversation into the project and the
@@ -15924,7 +15943,8 @@ class AppNotifier extends ChangeNotifier {
) async {
change.handoffHeld = true;
try {
- final reply = await _conn(machineId).request(
+ final reply = await _agentHandoffRequest(
+ machineId,
'agent_handoff_prepare',
payload: {
'agentId': source.id,
@@ -16106,7 +16126,8 @@ class AppNotifier extends ChangeNotifier {
}
if (!change.replacementReported && source.dsh != 'autonomous/pair') {
try {
- final reported = await _conn(machineId).request(
+ final reported = await _agentHandoffRequest(
+ machineId,
'agent_handoff_complete',
payload: {
'agentId': source.id,
diff --git a/desktop/test/agent_change_test.dart b/desktop/test/agent_change_test.dart
index 218e588cd..305aaa372 100644
--- a/desktop/test/agent_change_test.dart
+++ b/desktop/test/agent_change_test.dart
@@ -9,7 +9,7 @@ import 'package:harness/core/config.dart';
import 'package:harness/e2ee/envelope.dart';
import 'package:harness/state/agent_handoff_file.dart';
import 'package:harness/state/agent_switch_handoff.dart';
-import 'package:harness/ws/ws_conn.dart' show WsRequestTimeout;
+import 'package:harness/ws/ws_conn.dart' show WsRequestFailure, WsRequestTimeout;
import 'package:harness/state/desk_sync.dart';
import 'package:harness/core/dsh_catalog.dart';
import 'package:harness/core/models.dart';
@@ -45,6 +45,7 @@ class SwitchConnection extends MonitorConnection {
final eventsAtHandoff = <List<String>>[];
final completedHandoffs = <Map<String, dynamic>>[];
String? completionError;
+ Object? completionThrows;
@override
Future<Map<String, dynamic>> request(
@@ -54,6 +55,7 @@ class SwitchConnection extends MonitorConnection {
}) async {
if (type == 'agent_handoff_complete') {
completedHandoffs.add(Map.of(payload));
+ if (completionThrows case final error?) throw error;
return completionError == null ? {'ok': true} : {'error': completionError};
}
if (type == 'agent_handoff_prepare') {
@@ -182,15 +184,67 @@ class SwitchDeskApi extends ApiClient {
}
void main() {
+ test('a wire UNSUPPORTED completion opens the replacement terminal', () async {
+ final connection = SwitchConnection()
+ ..completionThrows = const WsRequestFailure(
+ responseType: 'agent_handoff_complete_result',
+ code: 'UNSUPPORTED',
+ );
+ final app = fixture(connection);
+ addTearDown(app.dispose);
+ await app.addAgentToSwarm('m', 'a0');
+ final pane = app.panes.single;
+ expect(await app.changeAgent('m', 'a0', 'claude'), isNull);
+ expect(app.panes.single, same(pane));
+ expect(pane.agentId, 'local-session');
+ expect(connection.creations.single['engine'], 'claude');
+ });
+
+ test('a wire UNSUPPORTED preparation uses the older history protocol', () async {
+ final connection = SwitchConnection()
+ ..handoffThrows = const WsRequestFailure(
+ responseType: 'agent_handoff_prepare_result',
+ code: 'UNSUPPORTED',
+ );
+ final app = fixture(connection);
+ addTearDown(app.dispose);
+ await app.addAgentToSwarm('m', 'a0');
+ expect(await app.changeAgent('m', 'a0', 'claude'), isNull);
+ expect(connection.recentReads, 1);
+ expect(connection.creations, hasLength(1));
+ });
+
+ test('a wire identity hold retains its restart guidance without stopping', () async {
+ final connection = SwitchConnection()
+ ..handoffThrows = const WsRequestFailure(
+ responseType: 'agent_handoff_prepare_result',
+ code: 'IDENTITY_UNAVAILABLE',
+ payload: {'held': true, 'hold': 'PROGRAM_REPLACED'},
+ );
+ final app = fixture(connection);
+ addTearDown(app.dispose);
+ await app.addAgentToSwarm('m', 'a0');
+ expect(
+ await app.changeAgent('m', 'a0', 'claude'),
+ contains('Use Restart Harness'),
+ );
+ expect(connection.events, isEmpty);
+ expect(connection.recentReads, 0);
+ });
+
test('a lost completion report retries the same replacement without another launch', () async {
- final connection = SwitchConnection()..completionError = 'SERVICE_UNAVAILABLE';
+ final connection = SwitchConnection()
+ ..completionThrows = const WsRequestFailure(
+ responseType: 'agent_handoff_complete_result',
+ code: 'SERVICE_UNAVAILABLE',
+ );
final app = fixture(connection);
addTearDown(app.dispose);
await app.addAgentToSwarm('m', 'a0');
expect(await app.changeAgent('m', 'a0', 'claude'), contains('new agent started'));
expect(app.panes.single.agentId, 'a0');
expect(connection.creations, hasLength(1));
- connection.completionError = null;
+ connection.completionThrows = null;
expect(await app.changeAgent('m', 'a0', 'claude'), isNull);
expect(connection.creations, hasLength(1));
expect(connection.completedHandoffs, hasLength(2));