0963a071d0
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>
68 lines
2.3 KiB
Dart
68 lines
2.3 KiB
Dart
import 'dart:convert';
|
|
|
|
/// Characters of output one tab holds before its oldest are dropped (about
|
|
/// the server's 1 MiB tail).
|
|
const int terminalHeldLimit = 1 << 20;
|
|
|
|
/// The text of [text] after its first [bytes] UTF-8 bytes (a cut inside a
|
|
/// character drops that character's remaining bytes).
|
|
String terminalSkipBytes(String text, int bytes) {
|
|
if (bytes <= 0) return text;
|
|
final encoded = utf8.encode(text);
|
|
if (bytes >= encoded.length) return '';
|
|
var cut = bytes;
|
|
while (cut < encoded.length && (encoded[cut] & 0xC0) == 0x80) {
|
|
cut++;
|
|
}
|
|
return utf8.decode(encoded.sublist(cut), allowMalformed: true);
|
|
}
|
|
|
|
/// One terminal's output as the client holds it: the text covering
|
|
/// absolute bytes [start, end) of the server's stream named [stream].
|
|
class TerminalStream {
|
|
String text = '';
|
|
int start = 0;
|
|
int end = 0;
|
|
String stream = '';
|
|
|
|
/// 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,
|
|
/// 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 = outputEnd;
|
|
stream = name;
|
|
_bound();
|
|
}
|
|
|
|
/// Adds the part of [chunk] (covering [offset, chunkEnd)) past [end] and
|
|
/// returns it; null when the chunk starts after [end] (a gap).
|
|
String? add(String chunk, int offset, int chunkEnd) {
|
|
if (chunkEnd <= end) return '';
|
|
if (offset > end) return null;
|
|
final fresh = terminalSkipBytes(chunk, end - offset);
|
|
text += fresh;
|
|
end = chunkEnd;
|
|
_bound();
|
|
return fresh;
|
|
}
|
|
|
|
void _bound() {
|
|
if (text.length <= terminalHeldLimit + terminalHeldLimit ~/ 2) return;
|
|
var cut = text.length - terminalHeldLimit;
|
|
// 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);
|
|
// 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;
|
|
}
|
|
}
|