Terminals: one active client per terminal; keystrokes skip the request queue

Several clients drawing one terminal (machine terminal or chat terminal
command) no longer interfere. The server tracks, per terminal, the
connections drawing it, most recently active first: the active client
(the opener, then whoever last typed or focused it) owns the PTY size and
is the only one whose emulator's automatic query answers reach the
process; other clients' answers are acknowledged and dropped, and their
sizes recorded but not applied. When the active connection closes the next
most recent client's size is applied.

Protocol (backwards compatible; all new fields optional):
- terminal_input / chat_shell_input: reply (emulator answer), cols/rows
  (the typist's size, applied before the input).
- terminal_resize / chat_shell_resize: activate (the client focused it).

Latency: PipelinesClient sends terminal_input, terminal_resize,
chat_shell_input and chat_shell_resize at once rather than behind the
previous request's reply. The server reads those onto a per-connection
terminal lane handled in order beside the ordinary request loop, and
terminal_open (up to 5 s waiting for su) now runs off the read loop.

Client: ChatShellTerminalSession routes what the emulator emits while
drawing output to onReply and reports focus through activate();
ChatShellInputPipe keeps answers and typing in separate requests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 23:37:16 +00:00
parent 5ab1ec9e00
commit c3458d8a7f
28 changed files with 1229 additions and 87 deletions
@@ -72,6 +72,76 @@ void main() {
await client.dispose();
});
test('terminal keystrokes do not wait behind a slow request', () async {
final channel = FakeChannel();
final client = PipelinesClient(channelFactory: (_) => channel);
final connecting = client.connect(Uri.parse('ws://example.test'));
channel.readyCompleter.complete();
await connecting;
Map<String, dynamic> sent(int i) =>
jsonDecode(channel.outgoing[i] as String) as Map<String, dynamic>;
// A terminal_open waiting for its login prompt.
final open = client.call('terminal_open');
final later = client.call('terminal_list');
await Future<void>.delayed(Duration.zero);
final keys = [
for (final data in ['l', 's', '\r'])
client.call('terminal_input', {
'terminal_request': {'terminal_id': 't', 'data': data},
}),
];
final size = client.call('chat_shell_resize', {
'chat_shell_run_request': {'cols': 80, 'rows': 24},
});
await Future<void>.delayed(Duration.zero);
// The keystrokes and the size went out at once, in order; the ordinary
// request behind the slow one still waits for it.
expect(channel.outgoing, hasLength(5));
expect(
[for (var i = 0; i < 5; i++) sent(i)['type']],
[
'terminal_open',
'terminal_input',
'terminal_input',
'terminal_input',
'chat_shell_resize',
],
);
expect(
[for (var i = 1; i < 4; i++) sent(i)['terminal_request']['data']],
['l', 's', '\r'],
);
for (var i = 1; i < 5; i++) {
channel.incoming.add(
jsonEncode({'id': sent(i)['id'], 'type': sent(i)['type']}),
);
}
for (final key in keys) {
expect((await key)['type'], 'terminal_input');
}
expect((await size)['type'], 'chat_shell_resize');
channel.incoming.add(
jsonEncode({'id': sent(0)['id'], 'type': 'terminal_open'}),
);
expect((await open)['type'], 'terminal_open');
await Future<void>.delayed(Duration.zero);
expect(sent(5)['type'], 'terminal_list');
channel.incoming.add(
jsonEncode({'id': sent(5)['id'], 'type': 'terminal_list'}),
);
expect((await later)['type'], 'terminal_list');
await client.dispose();
});
test('a keystroke while disconnected fails at once', () async {
final client = PipelinesClient(channelFactory: (_) => FakeChannel());
await expectLater(
client.call('terminal_input', {'terminal_request': {}}),
throwsA(isA<NotConnectedError>()),
);
await client.dispose();
});
test('disconnect fails pending requests', () async {
final channel = FakeChannel();
final client = PipelinesClient(channelFactory: (_) => channel);