Terminals: flow-controlled output, immediate echo, sliced emulator writes
Machine terminals no longer skip output for live viewers. Each connection's subscription is a cursor into the 1 MiB retained tail, drained by the connection's writer as soon as the PTY reader writes (no 50 ms tick; a streaming frame gathers for at most 2 ms). A subscription opened with flow: true gets window (256 KiB) in its reply and acknowledges drawn output with terminal_ack; while one is full the reader stops reading the PTY, so the program waits. A subscription full for 3 s without acking stops holding the terminal and later sees a gap; a gone connection drops its subscriptions. Clients that never ask for flow keep the old pacing (newest 64 KiB per 50 ms). terminal_open/terminal_output replies precede the frames that follow them, which start exactly at the reply's output_end. Chat shell runs share the buffer code: output is pushed as it arrives, oldest first, while the chat's watchers keep short queues; 256 KiB unpushed stops the reader, and watchers behind for 3 s get the old pacing. The app asks for flow, takes offsets from the server (frame end, reply output_end) instead of re-encoded text length, acks per quarter window, and writes replays and bursts into the emulator in slices that yield to the event loop (ChatShellTerminalFeed, shared with chat shells). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -101,7 +101,7 @@ class TerminalHub extends ChangeNotifier {
|
||||
}
|
||||
try {
|
||||
final reply = await machine.gateway.call('terminal_open', {
|
||||
'terminal_request': {'cwd': ?(cwd.isEmpty ? null : cwd)},
|
||||
'terminal_request': {'cwd': ?(cwd.isEmpty ? null : cwd), 'flow': true},
|
||||
});
|
||||
final record = reply['terminal'];
|
||||
if (record is! Map) throw StateError('The server sent no terminal.');
|
||||
|
||||
@@ -28,11 +28,13 @@ class TerminalStream {
|
||||
/// Whether nothing was received yet.
|
||||
bool get empty => stream.isEmpty && end == 0;
|
||||
|
||||
/// Replaces the held copy with a read of the server's retained tail.
|
||||
void reset(String output, int outputStart, String name) {
|
||||
/// Replaces the held copy with a read of the server's retained tail,
|
||||
/// covering bytes [outputStart, outputEnd) (the server's offsets: the
|
||||
/// text's own UTF-8 length differs where invalid bytes were replaced).
|
||||
void reset(String output, int outputStart, int outputEnd, String name) {
|
||||
text = output;
|
||||
start = outputStart;
|
||||
end = outputStart + utf8.encode(output).length;
|
||||
end = outputEnd;
|
||||
stream = name;
|
||||
_bound();
|
||||
}
|
||||
@@ -55,7 +57,11 @@ class TerminalStream {
|
||||
// Never split a surrogate pair.
|
||||
final unit = text.codeUnitAt(cut);
|
||||
if (unit >= 0xDC00 && unit <= 0xDFFF) cut++;
|
||||
final dropped = text.substring(0, cut);
|
||||
text = text.substring(cut);
|
||||
start = end - utf8.encode(text).length;
|
||||
// The server's offsets are not re-derived from the text: only the
|
||||
// dropped part is measured (approximately, for replaced bytes).
|
||||
start += utf8.encode(dropped).length;
|
||||
if (start > end) start = end;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ class TerminalTab extends ChangeNotifier {
|
||||
onResize: _resized,
|
||||
);
|
||||
session.terminal.onTitleChange = _titled;
|
||||
session.onDrawn = _drawn;
|
||||
}
|
||||
|
||||
final String machineId;
|
||||
@@ -45,6 +46,12 @@ class TerminalTab extends ChangeNotifier {
|
||||
bool _reading = false;
|
||||
bool _again = false;
|
||||
|
||||
/// The flow window the server granted this connection's subscription
|
||||
/// (0: an older server, which takes no acknowledgements), and the end
|
||||
/// last acknowledged.
|
||||
int _window = 0;
|
||||
int _acked = 0;
|
||||
|
||||
/// False while the terminal's machine is not connected.
|
||||
bool connected = true;
|
||||
|
||||
@@ -83,6 +90,7 @@ class TerminalTab extends ChangeNotifier {
|
||||
_gateway = gateway;
|
||||
connected = true;
|
||||
_sentSize = null;
|
||||
_window = 0;
|
||||
_update(info);
|
||||
unawaited(resume());
|
||||
}
|
||||
@@ -119,7 +127,11 @@ class TerminalTab extends ChangeNotifier {
|
||||
|
||||
Future<void> _read(int? after) async {
|
||||
final reply = await _gateway.call('terminal_output', {
|
||||
'terminal_request': {'terminal_id': _info.id, 'after': ?after},
|
||||
'terminal_request': {
|
||||
'terminal_id': _info.id,
|
||||
'after': ?after,
|
||||
'flow': true,
|
||||
},
|
||||
});
|
||||
final record = reply['terminal'];
|
||||
if (_disposed || record is! Map) return;
|
||||
@@ -131,11 +143,18 @@ class TerminalTab extends ChangeNotifier {
|
||||
}
|
||||
final output = '${record['output'] ?? ''}';
|
||||
final start = (record['output_start'] as num?)?.toInt() ?? 0;
|
||||
// The reply's own end (an older server's is its total, which a text
|
||||
// of only valid characters also reaches).
|
||||
var end = (record['output_end'] as num?)?.toInt() ?? 0;
|
||||
if (end < start) end = start + _byteLength(output);
|
||||
// The server counts this connection's window from the reply's end.
|
||||
_window = (record['window'] as num?)?.toInt() ?? 0;
|
||||
_acked = end;
|
||||
if (after == null || start > _stream.end) {
|
||||
_stream.reset(output, start, info.stream);
|
||||
_stream.reset(output, start, end, info.stream);
|
||||
session.replay(_run());
|
||||
} else {
|
||||
_append(output, start, start + _byteLength(output));
|
||||
_append(output, start, end);
|
||||
}
|
||||
if (!info.running) session.finish(_run());
|
||||
}
|
||||
@@ -157,8 +176,9 @@ class TerminalTab extends ChangeNotifier {
|
||||
final before = _stream.end;
|
||||
final fresh = _stream.add(output, offset, end);
|
||||
if (fresh == null) {
|
||||
// Frames were missed: read the retained tail again.
|
||||
unawaited(resume());
|
||||
// A flow-controlled read in flight covers whatever was sent before
|
||||
// it; otherwise frames were missed: read the retained tail again.
|
||||
if (!(_reading && _window > 0)) unawaited(resume());
|
||||
return;
|
||||
}
|
||||
if (fresh.isNotEmpty) session.append(_run(), fresh, before, end);
|
||||
@@ -176,6 +196,21 @@ class TerminalTab extends ChangeNotifier {
|
||||
_notify();
|
||||
}
|
||||
|
||||
/// The emulator drew output up to [end]: acknowledge it once a quarter
|
||||
/// of the window is consumed, so the server sends (and reads) more.
|
||||
void _drawn(int end) {
|
||||
if (_window <= 0 || !connected || _disposed) return;
|
||||
if (end - _acked < _window ~/ 4) return;
|
||||
_acked = end;
|
||||
unawaited(
|
||||
_gateway
|
||||
.call('terminal_ack', {
|
||||
'terminal_request': {'terminal_id': _info.id, 'after': end},
|
||||
})
|
||||
.catchError((Object _) => <String, dynamic>{}),
|
||||
);
|
||||
}
|
||||
|
||||
void _titled(String title) {
|
||||
if (title == _oscTitle) return;
|
||||
_oscTitle = title.trim();
|
||||
@@ -229,6 +264,8 @@ class TerminalTab extends ChangeNotifier {
|
||||
_resize?.cancel();
|
||||
_pipe = null;
|
||||
session.terminal.onTitleChange = null;
|
||||
session.onDrawn = null;
|
||||
session.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user