Merge main into the terminal flow-control branch

Conflicts resolved keeping both sides: main's active-client rules, input
lane, reply marking and key input, and this branch's flow control,
acknowledgements, subscriptions and sliced emulator feed. terminal_ack
joins the terminal lane; the feed marks each live slice it writes as
drawing, so the emulator's answers still go to onReply.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 00:03:20 +00:00
148 changed files with 18443 additions and 153 deletions
@@ -43,7 +43,9 @@ class ChatShellTerminals {
() => ChatShellTerminalSession(
run,
onInput: (data) => _input(run.id, data),
onReply: (data) => _input(run.id, data, reply: true),
onResize: (cols, rows) => _resized(run.id, cols, rows),
onActivate: (cols, rows) => _activated(run.id, cols, rows),
),
);
@@ -69,17 +71,35 @@ class ChatShellTerminals {
_sessions[run.id]?.replay(run);
}
void _input(String runId, String data) {
void _input(String runId, String data, {bool reply = false}) {
if (_disposed) return;
_inputs
.putIfAbsent(
runId,
() => ChatShellInputPipe(
(data) => _repository.input(_sessionId, runId, data),
(data, reply) => _repository.input(
_sessionId,
runId,
data,
reply: reply,
// Typing makes this client the active one at its size.
size: reply ? null : _sessions[runId]?.viewSize,
),
(error) => onError(runId, error),
),
)
.add(data);
.add(data, reply: reply);
}
/// A view of [runId] gained focus: it becomes the active client and the
/// process takes its size at once.
void _activated(String runId, int cols, int rows) {
if (_disposed || !(_sessions[runId]?.live ?? false)) return;
_resizes.remove(runId)?.cancel();
_sentSizes[runId] = (cols, rows);
_repository
.resize(_sessionId, runId, cols, rows, activate: true)
.catchError((Object error) => onError(runId, error));
}
void _resized(String runId, int cols, int rows) {
@@ -113,16 +133,25 @@ class ChatShellTerminals {
/// machine terminal's), in order: what arrives
/// while a request is in flight goes in the next one. Nothing is kept once
/// sent, and a refusal discards what was waiting rather than retyping it.
///
/// The emulator's automatic answers ([add] with reply) travel in requests
/// of their own, marked so, never merged with typing on either side.
class ChatShellInputPipe {
ChatShellInputPipe(this._send, this._failed);
final Future<void> Function(String data) _send;
final Future<void> Function(String data, bool reply) _send;
final void Function(Object error) _failed;
final StringBuffer _pending = StringBuffer();
/// Waiting text in order, each run of typing or of answers in one buffer.
final List<(StringBuffer, bool)> _pending = [];
bool _busy = false;
void add(String data) {
_pending.write(data);
void add(String data, {bool reply = false}) {
if (data.isEmpty) return;
if (_pending.isEmpty || _pending.last.$2 != reply) {
_pending.add((StringBuffer(), reply));
}
_pending.last.$1.write(data);
if (!_busy) unawaited(_pump());
}
@@ -130,19 +159,22 @@ class ChatShellInputPipe {
_busy = true;
try {
while (_pending.isNotEmpty) {
var waiting = _pending.toString();
_pending.clear();
final (buffer, reply) = _pending.first;
var waiting = buffer.toString();
buffer.clear();
var cut = waiting.length;
if (cut > chatShellInputChunk) {
cut = chatShellInputChunk;
// Never split a surrogate pair across two requests.
final unit = waiting.codeUnitAt(cut - 1);
if (unit >= 0xD800 && unit <= 0xDBFF) cut--;
_pending.write(waiting.substring(cut));
buffer.write(waiting.substring(cut));
} else {
_pending.removeAt(0);
}
final chunk = waiting.substring(0, cut);
waiting = '';
await _send(chunk);
await _send(chunk, reply);
}
} catch (error) {
_pending.clear();