Merge branch 'worktree-agent-ab9de97501bc94a9c'

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 23:38:04 +00:00
28 changed files with 1223 additions and 87 deletions
@@ -14,11 +14,19 @@ const int chatShellTerminalScrollback = 5000;
///
/// It keeps no copy of typed input: [Terminal.onOutput] hands each key or
/// paste straight to [onInput], which sends it.
///
/// Several clients may draw one terminal. The emulator's automatic answers
/// to queries in the output (cursor position, device attributes) go to
/// [onReply] instead, so the server can let only the active client's
/// answers through; a view gaining focus calls [activate], which makes this
/// client the active one at its size ([onActivate]).
class ChatShellTerminalSession {
ChatShellTerminalSession(
ChatShellRun run, {
required this.onInput,
required this.onResize,
this.onReply,
this.onActivate,
bool Function()? optionAsMeta,
}) : optionAsMeta =
optionAsMeta ?? (() => TerminalLayout.instance.optionAsMeta),
@@ -30,7 +38,10 @@ class ChatShellTerminalSession {
// Drawn at the process's own size until a view lays it out at its own.
if (run.cols > 0 && run.rows > 0) terminal.resize(run.cols, run.rows);
terminal.onOutput = _output;
terminal.onResize = (cols, rows, _, _) => onResize(cols, rows);
terminal.onResize = (cols, rows, _, _) {
_viewSize = (cols, rows);
onResize(cols, rows);
};
replay(run);
}
@@ -39,15 +50,25 @@ class ChatShellTerminalSession {
/// Whether a Mac's Option key is Meta (Settings → Appearance → Terminal).
final bool Function() optionAsMeta;
/// Receives input for the process; only called while the run is live.
/// Receives what the operator types for the process; only called while
/// the run is live.
final void Function(String data) onInput;
/// Receives the size the view laid the terminal out at.
final void Function(int cols, int rows) onResize;
/// Receives the emulator's automatic answers to queries in the live
/// output; [onInput] receives them when this is null.
final void Function(String data)? onReply;
/// Receives this client's size when a view of it gains focus.
final void Function(int cols, int rows)? onActivate;
int _end = 0;
bool _live = false;
bool _replaying = false;
bool _drawing = false;
(int, int)? _viewSize;
/// The absolute output byte this terminal has drawn up to.
int get end => _end;
@@ -55,6 +76,16 @@ class ChatShellTerminalSession {
/// Whether typing reaches a running process.
bool get live => _live;
/// The size a view last laid the terminal out at; null before any did.
(int, int)? get viewSize => _viewSize;
/// A view of this terminal gained focus: this client becomes the one the
/// process is sized for and whose answers it receives.
void activate() {
final size = _viewSize;
if (_live && size != null) onActivate?.call(size.$1, size.$2);
}
/// Draws [run]'s held output from scratch: on open, after a gap and after
/// a readback replaced the card's copy. Answers the emulator gives to old
/// queries in that output are not sent: the process asked them long ago.
@@ -92,7 +123,13 @@ class ChatShellTerminalSession {
replay(run);
return;
}
terminal.write(_drawable(run, text));
// What the emulator sends while drawing output answers that output.
_drawing = true;
try {
terminal.write(_drawable(run, text));
} finally {
_drawing = false;
}
_end = end;
}
@@ -128,7 +165,13 @@ class ChatShellTerminalSession {
}
void _output(String data) {
if (_live && !_replaying) onInput(data);
if (!_live || _replaying) return;
final reply = onReply;
if (_drawing && reply != null) {
reply(data);
} else {
onInput(data);
}
}
/// Plain-pipe output has bare newlines, which a terminal draws as a line