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.

76.0 KB
Raw Blame
import 'dart:async';
import 'dart:convert';
import 'dart:math';

import 'package:flutter/foundation.dart';
import 'package:archive/archive.dart' show ZLibDecoder;
import 'package:xterm/xterm.dart';

import '../core/crash_log.dart';
import '../core/sleep_aware.dart';
import '../logging/app_log.dart';
import 'terminal_binary.dart';
import 'terminal_input.dart';
import 'terminal_viewport.dart';

typedef TerminalFrameSender = Future<bool> Function(
  String type,
  Map<String, dynamic> payload,
);
typedef TerminalBinarySender = Future<bool> Function(TerminalBinaryFrame frame);

enum TerminalSessionStatus {
  closed,
  opening,
  controlling,
  resyncing,
  takenOver,
  error,
}

/// Who a terminal client is, as it says on `terminal_open` (`client`) and as
/// the daemon repeats to the client it displaces (`terminal_closed.takenBy`) —
/// so the banner can say "Mac mini took control" rather than "another app".
/// Self-declared: every client of a machine is the same account's, and the
/// daemon has no better name for a relayed desktop or a phone. [machineId] is
/// a desktop's own machine in the fleet, so the receiver can show that
/// machine's current name over the one declared.
class TerminalClientDescriptor {
  const TerminalClientDescriptor({
    required this.kind,
    required this.name,
    this.machineId,
  });

  /// `desktop`, `phone`, … — one lower-case word.
  final String kind;
  final String name;
  final String? machineId;

  static const nameMax = 64;

  Map<String, dynamic> toJson() => {
    'kind': kind,
    'name': name,
    if (machineId != null) 'machineId': machineId,
  };

  /// The wire shape, or null for anything else — a daemon that predates the
  /// field sends nothing, and a malformed one is treated the same.
  static TerminalClientDescriptor? fromJson(Object? raw) {
    if (raw is! Map) return null;
    final kind = raw['kind'];
    final name = raw['name'];
    final machineId = raw['machineId'];
    if (kind is! String || !RegExp(r'^[a-z]{1,16}$').hasMatch(kind)) {
      return null;
    }
    if (name is! String) return null;
    final clean = name.replaceAll(RegExp(r'[\u0000-\u001f\u007f]'), ' ').trim();
    if (clean.isEmpty || clean.length > nameMax) return null;
    if (machineId != null &&
        (machineId is! String ||
            !RegExp(r'^[a-f0-9]{16,64}$').hasMatch(machineId))) {
      return null;
    }
    return TerminalClientDescriptor(
      kind: kind,
      name: clean,
      machineId: machineId as String?,
    );
  }

  /// The name to show: the fleet's current name for [machineId] when the
  /// caller knows one, else the name the client declared.
  String label(String? Function(String machineId) resolveMachine) {
    final id = machineId;
    final fleet = id == null ? null : resolveMachine(id)?.trim();
    return fleet == null || fleet.isEmpty ? name : fleet;
  }
}

/// Transient progress for an in-flight [TerminalSession.pasteImage]/[TerminalSession.pasteFile]
/// chunked upload. `bytesWritten` reflects the daemon's own per-chunk ACKs, not bytes merely handed
/// to the local socket.
class UploadProgress {
  const UploadProgress({
    required this.label,
    required this.bytesWritten,
    required this.totalBytes,
  });

  /// The image/file name shown in the overlay — a filename for a file upload, a generic word for
  /// an image (which carries no name of its own).
  final String label;
  final int bytesWritten;
  final int totalBytes;

  double get percent =>
      totalBytes <= 0 ? 0 : (bytesWritten / totalBytes).clamp(0, 1);

  UploadProgress copyWith({int? bytesWritten, int? totalBytes}) =>
      UploadProgress(
        label: label,
        bytesWritten: bytesWritten ?? this.bytesWritten,
        totalBytes: totalBytes ?? this.totalBytes,
      );
}

/// Internal bookkeeping for one in-flight upload — see [TerminalSession._uploadBytes]. Two
/// Completers rather than one, since "the daemon accepted the announcement" and "the transfer is
/// over (however it ended)" are resolved at different points and by different frames.
class _ActiveUpload {
  Completer<bool> beginAccepted = Completer<bool>();
  final Completer<bool> finished = Completer<bool>();

  /// Why the daemon refused the last begin, as it said it.
  String? refusal;

  /// Bytes the daemon has confirmed holding (`terminal_chunked_upload_progress`).
  int acked = 0;
  Completer<void>? _ackWaiter;

  void ack(int bytes) {
    if (bytes <= acked) return;
    acked = bytes;
    final waiter = _ackWaiter;
    _ackWaiter = null;
    waiter?.complete();
  }

  /// Completes when the daemon next confirms more of the upload.
  Future<void> nextAck() => (_ackWaiter ??= Completer<void>()).future;
}

/// What a daemon answers a begin while it still holds an upload for the same pane. This pane runs
/// one upload at a time, so that one is an upload it already gave up on.
const _uploadAlreadyInProgress =
    'an upload is already in progress on this pane';

/// One controller stream for one remote Harness agent.
///
/// Sequence corruption is recovered with bounded resync retries and one clean
/// reopen. Transport loss still freezes input until the user opens a session.
class TerminalSession extends ChangeNotifier {
  static const protocolVersion = 3;

  /// [errorCode] after [transportLost]: the link dropped, not the terminal.
  static const disconnectedCode = 'TERMINAL_DISCONNECTED';
  static const minCols = 40;
  static const maxCols = 300;
  static const minRows = 12;
  static const maxRows = 120;

  final String machineId;
  final String agentId;
  String agentName;

  /// The engine the pane runs — what `terminal_ready` said, until the daemon
  /// says otherwise. Not final: a terminal tile becomes a Claude tile the
  /// moment `claude` is typed into it (and a terminal again when it exits),
  /// and the stream underneath is the same tmux pane throughout, so the
  /// session is told rather than reopened — see [setEngineId].
  String? engineId;
  final TerminalFrameSender send;
  final TerminalBinarySender sendBinary;

  final Duration resyncTimeout;

  /// How long an upload may wait for the daemon to confirm another chunk before it is taken as
  /// lost on the way and given up (see [_uploadBytes]).
  final Duration uploadStallTimeout;

  /// The progress of the last upload given up because it stalled; null once another starts.
  UploadProgress? lastStalledUpload;
  final bool _readOnly;
  bool get readOnly => _readOnly || _watchOnly;
  String? _inputContext;
  String? _openedInputContext;
  bool _watchOnly;
  bool _openedWatchOnly = false;
  String? get inputContext => _inputContext;

  // A controllable clock keeps coalescing tests independent of host scheduling.
  final DateTime Function() _now;

  /// Lets input batching tests hold the clock inside the four-millisecond window under host load.
  @visibleForTesting
  late DateTime Function() inputClockForTest = _now;

  /// Forces a fresh transport dial (see `WsConn.forceReconnect`) — called once when the very first
  /// `terminal_open` never gets a `terminal_ready` back within [resyncTimeout]. Covers the relay
  /// going stale silently (the relayed machine's own Harness process restarted, dropping its E2EE
  /// session without the transport ever closing) so a reconnect looks like a couple of extra seconds
  /// of "attaching" instead of a hang the user has to notice and manually retry.
  final Future<void> Function()? onOpenStalled;

  /// This client's own introduction, sent with every `terminal_open`; null
  /// for a viewer that never takes control and so is never anyone's taker.
  final TerminalClientDescriptor? client;

  /// Whether the next `terminal_open` may TAKE the terminal from whoever holds
  /// it. A terminal has one controller, and an ordinary open wins it — which is
  /// right when a person on THIS window asked for the pane, and wrong for every
  /// other reason a pane attaches: a tab another Mac opened arriving over the
  /// desk, a reconnect, a machine answering its agent list, the dial turning.
  /// Those open with `takeover: false` and are answered as a WATCHER (the
  /// daemon attaches tmux read-only and the person typing keeps the terminal),
  /// or `CONTROL_LEASE_HELD` if the race is lost.
  ///
  /// ⚠️ **An arrival arms ONE open.** Lowered again the moment an open it armed
  /// is answered, because from then on this session HOLDS the lease and its
  /// later opens rest on holding it. Left standing, it would outlive the
  /// gesture: a window reconnecting hours later would take the terminal off
  /// whoever had it by then, with nobody having touched this Mac at all.
  ///
  /// ⚠️ Only meaningful against a daemon that advertises `noTakeover`; an older
  /// one ignores the key and takes over regardless, so the caller checks first
  /// — `MachineState.terminalNoTakeoverAvailable`.
  bool takeover;

  /// What the open now in flight asked for — [takeover] as it was when that
  /// frame was built. The two differ when a person lands on the pane while a
  /// polite open is still out.
  bool _openAskedTakeover = true;

  /// Whether the stream this session holds is a WATCHER: real output, live, but
  /// another client drives it and the daemon refuses anything typed here. Told
  /// by `readOnly` on `terminal_ready`. A normal state, not a failure — the
  /// pane shows the band and its "Take control" button. Reset by every open.
  bool watching = false;

