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:
@@ -0,0 +1,130 @@
|
||||
import 'dart:async';
|
||||
import 'dart:collection';
|
||||
|
||||
/// UTF-16 units one emulator write takes at most.
|
||||
const int chatShellFeedSlice = 16 * 1024;
|
||||
|
||||
/// How long one turn of the event loop writes output before yielding, so a
|
||||
/// large replay or burst never holds a UI frame for long.
|
||||
const Duration chatShellFeedBudget = Duration(milliseconds: 6);
|
||||
|
||||
/// Output on its way into one emulator, in order: written in slices of at
|
||||
/// most [slice] units, and after [budget] of writing the rest waits for the
|
||||
/// next turn of the event loop. Each piece carries the server's absolute
|
||||
/// byte offset its text ends at; [drawn] is the end of the last piece
|
||||
/// written whole (what a terminal acknowledges as consumed).
|
||||
class ChatShellTerminalFeed {
|
||||
ChatShellTerminalFeed(
|
||||
this._write, {
|
||||
this.budget = chatShellFeedBudget,
|
||||
this.slice = chatShellFeedSlice,
|
||||
});
|
||||
|
||||
/// Writes [text] into the emulator; [replay] marks a redraw of old output.
|
||||
final void Function(String text, bool replay) _write;
|
||||
final Duration budget;
|
||||
final int slice;
|
||||
|
||||
/// Called with a piece's end once it is drawn.
|
||||
void Function(int end)? onDrawn;
|
||||
|
||||
final Queue<_Piece> _pieces = Queue();
|
||||
Timer? _timer;
|
||||
bool _pumping = false;
|
||||
int _drawn = 0;
|
||||
int _end = 0;
|
||||
|
||||
/// The end of the output drawn so far.
|
||||
int get drawn => _drawn;
|
||||
|
||||
/// The end of the output drawn or waiting to be.
|
||||
int get end => _end;
|
||||
|
||||
/// Whether nothing waits to be written.
|
||||
bool get idle => _pieces.isEmpty;
|
||||
|
||||
/// Drops what waits: the emulator is redrawn and continues from [end].
|
||||
void restart(int end) {
|
||||
_pieces.clear();
|
||||
_timer?.cancel();
|
||||
_timer = null;
|
||||
_drawn = end;
|
||||
_end = end;
|
||||
}
|
||||
|
||||
/// Queues [text] ending at byte [end]; as much as the budget allows is
|
||||
/// written at once.
|
||||
void add(String text, int end, {bool replay = false}) {
|
||||
_pieces.add(_Piece(text, end, replay, null));
|
||||
_end = end;
|
||||
_schedule();
|
||||
}
|
||||
|
||||
/// Runs [action] once everything queued before it is written.
|
||||
void then(void Function() action) {
|
||||
if (_pieces.isEmpty && !_pumping) {
|
||||
action();
|
||||
return;
|
||||
}
|
||||
_pieces.add(_Piece('', _end, false, action));
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_pieces.clear();
|
||||
_timer?.cancel();
|
||||
_timer = null;
|
||||
}
|
||||
|
||||
void _schedule() {
|
||||
if (_timer == null && !_pumping) _pump();
|
||||
}
|
||||
|
||||
void _pump() {
|
||||
_timer = null;
|
||||
_pumping = true;
|
||||
final clock = Stopwatch()..start();
|
||||
try {
|
||||
while (_pieces.isNotEmpty) {
|
||||
final piece = _pieces.first;
|
||||
if (piece.at < piece.text.length) {
|
||||
var cut = piece.at + slice;
|
||||
if (cut >= piece.text.length) {
|
||||
cut = piece.text.length;
|
||||
} else if (_highSurrogate(piece.text.codeUnitAt(cut - 1))) {
|
||||
// Never split a surrogate pair between two writes.
|
||||
cut++;
|
||||
}
|
||||
_write(piece.text.substring(piece.at, cut), piece.replay);
|
||||
piece.at = cut;
|
||||
}
|
||||
if (piece.at >= piece.text.length) {
|
||||
_pieces.removeFirst();
|
||||
if (piece.action != null) {
|
||||
piece.action!();
|
||||
} else {
|
||||
_drawn = piece.end;
|
||||
onDrawn?.call(piece.end);
|
||||
}
|
||||
}
|
||||
if (_pieces.isNotEmpty && clock.elapsed >= budget) {
|
||||
_timer = Timer(Duration.zero, _pump);
|
||||
return;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
_pumping = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class _Piece {
|
||||
_Piece(this.text, this.end, this.replay, this.action);
|
||||
|
||||
final String text;
|
||||
final int end;
|
||||
final bool replay;
|
||||
final void Function()? action;
|
||||
int at = 0;
|
||||
}
|
||||
|
||||
bool _highSurrogate(int unit) => unit >= 0xD800 && unit <= 0xDBFF;
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'package:xterm/core.dart';
|
||||
|
||||
import 'chat_shell_run.dart';
|
||||
import 'chat_shell_terminal_feed.dart';
|
||||
|
||||
/// Lines of scrollback one card's terminal keeps.
|
||||
const int chatShellTerminalScrollback = 5000;
|
||||
@@ -17,6 +18,7 @@ class ChatShellTerminalSession {
|
||||
required this.onInput,
|
||||
required this.onResize,
|
||||
}) : terminal = _ChatShellTerminal() {
|
||||
_feed = ChatShellTerminalFeed(_write);
|
||||
// Drawn at the process's own size until a view lays it out at its own.
|
||||
if (run.cols > 0 && run.rows > 0) terminal.resize(run.cols, run.rows);
|
||||
terminal.onOutput = _output;
|
||||
@@ -32,12 +34,19 @@ class ChatShellTerminalSession {
|
||||
/// Receives the size the view laid the terminal out at.
|
||||
final void Function(int cols, int rows) onResize;
|
||||
|
||||
int _end = 0;
|
||||
late final ChatShellTerminalFeed _feed;
|
||||
bool _live = false;
|
||||
bool _replaying = false;
|
||||
|
||||
/// The absolute output byte this terminal has drawn up to.
|
||||
int get end => _end;
|
||||
/// The absolute output byte this terminal has drawn up to, or will once
|
||||
/// the output waiting in its feed is written.
|
||||
int get end => _feed.end;
|
||||
|
||||
/// The absolute output byte actually drawn so far.
|
||||
int get drawn => _feed.drawn;
|
||||
|
||||
/// Called with the new [drawn] end each time a chunk is drawn whole.
|
||||
set onDrawn(void Function(int end)? callback) => _feed.onDrawn = callback;
|
||||
|
||||
/// Whether typing reaches a running process.
|
||||
bool get live => _live;
|
||||
@@ -45,8 +54,11 @@ class ChatShellTerminalSession {
|
||||
/// Draws [run]'s held output from scratch: on open, after a gap and after
|
||||
/// a readback replaced the card's copy. Answers the emulator gives to old
|
||||
/// queries in that output are not sent: the process asked them long ago.
|
||||
/// A large output is drawn in slices across turns of the event loop
|
||||
/// (ChatShellTerminalFeed), so it never stalls the UI.
|
||||
void replay(ChatShellRun run) {
|
||||
_live = run.running && run.terminal;
|
||||
_feed.restart(run.outputStart);
|
||||
_replaying = true;
|
||||
try {
|
||||
terminal.useMainBuffer();
|
||||
@@ -57,36 +69,46 @@ class ChatShellTerminalSession {
|
||||
if (run.truncated) {
|
||||
terminal.write('\x1b[2m[earlier output was dropped]\x1b[0m\r\n');
|
||||
}
|
||||
var output = _drawable(run, run.output);
|
||||
// A finished run's final newline would leave an empty line below it.
|
||||
if (!run.running && output.endsWith('\r\n')) {
|
||||
output = output.substring(0, output.length - 2);
|
||||
}
|
||||
terminal.write(output);
|
||||
if (!run.running) terminal.setCursorVisibleMode(false);
|
||||
} finally {
|
||||
_replaying = false;
|
||||
}
|
||||
_end = run.outputEnd;
|
||||
var output = _drawable(run, run.output);
|
||||
// A finished run's final newline would leave an empty line below it.
|
||||
if (!run.running && output.endsWith('\r\n')) {
|
||||
output = output.substring(0, output.length - 2);
|
||||
}
|
||||
_feed.add(output, run.outputEnd, replay: true);
|
||||
if (!run.running) _feed.then(() => terminal.setCursorVisibleMode(false));
|
||||
}
|
||||
|
||||
/// Draws one live chunk covering bytes [offset, end) of [run], which
|
||||
/// already holds it. A chunk after a hole redraws the whole held copy.
|
||||
void append(ChatShellRun run, String text, int offset, int end) {
|
||||
if (end <= _end) return;
|
||||
if (offset != _end) {
|
||||
if (end <= _feed.end) return;
|
||||
if (offset != _feed.end) {
|
||||
replay(run);
|
||||
return;
|
||||
}
|
||||
terminal.write(_drawable(run, text));
|
||||
_end = end;
|
||||
_feed.add(_drawable(run, text), end);
|
||||
}
|
||||
|
||||
/// The run ended: nothing typed reaches it any more.
|
||||
void finish(ChatShellRun run) {
|
||||
if (!_live) return;
|
||||
_live = false;
|
||||
terminal.setCursorVisibleMode(false);
|
||||
_feed.then(() => terminal.setCursorVisibleMode(false));
|
||||
}
|
||||
|
||||
/// Stops drawing what still waits (the emulator is going away).
|
||||
void dispose() => _feed.dispose();
|
||||
|
||||
void _write(String text, bool replay) {
|
||||
_replaying = replay;
|
||||
try {
|
||||
terminal.write(text);
|
||||
} finally {
|
||||
_replaying = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// The rows the drawn output takes, through the cursor's line, when the
|
||||
|
||||
@@ -59,7 +59,7 @@ class ChatShellTerminals {
|
||||
_resizes.remove(runId)?.cancel();
|
||||
_inputs.remove(runId);
|
||||
_sentSizes.remove(runId);
|
||||
_sessions.remove(runId);
|
||||
_sessions.remove(runId)?.dispose();
|
||||
}
|
||||
|
||||
/// [run] ended: draw its final output read-only.
|
||||
@@ -98,6 +98,9 @@ class ChatShellTerminals {
|
||||
|
||||
void dispose() {
|
||||
_disposed = true;
|
||||
for (final session in _sessions.values) {
|
||||
session.dispose();
|
||||
}
|
||||
for (final timer in _resizes.values) {
|
||||
timer.cancel();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user