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.

Preserve latest terminal output when opening smaller swarm panes

dee26a372ef3558
diff --git a/desktop/lib/state/app_state.dart b/desktop/lib/state/app_state.dart
index 6b4b61b7d..b83348c73 100644
--- a/desktop/lib/state/app_state.dart
+++ b/desktop/lib/state/app_state.dart
@@ -4012,6 +4012,7 @@ class AppNotifier extends ChangeNotifier {
final pane =
shared ??
TerminalPane(id: _nextPaneId++, machineId: machineId, agentId: agentId);
+ final firstAgent = targetPanes.every((pane) => pane.agentId == null);
targetPanes.insert(insertion.clamp(0, targetPanes.length), pane);
if (split != null) {
target.pinnedSlots.updateAll(
@@ -4022,6 +4023,12 @@ class AppNotifier extends ChangeNotifier {
target.arranged = split.after;
target.arrangedKey = key;
}
+ if (firstAgent && target.name == 'New swarm') {
+ final name = agent.name.trim();
+ if (name.isNotEmpty) {
+ target.name = name.length > 80 ? name.substring(0, 80) : name;
+ }
+ }
if (replaced != null && !allPanes.contains(replaced)) {
// Release just the desktop stream. The CLI agent process keeps running.
unawaited(_detachSession(replaced, sendClose: true));
diff --git a/desktop/lib/widgets/terminal_panel.dart b/desktop/lib/widgets/terminal_panel.dart
index 6a60eff8d..0fd3d57ba 100644
--- a/desktop/lib/widgets/terminal_panel.dart
+++ b/desktop/lib/widgets/terminal_panel.dart
@@ -1111,6 +1111,7 @@ class _TerminalPanelState extends State<TerminalPanel>
key: _terminalViewKey,
controller: _controller,
autoResize: widget.visible,
+ resizeBuffer: false,
renderingEnabled: widget.visible,
scrollController: _scrollController,
focusNode: _focusNode,
diff --git a/desktop/test/swarm_state_test.dart b/desktop/test/swarm_state_test.dart
index d65c1d875..f662d2215 100644
--- a/desktop/test/swarm_state_test.dart
+++ b/desktop/test/swarm_state_test.dart
@@ -58,6 +58,39 @@ AppNotifier createApp({
}
void main() {
+ test(
+ 'first agent names a swarm and survives closing and reopening',
+ () async {
+ final app = createApp();
+ addTearDown(app.dispose);
+ await app.addAgentToSwarm('m', 'a0');
+ expect(app.activeSwarm.name, 'Agent 0');
+ await app.addAgentToSwarm('m', 'a1');
+ expect(app.activeSwarm.name, 'Agent 0');
+ expect(app.activeSwarm.toJson()['name'], 'Agent 0');
+ await app.closeSwarm(app.activeSwarmId);
+ app.reopenClosedSwarm();
+ expect(app.activeSwarm.name, 'Agent 0');
+ },
+ );
+
+ test(
+ 'first agent preserves custom names and names the requested swarm',
+ () async {
+ final app = createApp();
+ addTearDown(app.dispose);
+ app.renameSwarm(app.activeSwarmId, 'My project');
+ await app.addAgentToSwarm('m', 'a0');
+ expect(app.activeSwarm.name, 'My project');
+ app.newSwarm();
+ final target = app.activeSwarm;
+ app.newSwarm(name: 'Elsewhere');
+ await app.addAgentToSwarm('m', 'a1', swarmId: target.id);
+ expect(target.name, 'Agent 1');
+ expect(app.activeSwarm.name, 'Elsewhere');
+ },
+ );
+
test(
'shared memberships own one controller and close only the final stream',
() async {
diff --git a/desktop/test/terminal_initial_output_test.dart b/desktop/test/terminal_initial_output_test.dart
new file mode 100644
index 000000000..db2cfc24d
--- /dev/null
+++ b/desktop/test/terminal_initial_output_test.dart
@@ -0,0 +1,156 @@
+import 'dart:convert';
+
+import 'package:flutter/material.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness/state/pane_preset.dart';
+import 'package:harness/terminal/terminal_binary.dart';
+import 'package:xterm/xterm.dart';
+
+import 'swarm_screen_test.dart' show mount, terminal;
+import 'swarm_state_test.dart' show createApp;
+import 'terminal_find_test.dart' show output, terminalView;
+
+void main() {
+ testWidgets(
+ 'a tall captured screen keeps its latest rows in a new smaller pane',
+ (tester) async {
+ final app = createApp();
+ final session = terminal('a0', []);
+ app.adoptSessionForTest(session);
+ await mount(tester, app);
+ final snapshot = StringBuffer(
+ '\x1bc\x1b[?1049l\x1b[?25l\x1b[?7l\x1b[H\x1b[2J',
+ );
+ for (var i = 0; i < 500; i++) {
+ snapshot.write('Earlier output $i\x1b[0m\r\n');
+ }
+ snapshot.write('${'\r\n' * 100}\x1b[H\x1b[2J');
+ for (var row = 1; row <= 100; row++) {
+ snapshot.write('\x1b[$row;1HCurrent output $row\x1b[0m');
+ }
+ snapshot.write('\x1b[1;1H\x1b[?7h\x1b[?25l');
+ await session.handleBinary(
+ TerminalBinaryFrame(
+ kind: TerminalBinaryKind.keyframe,
+ streamId: session.streamId!,
+ seq: 0,
+ compressed: false,
+ cols: 120,
+ rows: 100,
+ bytes: utf8.encode(snapshot.toString()),
+ ),
+ );
+ expect(session.terminal.buffer.getText(), contains('Current output 100'));
+ final requested = <(int, int)>[];
+ final resize = session.terminal.onResize;
+ session.terminal.onResize = (cols, rows, width, height) {
+ requested.add((cols, rows));
+ resize?.call(cols, rows, width, height);
+ };
+ await tester.pump();
+ expect(session.terminal.buffer.getText(), contains('Current output 100'));
+ expect(
+ session.terminal.viewHeight,
+ 100,
+ reason: 'The remote screen stays intact until its resized replacement arrives',
+ );
+ expect(requested, hasLength(1));
+ expect(requested.single.$2, lessThan(100));
+ final scroll = terminalView(tester, session).widget.scrollController!;
+ expect(scroll.offset, scroll.position.maxScrollExtent);
+ for (var i = 0; i < 5; i++) {
+ session.terminal.write('\x1b[1;1HWorking');
+ await tester.pump();
+ }
+ expect(
+ requested,
+ hasLength(1),
+ reason: 'Output does not repeat the resize request',
+ );
+ await session.handleBinary(
+ TerminalBinaryFrame(
+ kind: TerminalBinaryKind.keyframe,
+ streamId: session.streamId!,
+ seq: 1,
+ compressed: false,
+ cols: requested.single.$1,
+ rows: requested.single.$2,
+ bytes: utf8.encode('Resized output\r\nLatest response'),
+ ),
+ );
+ await tester.pump();
+ expect(session.terminal.viewHeight, requested.single.$2);
+ expect(session.terminal.buffer.getText(), contains('Latest response'));
+ await tester.pumpWidget(const SizedBox());
+ app.dispose();
+ },
+ );
+ testWidgets(
+ 'incremental project loading and layout changes keep fresh views at the latest output',
+ (tester) async {
+ final app = createApp();
+ final sessions = [for (var i = 0; i < 5; i++) terminal('a$i', [])];
+ final history = List.generate(500, (i) => 'Output line $i\r\n').join();
+ app.adoptSessionForTest(sessions.first);
+ await mount(tester, app);
+ tester.view.physicalSize = const Size(2000, 1240);
+ await output(sessions.first, 0, history, keyframe: true);
+ await tester.pump();
+ for (final session in sessions.skip(1)) {
+ app.adoptSessionForTest(session);
+ await tester.pump();
+ await output(session, 0, history, keyframe: true);
+ await tester.pump();
+ }
+ app.setPreset(5, PanePreset.cols4);
+ await tester.pump();
+ for (final session in sessions) {
+ final scroll = terminalView(tester, session).widget.scrollController!;
+ expect(scroll.offset, scroll.position.maxScrollExtent);
+ }
+ await tester.pumpWidget(const SizedBox());
+ app.dispose();
+ },
+ );
+ for (final delayed in [false, true]) {
+ testWidgets(
+ 'five fresh agent views start at the latest output (delayed=$delayed)',
+ (tester) async {
+ final app = createApp();
+ final sessions = [for (var i = 0; i < 5; i++) terminal('a$i', [])];
+ final history = List.generate(500, (i) => 'Output line $i\r\n').join();
+ for (final session in sessions) {
+ if (!delayed) session.terminal.write(history);
+ app.adoptSessionForTest(session);
+ }
+ await mount(tester, app);
+ if (delayed) {
+ for (final session in sessions) {
+ await output(session, 0, history, keyframe: true);
+ }
+ await tester.pump();
+ }
+ for (final session in sessions) {
+ final scroll = terminalView(tester, session).widget.scrollController!;
+ expect(scroll.position.maxScrollExtent, greaterThan(0));
+ expect(scroll.offset, scroll.position.maxScrollExtent);
+ }
+ // Revisiting an intentionally scrolled view preserves the reader's place.
+ final first = terminalView(tester, sessions.first);
+ final scroll = first.widget.scrollController!;
+ scroll.jumpTo(100);
+ await tester.pump();
+ final original = app.activeSwarmId;
+ app.newSwarm();
+ await tester.pump();
+ app.selectSwarm(original);
+ await tester.pump();
+ expect(terminalView(tester, sessions.first), same(first));
+ expect(scroll.offset, 100);
+ expect(find.byType(TerminalView), findsNWidgets(5));
+ await tester.pumpWidget(const SizedBox());
+ app.dispose();
+ },
+ );
+ }
+}
diff --git a/desktop/third_party/xterm/README.autonomous.md b/desktop/third_party/xterm/README.autonomous.md
index b1aa6df5f..5912e9a02 100644
--- a/desktop/third_party/xterm/README.autonomous.md
+++ b/desktop/third_party/xterm/README.autonomous.md
@@ -13,6 +13,15 @@ fix and the regression in `test/terminal_session_test.dart` passes against it.
## Local patches
+- **Remote grids survive viewport resizing** (`lib/src/terminal_view.dart`,
+ `lib/src/ui/render.dart`). `TerminalView.resizeBuffer` defaults to true;
+ Harness sets it to false so a smaller local pane reports its desired size
+ without deleting rows from the captured remote screen. The next remote
+ keyframe supplies the resized grid. Repeated output does not repeat the
+ resize request. Regressions: `test/terminal_initial_output_test.dart`, including
+ a tall screen with its cursor parked above the newest output, five fresh
+ panes, delayed snapshots, and preserved reading positions on return.
+
- **On-demand terminal find** (`lib/src/core/buffer/line.dart`,
`lib/src/terminal.dart`). Text mutations advance a local line version so a
search can reuse decoded rows; color-only changes keep the version. Optional
diff --git a/desktop/third_party/xterm/lib/src/terminal_view.dart b/desktop/third_party/xterm/lib/src/terminal_view.dart
index cb474af85..adbbbc56f 100644
--- a/desktop/third_party/xterm/lib/src/terminal_view.dart
+++ b/desktop/third_party/xterm/lib/src/terminal_view.dart
@@ -33,6 +33,7 @@ class TerminalView extends StatefulWidget {
this.padding,
this.scrollController,
this.autoResize = true,
+ this.resizeBuffer = true,
this.renderingEnabled = true,
this.backgroundOpacity = 1,
this.focusNode,
@@ -78,6 +79,11 @@ class TerminalView extends StatefulWidget {
/// size changes. [true] by default.
final bool autoResize;
+ /// Resize the emulator together with the viewport. Set false for a remote
+ /// grid: report the requested size through Terminal.onResize and retain the
+ /// captured cells until the remote side supplies its resized screen.
+ final bool resizeBuffer;
+
/// Whether output should schedule renderer layout/paint work. Disable while
/// retaining a hidden view; the terminal buffer continues receiving output.
/// Re-enabling reconciles geometry and scroll position on the next layout.
@@ -267,6 +273,7 @@ class TerminalViewState extends State<TerminalView> {
offset: offset,
padding: MediaQuery.of(context).padding,
autoResize: widget.autoResize,
+ resizeBuffer: widget.resizeBuffer,
renderingEnabled: widget.renderingEnabled,
textStyle: widget.textStyle,
textScaler: widget.textScaler ?? MediaQuery.textScalerOf(context),
@@ -648,6 +655,7 @@ class _TerminalView extends LeafRenderObjectWidget {
required this.offset,
required this.padding,
required this.autoResize,
+ required this.resizeBuffer,
required this.renderingEnabled,
required this.textStyle,
required this.textScaler,
@@ -670,6 +678,8 @@ class _TerminalView extends LeafRenderObjectWidget {
final bool autoResize;
+ final bool resizeBuffer;
+
final bool renderingEnabled;
final TerminalStyle textStyle;
@@ -698,6 +708,7 @@ class _TerminalView extends LeafRenderObjectWidget {
offset: offset,
padding: padding,
autoResize: autoResize,
+ resizeBuffer: resizeBuffer,
renderingEnabled:
renderingEnabled && TickerMode.valuesOf(context).enabled,
textStyle: textStyle,
@@ -722,6 +733,7 @@ class _TerminalView extends LeafRenderObjectWidget {
..offset = offset
..padding = padding
..autoResize = autoResize
+ ..resizeBuffer = resizeBuffer
..textStyle = textStyle
..textScaler = textScaler
..theme = theme
diff --git a/desktop/third_party/xterm/lib/src/ui/render.dart b/desktop/third_party/xterm/lib/src/ui/render.dart
index df79edd58..2eccb472f 100644
--- a/desktop/third_party/xterm/lib/src/ui/render.dart
+++ b/desktop/third_party/xterm/lib/src/ui/render.dart
@@ -25,6 +25,7 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
required ViewportOffset offset,
required EdgeInsets padding,
required bool autoResize,
+ bool resizeBuffer = true,
bool renderingEnabled = true,
required TerminalStyle textStyle,
required TextScaler textScaler,
@@ -40,6 +41,7 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
_offset = offset,
_padding = padding,
_autoResize = autoResize,
+ _resizeBuffer = resizeBuffer,
_renderingEnabled = renderingEnabled,
_focusNode = focusNode,
_cursorType = cursorType,
@@ -60,6 +62,7 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
_terminal.removeListener(_onTerminalChange);
}
_terminal = terminal;
+ _reportedViewportSize = null;
if (attached && _renderingEnabled) _terminal.addListener(_onTerminalChange);
_resizeTerminalIfNeeded();
markNeedsLayout();
@@ -97,6 +100,14 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
markNeedsLayout();
}
+ bool _resizeBuffer;
+ set resizeBuffer(bool value) {
+ if (value == _resizeBuffer) return;
+ _resizeBuffer = value;
+ _reportedViewportSize = null;
+ markNeedsLayout();
+ }
+
bool _renderingEnabled;
set renderingEnabled(bool value) {
if (value == _renderingEnabled) return;
@@ -176,6 +187,7 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
}
TerminalSize? _viewportSize;
+ TerminalSize? _reportedViewportSize;
final TerminalPainter _painter;
@@ -401,6 +413,24 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
void _resizeTerminalIfNeeded() {
if (_renderingEnabled &&
_autoResize &&
+ !_resizeBuffer &&
+ _viewportSize != null &&
+ _reportedViewportSize != _viewportSize) {
+ _reportedViewportSize = _viewportSize;
+ // Shrinking a captured TUI locally can discard its latest rows when
+ // its cursor is parked near the top. Keep the authoritative remote
+ // cells while requesting a new grid; the next keyframe replaces them.
+ _terminal.onResize?.call(
+ _viewportSize!.width,
+ _viewportSize!.height,
+ _painter.cellSize.width.round(),
+ _painter.cellSize.height.round(),
+ );
+ return;
+ }
+ if (_renderingEnabled &&
+ _autoResize &&
+ _resizeBuffer &&
_viewportSize != null &&
(_terminal.viewWidth != _viewportSize!.width ||
_terminal.viewHeight != _viewportSize!.height)) {