  TerminalSession({
    required this.machineId,
    required this.agentId,
    required this.agentName,
    required this.engineId,
    required this.send,
    required this.sendBinary,
    this.client,
    this.onOpenStalled,
    this.takeover = true,
    bool readOnly = false,
    String? inputContext,
    bool watchOnly = false,
    this.resyncTimeout = const Duration(seconds: 4),
    this.uploadStallTimeout = const Duration(seconds: 30),
    DateTime Function()? now,
  }) : _now = now ?? DateTime.now,
       // ignore: prefer_initializing_formals
       _readOnly = readOnly,
       // The public parameter deliberately omits the private field's underscore.
       // ignore: prefer_initializing_formals
       _inputContext = inputContext,
       // ignore: prefer_initializing_formals
       _watchOnly = watchOnly {
    terminal = _newTerminal();
  }

  late Terminal terminal;
  TerminalSessionStatus status = TerminalSessionStatus.closed;
  String? streamId;

  /// How this pane's terminal bytes are currently reaching it — one of:
  ///
  ///   'p2p'   direct WebRTC, ICE nominated a direct candidate pair.
  ///   'turn'  WebRTC too, but ICE could only manage a relay pair, so Cloudflare TURN carries it.
  ///   'relay' no data channel at all; the bytes ride the backend WebSocket.
  ///
  /// Only ever set for a `harness link connect`-linked remote machine (the CLI never sends this frame
  /// for a local machine's own terminal, which has no such distinction), and reset alongside [streamId]
  /// so a stale mode can never survive into the next stream.
  String? linkMode;
  String? errorCode;
  String? errorMessage;

  /// This app ended the stream itself ([close]). A dead stream is reattached automatically
  /// (`AppNotifier` pane retries); one this app closed on purpose is not. Cleared by the next open.
  bool closedHere = false;

  /// Who took this terminal, while [status] is [TerminalSessionStatus.takenOver]
  /// and the daemon said (`terminal_closed.takenBy`); null from an older daemon
  /// or a taker that did not introduce itself — "another app", then.
  TerminalClientDescriptor? takenOverBy;
  int cols = 80;
  int rows = 24;

  /// Set while an image/file upload is in flight ([pasteImage]/[pasteFile]); `null` otherwise. The
  /// terminal panel's overlay shows a progress pill exactly when this is non-null.
  UploadProgress? uploadProgress;
  _ActiveUpload? _activeUpload;

  String? _openRequestId;
  int? _expectedSeq;
  int _lastRenderedSeq = -1;
  int _framesSinceAck = 0;
  int _renderedSinceAckBytes = 0;
  int _inputSeq = 0;

  /// The last `expectedSeq` a TERMINAL_INPUT_INVALID realigned the counter to.
  /// The daemon refuses every in-flight frame it cannot accept, each naming
  /// the SAME expected seq; only the first of those may rewind the counter,
  /// or frames accepted since would be re-sent under numbers already used.
  int? _lastRealignedTo;
  int _resizeSeq = 0;
  bool _resyncRequested = false;
  int _resyncAttempts = 0;
  int _autoReopenAttempts = 0;
  bool _openStallRecovered = false;
  bool _disposed = false;
  int _generation = 0;
  final _remoteCursorVisible = ValueNotifier(true);

  /// The program's cursor visibility, independent of the local blink phase.
  /// Views can stop their cursor clock while the program draws its own caret.
  /// This is separate from session notifications: an escape sequence must not
  /// rebuild the workspace or change input ownership.
  ValueListenable<bool> get remoteCursorVisibility => _remoteCursorVisible;

  bool _cursorBlinkPhaseVisible = true;
  List<int> _utf8Tail = const [];
  final List<int> _inputBytes = [];
  Timer? _heartbeat;
  int _heartbeatTick = 0;
  DateTime? _lastStreamActivityAt;

  /// When the oldest input frame still waiting for the stream to answer went out; null when nothing
  /// typed is waiting. See [_inputAnswered].
  DateTime? _inputUnansweredSince;
  Timer? _ackTimer;
  Timer? _inputTimer;
  Timer? _resizeTimer;
  DateTime? _lastInputFlushAt;
  DateTime? _lastResizeFlushAt;
  Timer? _resyncTimer;
  Timer? _scrollTimer;
  bool? _pendingScrollUp;
  int _pendingScrollLines = 0;
  int? _pendingCols;
  int? _pendingRows;
  TerminalViewport? _viewport;
  ({int cols, int rows})? _measuredViewport;
  final Completer<({int cols, int rows})> _viewportSize = Completer();
  Future<void> _renderTail = Future<void>.value();
  Future<void> _inputSendTail = Future<void>.value();

  bool get acceptsInput =>
      !readOnly &&
      !_watchOnly &&
      !watching &&
      _inputContext == _openedInputContext &&
      _watchOnly == _openedWatchOnly &&
      status == TerminalSessionStatus.controlling &&
      streamId != null;

  /// Grok's CLI declares terminal mouse-tracking (so tmux defers wheel bytes to it, same as any
  /// alt-buffer program) but doesn't correctly handle wheel reports itself — confirmed live: it
  /// echoes the raw SGR escape bytes into its own prompt as literal characters instead of
  /// scrolling. Its alt-buffer scroll gestures route through the daemon's tmux copy-mode
  /// (`sendScrollCommand`/`terminal_scroll`) instead of the raw `mouseInput` path every other
  /// engine already uses correctly.
  bool get scrollViaTmuxCopyMode => engineId == 'grok';

  void renameAgent(String name) {
    final cleanName = name.trim();
    if (cleanName.isEmpty || cleanName == agentName) return;
    agentName = cleanName;
    notifyListeners();
  }

  /// The daemon re-labelled this pane's engine (`agent_synced` with a new
  /// `engine`): the header mark, the model picker and the scroll strategy
  /// follow, over the stream already open.
  void setEngineId(String? engine) {
    if (engine == engineId) return;
    engineId = engine;
    notifyListeners();
  }

  /// An opaque provenance hint, never an access grant. Staging a new scope
  /// immediately withholds input until its replacement stream is ready.
  void stageInputContext(String? value, {bool watchOnly = false}) {
    if (_inputContext == value && _watchOnly == watchOnly) return;
    _inputContext = value;
    _watchOnly = watchOnly;
    _inputBytes.clear();
  }

  Future<void> setInputContext(String? value, {bool watchOnly = false}) async {
    stageInputContext(value, watchOnly: watchOnly);
    if (_disposed ||
        (_openedInputContext == value && _openedWatchOnly == watchOnly)) {
      return;
    }
    await _open(
      initialCols: _measuredViewport?.cols ?? cols,
      initialRows: _measuredViewport?.rows ?? rows,
      waitForViewportSize: false,
      resetRecovery: true,
      preserveTerminal: true,
      allowRedial: false,
    );
  }

  Future<void> open({
    int initialCols = 80,
    int initialRows = 24,
    bool waitForViewportSize = false,
  }) => _open(
    initialCols: initialCols,
    initialRows: initialRows,
    waitForViewportSize: waitForViewportSize,
    resetRecovery: true,
    preserveTerminal: false,
  );

  /// Reconnect the same agent without discarding its last usable screen.
  /// Input resumes only after the replacement stream's first keyframe.
  /// [force] is a person asking: it arms one takeover and reopens a stream this
  /// session is merely watching, which is what the band's "Take control" does.
  /// [redial] false keeps an unanswered open from forcing a fresh dial of the machine's link
  /// ([onOpenStalled]): an automatic retry must not knock the machine's other panes over.
  Future<void> reopen({bool force = false, bool redial = true}) async {
    if (force) takeover = true;
    if (_disposed ||
        status == TerminalSessionStatus.opening ||
        (status == TerminalSessionStatus.controlling && !(force && watching)) ||
        status == TerminalSessionStatus.resyncing) {
      return;
    }
    await _open(
      initialCols: _measuredViewport?.cols ?? cols,
      initialRows: _measuredViewport?.rows ?? rows,
      waitForViewportSize: false,
      resetRecovery: true,
      preserveTerminal: true,
      allowRedial: redial,
    );
  }

  bool _isCurrent(int generation) => !_disposed && generation == _generation;

