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
perf(desktop): avoid copying terminal output before decoding
deee7846c017da4
diff --git a/desktop/lib/terminal/terminal_session.dart b/desktop/lib/terminal/terminal_session.dartindex 93d9339d3..de76f6372 100644--- a/desktop/lib/terminal/terminal_session.dart+++ b/desktop/lib/terminal/terminal_session.dart@@ -627,15 +627,20 @@ class TerminalSession extends ChangeNotifier {}bool _writeBytes(List<int> bytes) {- final combined = <int>[..._utf8Tail, ...bytes];+ // Most packets end on a scalar boundary. Decode their existing byte view+ // directly; only a split UTF-8 scalar needs a joined buffer.+ final combined = _utf8Tail.isEmpty ? bytes : <int>[..._utf8Tail, ...bytes];for (var tailLength = 0;tailLength <= min(3, combined.length);tailLength++) {try {- final prefix = combined.sublist(0, combined.length - tailLength);- final text = utf8.decode(prefix, allowMalformed: false);+ final text = utf8.decoder.convert(+ combined,+ 0,+ combined.length - tailLength,+ );if (text.isNotEmpty) _writeTerminalText(text);_utf8Tail = tailLength == 0? const []diff --git a/desktop/test/benchmarks/swarm_benchmark.dart b/desktop/test/benchmarks/swarm_benchmark.dartindex 85a14ea6e..e3ba4fe17 100644--- a/desktop/test/benchmarks/swarm_benchmark.dart+++ b/desktop/test/benchmarks/swarm_benchmark.dart@@ -9,6 +9,7 @@ import 'package:harness/core/config.dart';import 'package:harness/core/models.dart';import 'package:harness/state/app_state.dart';import 'package:harness/state/swarm_catalog.dart';+import 'package:harness/terminal/terminal_binary.dart';import 'package:harness/terminal/terminal_session.dart';import 'package:harness/widgets/terminal_panel.dart';@@ -38,6 +39,60 @@ Map<String, num> measure(void Function() operation) {}void main() {+ for (final sampleText in ['abcd', '界 ']) {+ test(+ 'terminal output CPU benchmark for ${sampleText == 'abcd' ? 'ASCII' : 'Unicode'}',+ () async {+ final session = TerminalSession(+ machineId: 'isolated-machine',+ agentId: 'isolated-agent',+ agentName: 'Benchmark',+ engineId: 'codex',+ send: (_, _) async => true,+ sendBinary: (_) async => true,+ )..streamId = 'isolated-stream';+ addTearDown(session.dispose);+ final content = List.filled(31, sampleText).join();+ // Repaint one row so scrollback growth does not change the workload.+ // Each packet is exactly 16 KiB in either case.+ final bytes = utf8.encode(List.filled(128, '\r$content\x1b[K').join());+ var sequence = 0;+ Future<void> output({bool keyframe = false}) => session.handleBinary(+ TerminalBinaryFrame(+ kind: keyframe+ ? TerminalBinaryKind.keyframe+ : TerminalBinaryKind.output,+ streamId: 'isolated-stream',+ seq: sequence++,+ bytes: bytes,+ cols: keyframe ? 160 : null,+ rows: keyframe ? 24 : null,+ compressed: false,+ ),+ );+ expect(bytes, hasLength(16 * 1024));+ await output(keyframe: true);+ for (var warmup = 0; warmup < 20; warmup++) {+ await output();+ }+ final times = <int>[];+ for (var sample = 0; sample < 100; sample++) {+ final watch = Stopwatch()..start();+ await output();+ times.add(watch.elapsedMicroseconds);+ }+ expect(session.status, TerminalSessionStatus.controlling);+ expect(+ session.terminal.buffer.getText(),+ startsWith(content.trimRight()),+ );+ debugPrint(+ 'SWARM_BENCH ${jsonEncode({'kind': 'headless_debug_cpu', 'operation': 'terminal_output_decode_and_parse', 'content': sampleText == 'abcd' ? 'ASCII' : 'Unicode', 'bytesPerFrame': bytes.length, 'decodeAndParse': distribution(times)})}',+ );+ },+ );+ }+test('large live catalog CPU benchmark', () {final app = AppNotifier(config: AppConfig.dev, authSession: AuthSession());addTearDown(app.dispose);diff --git a/docs/harness-v2-performance.md b/docs/harness-v2-performance.mdindex 54a7acad0..69055ffc7 100644--- a/docs/harness-v2-performance.md+++ b/docs/harness-v2-performance.md@@ -18,11 +18,15 @@ It prints `SWARM_BENCH` JSON records. Its filename deliberately does not end in| Group the same catalog into 50 projects | 1.14 ms | 1.27 ms || Switch tab and pump a frame: 4 swarms, 16 terminals | 18.45 ms | 25.88 ms || Switch tab and pump a frame: 12 swarms, 48 terminals | 14.51 ms | 16.95 ms |+| Decode and parse a 16 KiB ASCII output frame | 0.51 ms | 0.68 ms |+| Decode and parse a 16 KiB Unicode output frame | 0.31 ms | 0.35 ms |Catalog operations use 20 warmups and 100 samples. Each terminal has 1,000 scrollback lines; the window is 1280 × 800 with four visible terminals. Tab checks warm up for three full cycles, then take 60 samples. They retain one renderer per session and include Flutter frame work and test-runner overhead. Network, persistence I/O, AppKit titlebar rendering and physical display latency are outside these measurements.These are **debug CPU measurements**, not end-to-end input latency or release frame budgets. JIT warming and host load affect timings; the later 12-swarm case being faster does not establish that more tabs make the app faster. Use the structural counts below to assess the specific optimization.+The output checks call the production `TerminalSession.handleBinary` with 16 KiB packets that repeatedly repaint one row, keeping scrollback size fixed. Each uses 20 warmups and 100 samples. They include the normal frame queue, UTF-8 decoder and terminal parser, with transport callbacks isolated; they create no widgets and exclude painting. Run only these checks with `--plain-name 'terminal output CPU benchmark'`.+## Reduced work when switching tabsPreviously, switching between two swarms rebuilt headers, gestures and menus for terminals in every previously visited swarm. Hidden panes now retain their widget configuration as well as their renderer, with stable identities in the parked list. Session replacement updates a parked view; showing it applies current machine and agent metadata.@@ -33,6 +37,12 @@ Rapid navigation also coalesces pending arrangement writes. While the first writIncoming binary data now skips unrelated sessions before awaiting the matching renderer queue. This removes one async scheduling turn per unrelated view from each frame's dispatch, while retaining socket FIFO, current stream identity and machine isolation. This change is verified by routing tests; the CPU table above does not measure network delivery.+## Terminal output allocations++Ordinary output packets now decode directly from their existing byte view. Previously every packet was copied into a joined list and copied again to select the decodable prefix. A joined buffer is still used when a UTF-8 character crosses packet boundaries, and the decoder reads a bounded range without copying that prefix. Malformed-byte replacement and terminal recovery behavior are unchanged.++In the paired headless checks, median decoding-and-parsing time fell from 0.683 to 0.514 ms for the ASCII workload and from 0.484 to 0.314 ms for Unicode. Corresponding p95 values were 1.088 to 0.680 ms and 0.686 to 0.348 ms. These measurements establish the benefit for these workloads; they do not measure user-visible latency or every terminal output pattern. Existing terminal-session regressions cover split UTF-8, compressed keyframes, parser sequences and recovery.+## Native follow-upThe optimized real-data app builds and runs locally. A separate windowless AppKit check covers tab overflow geometry, resizing, accessibility order and disabled actions (`bash tool/check_swarm_titlebar.sh`, with an optional Flutter SDK path). Measure release input-to-display and tab-switch latency in the unlocked native window before making latency claims. The screen was locked during this pass, so native end-to-end drag and visual inspection remain in the development handoff.diff --git a/docs/harness-v2-progress.md b/docs/harness-v2-progress.mdindex c2f07936d..8046b5618 100644--- a/docs/harness-v2-progress.md+++ b/docs/harness-v2-progress.md@@ -68,6 +68,7 @@ Approved reference: `/Users/ab/code/harness-new-ui`, React prototype commit `f83- Compact headers remove routine transport/status/pin clutter, retaining exceptional states and offline/unavailable placeholders.- Existing terminal sessions/transports/renderer and patched `third_party/xterm` are retained. One regression verifies renderer identity, hidden geometry during window resize, and input going only to the active view.- Binary dispatch reads current session stream IDs and skips unrelated sessions before awaiting them, avoiding a scheduling turn per unrelated view. No duplicate stream registry is maintained. Socket FIFO remains in `WsConn`; regressions additionally exercise direct concurrent dispatch during tab reorder, hidden shared views, machine isolation, unknown streams and corrupt-frame recovery.+- Terminal output decodes directly from the received byte view, avoiding two full-packet copies on the ordinary path. Split UTF-8 scalars still join the retained tail; bounded decoding avoids copying each candidate prefix. Explicit 16 KiB ASCII/Unicode output benchmarks measure the production decoder and parser, with results and limits in `docs/harness-v2-performance.md`.- Compact remote panes expose a Show/Hide message composer button in their header. Closing the final visible pane restores keyboard focus even when its shared terminal remains parked in another Swarm.### Shell and dialogs@@ -143,14 +144,14 @@ The reopen binding follows the documented Mac tab-recovery shortcut in [Safari](| --- | --- || macOS debug build | Passed and launched from the monorepo with real saved Swarms and terminals. || macOS optimized local build | Passed and running with the real entry point and saved V2 state. Local ad hoc signing requires the command-line `ENABLE_HARDENED_RUNTIME=NO` override for Flutter's framework; distribution signing settings remain unchanged. The distribution Developer ID certificate is unavailable; nothing was uploaded. Normal asynchronous quit and relaunch were verified. The screen remains locked, so final native visual review is pending. |-| Full Flutter suite | **987 passed, 1 skipped** after persistence, modal menus, retained-terminal performance, stream dispatch and closed-Swarm recovery. |+| Full Flutter suite | **987 passed, 1 skipped** after persistence, modal menus, retained-terminal performance, stream dispatch, closed-Swarm recovery and output decoding changes. || Focused interaction checks | 8 passed: picker navigation/scroll/refresh, welcome keys, shared-view close, composer, native modal guard, profile and folder races. || Closed-Swarm recovery | 7 passed: arrangement and shared-view restoration, untouched welcome replacement, rapid close/reopen with delayed cleanup, capacity, bounded history, sign-out isolation and the keyboard command. Native command availability and modal behavior also pass in the interaction suite. || Local CLI discovery | 25 passed, including older-daemon folder parsing and continuous snapshots without spawning/reconnecting. || CLI typecheck | `npm run typecheck` passed. || CLI targeted tests | 135 passed across project/frame, registry/restore, and terminal recovery files. Project/frame tests rerun after port normalization: 9 passed. || Flutter analyzer | **0 errors, 0 warnings, 12 existing vendored xterm infos**; passes with `--no-fatal-infos`. |-| Headless performance | 3 explicit benchmarks passed: 2,000-agent catalog and 16/48 retained terminals. Reproducible command and limits in `docs/harness-v2-performance.md`. |+| Headless performance | 5 explicit benchmarks passed: 2,000-agent catalog, 16/48 retained terminals and two terminal output workloads. Reproducible commands and limits in `docs/harness-v2-performance.md`. || AppKit components | **151 assertions passed** for overflow, selected-tab visibility after resize, pre-paint labels, accessible order after reorder, retained tab controls, capacity and modal-disabled actions. No window opened. This complements the pending native end-to-end visual audit. || Remote terminals / production release | No remote takeover, release, installer, or production CLI update/restart. Cross-platform and end-to-end latency measurements remain. |@@ -170,6 +171,7 @@ Evidence on this Mac under `/private/tmp`:- `harness-v2-retained-tests.log`: 27 terminal/menu/layout checks.- `harness-v2-persistence-tests.log`: 21 state/async checks including rapid writes and bounded quit.- `harness-v2-benchmark-final.log`: latest catalog and retained-terminal CPU measurements.+- `harness-v2-output-before.log`, `harness-v2-output-after.log`: paired production terminal decode-and-parse CPU measurements.- `harness-v2-native-titlebar-tests.log`: windowless checks against the actual AppKit tab source.- `harness-v2-routing-tests.log`: 68 stream-routing, binary protocol and terminal session checks.- `harness-v2-reopen-tests.log`: 23 Swarm recovery, state and interaction checks.