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
import 'dart:async';
import 'package:flutter_test/flutter_test.dart';
import 'package:harness/core/models.dart';
import 'package:harness/state/app_state.dart';
import 'package:harness/ws/ws_conn.dart';
import 'support/restart_connection.dart';
import 'swarm_state_test.dart' show createApp;
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
late AppNotifier app;
late RestartConnection connection;
const source = Agent(
id: 'a0',
name: 'Disposable harness',
engine: 'codex',
sessionId: 'original-history',
terminalAvailable: true,
);
Map<String, dynamic> inventory({bool paused = true}) => {
'agents': [
{
'id': source.id,
'name': source.name,
'engine': source.engine,
'sessionId': source.sessionId,
'status': paused ? 'stopped' : 'active',
'terminal': {'available': !paused},
},
],
};
setUp(() {
connection = RestartConnection();
app = createApp(connectionForTest: (_) => connection);
app.stateOf('m')!
..agents = [source]
..connectionStatus = ConnectionStatus.connected
..nodeOnline = true;
});
tearDown(() => app.dispose());
test(
'opening an offline saved harness reconnects before sending its resume',
() async {
final machine = app.stateOf('m')!;
machine.agents = [Agent.fromJson((inventory()['agents'] as List).single)];
machine.connectionStatus = ConnectionStatus.disconnected;
final resume = app.resumeAgent('m', 'a0');
await Future<void>.delayed(Duration.zero);
expect(connection.requests, isEmpty);
machine.connectionStatus = ConnectionStatus.connected;
app.notifyListeners();
await Future<void>.delayed(Duration.zero);
expect(connection.types, ['agent_resume']);
connection.restartReplies.single.complete(
restartReceipt(
connection.requests.single['creationId'] as String,
sessionId: source.sessionId,
),
);
expect((await resume).error, isNull);
},
);
test('a refusal in a durable receipt also opens the already resumed conversation', () async {
app.stateOf('m')!.agents = [
Agent.fromJson((inventory()['agents'] as List).single),
];
final resume = app.resumeAgent('m', 'a0');
app.stateOf('m')!.agents = [source];
connection.restartReplies.single.complete({
'creationId': connection.requests.single['creationId'],
'state': 'failed',
'failure': {'code': 'AGENT_BUSY'},
});
expect((await resume).error, isNull);
});
test(
'a thrown resume refusal is actionable and a retry starts a new receipt',
() async {
app.stateOf('m')!.agents = [
Agent.fromJson((inventory()['agents'] as List).single),
];
final first = app.resumeAgent('m', 'a0');
final original = connection.requests.last['creationId'];
connection.restartReplies.last.completeError(
const WsRequestFailure(
responseType: 'agent_resume_result',
code: 'IDENTITY_UNAVAILABLE',
detail: 'identity check failed',
),
);
expect((await first).error, contains('could not be verified'));
expect(app.restartAttempt('m', 'a0').awaitingConfirmation, isFalse);
final retry = app.resumeAgent('m', 'a0');
expect(connection.requests.last['creationId'], isNot(original));
expect(connection.checks, isEmpty);
connection.restartReplies.last.complete(
restartReceipt(
connection.requests.last['creationId'] as String,
sessionId: source.sessionId,
),
);
expect((await retry).error, isNull);
},
);
test(
'a busy refusal opens a concurrently resumed copy of the same conversation',
() async {
app.stateOf('m')!.agents = [
Agent.fromJson((inventory()['agents'] as List).single),
];
final first = app.resumeAgent('m', 'a0');
app.stateOf('m')!.agents = [source];
connection.restartReplies.last.completeError(
const WsRequestFailure(
responseType: 'agent_resume_result',
code: 'AGENT_BUSY',
),
);
expect((await first).error, isNull);
expect(connection.requests, hasLength(1));
},
);
test('thirty pause/resume cycles preserve identity and issue one command per transition', () async {
for (var cycle = 0; cycle < 30; cycle++) {
connection.inventory = Completer<Map<String, dynamic>>()
..complete(inventory());
final pause = app.pauseAgent('m', 'a0');
expect(app.pauseAgent('m', 'a0'), same(pause));
connection.stopReplies.last.complete({'deleted': true});
expect(await pause, isNull);
expect(app.stateOf('m')!.agents.single.isStopped, isTrue);
final resume = app.resumeAgent('m', 'a0');
expect(app.resumeAgent('m', 'a0'), same(resume));
connection.restartReplies.last.complete(
restartReceipt(
connection.requests.last['creationId'] as String,
sessionId: source.sessionId,
),
);
expect((await resume).error, isNull);
expect(app.stateOf('m')!.agents.single.sessionId, source.sessionId);
expect(app.stateOf('m')!.agents.single.isStopped, isFalse);
}
expect(connection.stops, hasLength(30));
expect(connection.types, List.filled(30, 'agent_resume'));
expect(connection.checks, isEmpty);
});
test(
'a resume that never reached the daemon can retry the same receipt',
() async {
app.stateOf('m')!.agents = [
Agent.fromJson((inventory()['agents'] as List).single),
];
final first = app.resumeAgent('m', 'a0');
final creationId = connection.requests.single['creationId'] as String;
connection.restartReplies.single.completeError(
const WsRequestTimeout('agent_resume'),
);
expect((await first).error, contains('Still waiting'));
final retry = app.resumeAgent('m', 'a0');
connection.checkReplies.single.complete({
'creationId': creationId,
'state': 'missing',
});
await Future<void>.delayed(Duration.zero);
expect(connection.types, ['agent_resume', 'agent_resume']);
expect(
connection.requests.last,
connection.requests.first,
reason: 'replay the same durable intent so a delayed original cannot launch twice',
);
connection.restartReplies.last.complete(
restartReceipt(creationId, sessionId: source.sessionId),
);
expect((await retry).error, isNull);
expect(app.stateOf('m')!.agents.single.sessionId, source.sessionId);
},
);
test('a resume the terminal service could not take says what to do', () async {
app.stateOf('m')!.agents = [
Agent.fromJson((inventory()['agents'] as List).single),
];
final resume = app.resumeAgent('m', 'a0');
connection.restartReplies.single.complete({
'creationId': connection.requests.single['creationId'],
'state': 'failed',
'failure': {
'code': 'PTY_HOST_UNAVAILABLE',
'detail': 'harness-pty is not installed',
},
});
expect(
(await resume).error,
'Harness could not open a terminal on this machine. Run harness start on it, then try again.',
);
});
for (final (detail, expected) in [
('Use another engine.', 'Use another engine.'),
('', 'This engine cannot start on this machine. Nothing was started.'),
]) {
test('a resume an engine refuses says why (detail "$detail")', () async {
app.stateOf('m')!.agents = [
Agent.fromJson((inventory()['agents'] as List).single),
];
final resume = app.resumeAgent('m', 'a0');
connection.restartReplies.single.complete({
'creationId': connection.requests.single['creationId'],
'state': 'failed',
'failure': {'code': 'ENGINE_LAUNCH_REFUSED', 'detail': detail},
});
expect((await resume).error, expected);
});
}
for (final state in ['pending', 'unconfirmed', 'wrong receipt']) {
test(
'an uncertain resume ($state) does not issue another launch',
() async {
app.stateOf('m')!.agents = [
Agent.fromJson((inventory()['agents'] as List).single),
];
final first = app.resumeAgent('m', 'a0');
final creationId = connection.requests.single['creationId'];
connection.restartReplies.single.completeError(
const WsRequestTimeout('agent_resume'),
);
await first;
final retry = app.resumeAgent('m', 'a0');
connection.checkReplies.single.complete({
'creationId': state == 'wrong receipt'
? 'another-intent'
: creationId,
'state': state == 'wrong receipt' ? 'missing' : state,
});
expect((await retry).error, isNotNull);
expect(connection.types, ['agent_resume']);
},
);
}
test(
'a confirmed pause remains in inventory when its refresh fails',
() async {
connection.inventory = Completer<Map<String, dynamic>>();
final pause = app.pauseAgent('m', 'a0');
connection.stopReplies.single.complete({'deleted': true});
await Future<void>.delayed(Duration.zero);
expect(app.stateOf('m')!.agents.single.isStopped, isTrue);
expect(app.pendingAgentPause('m', 'a0'), same(pause));
expect(
(await app.resumeAgent('m', 'a0')).error,
contains('still stopping'),
);
expect(connection.requests, isEmpty);
connection.inventory!.completeError(StateError('inventory unavailable'));
expect(await pause, isNull);
expect(app.stateOf('m')!.agents.single.sessionId, source.sessionId);
expect(app.stateOf('m')!.agents.single.isStopped, isTrue);
expect(app.pendingAgentPause('m', 'a0'), isNull);
},
);
test(
'a lost stop reply resolves through inventory without another stop',
() async {
connection.inventory = Completer<Map<String, dynamic>>()
..complete(inventory());
final pause = app.pauseAgent('m', 'a0');
connection.stopReplies.single.completeError(
const WsRequestTimeout('agent_delete'),
);
expect(await pause, isNull);
expect(app.stateOf('m')!.agents.single.isStopped, isTrue);
expect(connection.stops, ['a0']);
},
);
test(
'a native refusal keeps the live harness and its actionable error',
() async {
connection.inventory = Completer<Map<String, dynamic>>()
..complete(inventory(paused: false));
final pause = app.pauseAgent('m', 'a0');
connection.stopReplies.single.completeError(
const WsRequestFailure(
responseType: 'agent_delete_result',
code: 'STOP_UNCONFIRMED',
detail: 'Could not confirm that the harness stopped.',
),
);
expect(
await pause,
contains('Could not confirm that the harness stopped.'),
);
expect(app.stateOf('m')!.agents.single.isStopped, isFalse);
expect(connection.stops, ['a0']);
},
);
test(
'an ambiguous stop reply cannot turn a live harness into paused',
() async {
connection.inventory = Completer<Map<String, dynamic>>()
..complete(inventory(paused: false));
final pause = app.pauseAgent('m', 'a0');
connection.stopReplies.single.complete({});
expect(await pause, contains('Could not confirm'));
expect(app.stateOf('m')!.agents.single.isStopped, isFalse);
},
);
test('pause is refused only where the daemon could not carry it out', () async {
// No `resumeMode` on the frame: an older CLI, whose resume refuses anything
// but claude/codex with a recorded conversation.
app.stateOf('m')!.agents = [
const Agent(
id: 'a0',
name: 'Other engine',
engine: 'opencode',
sessionId: 'saved',
terminalAvailable: true,
),
];
expect(await app.pauseAgent('m', 'a0'), isNotNull);
app.stateOf('m')!.agents = [
const Agent(
id: 'a0',
name: 'Not saved yet',
engine: 'claude',
terminalAvailable: true,
),
];
expect(await app.pauseAgent('m', 'a0'), isNotNull);
expect(connection.stops, isEmpty);
});
test(
'any engine a current daemon reports pauses, whatever it can restore',
() async {
for (final mode in ['conversation', 'fresh']) {
app.stateOf('m')!.agents = [
Agent(
id: 'a0',
name: 'Other engine',
engine: 'opencode',
sessionId: mode == 'conversation' ? 'saved' : null,
resumeMode: mode,
terminalAvailable: true,
),
];
connection.stops.clear();
connection.inventory = Completer<Map<String, dynamic>>()
..complete({
'agents': [
{
'id': 'a0',
'name': 'Other engine',
'engine': 'opencode',
'resumeMode': mode,
'status': 'stopped',
'terminal': {'available': false},
},
],
});
final pause = app.pauseAgent('m', 'a0');
connection.stopReplies.last.complete({'deleted': true});
expect(await pause, isNull, reason: mode);
expect(connection.stops, ['a0'], reason: mode);
expect(app.stateOf('m')!.agents.single.isStopped, isTrue, reason: mode);
}
},
);
test(
'a resume that opens a new conversation is a success, not a surprise',
() async {
// devin has no resume argv: the daemon says `fresh`, answers `resumed:false`
// and hands back a different session id — all three as promised.
app.stateOf('m')!.agents = [
const Agent(
id: 'a0',
name: 'No resume flag',
engine: 'devin',
sessionId: 'archived',
resumeMode: 'fresh',
status: 'stopped',
),
];
final resume = app.resumeAgent('m', 'a0');
connection.restartReplies.single.complete({
'creationId': connection.requests.single['creationId'],
'state': 'created',
'resumed': false,
'agent': {
'id': 'a0',
'name': 'No resume flag',
'engine': 'devin',
'resumeMode': 'fresh',
'sessionId': 'brand-new',
'terminal': {'available': true},
},
});
final result = await resume;
expect(result.error, isNull);
// `resumed: false` is reported as the truth it is, not a failed resume.
expect(result.resumed, isFalse);
expect(app.stateOf('m')!.agents.single.sessionId, 'brand-new');
expect(app.stateOf('m')!.agents.single.isStopped, isFalse);
},
);
test(
'a terminal pauses without a saved conversation, and comes back',
() async {
// The daemon relaunches a shell with no sessionId (`resumeStoppedAgent.ts`),
// so nothing here has a conversation to wait for.
app.stateOf('m')!.agents = [
const Agent(
id: 'shell',
name: 'Untitled Pane',
engine: 'terminal',
terminalAvailable: true,
),
];
connection.inventory = Completer<Map<String, dynamic>>()
..complete({
'agents': [
{
'id': 'shell',
'name': 'Untitled Pane',
'engine': 'terminal',
'status': 'stopped',
'terminal': {'available': false},
},
],
});
final pause = app.pauseAgent('m', 'shell');
connection.stopReplies.single.complete({'deleted': true});
expect(await pause, isNull);
expect(connection.stops, ['shell']);
expect(app.stateOf('m')!.agents.single.isStopped, isTrue);
final resume = app.resumeAgent('m', 'shell');
connection.restartReplies.single.complete(
restartReceipt(
connection.requests.single['creationId'] as String,
agentId: 'shell',
name: 'Untitled Pane',
),
);
expect((await resume).error, isNull);
expect(app.stateOf('m')!.agents.single.isStopped, isFalse);
},
);
}