  Future<void> _open({
    required int initialCols,
    required int initialRows,
    required bool waitForViewportSize,
    required bool resetRecovery,
    required bool preserveTerminal,
    bool allowRedial = true,
  }) async {
    if (_disposed) return;
    closedHere = false;
    final generation = ++_generation;
    _cancelTimers();
    _abortActiveUpload();
    _inputBytes.clear();
    _pendingScrollUp = null;
    _pendingScrollLines = 0;
    _lastInputFlushAt = null;
    _lastResizeFlushAt = null;
    _lastStreamActivityAt = null;
    _inputUnanswered('reopen');
    _renderTail = Future<void>.value();
    _inputSendTail = Future<void>.value();
    streamId = null;
    linkMode = null;
    watching = false;
    errorCode = null;
    errorMessage = null;
    takenOverBy = null;
    _expectedSeq = null;
    _lastRenderedSeq = -1;
    _framesSinceAck = 0;
    _renderedSinceAckBytes = 0;
    _inputSeq = 0;
    _lastRealignedTo = null;
    _resizeSeq = 0;
    _utf8Tail = const [];
    _cursorBlinkPhaseVisible = true;
    _resyncRequested = false;
    _resyncAttempts = 0;
    if (resetRecovery) {
      _autoReopenAttempts = 0;
      _openStallRecovered = !allowRedial;
    }
    cols = _clampCols(initialCols);
    rows = _clampRows(initialRows);
    if (!preserveTerminal) terminal = _newTerminal()..resize(cols, rows);
    status = TerminalSessionStatus.opening;
    _remoteCursorVisible.value = true;
    _openRequestId =
        'term_${DateTime.now().microsecondsSinceEpoch}_${Random.secure().nextInt(1 << 31)}';
    notifyListeners();

    if (waitForViewportSize) {
      try {
        final measured = await _viewportSize.future.timeout(
          const Duration(seconds: 2),
        );
        if (!_isCurrent(generation) ||
            status != TerminalSessionStatus.opening) {
          return;
        }
        cols = _clampCols(measured.cols);
        rows = _clampRows(measured.rows);
        if (!preserveTerminal) terminal.resize(cols, rows);
        notifyListeners();
      } on TimeoutException {
        // Keep the conservative fallback when the terminal viewport cannot be
        // measured; the normal resize path will reconcile it after attach.
      }
    }

    if (!_isCurrent(generation) || status != TerminalSessionStatus.opening) {
      return;
    }

    final openPayload = {
      'protocolVersion': protocolVersion,
      'requestId': _openRequestId,
      'agentId': agentId,
      'cols': cols,
      'rows': rows,
      'compression': const ['zlib', 'none'],
      if (client != null) 'client': client!.toJson(),
      if (_inputContext != null) 'inputContext': _inputContext,
      if (_watchOnly) 'watchOnly': true,
      // Absent is the takeover an open has always been; `false` asks the daemon
      // to leave whoever holds the terminal alone and hand this one a watcher.
      if (!takeover) 'takeover': false,
    };
    _openAskedTakeover = takeover;
    _openedInputContext = _inputContext;
    _openedWatchOnly = _watchOnly;
    var sent = await send('terminal_open', openPayload);
    if (!_isCurrent(generation) ||
        status != TerminalSessionStatus.opening ||
        streamId != null) {
      return;
    }
    if (!sent) {
      // Most commonly transient: the local transport is mid-reconnect at this exact instant (e.g.
      // right after the app itself just started, or another machine's relay hiccupped a moment ago).
      sent = await _recoverAndResend(openPayload, generation);
    }
    if (!_isCurrent(generation) ||
        status != TerminalSessionStatus.opening ||
        streamId != null) {
      return;
    }
    if (!sent) {
      transportLost('Could not send terminal_open');
      return;
    }
    // An immediate ready/keyframe can arrive before send() resolves. Its
    // watchdog or live stream must not be replaced by an open timeout.
    if (status != TerminalSessionStatus.opening || streamId != null) return;
    // Armed unconditionally (not just on reopen attempts): a `terminal_open` sent through a silently
    // stale relay session never gets ANY reply — nothing else would ever notice or recover from that.
    _resyncTimer?.cancel();
    _resyncTimer = SleepAwareTimer(
      resyncTimeout,
      () => unawaited(_handleOpenTimeout(openPayload, generation)),
    );
  }

  /// The armed-on-every-open watchdog fired: no `terminal_ready` arrived in time even though the send
  /// itself succeeded — the relay session was silently stale (ciphertext for a dead E2EE session gets
  /// dropped, not rejected). Force a fresh dial and resend the SAME open request (same `requestId`, so
  /// a late reply for the original still matches) before giving up.
  Future<void> _handleOpenTimeout(
    Map<String, dynamic> openPayload,
    int generation,
  ) async {
    if (!_isCurrent(generation) ||
        status != TerminalSessionStatus.opening ||
        streamId != null) {
      return;
    }
    final sent = await _recoverAndResend(openPayload, generation);
    if (!_isCurrent(generation) ||
        status != TerminalSessionStatus.opening ||
        streamId != null) {
      return;
    }
    if (sent) {
      _resyncTimer?.cancel();
      _resyncTimer = SleepAwareTimer(
        resyncTimeout,
        () => unawaited(_handleOpenTimeout(openPayload, generation)),
      );
      return;
    }
    _fail(
      'TERMINAL_RESYNC_TIMEOUT',
      _autoReopenAttempts > 0
          ? 'Terminal did not reopen after resync failed.'
          : 'Harness did not respond — check your connection.',
    );
  }

  /// Shared recovery for both open-failure paths (`send()` itself failing, and `terminal_ready` never
  /// arriving): force a fresh transport dial (see `WsConn.forceReconnect`), then poll-resend — a forced
  /// relay redial is a REAL network round trip (fresh E2EE handshake for a relayed machine), and
  /// `forceReconnect()` resolving only means the redial STARTED, not that the transport is ready again.
  /// Bounded by [_openStallRecovered] (one forced reconnect per open) and a fixed poll budget, so a
  /// persistently broken connection still fails closed instead of retrying forever.
  Future<bool> _recoverAndResend(
    Map<String, dynamic> openPayload,
    int generation,
  ) async {
    final recover = onOpenStalled;
    if (_openStallRecovered || recover == null) return false;
    _openStallRecovered = true;
    try {
      await recover();
    } catch (_) {
      // Still worth polling for readiness even if the forced reconnect itself errored.
    }
    for (var attempt = 0; attempt < 10; attempt++) {
      if (!_isCurrent(generation) ||
          status != TerminalSessionStatus.opening ||
          streamId != null) {
        return false;
      }
      if (await send('terminal_open', openPayload)) return true;
      await Future.delayed(const Duration(milliseconds: 300));
    }
    return false;
  }

