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:
2026-11-25 23:57:18 +00:00
parent 5ab1ec9e00
commit 0963a071d0
30 changed files with 1958 additions and 182 deletions
@@ -0,0 +1,124 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/shell/chat_shell_run.dart';
import 'package:pipelines_app/features/chat/shell/chat_shell_terminal_feed.dart';
import 'package:pipelines_app/features/chat/shell/chat_shell_terminal_session.dart';
import 'package:xterm/core.dart';
Future<void> turn() => Future<void>.delayed(Duration.zero);
void main() {
test('output is written in slices, one turn at a time past the '
'budget, in order', () async {
final writes = <(String, bool)>[];
final drawn = <int>[];
final feed = ChatShellTerminalFeed(
(text, replay) => writes.add((text, replay)),
budget: Duration.zero,
slice: 1000,
)..onDrawn = drawn.add;
feed.add('a' * 2500, 2500, replay: true);
feed.add('b' * 10, 2510);
// Only the first slice is written at once.
expect(writes, [('a' * 1000, true)]);
expect(feed.drawn, 0);
expect(feed.end, 2510);
await turn();
await turn();
expect(writes.map((w) => w.$1.length), [1000, 1000, 500]);
expect(drawn, [2500]);
await turn();
expect(writes.last, ('b' * 10, false));
expect(drawn, [2500, 2510]);
expect(feed.idle, isTrue);
});
test(
'a restart drops what waits; then() runs after what is queued',
() async {
final writes = <String>[];
final feed = ChatShellTerminalFeed(
(text, _) => writes.add(text),
budget: Duration.zero,
slice: 4,
);
feed.add('abcdefgh', 8);
feed.restart(20);
await turn();
expect(writes, ['abcd']);
expect((feed.drawn, feed.end), (20, 20));
final order = <String>[];
feed.add('wxyz12', 26);
feed.then(() => order.add('then@${writes.last}'));
await turn();
await turn();
expect(order, ['then@12']);
},
);
test('a surrogate pair is never split between two writes', () {
final writes = <String>[];
final feed = ChatShellTerminalFeed((text, _) => writes.add(text), slice: 3);
feed.add('ab\u{1F600}cd', 8);
expect(writes.first, 'ab\u{1F600}');
expect(writes.join(), 'ab\u{1F600}cd');
});
test('a 1 MiB replay never blocks a turn for long (measured)', () async {
final big = List.filled(1 << 16, 'y\r\nline 0123\r\n').join();
final direct = Stopwatch()..start();
(Terminal(
maxLines: chatShellTerminalScrollback,
)..resize(80, 24)).write(big);
direct.stop();
final run = ChatShellRun.fromJson({
'id': 'run_1',
'command': 'yes',
'directory': '/w',
'status': 'running',
'started_at': 1,
'output': '',
'output_bytes': 0,
'terminal': true,
'cols': 80,
'rows': 24,
})!;
final session = ChatShellTerminalSession(
run,
onInput: (_) {},
onResize: (_, _) {},
);
final held = ChatShellRun.fromJson({
...{
'id': 'run_1',
'command': 'yes',
'directory': '/w',
'status': 'running',
'started_at': 1,
'terminal': true,
'cols': 80,
'rows': 24,
},
'output': big,
'output_bytes': big.length,
})!;
final clock = Stopwatch()..start();
session.replay(held);
var longest = clock.elapsed;
var turns = 1;
while (session.drawn < big.length) {
final before = clock.elapsed;
await turn();
final spent = clock.elapsed - before;
if (spent > longest) longest = spent;
turns++;
}
// ignore: avoid_print
print(
'replay of ${big.length >> 10} KiB: one write ${direct.elapsedMilliseconds} ms; '
'sliced ${clock.elapsedMilliseconds} ms over $turns turns, '
'longest turn ${longest.inMicroseconds / 1000} ms',
);
expect(longest, lessThan(const Duration(milliseconds: 60)));
expect(session.terminal.buffer.lines.length, greaterThan(1000));
});
}