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:
@@ -67,6 +67,35 @@ void main() {
|
||||
expect(sent, hasLength(2));
|
||||
});
|
||||
|
||||
test('query answers go to onReply; focus reports the laid out size', () {
|
||||
final typed = <String>[];
|
||||
final replies = <String>[];
|
||||
final activations = <(int, int)>[];
|
||||
final session = ChatShellTerminalSession(
|
||||
_run('prompt'),
|
||||
onInput: typed.add,
|
||||
onReply: replies.add,
|
||||
onResize: (_, _) {},
|
||||
onActivate: (cols, rows) => activations.add((cols, rows)),
|
||||
);
|
||||
session.append(_run('prompt\x1b[6n'), '\x1b[6n', 6, 10);
|
||||
session.terminal.textInput('y');
|
||||
expect(replies, ['\x1b[1;7R']);
|
||||
expect(typed, ['y']);
|
||||
// Drawn at the run's own size until a view lays it out.
|
||||
expect(session.viewSize, isNull);
|
||||
session.activate();
|
||||
expect(activations, isEmpty);
|
||||
session.terminal.resize(90, 20);
|
||||
expect(session.viewSize, (90, 20));
|
||||
session.activate();
|
||||
expect(activations, [(90, 20)]);
|
||||
// A finished run claims nothing.
|
||||
session.finish(_run('prompt', status: 'exited'));
|
||||
session.activate();
|
||||
expect(activations, hasLength(1));
|
||||
});
|
||||
|
||||
test('a chunk after a hole redraws the held copy', () {
|
||||
final session = ChatShellTerminalSession(
|
||||
_run('a\r\n'),
|
||||
|
||||
@@ -274,6 +274,47 @@ void main() {
|
||||
expect(gateway.bodies('chat_shell_input'), isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('focusing the terminal claims it at its size; typing carries '
|
||||
'the size and query answers are marked', (tester) async {
|
||||
await openRunning(tester, output: 'size? \x1b[6n');
|
||||
await tester.pump(const Duration(milliseconds: 200));
|
||||
final laidOut = gateway.bodies('chat_shell_resize').single;
|
||||
// The replayed query is not answered; nothing was claimed yet.
|
||||
expect(gateway.bodies('chat_shell_input'), isEmpty);
|
||||
await tester.tap(find.byType(TerminalView));
|
||||
await tester.pump();
|
||||
final claim = gateway.bodies('chat_shell_resize').last;
|
||||
expect(claim['activate'], isTrue);
|
||||
expect((claim['cols'], claim['rows']), (laidOut['cols'], laidOut['rows']));
|
||||
// Typing carries this client's size.
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyG);
|
||||
await tester.pump();
|
||||
var inputs = gateway.bodies('chat_shell_input');
|
||||
expect(inputs.single['data'], 'g');
|
||||
expect(inputs.single['reply'], isNull);
|
||||
expect(
|
||||
(inputs.single['cols'], inputs.single['rows']),
|
||||
(laidOut['cols'], laidOut['rows']),
|
||||
);
|
||||
// A live query is answered with the answer marked, without a size.
|
||||
gateway.emitOutput('run_t', '\x1b[6n', 'size? \x1b[6n'.length);
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
inputs = gateway.bodies('chat_shell_input');
|
||||
expect(inputs.last['data'], '\x1b[1;7R');
|
||||
expect(inputs.last['reply'], isTrue);
|
||||
expect(inputs.last['cols'], isNull);
|
||||
// Focusing again after the focus left claims again.
|
||||
FocusManager.instance.primaryFocus?.unfocus();
|
||||
await tester.pump();
|
||||
await tester.tap(find.byType(TerminalView));
|
||||
await tester.pump();
|
||||
expect(
|
||||
gateway.bodies('chat_shell_resize').where((b) => b['activate'] == true),
|
||||
hasLength(2),
|
||||
);
|
||||
}, variant: TargetPlatformVariant.only(TargetPlatform.linux));
|
||||
|
||||
testWidgets('coloured output is drawn without its escape codes', (
|
||||
tester,
|
||||
) async {
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/chat/shell/chat_shell_terminals.dart';
|
||||
import 'package:pipelines_app/features/terminal/terminal_info.dart';
|
||||
import 'package:pipelines_app/features/terminal/terminal_tab.dart';
|
||||
|
||||
import 'terminal_fixtures.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('answers are marked, typing carries the size, focus activates', (
|
||||
tester,
|
||||
) async {
|
||||
final gateway = TerminalGateway();
|
||||
final tab = TerminalTab(
|
||||
machineId: 'm',
|
||||
machineName: 'm',
|
||||
gateway: gateway,
|
||||
info: TerminalInfo.fromJson(gateway.add('t1')),
|
||||
);
|
||||
// Before a view lays it out, focus has no size to claim.
|
||||
tab.session.activate();
|
||||
await tester.pump();
|
||||
expect(gateway.bodies('terminal_resize'), isEmpty);
|
||||
|
||||
tab.session.terminal.resize(100, 30);
|
||||
await tester.pump(chatShellResizeDelay);
|
||||
expect(gateway.bodies('terminal_resize'), [
|
||||
{'terminal_id': 't1', 'cols': 100, 'rows': 30},
|
||||
]);
|
||||
|
||||
// A cursor position query in the output: the answer is marked.
|
||||
tab.frame({'output': '\x1b[6n', 'offset': 0, 'end': 4});
|
||||
await tester.pump();
|
||||
// Typing carries the size this client draws at.
|
||||
tab.session.terminal.textInput('ls');
|
||||
await tester.pump();
|
||||
expect(gateway.bodies('terminal_input'), [
|
||||
{'terminal_id': 't1', 'data': '\x1b[1;1R', 'reply': true},
|
||||
{'terminal_id': 't1', 'data': 'ls', 'cols': 100, 'rows': 30},
|
||||
]);
|
||||
|
||||
// A view gaining focus claims the terminal at once, at its size.
|
||||
tab.session.activate();
|
||||
await tester.pump();
|
||||
expect(gateway.bodies('terminal_resize').last, {
|
||||
'terminal_id': 't1',
|
||||
'cols': 100,
|
||||
'rows': 30,
|
||||
'activate': true,
|
||||
});
|
||||
|
||||
// A new connection reports its size again.
|
||||
tab.attach(gateway, TerminalInfo.fromJson(gateway.terminals['t1']!));
|
||||
await tester.pump(chatShellResizeDelay);
|
||||
expect(gateway.bodies('terminal_resize'), hasLength(3));
|
||||
expect(gateway.bodies('terminal_resize').last, {
|
||||
'terminal_id': 't1',
|
||||
'cols': 100,
|
||||
'rows': 30,
|
||||
});
|
||||
tab.dispose();
|
||||
});
|
||||
|
||||
test('the input pipe keeps answers and typing apart, in order', () async {
|
||||
final sent = <(String, bool)>[];
|
||||
final pipe = ChatShellInputPipe((data, reply) async {
|
||||
sent.add((data, reply));
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
}, (_) {});
|
||||
pipe.add('a');
|
||||
// While the first is in flight, typing, an answer and typing wait.
|
||||
pipe.add('b');
|
||||
pipe.add('c');
|
||||
pipe.add('\x1b[1;1R', reply: true);
|
||||
pipe.add('d');
|
||||
pipe.add('${'x' * chatShellInputChunk}y');
|
||||
await pumpEventQueue();
|
||||
expect(sent, [
|
||||
('a', false),
|
||||
('bc', false),
|
||||
('\x1b[1;1R', true),
|
||||
('d${'x' * (chatShellInputChunk - 1)}', false),
|
||||
('xy', false),
|
||||
]);
|
||||
});
|
||||
|
||||
test('a refusal drops what was waiting and reports once', () async {
|
||||
final failures = <Object>[];
|
||||
final sent = <String>[];
|
||||
final pipe = ChatShellInputPipe((data, reply) async {
|
||||
sent.add(data);
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
throw StateError('not running');
|
||||
}, failures.add);
|
||||
pipe.add('a');
|
||||
pipe.add('b');
|
||||
pipe.add('?', reply: true);
|
||||
await pumpEventQueue();
|
||||
expect(sent, ['a']);
|
||||
expect(failures, hasLength(1));
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user