  /// Returns true when [type] belongs to this session's protocol.
  Future<bool> handleFrame(String type, Map<String, dynamic> payload) async {
    if (_disposed) return false;
    switch (type) {
      case 'terminal_ready':
        if (status != TerminalSessionStatus.opening ||
            payload['requestId'] != _openRequestId ||
            payload['agentId'] != agentId ||
            payload['protocolVersion'] != protocolVersion) {
          return true;
        }
        streamId = payload['streamId'] as String?;
        if (streamId == null || streamId!.isEmpty) {
          _fail('TERMINAL_READY_INVALID', 'Harness returned no stream id');
          return true;
        }
        // Which kind of stream came back. A watcher renders the terminal and
        // may not type into it; the band says who has it and offers to ask.
        watching = payload['readOnly'] == true;
        // The claim is spent here: from now on this session holds the lease,
        // and every later open of its own rests on holding it rather than on a
        // gesture nobody has made since. See [takeover].
        if (!watching) takeover = false;
        _resyncTimer?.cancel();
        _resyncTimer = null;
        _heartbeatTick = 0;
        _heartbeat = Timer.periodic(const Duration(seconds: 5), (timer) {
          final slept = sleptSinceTick(timer, _heartbeatTick);
          _heartbeatTick = timer.tick;
          unawaited(_sendHeartbeat(afterSleep: slept));
        });
        _armInitialKeyframeWatchdog();
        return true;
      case 'terminal_keyframe':
      case 'terminal_output':
        _fail(
          'TERMINAL_BINARY_REQUIRED',
          'Harness sent terminal bulk data as JSON',
        );
        return true;
      case 'terminal_chunked_upload_begin_result':
        if (!_matchesStream(payload)) return true;
        final accepted = payload['accepted'] == true;
        if (_activeUpload?.beginAccepted.isCompleted == false) {
          _activeUpload!.refusal = accepted
              ? null
              : payload['reason']?.toString();
          _activeUpload!.beginAccepted.complete(accepted);
        }
        // Older daemons only sent this text. A vanished stream cannot accept input or upload
        // chunks; retain its screen and reopen once, without replaying the failed paste.
        if (!accepted &&
            (payload['code'] == 'TERMINAL_STREAM_NOT_FOUND' ||
                payload['reason'] ==
                    'no live terminal stream for this pane (reopen it and try again)')) {
          await _recoverByReopen(reason: 'TERMINAL_STREAM_NOT_FOUND');
        }
        return true;
      case 'terminal_chunked_upload_progress':
        if (!_matchesStream(payload)) return true;
        final bytesWritten = (payload['bytesWritten'] as num?)?.toInt();
        final totalBytes = (payload['totalBytes'] as num?)?.toInt();
        if (bytesWritten != null) _activeUpload?.ack(bytesWritten);
        if (bytesWritten != null &&
            totalBytes != null &&
            uploadProgress != null) {
          uploadProgress = uploadProgress!.copyWith(
            bytesWritten: bytesWritten,
            totalBytes: totalBytes,
          );
          notifyListeners();
        }
        return true;
      case 'terminal_paste_image_result':
      case 'terminal_paste_file_result':
        if (!_matchesStream(payload)) return true;
        if (_activeUpload?.finished.isCompleted == false) {
          _activeUpload!.finished.complete(true);
        }
        return true;
      case 'terminal_link_mode':
        if (!_matchesStream(payload)) return true;
        final mode = payload['mode']?.toString();
        // Allow-list, not a blind assign: an older CLI knows only 'p2p'/'relay', a newer one may learn
        // values this build cannot draw, and either way linkMode must stay something the header has an
        // icon for. An unknown value leaves the previous state alone rather than blanking it.
        if (mode == 'p2p' || mode == 'turn' || mode == 'relay') {
          linkMode = mode;
          notifyListeners();
        }
        return true;
      case 'terminal_closed':
        if (!_matchesStream(payload)) return true;
        _generation++;
        _cancelTimers();
        final code = payload['code']?.toString();
        final takenOver = code == 'TERMINAL_TAKEN_OVER';
        status = takenOver
            ? TerminalSessionStatus.takenOver
            : TerminalSessionStatus.closed;
        errorCode = takenOver ? code : null;
        takenOverBy = takenOver
            ? TerminalClientDescriptor.fromJson(payload['takenBy'])
            : null;
        errorMessage = takenOver
            ? (takenOverBy == null
                  ? 'Another client connected to this terminal.'
                  : '${takenOverBy!.name} connected to this terminal.')
            : payload['reason']?.toString();
        streamId = null;
        linkMode = null;
        _abortActiveUpload();
        notifyListeners();
        return true;
      case 'terminal_error':
        final errorStream = payload['streamId'];
        final errorRequest = payload['requestId'];
        if (errorStream != null && errorStream != streamId) return true;
        if (errorRequest != null && errorRequest != _openRequestId) return true;
        final errorCode = payload['code']?.toString() ?? 'TERMINAL_ERROR';
        // A rejected paste (empty, or over the daemon's sanity ceiling) never touched tmux — the
        // stream itself is completely fine, so this must not freeze it the way a real transport/
        // protocol failure does. `debugPrint` only: there is no dedicated per-action failure surface
        // to show the user something better than silence, and silence beats losing the terminal.
        // A malformed upload chunk (bad seq/size) is the same shape of non-event for the same
        // reason — nothing it describes ever reached tmux either.
        if (errorCode == 'TERMINAL_PASTE_INVALID' ||
            errorCode == 'TERMINAL_CHUNKED_UPLOAD_INVALID') {
          _abortActiveUpload();
          notifyListeners();
          debugPrint(
            'TerminalSession: paste rejected: ${payload['message'] ?? errorCode}',
          );
          return true;
        }
        // A refused INPUT frame never reached tmux either: the stream is fine, only our counter
        // disagrees with the daemon's (a frame dropped on this side while resyncing, say). A daemon
        // that says what it expects gets realigned in place — the refused keystroke is gone either
        // way, and the person retypes it — and an older one that does not gets a fresh stream. What
        // it must NOT get is the frozen pane this used to be: every later keystroke was refused too,
        // and a working terminal read as a dead one.
        if (errorCode == 'TERMINAL_INPUT_INVALID') {
          final expected = payload['expectedSeq'];
          if (expected is int && expected >= 0) {
            // A stale duplicate: the daemon refused several in-flight frames
            // at once, each naming this same seq, and the counter has since
            // moved on from it and been accepted. Rewinding again would put the
            // next keystroke under a number the daemon has already taken.
            // Safe because its expectation only ever grows and its errors
            // arrive in order — a NEW gap always names a larger seq.
            if (expected == _lastRealignedTo) return true;
            _lastRealignedTo = expected;
            _inputSeq = expected;
            // Bytes still waiting in the coalescer are kept: they are numbered
            // on the way out, after this, so they go under the right seq.
            debugPrint(
              'TerminalSession: input seq realigned to $expected '
              '(${payload['reason'] ?? payload['message'] ?? 'no reason given'})',
            );
            notifyListeners();
          } else {
            _inputBytes.clear();
            unawaited(_recoverByReopen(reason: 'TERMINAL_INPUT_INVALID'));
          }
          return true;
        }
        // The daemon no longer holds this stream (it closed it while its link to the backend was
        // down, say) and said so because this session asked (`reportMissing`). Reopen in place now
        // rather than waiting out 15 s of silence and three resyncs (2026-10-16).
        if (errorCode == 'TERMINAL_STREAM_NOT_FOUND') {
          unawaited(_recoverByReopen(reason: 'TERMINAL_STREAM_NOT_FOUND'));
          return true;
        }
        // A polite open, refused: another client is driving this terminal and
        // this open asked not to take it from them. Not a failure, and it must
        // not read as one — `error` is what the notifier's reattach sweep
        // retries, which would ask again for as long as the other client stayed.
        // `takenOver` already means "someone else has it, and only a person
        // gets it back", which is exactly this.
        //
        // Only for an open that ASKED to be polite: the daemon answers the same
        // code to an ordinary open that lost a race, and that one keeps the
        // retry it has always had, below.
        if (errorCode == 'CONTROL_LEASE_HELD' && !_openAskedTakeover) {
          _cancelTimers();
          streamId = null;
          watching = false;
          status = TerminalSessionStatus.takenOver;
          this.errorCode = 'TERMINAL_TAKEN_OVER';
          errorMessage = 'Another client is using this terminal.';
          notifyListeners();
          // Somebody landed on this pane while the polite open was still out:
          // ask again, the way a pane a person is looking at always has.
          if (takeover) unawaited(reopen(force: true));
          return true;
        }
        // A genuine mid-transfer failure (the daemon couldn't write the clipboard/disk, or the
        // pty write itself failed) IS treated like a real transport/protocol failure — same as
        // TERMINAL_PASTE_FAILED already is, since it may mean the pty is in an unknown state.
        // `_fail` itself resolves any in-flight upload (see `_abortActiveUpload`), so there is
        // nothing extra to do here for TERMINAL_PASTE_IMAGE_FAILED/TERMINAL_PASTE_FILE_FAILED.
        _fail(errorCode, payload['message']?.toString());
        return true;
      case 'terminal_transport_error':
        transportLost(
          payload['code']?.toString() ?? 'Terminal relay rejected the frame',
        );
        return true;
      default:
        return false;
    }
  }

  Future<void> handleBinary(TerminalBinaryFrame frame) async {
    if (_disposed || frame.streamId != streamId) return;
    final generation = _generation;
    _renderTail = _renderTail
        .then((_) async {
          if (!_isCurrent(generation) || frame.streamId != streamId) return;
          final bytes = _decodeBinaryBytes(frame);
          if (bytes == null) {
            await _requestResync('TERMINAL_BINARY_DECODE_FAILED');
            return;
          }
          if (frame.kind == TerminalBinaryKind.keyframe) {
            final nextCols = frame.cols;
            final nextRows = frame.rows;
            if (nextCols == null || nextRows == null) {
              await _requestResync('TERMINAL_KEYFRAME_INVALID');
              return;
            }
            // Publish a complete screen atomically. A damaged snapshot must
            // leave the retained screen available while resync recovers.
            final decoded = _decodeUtf8(_prepareKeyframeBytes(bytes));
            final replacement = _newTerminal(bindCallbacks: false)
              ..resize(_clampCols(nextCols), _clampRows(nextRows))
              ..write(decoded.text);
            _bindTerminal(replacement);
            terminal = replacement;
            cols = _clampCols(nextCols);
            rows = _clampRows(nextRows);
            _utf8Tail = decoded.tail;
            _cursorBlinkPhaseVisible = true;
            _remoteCursorVisible.value = terminal.cursorVisibleMode;
            _expectedSeq = frame.seq + 1;
            _lastRenderedSeq = frame.seq;
            _resyncRequested = false;
            _resyncAttempts = 0;
            _autoReopenAttempts = 0;
            errorCode = null;
            errorMessage = null;
            takenOverBy = null;
            _resyncTimer?.cancel();
            _resyncTimer = null;
            status = TerminalSessionStatus.controlling;
            _lastStreamActivityAt = _now();
            _inputAnswered('keyframe');
            _markForAck(bytes.length);
            notifyListeners();
            final measured = _measuredViewport;
            if (measured != null) {
              _pendingCols = measured.cols;
              _pendingRows = measured.rows;
            }
            if (_pendingCols != null && _pendingRows != null) {
              unawaited(_flushResize());
            }
            return;
          }
          if (frame.kind == TerminalBinaryKind.sync) {
            if (status == TerminalSessionStatus.resyncing) return;
            if (_expectedSeq == null || frame.seq != _expectedSeq) {
              await _requestResync('TERMINAL_SEQUENCE_GAP');
              return;
            }
            _expectedSeq = frame.seq + 1;
            _lastRenderedSeq = frame.seq;
            _lastStreamActivityAt = _now();
            _inputAnswered('sync');
            _markForAck(0);
            return;
          }
          if (frame.kind != TerminalBinaryKind.output ||
              status == TerminalSessionStatus.resyncing) {
            return;
          }
          if (_expectedSeq == null || frame.seq != _expectedSeq) {
            await _requestResync('TERMINAL_SEQUENCE_GAP');
            return;
          }
          if (!_writeBytes(bytes)) {
            await _requestResync('TERMINAL_OUTPUT_DECODE_FAILED');
            return;
          }
          _expectedSeq = frame.seq + 1;
          _lastRenderedSeq = frame.seq;
          _lastStreamActivityAt = _now();
          _inputAnswered('output');
          _markForAck(bytes.length);
        })
        .catchError((Object error, StackTrace stackTrace) async {
          if (!_isCurrent(generation)) return;
          debugPrint(
            '[terminal-session] renderer failed for '
            '$machineId/$agentId: $error\n$stackTrace',
          );
          CrashLog.record(error, stackTrace, context: 'renderer');
          await onRendererFailure();
        });
    await _renderTail;
  }

  /// Recover from an exception thrown while drawing a frame.
  ///
  /// A renderer fault is LOCAL, and calling it a lost transport was the wrong
  /// diagnosis: the bytes arrived intact and something in drawing them threw.
  /// That froze the tile behind a manual "attach new stream" for a fault the
  /// ordinary resync ladder already clears — it is what the decode failures in
  /// [handleBinary] do — and the ladder is bounded (resync x3, reopen x1, then
  /// a real failure), so a renderer that throws every time still ends up
  /// reporting itself rather than retrying for ever.
  ///
  /// A parser exception can leave the current buffer partially mutated, so the
  /// emulator is not reused: the resync's keyframe is authoritative and
  /// [handleBinary] builds a fresh Terminal for it. Nothing more is written to
  /// the damaged one in the meantime, because the output branch drops frames
  /// while the status is `resyncing`.
  @visibleForTesting
  Future<void> onRendererFailure() =>
      _requestResync('TERMINAL_RENDERER_FAILED');

