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
fix(mobile): a pager guess reopened after its stream died no longer takes the terminal
dee73cae1066ded
diff --git a/mobile/lib/state/app_state.dart b/mobile/lib/state/app_state.dartindex a5c51261e..75c36cc54 100644--- a/mobile/lib/state/app_state.dart+++ b/mobile/lib/state/app_state.dart@@ -6153,7 +6153,10 @@ class AppNotifier extends ChangeNotifier {// recovery every tile shares (`_attachPendingPanes`) to decide, never// for a guess about the next swipe.if (existing.warm && _paneNeedsAttach(existing)) {- await _reattachPane(existing);+ // ⚠️ Automatic, spelled out: [_reattachPane] defaults to a PERSON,+ // which raises the session's takeover — and a guess reopened that way+ // took the terminal off the desktop with nobody on the page.+ await _reattachPane(existing, intent: AttachIntent.automatic);}return;}diff --git a/mobile/test/app_state_terminals_test.dart b/mobile/test/app_state_terminals_test.dartnew file mode 100644index 000000000..cff823d44--- /dev/null+++ b/mobile/test/app_state_terminals_test.dart@@ -0,0 +1,621 @@+import 'dart:typed_data';++import 'package:flutter_test/flutter_test.dart';+import 'package:harness_mobile/state/app_state.dart';+import 'package:harness_mobile/terminal/terminal_binary.dart';+import 'package:harness_mobile/terminal/terminal_session.dart';++import 'viewer_app_fixture.dart';+import 'voice_fakes.dart' show MemoryKeyValueStore;++/// The terminals a phone holds open: the one a person opened, the ones the+/// pager opened ahead of the thumb, and what happens to each when the machine,+/// the stream or the person does something.+void main() {+ List<Map<String, dynamic>> opensFor(ViewerRig rig, String agentId) => [+ for (final open in rig.conn('m').opens)+ if (open['agentId'] == agentId) open,+ ];++ /// The pager warming [agentId] on `m`, its parked page measuring itself.+ Future<void> warm(ViewerRig rig, String agentId) async {+ final warming = rig.app.warmAgentPane('m', agentId);+ await settle();+ rig.app.paneOfAgent('m', agentId)?.session?.reportViewport(80, 24);+ await warming;+ }++ group('opening an agent', () {+ test('takes its terminal, says who is asking, and selects it', () async {+ final rig = await signedInWith({+ 'm': ['a', 'b'],+ });+ addTearDown(rig.app.dispose);++ final session = await openAgent(rig, 'm', 'a');++ final open = opensFor(rig, 'a').single;+ expect(open.containsKey('takeover'), isFalse, reason: 'absent is take');+ expect((open['client'] as Map)['kind'], 'phone');+ expect(open['cols'], 80);+ expect(session.status, TerminalSessionStatus.controlling);+ expect(rig.app.stateOf('m')!.activeAgentId, 'a');+ expect(rig.app.selectedMachineId, 'm');+ expect(rig.app.focusedPane?.agentId, 'a');+ });++ test('an agent already live is only focused, not opened again', () async {+ final rig = await signedInWith({+ 'm': ['a', 'b'],+ });+ addTearDown(rig.app.dispose);+ await openAgent(rig, 'm', 'a');+ await openAgent(rig, 'm', 'b');++ await rig.app.selectAgent('m', 'a');++ expect(opensFor(rig, 'a'), hasLength(1));+ expect(rig.app.focusedPane?.agentId, 'a');+ });++ test(+ 'an agent taken by another app is asked for again on arrival',+ () async {+ final rig = await signedInWith({+ 'm': ['a'],+ });+ addTearDown(rig.app.dispose);+ final session = await openAgent(rig, 'm', 'a');+ await push(rig, 'm', 'terminal_closed', {+ 'streamId': session.streamId,+ 'code': 'TERMINAL_TAKEN_OVER',+ 'takenBy': {'kind': 'desktop', 'name': 'MacBook Pro'},+ });+ expect(session.status, TerminalSessionStatus.takenOver);+ expect(session.errorMessage, 'MacBook Pro connected to this terminal.');++ await rig.app.selectAgent('m', 'a');++ expect(opensFor(rig, 'a'), hasLength(2));+ expect(opensFor(rig, 'a').last.containsKey('takeover'), isFalse);+ },+ );++ test(+ 'a watcher promoted by a person reopens to take the keyboard',+ () async {+ final rig = await signedInWith({+ 'm': ['a'],+ });+ addTearDown(rig.app.dispose);+ await warm(rig, 'a');+ final session = rig.app.paneOfAgent('m', 'a')!.session!;+ await settle();+ await push(rig, 'm', 'terminal_ready', {+ 'requestId': opensFor(rig, 'a').single['requestId'],+ 'agentId': 'a',+ 'protocolVersion': TerminalSession.protocolVersion,+ 'streamId': newStreamId(),+ 'readOnly': true,+ 'heldBy': {'kind': 'desktop', 'name': 'Studio'},+ });+ expect(session.watching, isTrue);+ expect(session.heldBy?.name, 'Studio');+ expect(session.acceptsInput, isFalse);++ await rig.app.selectAgent('m', 'a');++ expect(opensFor(rig, 'a'), hasLength(2));+ expect(opensFor(rig, 'a').last.containsKey('takeover'), isFalse);+ expect(rig.app.paneOfAgent('m', 'a')!.warm, isFalse);+ },+ );++ test('a dead stream is reopened in place, keeping its screen', () async {+ final rig = await signedInWith({+ 'm': ['a'],+ });+ addTearDown(rig.app.dispose);+ final session = await openAgent(rig, 'm', 'a');+ session.transportLost('gone');++ await rig.app.selectAgent('m', 'a');++ expect(opensFor(rig, 'a'), hasLength(2));+ expect(+ identical(rig.app.paneOfAgent('m', 'a')!.session, session),+ isTrue,+ );+ expect(session.status, TerminalSessionStatus.opening);+ });++ test('on a machine that is off, the page waits and remembers it', () async {+ final rig = await signedInWith({+ 'm': ['a'],+ });+ addTearDown(rig.app.dispose);+ await push(rig, 'm', 'node_status', {'online': false});++ await rig.app.selectAgent('m', 'a');++ expect(rig.app.paneOfAgent('m', 'a'), isNotNull);+ expect(rig.app.paneOfAgent('m', 'a')!.session, isNull);+ expect(rig.app.stateOf('m')!.pendingOfflineAgentId, 'a');+ expect(opensFor(rig, 'a'), isEmpty);+ });++ test('on a machine with no terminal protocol, nothing is opened', () async {+ final rig = await signedInWith({+ 'm': ['a'],+ });+ addTearDown(rig.app.dispose);+ rig.app.stateOf('m')!.terminalCapabilityAvailable = false;++ await rig.app.selectAgent('m', 'a');++ expect(rig.app.paneOfAgent('m', 'a')!.session, isNull);+ expect(opensFor(rig, 'a'), isEmpty);+ });++ test('an agent the machine does not list is not opened at all', () async {+ final rig = await signedInWith({+ 'm': ['a'],+ });+ addTearDown(rig.app.dispose);++ await rig.app.selectAgent('m', 'ghost');+ await rig.app.selectAgent('ghost', 'a');++ expect(rig.app.allPanes, isEmpty);+ });++ test('a page waiting for its terminal is opened by the next tap', () async {+ final rig = await signedInWith({+ 'm': ['a'],+ });+ addTearDown(rig.app.dispose);+ rig.app.stateOf('m')!.terminalCapabilityAvailable = false;+ await rig.app.selectAgent('m', 'a');+ rig.app.stateOf('m')!.terminalCapabilityAvailable = true;++ final opening = rig.app.selectAgent('m', 'a');+ await settle();+ rig.app.paneOfAgent('m', 'a')!.session!.reportViewport(80, 24);+ await opening;++ expect(opensFor(rig, 'a'), hasLength(1));+ });++ test(+ 'names this phone after the person when the device has no name',+ () async {+ final rig = await signedInWith({+ 'm': ['a'],+ });+ addTearDown(rig.app.dispose);++ final descriptor = rig.app.phoneClientDescriptor();++ expect(descriptor.kind, 'phone');+ expect(descriptor.name, "Pat's phone");+ },+ );+ });++ group('the pager opening ahead of the thumb', () {+ test('never on a machine that would take the terminal to do it', () async {+ final rig = await signedInWith({+ 'm': ['a'],+ }, noTakeover: false);+ addTearDown(rig.app.dispose);++ await rig.app.warmAgentPane('m', 'a');++ expect(rig.app.allPanes, isEmpty);+ expect(opensFor(rig, 'a'), isEmpty);+ });++ test('politely, without moving focus or the saved layout', () async {+ final storage = MemoryKeyValueStore();+ final rig = await signedInWith({+ 'm': ['a', 'b'],+ }, storage: storage);+ addTearDown(rig.app.dispose);+ await openAgent(rig, 'm', 'a');+ await rig.app.flushPaneLayout();+ final saved = storage.values['swarm_layout_v1'];++ await warm(rig, 'b');+ await rig.app.flushPaneLayout();++ final pane = rig.app.paneOfAgent('m', 'b')!;+ expect(pane.warm, isTrue);+ expect(opensFor(rig, 'b').single['takeover'], isFalse);+ expect(rig.app.focusedPane?.agentId, 'a');+ expect(rig.app.stateOf('m')!.activeAgentId, 'a');+ expect(storage.values['swarm_layout_v1'], saved);+ });++ test('a guess that comes true joins the layout', () async {+ final storage = MemoryKeyValueStore();+ final rig = await signedInWith({+ 'm': ['a', 'b'],+ }, storage: storage);+ addTearDown(rig.app.dispose);+ await warm(rig, 'b');++ await rig.app.selectAgent('m', 'b');+ await rig.app.flushPaneLayout();++ expect(rig.app.paneOfAgent('m', 'b')!.warm, isFalse);+ expect(storage.values['swarm_layout_v1'], contains('"agentId":"b"'));+ });++ test(+ 'a warm page with a dead stream is reopened; a live one is left',+ () async {+ final rig = await signedInWith({+ 'm': ['a'],+ });+ addTearDown(rig.app.dispose);+ await warm(rig, 'a');+ await rig.app.warmAgentPane('m', 'a');+ expect(opensFor(rig, 'a'), hasLength(1));++ rig.app.paneOfAgent('m', 'a')!.session!.transportLost('gone');+ await rig.app.warmAgentPane('m', 'a');++ expect(opensFor(rig, 'a'), hasLength(2));+ expect(opensFor(rig, 'a').last['takeover'], isFalse);+ },+ );++ test('a page a person has looked at is not reopened by a guess', () async {+ final rig = await signedInWith({+ 'm': ['a'],+ });+ addTearDown(rig.app.dispose);+ final session = await openAgent(rig, 'm', 'a');+ session.transportLost('gone');++ await rig.app.warmAgentPane('m', 'a');++ expect(opensFor(rig, 'a'), hasLength(1));+ });++ test(+ 'a refused polite open waits for a person, not a retry loop',+ () async {+ final rig = await signedInWith({+ 'm': ['a'],+ });+ addTearDown(rig.app.dispose);+ await warm(rig, 'a');+ final session = rig.app.paneOfAgent('m', 'a')!.session!;++ await push(rig, 'm', 'terminal_error', {+ 'requestId': opensFor(rig, 'a').single['requestId'],+ 'code': 'CONTROL_LEASE_HELD',+ });+ expect(session.status, TerminalSessionStatus.takenOver);++ // The machine's list landing again reattaches what needs it — not this.+ await rig.app.reloadMachineData('m');+ await settle();+ expect(opensFor(rig, 'a'), hasLength(1));+ },+ );+ });++ group('closing a page', () {+ test(+ 'closes its stream, and shows its last screen when it is back',+ () async {+ final rig = await signedInWith({+ 'm': ['a'],+ });+ addTearDown(rig.app.dispose);+ final session = await openAgent(rig, 'm', 'a');+ final streamId = session.streamId;++ await rig.app.closePane(rig.app.paneOfAgent('m', 'a')!.id);++ expect(rig.app.paneOfAgent('m', 'a'), isNull);+ expect(+ rig+ .conn('m')+ .frames+ .where(+ (f) => f.$1 == 'terminal_close' && f.$2['streamId'] == streamId,+ ),+ hasLength(1),+ );++ final reopening = rig.app.selectAgent('m', 'a');+ await settle();+ final again = rig.app.paneOfAgent('m', 'a')!.session!;+ expect(again.showingKeptScreen, isTrue);+ expect(again.hasScreen, isTrue);+ again.reportViewport(80, 24);+ await reopening;+ await answerOpen(rig, again);+ expect(again.showingKeptScreen, isFalse);+ },+ );++ test('a page closed before it drew anything keeps nothing', () async {+ final rig = await signedInWith({+ 'm': ['a'],+ });+ addTearDown(rig.app.dispose);+ final opening = rig.app.selectAgent('m', 'a');+ await settle();+ rig.app.paneOfAgent('m', 'a')!.session!.reportViewport(80, 24);+ await opening;++ await rig.app.closePane(rig.app.paneOfAgent('m', 'a')!.id);+ final reopening = rig.app.selectAgent('m', 'a');+ await settle();++ expect(rig.app.paneOfAgent('m', 'a')!.session!.hasScreen, isFalse);+ rig.app.paneOfAgent('m', 'a')!.session!.reportViewport(80, 24);+ await reopening;+ });++ test('only the last few screens are kept', () async {+ final rig = await signedInWith({+ 'm': ['a', 'b', 'c', 'd'],+ });+ addTearDown(rig.app.dispose);+ for (final id in ['a', 'b', 'c', 'd']) {+ await openAgent(rig, 'm', id);+ await rig.app.closePane(rig.app.paneOfAgent('m', id)!.id);+ }++ final reopening = rig.app.selectAgent('m', 'a');+ await settle();+ expect(rig.app.paneOfAgent('m', 'a')!.session!.hasScreen, isFalse);+ rig.app.paneOfAgent('m', 'a')!.session!.reportViewport(80, 24);+ await reopening;+ });++ test('an unknown page is nothing to close', () async {+ final rig = await signedInWith({+ 'm': ['a'],+ });+ addTearDown(rig.app.dispose);++ await rig.app.closePane(999);++ expect(rig.app.allPanes, isEmpty);+ });+ });++ group('frames from the machine', () {+ test(+ 'a frame that cannot be decoded takes down every stream on its machine',+ () async {+ final rig = await signedInWith({+ 'm': ['a', 'b'],+ });+ addTearDown(rig.app.dispose);+ final a = await openAgent(rig, 'm', 'a');+ final b = await openAgent(rig, 'm', 'b');++ await rig.app.handleTerminalBinaryForTest(+ 'm',+ Uint8List.fromList([1, 2, 3]),+ );++ expect(a.status, TerminalSessionStatus.error);+ expect(b.status, TerminalSessionStatus.error);+ },+ );++ test('a frame reaches only the stream it names', () async {+ final rig = await signedInWith({+ 'm': ['a', 'b'],+ });+ addTearDown(rig.app.dispose);+ final a = await openAgent(rig, 'm', 'a');+ final b = await openAgent(rig, 'm', 'b');+ final before = b.outputTicks.value;++ await rig.app.handleTerminalBinaryForTest(+ 'm',+ encodeTerminalLocal(+ TerminalBinaryFrame(+ kind: TerminalBinaryKind.output,+ streamId: a.streamId!,+ seq: 1,+ bytes: Uint8List.fromList('hello'.codeUnits),+ compressed: false,+ ),+ )!,+ );++ expect(a.outputTicks.value, greaterThan(0));+ expect(b.outputTicks.value, before);+ });++ test('a frame for a machine with nothing open is dropped', () async {+ final rig = await signedInWith({+ 'm': ['a'],+ });+ addTearDown(rig.app.dispose);++ await rig.app.handleTerminalBinaryForTest(+ 'm',+ Uint8List.fromList([1, 2, 3]),+ );++ expect(rig.app.allPanes, isEmpty);+ });++ test('what is typed is sent down the machine\'s socket, encoded', () async {+ final rig = await signedInWith({+ 'm': ['a'],+ });+ addTearDown(rig.app.dispose);+ final session = await openAgent(rig, 'm', 'a');++ session.terminal.textInput('ls\r');+ await settle();++ final input = rig+ .conn('m')+ .binaries+ .map(decodeTerminalLocal)+ .whereType<TerminalBinaryFrame>()+ .where((frame) => frame.kind == TerminalBinaryKind.input)+ .toList();+ expect(String.fromCharCodes(input.expand((f) => f.bytes)), 'ls\r');+ expect(input.first.streamId, session.streamId);+ });++ test('an open that stalls on a live socket redials it once', () async {+ final rig = await signedInWith({+ 'm': ['a'],+ });+ addTearDown(rig.app.dispose);+ final opening = rig.app.selectAgent('m', 'a');+ await settle();+ final session = rig.app.paneOfAgent('m', 'a')!.session!;+ session.reportViewport(80, 24);+ await opening;++ // No terminal_ready: past the session's watchdog, the socket is suspect.+ await Future<void>.delayed(+ session.resyncTimeout + const Duration(milliseconds: 100),+ );++ expect(rig.conn('m').redials, 1);+ expect(opensFor(rig, 'a'), hasLength(2), reason: 'the same open, again');+ expect(+ opensFor(rig, 'a').last['requestId'],+ opensFor(rig, 'a').first['requestId'],+ );+ });++ test('an open on a socket still shaking hands is not redialled', () async {+ final rig = await signedInWith({+ 'm': ['a'],+ });+ addTearDown(rig.app.dispose);+ rig.conn('m').ready = false;+ final opening = rig.app.selectAgent('m', 'a');+ await settle();+ final session = rig.app.paneOfAgent('m', 'a')!.session!;+ session.reportViewport(80, 24);+ await opening;++ await Future<void>.delayed(+ session.resyncTimeout + const Duration(milliseconds: 100),+ );++ expect(rig.conn('m').redials, 0);+ expect(session.status, TerminalSessionStatus.error);+ });+ });++ group('a machine dropping out with several pages open', () {+ test('the page being read is the one recovery brings back', () async {+ final rig = await signedInWith({+ 'm': ['a', 'b'],+ }, noTakeover: false);+ addTearDown(rig.app.dispose);+ await openAgent(rig, 'm', 'a');+ await openAgent(rig, 'm', 'b');+ // `b` is focused, but `a` comes first in the list.+ expect(rig.app.focusedPane?.agentId, 'b');++ await push(rig, 'm', 'node_status', {'online': false});++ expect(rig.app.stateOf('m')!.pendingOfflineAgentId, 'b');+ });++ test('a page someone else took is not recorded to be taken back', () async {+ final rig = await signedInWith({+ 'm': ['a'],+ });+ addTearDown(rig.app.dispose);+ final session = await openAgent(rig, 'm', 'a');+ await push(rig, 'm', 'terminal_closed', {+ 'streamId': session.streamId,+ 'code': 'TERMINAL_TAKEN_OVER',+ });++ await push(rig, 'm', 'node_status', {'online': false});++ expect(rig.app.stateOf('m')!.pendingOfflineAgentId, isNull);+ expect(session.status, TerminalSessionStatus.takenOver);+ });+ });++ group('the layout across launches', () {+ test('a page opened on one launch is waiting on the next', () async {+ final storage = MemoryKeyValueStore();+ final first = await signedInWith({+ 'm': ['a', 'b'],+ }, storage: storage);+ await openAgent(first, 'm', 'b');+ await first.app.flushPaneLayout();+ first.app.dispose();++ final next = viewerApp(storage: storage);+ addTearDown(next.app.dispose);+ await next.app.restorePaneLayoutForTest();++ final restored = next.app.allPanes.single;+ expect((restored.machineId, restored.agentId), ('m', 'b'));+ expect(restored.session, isNull, reason: 'intent, until m answers');+ });++ test('a layout that cannot be read is a first launch', () async {+ final storage = MemoryKeyValueStore()+ ..values['swarm_layout_v1'] = '{not json';+ final rig = viewerApp(storage: storage);+ addTearDown(rig.app.dispose);++ await rig.app.restorePaneLayoutForTest();++ expect(rig.app.allPanes, isEmpty);+ });++ test('an older build\'s single list of tiles is read too', () async {+ final storage = MemoryKeyValueStore()+ ..values['terminal_pane_layout'] =+ '[{"machineId":"m","agentId":"a"},{"machineId":"m","agentId":"a"}]';+ final rig = viewerApp(storage: storage);+ addTearDown(rig.app.dispose);++ await rig.app.restorePaneLayoutForTest();++ expect(rig.app.allPanes.map((p) => p.agentId), ['a']);+ expect(rig.app.focusedPane?.agentId, 'a');+ });+ });++ test(+ 'an agent on a machine the phone signs out of is not reopened',+ () async {+ final rig = await signedInWith({+ 'm': ['a'],+ });+ addTearDown(rig.app.dispose);+ await openAgent(rig, 'm', 'a');++ await rig.app.logout();++ expect(rig.app.allPanes, isEmpty);+ expect(rig.app.stateOf('m'), isNull);+ expect(rig.app.status, AppStatus.unauthenticated);+ expect(+ rig.conn('m').frames.where((f) => f.$1 == 'terminal_close'),+ hasLength(1),+ reason: 'its stream is let go of properly',+ );+ expect(rig.app.machines, isEmpty);+ },+ );+}diff --git a/mobile/test/viewer_app_fixture.dart b/mobile/test/viewer_app_fixture.dartindex dc04d8417..783f70648 100644--- a/mobile/test/viewer_app_fixture.dart+++ b/mobile/test/viewer_app_fixture.dart@@ -9,9 +9,13 @@ import 'package:harness_mobile/auth/cli_login.dart';import 'package:harness_mobile/auth/peer_link_client.dart';import 'package:harness_mobile/auth/sign_in_client.dart';import 'package:harness_mobile/core/config.dart';+import 'package:harness_mobile/core/local_key_value_store.dart';import 'package:harness_mobile/core/machine_cache.dart';import 'package:harness_mobile/core/models.dart';+import 'package:harness_mobile/core/snapshot_store.dart';+import 'package:harness_mobile/notify/system_notices.dart';import 'package:harness_mobile/state/app_state.dart';+import 'package:harness_mobile/state/pane_layout_store.dart';import 'package:harness_mobile/terminal/terminal_binary.dart';import 'package:harness_mobile/terminal/terminal_session.dart';import 'package:harness_mobile/viewer/direct_auth.dart';@@ -390,9 +394,15 @@ class ViewerRig {/// A phone app — a VIEWER, with no CLI beside it — signed out, on an account/// whose machines [FakeApi.onMachines] lists.+///+/// [storage] is what the app keeps across launches — the layout, the last+/// agent open — held in memory. Given one, the app also keeps its machine+/// cache (in memory unless [machineCache] says otherwise) and would reach the+/// OS notification centre, which is silenced here.ViewerRig viewerApp({bool signedIn = true,MachineCache? machineCache,+ LocalKeyValueStore? storage,Duration turnActivityTimeout = const Duration(seconds: 12),}) {final session = AuthSession(storage: MemoryKeyValueStore());@@ -408,7 +418,11 @@ ViewerRig viewerApp({cliLink: CliLink(),peerLinks: links,viewer: viewer,- machineCache: machineCache,+ paneLayoutStore: storage == null ? null : PaneLayoutStore(storage: storage),+ systemNotices: SilentSystemNotices(),+ machineCache:+ machineCache ??+ (storage == null ? null : MachineCache(store: MemorySnapshotStore())),turnActivityTimeout: turnActivityTimeout,connectionForTest: (machineId) =>conns.putIfAbsent(machineId, ScriptedConn.new),@@ -437,9 +451,13 @@ Future<void> settle() async {Future<ViewerRig> signedInWith(Map<String, List<String>> agents, {bool noTakeover = true,+ LocalKeyValueStore? storage,Duration turnActivityTimeout = const Duration(seconds: 12),}) async {- final rig = viewerApp(turnActivityTimeout: turnActivityTimeout);+ final rig = viewerApp(+ storage: storage,+ turnActivityTimeout: turnActivityTimeout,+ );rig.api.onMachines = () async => [for (final id in agents.keys) remoteMachine(id),];