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
+33 -6
View File
@@ -127,11 +127,28 @@ class PipelinesClient implements ApiGateway {
if (!_states.isClosed) _states.add(value);
}
/// Requests sent at once rather than after the previous request's reply:
/// a terminal's keystrokes and sizes. They are written to the socket in
/// call order, and the server handles them in arrival order on a lane of
/// their own, so they never wait behind a slow request (a terminal_open
/// waiting for its login prompt, a chat backend call) and stay in order
/// among themselves. An older server handles them in order with the rest.
static const pipelined = {
'terminal_input',
'terminal_resize',
'chat_shell_input',
'chat_shell_resize',
};
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
final completer = Completer<JsonMap>();
final generation = _generation;
final snapshot = Map<String, dynamic>.of(fields);
if (pipelined.contains(type)) {
_send(type, snapshot, completer, generation);
return completer.future;
}
final run = _queue.then(
(_) => _sendCall(type, snapshot, completer, generation),
);
@@ -145,6 +162,22 @@ class PipelinesClient implements ApiGateway {
Completer<JsonMap> result,
int generation,
) async {
_send(type, fields, result, generation);
// Match the existing client's serialized request semantics, even on errors.
try {
await result.future;
} catch (_) {
/* The caller receives the error. */
}
}
/// Writes one request now; [result] completes with its reply.
void _send(
String type,
JsonMap fields,
Completer<JsonMap> result,
int generation,
) {
if (generation != _generation) {
result.completeError(StateError('Connection replaced'));
return;
@@ -175,12 +208,6 @@ class PipelinesClient implements ApiGateway {
timer.cancel();
if (_pending.remove(id) != null) result.completeError(error);
}
// Match the existing client's serialized request semantics, even on errors.
try {
await result.future;
} catch (_) {
/* The caller receives the error. */
}
}
String sendOnly(String type, [JsonMap fields = const {}]) {