  Uint8List? _decodeBinaryBytes(TerminalBinaryFrame frame) {
    try {
      return frame.compressed
          ? const ZLibDecoder().decodeBytes(frame.bytes, verify: true)
          : frame.bytes;
    } catch (_) {
      return null;
    }
  }

  void attachViewport(TerminalViewport viewport) {
    _viewport = viewport;
  }

  /// Reports the first grid measured from the actual Flutter terminal render
  /// object. This is deliberately separate from [Terminal.onResize]: creating
  /// a terminal at the conservative 80x24 fallback also fires onResize and
  /// must not be mistaken for a measured viewport.
  void reportViewport(int width, int height) {
    final measured = (cols: _clampCols(width), rows: _clampRows(height));
    _measuredViewport = measured;
    if (_viewportSize.isCompleted) return;
    _viewportSize.complete(measured);
  }

  void detachViewport(TerminalViewport viewport) {
    if (identical(_viewport, viewport)) _viewport = null;
  }

  /// Changes only the local paint phase of the cursor. Incoming terminal data
  /// is always parsed against [_remoteCursorVisible], so blinking cannot turn
  /// a remote DECTCEM hide/show command into terminal input or corrupt its
  /// authoritative cursor state.
  /// Returns whether the effective cursor visibility changed and needs paint.
  bool setCursorBlinkPhase(bool visible) {
    if (_cursorBlinkPhaseVisible == visible) return false;
    final wasVisible = terminal.cursorVisibleMode;
    _cursorBlinkPhaseVisible = visible;
    _applyCursorVisibility();
    return wasVisible != terminal.cursorVisibleMode;
  }

  void _writeTerminalText(String text) {
    terminal.setCursorVisibleMode(_remoteCursorVisible.value);
    terminal.write(text);
    final remoteVisible = terminal.cursorVisibleMode;
    terminal.setCursorVisibleMode(remoteVisible && _cursorBlinkPhaseVisible);
    _remoteCursorVisible.value = remoteVisible;
  }

  void _applyCursorVisibility() {
    terminal.setCursorVisibleMode(
      _remoteCursorVisible.value && _cursorBlinkPhaseVisible,
    );
  }

