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.

feat(desktop): log typing that waits for the terminal to answer

reinSPQR641781bf43ff
diff --git a/desktop/lib/terminal/terminal_session.dart b/desktop/lib/terminal/terminal_session.dart
index d3f4f631d..3c9b139a3 100644
--- a/desktop/lib/terminal/terminal_session.dart
+++ b/desktop/lib/terminal/terminal_session.dart
@@ -301,6 +301,10 @@ class TerminalSession extends ChangeNotifier {
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;
@@ -433,6 +437,7 @@ class TerminalSession extends ChangeNotifier {
_lastInputFlushAt = null;
_lastResizeFlushAt = null;
_lastStreamActivityAt = null;
+ _inputUnanswered('reopen');
_renderTail = Future<void>.value();
_inputSendTail = Future<void>.value();
streamId = null;
@@ -854,6 +859,7 @@ class TerminalSession extends ChangeNotifier {
_resyncTimer = null;
status = TerminalSessionStatus.controlling;
_lastStreamActivityAt = _now();
+ _inputAnswered('keyframe');
_markForAck(bytes.length);
notifyListeners();
final measured = _measuredViewport;
@@ -875,6 +881,7 @@ class TerminalSession extends ChangeNotifier {
_expectedSeq = frame.seq + 1;
_lastRenderedSeq = frame.seq;
_lastStreamActivityAt = _now();
+ _inputAnswered('sync');
_markForAck(0);
return;
}
@@ -893,6 +900,7 @@ class TerminalSession extends ChangeNotifier {
_expectedSeq = frame.seq + 1;
_lastRenderedSeq = frame.seq;
_lastStreamActivityAt = _now();
+ _inputAnswered('output');
_markForAck(bytes.length);
})
.catchError((Object error, StackTrace stackTrace) async {
@@ -1365,12 +1373,22 @@ class TerminalSession extends ChangeNotifier {
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((_) {
@@ -1428,6 +1446,48 @@ class TerminalSession extends ChangeNotifier {
/// 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;
@@ -1584,6 +1644,7 @@ class TerminalSession extends ChangeNotifier {
_resyncAttempts = 0;
status = TerminalSessionStatus.resyncing;
errorCode = reason;
+ _inputUnanswered(reason);
_inputBytes.clear();
notifyListeners();
}
@@ -1676,6 +1737,7 @@ class TerminalSession extends ChangeNotifier {
final closingStream = streamId;
_cancelTimers();
_inputBytes.clear();
+ _inputUnansweredSince = null;
streamId = null;
linkMode = null;
status = TerminalSessionStatus.closed;
@@ -1702,6 +1764,7 @@ class TerminalSession extends ChangeNotifier {
'terminal',
'transport lost machine=$machineId agent=$agentId message=$message',
);
+ _inputUnanswered('transport_lost');
_generation++;
_cancelTimers();
_inputBytes.clear();
@@ -1722,6 +1785,7 @@ class TerminalSession extends ChangeNotifier {
'failed machine=$machineId agent=$agentId stream=$streamId code=$code'
'${message == null ? '' : ' message=$message'}',
);
+ _inputUnanswered(code);
_generation++;
_cancelTimers();
_inputBytes.clear();
diff --git a/desktop/test/terminal_session_test.dart b/desktop/test/terminal_session_test.dart
index 3071c5917..48606a8e5 100644
--- a/desktop/test/terminal_session_test.dart
+++ b/desktop/test/terminal_session_test.dart
@@ -1588,6 +1588,80 @@ void main() {
);
});
+ group('slow input log', () {
+ late _RecordingLog log;
+
+ Future<void> typing() async {
+ log = _RecordingLog();
+ final previous = appLog;
+ appLog = log;
+ addTearDown(() => appLog = previous);
+ controlledNow = DateTime(2026, 10, 19, 10);
+ await ready();
+ await session.handleBinary(
+ output(0, utf8.encode(r'$ '), keyframe: true, cols: 100, rows: 30),
+ );
+ session.terminal.onOutput?.call('a');
+ await pumpEventQueue();
+ expect(binarySent, hasLength(1));
+ }
+
+ List<String> slow() =>
+ log.lines.where((line) => line.contains('input ')).toList();
+
+ test('an echo that comes back late is logged with the wait', () async {
+ await typing();
+ controlledNow = controlledNow!.add(const Duration(seconds: 9));
+ await session.handleBinary(output(1, utf8.encode('a')));
+ await pumpEventQueue();
+
+ expect(slow(), [
+ 'terminal input answered late machine=machine-1 agent=agent-1 stream=$streamId '
+ 'link=null waitedMs=9000 by=output',
+ ]);
+ });
+
+ test('a prompt echo is not logged', () async {
+ await typing();
+ controlledNow = controlledNow!.add(const Duration(milliseconds: 300));
+ await session.handleBinary(output(1, utf8.encode('a')));
+ await pumpEventQueue();
+
+ expect(slow(), isEmpty);
+ });
+
+ test(
+ 'an idle sync after unechoed input is not logged inside its cadence',
+ () async {
+ await typing();
+ controlledNow = controlledNow!.add(const Duration(seconds: 8));
+ await session.handleBinary(
+ TerminalBinaryFrame(
+ kind: TerminalBinaryKind.sync,
+ streamId: streamId,
+ seq: 1,
+ bytes: Uint8List(0),
+ compressed: false,
+ ),
+ );
+ await pumpEventQueue();
+
+ expect(slow(), isEmpty);
+ },
+ );
+
+ test('a stream timeout with typing still waiting is logged', () async {
+ await typing();
+ controlledNow = controlledNow!.add(const Duration(seconds: 16));
+ session.transportLost();
+
+ expect(slow(), [
+ 'terminal input unanswered machine=machine-1 agent=agent-1 stream=$streamId '
+ 'link=null waitedMs=16000 until=transport_lost',
+ ]);
+ });
+ });
+
group('composer', () {
/// The `message` frames this session put on the wire.
List<Map<String, dynamic>> messages() => [