  bool _writeBytes(List<int> 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];
    final decoded = _decodeUtf8(combined);
    if (decoded.text.isNotEmpty) _writeTerminalText(decoded.text);
    _utf8Tail = decoded.tail;
    return true;
  }

  ({String text, List<int> tail}) _decodeUtf8(List<int> combined) {
    for (
      var tailLength = 0;
      tailLength <= min(3, combined.length);
      tailLength++
    ) {
      try {
        final text = utf8.decoder.convert(
          combined,
          0,
          combined.length - tailLength,
        );
        return (
          text: text,
          tail: tailLength == 0
              ? const []
              : combined.sublist(combined.length - tailLength),
        );
      } on FormatException {
        // A UTF-8 scalar can span at most four bytes; retain only a trailing
        // partial scalar before falling back to replacement rendering below.
      }
    }
    // PTY output is byte-oriented. A snapshot cut can rarely land between the
    // leading and continuation bytes of a scalar, leaving a continuation byte
    // at the start of the post-cut frame. Real terminals render malformed UTF-8
    // as U+FFFD; resyncing the entire screen creates a second keyframe race and
    // cannot recover the missing pre-cut byte anyway.
    return (text: utf8.decode(combined, allowMalformed: true), tail: const []);
  }

  List<int> _prepareKeyframeBytes(Uint8List bytes) {
    if (engineId != 'grok') return bytes;

    // Grok renders a full-screen TUI in tmux's normal buffer. Its tmux
    // scrollback therefore contains old full-screen repaint frames rather than
    // semantic history. Render Grok in the receiver's alternate buffer so the
    // stale frames cannot become local scrollback; wheel input is then routed
    // back to Grok, which owns and redraws its real transcript with ANSI style.
    const alternateBuffer = <int>[
      0x1b,
      0x5b,
      0x3f,
      0x31,
      0x30,
      0x34,
      0x39,
      0x68,
    ];
    if (bytes.length >= 2 && bytes[0] == 0x1b && bytes[1] == 0x63) {
      return <int>[bytes[0], bytes[1], ...alternateBuffer, ...bytes.sublist(2)];
    }
    return <int>[...alternateBuffer, ...bytes];
  }

  Terminal _newTerminal({bool bindCallbacks = true}) {
    final result = Terminal(
      maxLines: 10000,
      platform: switch (defaultTargetPlatform) {
        TargetPlatform.linux => TerminalTargetPlatform.linux,
        // The native app only: a browser on Windows keeps the keys it had.
        TargetPlatform.windows when !kIsWeb => TerminalTargetPlatform.windows,
        _ => TerminalTargetPlatform.macos,
      },
      // ⌥⏎ and ⌥⌫ have to become Meta-prefixed before they reach the pty, or the engine's prompt
      // reads the first as the submit it is byte-identical to and never hears the second at all.
      // See [AltAsMetaInputHandler].
      inputHandler: harnessInputHandler,
      // The remote pane owns its grid and redraws after resize. Reflowing TUI
      // rows locally both changes their geometry and exercises an xterm.dart
      // circular-buffer bug when a remote/local switch changes viewport size.
      reflowEnabled: false,
    );
    if (bindCallbacks) _bindTerminal(result);
    return result;
  }

  void _bindTerminal(Terminal result) {
    result.onOutput = _onTerminalOutput;
    // tmux/the PTY host owns cursor-position replies. Sending the renderer's
    // duplicate cursor reply to ConPTY makes PSReadLine eat the first key.
    result.onCursorPositionReport = (_) {};
    result.onResize = (width, height, _, _) => resize(width, height);
  }

  /// Sends a composed message as one turn.
  ///
  /// This does NOT write bytes into the pane. It sends the same `message` frame the web client
  /// uses to drive a turn (`sendMessage` in web's `lib/ws.ts`), and the machine's own handler owns
  /// the injection from there: it adapts slash commands to the pane's engine and retries the
  /// submit Enter. A client typing bytes can do neither — which is exactly how Codex ended up
  /// holding a composed line unsent, its Enter arriving in the same read as the text.
  Future<bool> sendComposerText(String text) async {
    if (!acceptsInput) return false;
    final content = text.trimRight();
    if (content.trim().isEmpty) return false;
    return send('message', {
      'content': content,
      'agentId': agentId,
      'mode': 'auto',
      if (_inputContext != null) 'inputContext': _inputContext,
      if (!kIsWeb && client?.kind == 'desktop') 'usageIntent': 'interactive',
    });
  }

  /// A clipboard paste made directly into this pane, delivered as one atomic unit instead of going
  /// through the same chunked pipeline as ordinary typing ([_onTerminalOutput]/[_flushInput]).
  ///
  /// A big paste run through that pipeline gets sliced into ≤8 KiB binary frames client-side and then
  /// into 2 KiB `send-keys -H` bursts on the daemon — each burst arrives far enough apart that the
  /// engine's own paste-detector (readline/Ink) can register it as a SEPARATE paste, which is why a
  /// large paste read as dozens of `[Pasted text #N]` markers instead of one. A [TerminalBinaryKind.paste]
  /// frame routes through the daemon's `tmux paste-buffer` instead, the same single-shot mechanism
  /// composer messages already use — see `pasteRawIntoTmux` in the harness CLI.
  ///
  /// Binary, not JSON: unlike a JSON frame type, a new binary `kind` needs no entry in the E2EE
  /// allowlist (`ENCRYPTED_DOWN_TYPES`) three OTHER codebases also pin a hash of — the AEAD wrapping
  /// that already covers `input`/`output` covers this too, with nothing extra to keep in sync. That is
  /// what lets this work over a relayed machine, not just a local one.
  ///
  /// The caller must check [MachineState.terminalPasteRawAvailable] first: an older CLI does not know
  /// this binary kind at all, so sending it there would silently go nowhere.
  Future<bool> pasteText(String text) async {
    if (!acceptsInput) return false;
    // Forwarded verbatim, including a stray 0x03 — same as _onTerminalOutput/sendComposerText.
    if (text.isEmpty) return false;
    final currentStreamId = streamId;
    if (currentStreamId == null) return false;
    final generation = _generation;
    final frame = TerminalBinaryFrame(
      kind: TerminalBinaryKind.paste,
      streamId: currentStreamId,
      // Unused server-side (a paste is one self-contained unit, not part of the ordered keystroke
      // stream `input`'s seq guards) — kept at 0 rather than threading a second counter for a field
      // nothing reads.
      seq: 0,
      bytes: Uint8List.fromList(utf8.encode(text)),
      compressed: false,
    );
    final sent = await sendBinary(frame);
    if (!sent && _isCurrent(generation)) {
      transportLost('Terminal paste was not sent');
    }
    return sent;
  }

  /// A clipboard IMAGE paste (raw PNG bytes) made directly into this pane, sent as a chunked
  /// upload (see [_uploadBytes]) over [TerminalBinaryKind.imagePaste] frames — the daemon's
  /// text-paste handler requires valid UTF-8 and would reject PNG bytes outright, hence its own
  /// kind, same reason [pasteText] doesn't carry it.
  ///
  /// The caller must check [MachineState.terminalImagePasteAvailable] first, same reason
  /// [pasteText] checks `terminalPasteRawAvailable`: an older CLI does not know this binary kind
  /// at all, so sending it there would silently go nowhere.
  Future<bool> pasteImage(Uint8List pngBytes) async {
    if (pngBytes.isEmpty) return false;
    return _uploadBytes(
      uploadKind: 'image',
      filename: null,
      bytes: pngBytes,
      binaryKind: TerminalBinaryKind.imagePaste,
    );
  }

  /// A dropped (non-image) FILE — sent as a chunked upload (see [_uploadBytes]) so the daemon can
  /// write it to disk on its own (REMOTE) machine and paste that path as text. Only meaningful for
  /// a genuinely remote pane: a LOCAL file already has a valid path on this same machine, so
  /// callers should paste that path directly via [pasteText] instead and never reach this method
  /// at all — see [MachineState.isLocalMachine].
  ///
  /// The caller must check [MachineState.terminalPasteFileAvailable] first, same reason
  /// [pasteImage] checks `terminalImagePasteAvailable`: an older CLI does not know this binary kind
  /// at all, so sending it there would silently go nowhere.
  Future<bool> pasteFile(String filename, Uint8List content) async {
    if (filename.isEmpty || content.isEmpty) return false;
    return _uploadBytes(
      uploadKind: 'file',
      filename: filename,
      bytes: content,
      binaryKind: TerminalBinaryKind.pasteFile,
    );
  }

  /// Cancels whatever image/file upload is currently in flight on this pane, if any — the user's
  /// own Cancel affordance on the upload-progress overlay. A no-op if nothing is uploading.
  Future<void> cancelUpload() async {
    final upload = _activeUpload;
    final currentStreamId = streamId;
    if (upload == null) return;
    if (!upload.beginAccepted.isCompleted) upload.beginAccepted.complete(false);
    if (!upload.finished.isCompleted) upload.finished.complete(false);
    _clearUpload();
    if (currentStreamId != null) {
      await send('terminal_chunked_upload_cancel', {
        'streamId': currentStreamId,
      });
    }
  }

  /// Shared orchestration for [pasteImage]/[pasteFile]: announce the upload
  /// (`terminal_chunked_upload_begin`), wait to be accepted, send the bytes as a series of
  /// `binaryKind` frames — [TerminalBinaryFrame.seq] is the chunk index — each at most
  /// [terminalUploadChunkBytes], then wait for the daemon's completion/error frame. Chunked rather
  /// than sent whole because a single frame over 512 KiB never reaches a relayed or P2P-connected
  /// remote machine at all (both cap a binary message there); see the plan this shipped from.
  ///
  /// [uploadProgress] reflects the daemon's own per-chunk ACKs
  /// (`terminal_chunked_upload_progress`), not "chunks handed to the local socket" — that is what
  /// keeps the percentage honest on a slow link, mirroring the firmware pusher's `fw.progress`
  /// philosophy (`autonomous-harness/cli/src/cable/fwPush.ts`). Only one upload runs at a time per
  /// pane — a second call while one is active is refused immediately, matching the daemon's own
  /// "an upload is already in progress" rejection.
  ///
  /// The same ACKs pace the chunks: at most [terminalUploadWindowBytes] go out ahead of them. A
  /// chunk lost on the way (a P2P link dropped with chunks in its buffer, say) is never asked for
  /// again, and the daemon waits for it for as long as the stream lives; without an ACK for
  /// [uploadStallTimeout], the upload is given up rather than left showing its last percentage.
  /// Every upload given up is cancelled on the daemon too: one it still held refused every later
  /// upload on the pane.
  Future<bool> _uploadBytes({
    required String uploadKind,
    required String? filename,
    required Uint8List bytes,
    required TerminalBinaryKind binaryKind,
  }) async {
    if (!acceptsInput) return false;
    if (_activeUpload != null) return false;
    final currentStreamId = streamId;
    if (currentStreamId == null) return false;

    final upload = _ActiveUpload();
    final generation = _generation;
    bool current() =>
        _isCurrent(generation) && identical(_activeUpload, upload);
    _activeUpload = upload;
    lastStalledUpload = null;
    uploadProgress = UploadProgress(
      label: filename ?? 'image',
      bytesWritten: 0,
      totalBytes: bytes.length,
    );
    notifyListeners();

    var accepted = false;
    // A daemon may still hold an upload an older build gave up on without cancelling it. Cancel
    // that one and ask once more.
    for (var attempt = 0; attempt < 2 && !accepted; attempt++) {
      if (attempt > 0) {
        upload.beginAccepted = Completer<bool>();
        await send('terminal_chunked_upload_cancel', {
          'streamId': currentStreamId,
        });
        if (!current()) return false;
      }
      final sentBegin = await send('terminal_chunked_upload_begin', {
        'streamId': currentStreamId,
        'uploadKind': uploadKind,
        'totalBytes': bytes.length,
        'filename': ?filename,
      });
      if (!current()) return false;
      if (!sentBegin) {
        transportLost('Terminal upload request was not sent');
        _clearUpload();
        return false;
      }
      accepted = await upload.beginAccepted.future.timeout(
        const Duration(seconds: 10),
        onTimeout: () => false,
      );
      if (!current()) return false;
      // An unanswered begin may still have been accepted.
      if (!accepted && !upload.beginAccepted.isCompleted) {
        return _abandonUpload(currentStreamId);
      }
      if (upload.refusal != _uploadAlreadyInProgress) break;
    }
    if (!accepted) {
      _clearUpload();
      return false;
    }

    var offset = 0;
    var seq = 0;
    while (offset < bytes.length && !upload.finished.isCompleted) {
      while (offset - upload.acked >= terminalUploadWindowBytes &&
          !upload.finished.isCompleted) {
        final moved = await _awaitUploadAck(upload);
        if (!current()) return false;
        if (!moved) return _abandonUpload(currentStreamId, stalled: true);
      }
      if (upload.finished.isCompleted) break;
      final end = min(offset + terminalUploadChunkBytes, bytes.length);
      final frame = TerminalBinaryFrame(
        kind: binaryKind,
        streamId: currentStreamId,
        seq: seq,
        bytes: Uint8List.sublistView(bytes, offset, end),
        compressed: false,
      );
      final sentChunk = await sendBinary(frame);
      if (!current()) return false;
      if (!sentChunk) {
        transportLost('Terminal upload chunk was not sent');
        _clearUpload();
        return false;
      }
      offset = end;
      seq++;
    }

    while (upload.acked < bytes.length && !upload.finished.isCompleted) {
      final moved = await _awaitUploadAck(upload);
      if (!current()) return false;
      if (!moved) return _abandonUpload(currentStreamId, stalled: true);
    }
    // Every byte is there: what remains is the daemon's own paste (a clipboard write, a file).
    final finished = await upload.finished.future.timeout(
      const Duration(minutes: 2),
      onTimeout: () => false,
    );
    if (!current()) return false;
    if (!finished) return _abandonUpload(currentStreamId);
    _clearUpload();
    return true;
  }

  /// True once the daemon confirms more of [upload] or it ends; false after [uploadStallTimeout]
  /// with neither.
  Future<bool> _awaitUploadAck(_ActiveUpload upload) async {
    try {
      await Future.any([upload.nextAck(), upload.finished.future])
          .timeout(uploadStallTimeout);
      return true;
    } on TimeoutException {
      return false;
    }
  }

  /// Gives up the active upload here and on the daemon. The cancel is queued before the upload is
  /// cleared, so it reaches the daemon ahead of any upload this pane starts next.
  Future<bool> _abandonUpload(
    String uploadStreamId, {
    bool stalled = false,
  }) async {
    final progress = uploadProgress;
    if (stalled) {
      lastStalledUpload = progress;
      appLog.warn(
        'terminal',
        'upload stalled machine=$machineId agent=$agentId '
            'confirmed=${progress?.bytesWritten}/${progress?.totalBytes}',
      );
    }
    final cancelled = send('terminal_chunked_upload_cancel', {
      'streamId': uploadStreamId,
    });
    _clearUpload();
    await cancelled;
    return false;
  }

  void _clearUpload() {
    _activeUpload = null;
    uploadProgress = null;
    notifyListeners();
  }

  void _onTerminalOutput(String data) {
    if (!acceptsInput || data.isEmpty) return;
    final bytes = utf8.encode(data);
    final isBoundary =
        data.contains('\r') ||
        data.contains('\x1b[200~') ||
        data.contains('\x1b[201~');
    if (isBoundary) unawaited(_flushInput());
    var offset = 0;
    while (offset < bytes.length) {
      final available = 8 * 1024 - _inputBytes.length;
      final take = min(available, bytes.length - offset);
      _inputBytes.addAll(bytes.sublist(offset, offset + take));
      offset += take;
      if (_inputBytes.length == 8 * 1024) unawaited(_flushInput());
    }
    if (isBoundary) {
      unawaited(_flushInput());
    } else if (_inputBytes.isNotEmpty) {
      // Leading edge: the first keystroke after a pause goes out at once. The window exists to
      // batch key-repeat, and a lone keypress has nothing to batch with — making it wait was
      // 4ms of pure latency on every character typed at human speed. Anything arriving inside
      // the window still rides the trailing timer, so the frame rate stays bounded.
      final last = _lastInputFlushAt;
      final idle =
          last == null ||
          inputClockForTest().difference(last) >= _inputCoalesceWindow;
      if (_inputTimer == null && idle) {
        unawaited(_flushInput());
      } else {
        _inputTimer ??= Timer(
          _inputCoalesceWindow,
          () => unawaited(_flushInput()),
        );
      }
    }
  }

  Future<void> _flushInput() async {
    _inputTimer?.cancel();
    _inputTimer = null;
    if (!acceptsInput || _inputBytes.isEmpty) {
      _inputBytes.clear();
      return;
    }
    final bytes = List<int>.from(_inputBytes);
    _inputBytes.clear();
    _lastInputFlushAt = inputClockForTest();
    final currentStreamId = streamId;
    if (currentStreamId == null) return;
    final generation = _generation;
    final queued = _inputSendTail.then((_) async {
      if (!_isCurrent(generation) ||
          !acceptsInput ||
          streamId != currentStreamId) {
        return;
      }
      // Numbered HERE, on the tail, not when the flush was asked for. A frame
      // the guard above drops — the session went `resyncing` while an earlier
      // send was still in flight — must not consume a seq: the daemon wants
      // them contiguous and never re-syncs its counter, so one skipped number
      // had the very next keystroke refused and the whole pane frozen. The
      // tail runs one closure at a time, which is what keeps the numbers in
      // order.
      //
      // Never larger than the daemon accepts, either — split rather than let
      // one oversized frame be refused with the counter intact behind it.
      for (
        var offset = 0;
        offset < bytes.length;
        offset += kInputFrameMaxBytes
      ) {
        if (!_isCurrent(generation) ||
            !acceptsInput ||
            streamId != currentStreamId) {
          return;
        }
        final end = min(offset + kInputFrameMaxBytes, bytes.length);
        final frame = TerminalBinaryFrame(
          kind: TerminalBinaryKind.input,
          streamId: currentStreamId,
          seq: _inputSeq++,
          bytes: Uint8List.fromList(bytes.sublist(offset, end)),
          compressed: false,
        );
        final sendStartedAt = _now();
        final sent = await sendBinary(frame);
        final sendTook = _now().difference(sendStartedAt);
        if (sendTook >= _slowInputThreshold) {
          appLog.warn(
            'terminal',
            'input send slow machine=$machineId agent=$agentId stream=$currentStreamId '
                'link=$linkMode tookMs=${sendTook.inMilliseconds} bytes=${frame.bytes.length}',
          );
        }
        if (!_isCurrent(generation)) return;
        if (!sent) {
          transportLost('Terminal input was not sent');
          return;
        }
        _inputUnansweredSince ??= sendStartedAt;
      }
    });
    _inputSendTail = queued.catchError((_) {
      if (_isCurrent(generation)) transportLost('Terminal input was not sent');
    });
    await _inputSendTail;
  }

  /// A finger on the dial moved — scroll whatever this session is showing.
  ///
  /// Goes STRAIGHT to the renderer rather than through the machine: the scrollback being moved is the copy
  /// in this window, and the agent on the other end has no idea the view scrolled. Nothing is sent over
  /// the wire and nothing is echoed back.
  void scroll(int phase, int dy, int velocity) {
    _viewport?.scroll(phase, dy, velocity);
  }

  void find(TerminalFindAction action) => _viewport?.find(action);

  Map<String, dynamic> selectPassage(Map<String, dynamic> command) {
    final viewport = _viewport;
    return viewport is TerminalPassageViewport
        ? (viewport as TerminalPassageViewport).selectPassage(command)
        : {'ok': false, 'error': 'Open the terminal pane first.'};
  }

  Future<Map<String, dynamic>> searchPassage(
    Map<String, dynamic> command,
  ) async {
    final viewport = _viewport;
    return viewport is TerminalPassageSearchViewport
        ? (viewport as TerminalPassageSearchViewport).searchPassage(command)
        : {'ok': false, 'error': 'Open the terminal pane first.'};
  }

  bool focusInput() => _viewport?.focusInput() ?? false;

  TerminalReadingBookmark? bookmarkReading() {
    final viewport = _viewport;
    return viewport is TerminalReadingViewport
        ? (viewport as TerminalReadingViewport).bookmarkReading()
        : null;
  }

  bool showLatestReading() {
    final viewport = _viewport;
    return viewport is TerminalLatestViewport &&
        (viewport as TerminalLatestViewport).showLatestReading();
  }

  /// Coalescing windows for the two things the user drives directly.
  ///
  /// Both are leading + trailing: act on the first event, batch the rest. A flat trailing debounce
  /// charged its full window to every single keystroke and to the start of every drag, which is
  /// latency paid for a batch that mostly never materializes.
  static const _inputCoalesceWindow = Duration(milliseconds: 4);

  /// Typing that takes this long to be sent, or to be answered, is logged: the person feels it,
  /// and a stall shorter than the 15s stream timeout leaves no other trace (2026-10-19).
  static const _slowInputThreshold = Duration(seconds: 3);

  /// A sync frame answers only after the daemon's idle cadence (5s interval, swept every 5s), so
  /// a keystroke the program never echoes waits up to ~10s for one on a healthy link.
  static const _slowInputSyncThreshold = Duration(seconds: 11);

  /// The stream spoke after input went out. Typing that waited long for it is logged with the
  /// time it waited and what answered, so a lag a person reports can be matched against both
  /// machines' logs. An echo normally answers within a round trip; a burst of output after a
  /// long wait is a link that stalled without closing.
  void _inputAnswered(String by) {
    final since = _inputUnansweredSince;
    if (since == null) return;
    _inputUnansweredSince = null;
    final waited = _now().difference(since);
    final threshold = by == 'sync'
        ? _slowInputSyncThreshold
        : _slowInputThreshold;
    if (waited < threshold) return;
    appLog.warn(
      'terminal',
      'input answered late machine=$machineId agent=$agentId stream=$streamId '
          'link=$linkMode waitedMs=${waited.inMilliseconds} by=$by',
    );
  }

  /// The stream is being given up on while typing still waits for it: say how long it waited.
  void _inputUnanswered(String until) {
    final since = _inputUnansweredSince;
    if (since == null) return;
    _inputUnansweredSince = null;
    final waited = _now().difference(since);
    if (waited < _slowInputThreshold) return;
    appLog.warn(
      'terminal',
      'input unanswered machine=$machineId agent=$agentId stream=$streamId '
          'link=$linkMode waitedMs=${waited.inMilliseconds} until=$until',
    );
  }

  /// The daemon's ceiling on one input frame (`INPUT_MAX_BYTES` in
  /// `cli/src/lib/terminalStreamManager.ts`). Larger is refused, not split.
  static const kInputFrameMaxBytes = 64 * 1024;
  static const _resizeCoalesceWindow = Duration(milliseconds: 50);

  void resize(int width, int height) {
    if (readOnly) return;
    _pendingCols = _clampCols(width);
    _pendingRows = _clampRows(height);
    final last = _lastResizeFlushAt;
    final idle =
        last == null || _now().difference(last) >= _resizeCoalesceWindow;
    if (_resizeTimer == null && idle) {
      unawaited(_flushResize());
      return;
    }
    // Mid-drag: keep resetting so tmux is asked once, for the size the drag settles on.
    _resizeTimer?.cancel();
    _resizeTimer = Timer(
      _resizeCoalesceWindow,
      () => unawaited(_flushResize()),
    );
  }

  Future<void> _flushResize() async {
    // A keyframe can flush the measured viewport before the debounce fires.
    // Cancel that callback before releasing its handle.
    _resizeTimer?.cancel();
    _resizeTimer = null;
    if (!acceptsInput || _pendingCols == null || _pendingRows == null) return;
    final nextCols = _pendingCols!;
    final nextRows = _pendingRows!;
    _pendingCols = null;
    _pendingRows = null;
    if (nextCols == cols && nextRows == rows) return;
    cols = nextCols;
    rows = nextRows;
    _lastResizeFlushAt = _now();
    final generation = _generation;
    final sent = await send('terminal_resize', {
      'streamId': streamId,
      'resizeSeq': _resizeSeq++,
      'cols': cols,
      'rows': rows,
    });
    if (!_isCurrent(generation)) return;
    if (!sent) transportLost('Terminal resize was not sent');
    notifyListeners();
  }

  static const _scrollCoalesceWindow = Duration(milliseconds: 16);

  /// Alt-buffer scroll gesture for a [scrollViaTmuxCopyMode] session — see that getter's doc.
  /// Coalesces rapid deltas (a fast trackpad swipe can emit dozens a second) into one
  /// `terminal_scroll` frame per short burst, mirroring `resize()`'s own debounce. Fire-and-forget
  /// like resize, but a dropped frame here is just a missed scroll, not a transport-health signal —
  /// unlike resize, it never calls `transportLost`.
  void sendScrollCommand(bool up, int lines) {
    if (!scrollViaTmuxCopyMode || lines <= 0) return;
    if (_pendingScrollUp != null && _pendingScrollUp != up) {
      // Direction reversed mid-burst — flush what's accumulated before starting the new direction,
      // rather than let it cancel out into a smaller net scroll the user never asked for.
      unawaited(_flushScrollCommand());
    }
    _pendingScrollUp = up;
    _pendingScrollLines += lines;
    _scrollTimer?.cancel();
    _scrollTimer = Timer(
      _scrollCoalesceWindow,
      () => unawaited(_flushScrollCommand()),
    );
  }

  Future<void> _flushScrollCommand() async {
    _scrollTimer?.cancel();
    _scrollTimer = null;
    final up = _pendingScrollUp;
    final lines = _pendingScrollLines;
    _pendingScrollUp = null;
    _pendingScrollLines = 0;
    if (!acceptsInput || up == null || lines <= 0) return;
    await send('terminal_scroll', {
      'streamId': streamId,
      'direction': up ? 'up' : 'down',
      'lines': lines,
    });
  }

  void _markForAck(int renderedBytes) {
    _framesSinceAck++;
    _renderedSinceAckBytes += renderedBytes;
    if (_renderedSinceAckBytes >= 64 * 1024) {
      unawaited(_flushAck());
      return;
    }
    _ackTimer ??= Timer(
      const Duration(milliseconds: 16),
      () => unawaited(_flushAck()),
    );
  }

  Future<void> _flushAck() async {
    _ackTimer?.cancel();
    _ackTimer = null;
    if (streamId == null || _lastRenderedSeq < 0 || _framesSinceAck == 0) {
      return;
    }
    _framesSinceAck = 0;
    _renderedSinceAckBytes = 0;
    final generation = _generation;
    final sent = await send('terminal_ack', {
      'streamId': streamId,
      'lastSeq': _lastRenderedSeq,
    });
    if (!sent && _isCurrent(generation)) {
      transportLost('Terminal ACK was not sent');
    }
  }

  Future<void> _sendHeartbeat({bool afterSleep = false}) async {
    if (status != TerminalSessionStatus.controlling || streamId == null) return;
    // The first beat after the computer slept: the stream was quiet because nobody here was running,
    // not because it died. Its silence is measured afresh from now — the daemon carries the lease over
    // the same gap — instead of declaring TERMINAL_STREAM_TIMEOUT and throwing away typed input.
    if (afterSleep) _lastStreamActivityAt = _now();
    final generation = _generation;
    final sent = await send('terminal_alive', {
      'streamId': streamId,
      // Ask the daemon to say so if it no longer holds this stream (CLI `reportMissing`).
      'reportMissing': true,
    });
    if (!sent && _isCurrent(generation)) {
      transportLost('Terminal heartbeat was not sent');
      return;
    }
    // The daemon sends sync frames every five seconds even when the terminal is idle. Sending
    // successfully only proves the shared socket is open, not that THIS stream still exists.
    final lastActivity = _lastStreamActivityAt;
    if (_isCurrent(generation) &&
        status == TerminalSessionStatus.controlling &&
        lastActivity != null &&
        _now().difference(lastActivity) >= const Duration(seconds: 15)) {
      await _requestResync('TERMINAL_STREAM_TIMEOUT');
    }
  }

  Future<void> _requestResync(String reason) async {
    if (streamId == null) {
      _fail(reason, null);
      return;
    }
    if (!_resyncRequested) {
      _resyncRequested = true;
      _resyncAttempts = 0;
      status = TerminalSessionStatus.resyncing;
      errorCode = reason;
      _inputUnanswered(reason);
      _inputBytes.clear();
      notifyListeners();
    }
    if (_resyncAttempts > 0) return;
    await _sendResyncAttempt();
  }

  void _armInitialKeyframeWatchdog() {
    _resyncTimer?.cancel();
    _resyncTimer = SleepAwareTimer(resyncTimeout, () {
      if (streamId != null && _expectedSeq == null) {
        unawaited(_requestResync('TERMINAL_KEYFRAME_TIMEOUT'));
      }
    });
  }

  Future<void> _sendResyncAttempt() async {
    final currentStream = streamId;
    final generation = _generation;
    if (!_resyncRequested || currentStream == null) return;
    if (_resyncAttempts >= 3) {
      await _recoverByReopen();
      return;
    }
    _resyncAttempts++;
    debugPrint(
      '[terminal-session] resync_request agent=$agentId stream=$currentStream '
      'attempt=$_resyncAttempts code=$errorCode',
    );
    final sent = await send('terminal_resync', {
      'streamId': currentStream,
      'attempt': _resyncAttempts,
      'reason': errorCode,
      'reportMissing': true,
    });
    if (!_isCurrent(generation) ||
        streamId != currentStream ||
        !_resyncRequested) {
      return;
    }
    if (!sent) {
      transportLost('Could not request terminal resync');
      return;
    }
    _resyncTimer?.cancel();
    _resyncTimer = SleepAwareTimer(resyncTimeout, () {
      if (_resyncRequested && streamId == currentStream) {
        unawaited(_sendResyncAttempt());
      }
    });
  }

  Future<void> _recoverByReopen({
    String reason = 'TERMINAL_RESYNC_TIMEOUT',
  }) async {
    final previousStream = streamId;
    if (_autoReopenAttempts >= 1) {
      debugPrint(
        '[terminal-session] recovery_failed agent=$agentId '
        'stream=$previousStream reason=$reason attempts=$_resyncAttempts',
      );
      _fail(
        reason,
        reason == 'TERMINAL_RESYNC_TIMEOUT'
            ? 'Terminal did not recover after resync and reopen.'
            : 'Terminal did not recover after reopening the stream.',
      );
      return;
    }
    _autoReopenAttempts++;
    appLog.info(
      'terminal',
      'reopen machine=$machineId agent=$agentId stream=$previousStream reason=$reason',
    );
    if (previousStream != null) {
      unawaited(send('terminal_close', {'streamId': previousStream}));
    }
    await _open(
      initialCols: cols,
      initialRows: rows,
      waitForViewportSize: false,
      resetRecovery: false,
      preserveTerminal: true,
    );
  }

  Future<void> close() async {
    closedHere = true;
    _generation++;
    final closingStream = streamId;
    _cancelTimers();
    _inputBytes.clear();
    _inputUnansweredSince = null;
    streamId = null;
    linkMode = null;
    status = TerminalSessionStatus.closed;
    _abortActiveUpload();
    notifyListeners();
    if (closingStream != null) {
      await send('terminal_close', {'streamId': closingStream});
    }
  }

  void transportLost([
    String message = 'Connection lost. Select the harness to reconnect.',
  ]) {
    // `takenOver` is a deliberate dead end (see `_paneNeedsAttach`): only the user's own retry
    // may reopen a stream someone else claimed. A WS hiccup must not quietly overwrite that into
    // `error`, which auto-reattach WOULD pick back up — that is exactly the two-machine tug-of-war
    // this status exists to prevent.
    if (_disposed ||
        status == TerminalSessionStatus.closed ||
        status == TerminalSessionStatus.takenOver) {
      return;
    }
    appLog.warn(
      'terminal',
      'transport lost machine=$machineId agent=$agentId message=$message',
    );
    _inputUnanswered('transport_lost');
    _generation++;
    _cancelTimers();
    _inputBytes.clear();
    streamId = null;
    linkMode = null;
    status = TerminalSessionStatus.error;
    errorCode = disconnectedCode;
    errorMessage = message;
    _abortActiveUpload();
    notifyListeners();
  }

  void _fail(String code, String? message) {
    if (_disposed) return;
    // Why a pane shows Reconnect was only ever in debugPrint, invisible on a person's machine (2026-10-16).
    appLog.warn(
      'terminal',
      'failed machine=$machineId agent=$agentId stream=$streamId code=$code'
          '${message == null ? '' : ' message=$message'}',
    );
    _inputUnanswered(code);
    _generation++;
    _cancelTimers();
    _inputBytes.clear();
    streamId = null;
    linkMode = null;
    status = TerminalSessionStatus.error;
    errorCode = code;
    errorMessage = message;
    _abortActiveUpload();
    notifyListeners();
  }

  /// A safety net for any path that resets the stream (transport loss, a real failure) while an
  /// image/file upload happens to be in flight — resolves its awaiting Future rather than leaving
  /// it to hang until [_uploadBytes]'s own 2-minute timeout. The specific error codes that mean
  /// "the upload itself failed" already call this via [transportLost]/[_fail]; this also covers
  /// every OTHER way the stream can go away mid-upload.
  void _abortActiveUpload() {
    if (_activeUpload?.beginAccepted.isCompleted == false) {
      _activeUpload!.beginAccepted.complete(false);
    }
    if (_activeUpload?.finished.isCompleted == false) {
      _activeUpload!.finished.complete(false);
    }
    _activeUpload = null;
    uploadProgress = null;
  }

  bool _matchesStream(Map<String, dynamic> payload) =>
      streamId != null && payload['streamId'] == streamId;

  int _clampCols(int value) => value.clamp(minCols, maxCols);
  int _clampRows(int value) => value.clamp(minRows, maxRows);

  void _cancelTimers() {
    _heartbeat?.cancel();
    _heartbeat = null;
    _ackTimer?.cancel();
    _ackTimer = null;
    _inputTimer?.cancel();
    _inputTimer = null;
    _resizeTimer?.cancel();
    _resizeTimer = null;
    _resyncTimer?.cancel();
    _resyncTimer = null;
    _scrollTimer?.cancel();
    _scrollTimer = null;
  }

  @override
  void dispose() {
    _disposed = true;
    _generation++;
    _cancelTimers();
    _remoteCursorVisible.dispose();
    super.dispose();
  }
}