diff --git a/app/lib/features/settings/settings_terminal_section.dart b/app/lib/features/settings/settings_terminal_section.dart index fb2a827b..799508b2 100644 --- a/app/lib/features/settings/settings_terminal_section.dart +++ b/app/lib/features/settings/settings_terminal_section.dart @@ -43,7 +43,7 @@ class SettingsTerminalSection extends StatelessWidget { ), icon: Icon( mode == TerminalMode.panel - ? Icons.vertical_split_outlined + ? Icons.call_to_action_outlined : Icons.picture_in_picture_alt_outlined, ), ), diff --git a/app/tool/perf/perf_fixture.dart b/app/tool/perf/perf_fixture.dart index 3ae75cdd..698f5e7c 100644 --- a/app/tool/perf/perf_fixture.dart +++ b/app/tool/perf/perf_fixture.dart @@ -27,6 +27,7 @@ import 'chat_new_session_fixture.dart'; import 'chat_turn_diff_fixture.dart'; import 'chat_question_fixture.dart'; import 'chat_shell_fixture.dart'; +import 'terminal_fixture.dart'; import 'chat_shown_fixture.dart'; import 'chat_scroll_fixture.dart'; @@ -42,6 +43,7 @@ void configurePerfFixture(Map params) { configureChatScrollFixture(params); configureChatQuestionFixture(params); configureChatShellFixture(params); + configureTerminalFixture(params); } int get _hits => int.tryParse(_params['hits'] ?? '') ?? 0; @@ -132,6 +134,8 @@ bool get _scrollVariety => ['msgs', 'rich', 'older'].any(_params.containsKey); /// Unsolicited envelopes that follow a reply: the `?quota=` usage frame. List perfChatFollowUps(JsonMap request) { + final terminal = terminalFixtureFollowUps(request); + if (terminal.isNotEmpty) return terminal; final answered = chatQuestionFollowUps(request); if (answered.isNotEmpty) return answered; final shell = chatShellFollowUps(request); @@ -233,6 +237,8 @@ String _answer(int i) => /// Chat replies for `?chats=N`, or null to fall through to the fixture. JsonMap? perfChatReply(JsonMap request) { + final terminal = terminalFixtureReply(request); + if (terminal != null) return terminal; if (_chats == 0) return null; final question = chatQuestionReply(request); if (question != null) return question; diff --git a/app/tool/perf/terminal_fixture.dart b/app/tool/perf/terminal_fixture.dart new file mode 100644 index 00000000..bc6c26bf --- /dev/null +++ b/app/tool/perf/terminal_fixture.dart @@ -0,0 +1,162 @@ +// Machine terminals for the full-app probe (`?terminals=1`): the machine +// lists two running tmux-backed shells (a coloured `go test` run and a +// home shell) and terminal_open/input/resize/output/close answer as the +// server does, with the output frames that follow. Typed text is echoed and +// Enter prints a fresh prompt. `?terminal=panel` or `?terminal=float` opens +// the terminal at launch in that mode. +import 'dart:convert'; + +import 'package:pipelines_app/core/api_gateway.dart'; +import 'package:pipelines_app/features/terminal/terminal_layout.dart'; + +bool _enabled = false; +final Map _terminals = {}; +final Map _output = {}; +int _opened = 0; + +const String _prompt = '\x1b[1;32moperator@mac\x1b[0m:\x1b[1;34m%s\x1b[0m\$ '; + +String _promptAt(String folder) => _prompt.replaceFirst('%s', folder); + +void configureTerminalFixture(Map params) { + _enabled = params['terminals'] == '1'; + if (!_enabled) return; + final now = DateTime.now().millisecondsSinceEpoch; + _terminals + ..clear() + ..['term_build'] = _record( + 'term_build', + '/Users/operator/src/pipelines', + now - 600000, + title: 'go test ./internal/server', + ) + ..['term_home'] = _record('term_home', '/Users/operator', now - 300000); + _output + ..clear() + ..['term_build'] = + '${_promptAt('~/src/pipelines')}go test ./internal/server -run Terminal\r\n' + '\x1b[32m=== RUN\x1b[0m TestTerminalOverWebsocket\r\n' + '\x1b[32m--- PASS\x1b[0m: TestTerminalOverWebsocket (0.19s)\r\n' + '\x1b[32m=== RUN\x1b[0m TestTerminalTmuxSurvivesRestart\r\n' + '\x1b[32m--- PASS\x1b[0m: TestTerminalTmuxSurvivesRestart (0.27s)\r\n' + '\x1b[33m--- SKIP\x1b[0m: TestTerminalWindows (0.00s)\r\n' + '\x1b[1;32mok\x1b[0m \tgit.a-wan.net/alan/pipelines/internal/server\t2.758s\r\n' + '${_promptAt('~/src/pipelines')}' + ..['term_home'] = + '${_promptAt('~')}ls\r\n' + '\x1b[1;34mDesktop\x1b[0m \x1b[1;34mDocuments\x1b[0m ' + '\x1b[1;34mDownloads\x1b[0m \x1b[1;34msrc\x1b[0m notes.md\r\n' + '${_promptAt('~')}'; + final mode = switch (params['terminal']) { + 'panel' => TerminalMode.panel, + 'float' => TerminalMode.float, + _ => null, + }; + if (mode != null) { + TerminalLayout.instance + ..mode = mode + ..visible = true; + } +} + +JsonMap _record(String id, String cwd, int created, {String title = ''}) => { + 'id': id, + 'cwd': cwd, + 'title': title, + 'shell': '/bin/zsh', + 'created_at': created, + 'tmux': 'pipelines-$id', + 'status': 'running', + 'cols': 100, + 'rows': 24, +}; + +JsonMap _withOutput(String id, int after) { + final bytes = utf8.encode(_output[id] ?? ''); + final start = after.clamp(0, bytes.length); + return { + ..._terminals[id]!, + 'stream': 'fixture', + 'output_end': bytes.length, + 'output': utf8.decode(bytes.sublist(start)), + 'output_start': start, + }; +} + +JsonMap _body(JsonMap request) => + Map.from(request['terminal_request'] as Map? ?? {}); + +JsonMap? terminalFixtureReply(JsonMap request) { + if (!_enabled) return null; + final id = request['id']; + final type = '${request['type']}'; + final body = _body(request); + final terminal = '${body['terminal_id'] ?? ''}'; + switch (type) { + case 'terminal_list': + return { + 'id': id, + 'type': type, + 'terminals': [ + for (final key in _terminals.keys) + {..._terminals[key]!, 'stream': 'fixture'}, + ], + }; + case 'terminal_open': + final next = 'term_new${++_opened}'; + final cwd = '${body['cwd'] ?? ''}'; + _terminals[next] = _record( + next, + cwd.isEmpty ? '/Users/operator' : cwd, + DateTime.now().millisecondsSinceEpoch, + ); + _output[next] = _promptAt(cwd.isEmpty ? '~' : cwd); + return {'id': id, 'type': type, 'terminal': _terminals[next]}; + case 'terminal_output': + if (!_terminals.containsKey(terminal)) break; + return { + 'id': id, + 'type': type, + 'terminal': _withOutput( + terminal, + (body['after'] as num?)?.toInt() ?? 0, + ), + }; + case 'terminal_input': + case 'terminal_resize': + return {'id': id, 'type': type}; + case 'terminal_close': + final record = _terminals.remove(terminal); + return { + 'id': id, + 'type': type, + 'terminal': {...?record, 'status': 'closed'}, + }; + } + return null; +} + +/// The output the shell writes after typed input: the echo, and a new +/// prompt on Enter. +List terminalFixtureFollowUps(JsonMap request) { + if (!_enabled || request['type'] != 'terminal_input') return const []; + final body = _body(request); + final terminal = '${body['terminal_id'] ?? ''}'; + if (!_terminals.containsKey(terminal)) return const []; + final data = '${body['data'] ?? ''}'; + final folder = terminal == 'term_build' ? '~/src/pipelines' : '~'; + final echo = data.replaceAll('\r', '\r\n${_promptAt(folder)}'); + final offset = utf8.encode(_output[terminal]!).length; + _output[terminal] = _output[terminal]! + echo; + return [ + { + 'type': 'terminal_event', + 'terminal_event': { + 'terminal_id': terminal, + 'output': echo, + 'offset': offset, + 'end': offset + utf8.encode(echo).length, + }, + }, + ]; +} diff --git a/app/tool/search_selection_probe_entry.dart b/app/tool/search_selection_probe_entry.dart index d33ea846..330e7100 100644 --- a/app/tool/search_selection_probe_entry.dart +++ b/app/tool/search_selection_probe_entry.dart @@ -782,6 +782,8 @@ class _FixtureSink implements WebSocketSink { /// `?perf=1` prints frame timings (tool/perf/frame_log.dart); /// `?hits=N`, `?chats=N` and `?graph=1` add bench data /// (tool/perf/perf_fixture.dart); +/// `?terminals=1` lists two machine terminals, and `?terminal=panel` or +/// `?terminal=float` opens them at launch (tool/perf/terminal_fixture.dart); /// `?theme=dark` / `?theme=light` pins the platform brightness the app's /// `ThemeMode.system` follows (omitted: the browser's color scheme). /// diff --git a/docs/cli.md b/docs/cli.md index e2834db9..e0a03a36 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -1291,3 +1291,28 @@ browses `_pipelines._tcp.local` on its own LAN, with a bounded DNS-SD query; it does not scan arbitrary ports. An unavailable browse returns a protocol error. The authenticated web front door exposes the same result at `GET /api/discovery`, including before the browser has opened its application websocket. + +### Machine terminals + +Interactive login shells on a server's machine, independent of chats (the +app's terminal panel). Every request carries `terminal_request` +`{terminal_id?, cwd?, cols?, rows?, data?, after?}`: + +- `terminal_open {cwd, cols, rows}` starts `$SHELL -l` (else `/bin/sh`) in + `cwd` (default home; it must be an existing folder inside home) and replies + with `terminal`. Where `tmux` is installed the shell runs in its own session + of a Pipelines-owned tmux server (`tmux -L pipelines`, never the operator's + own), so it outlives a server restart and the next server reattaches it. + At most 8 running terminals per machine. +- `terminal_list` replies with `terminals` (running and ended, oldest first). +- `terminal_input {terminal_id, data}` writes UTF-8 input (at most 16 KiB); + `terminal_resize {terminal_id, cols, rows}` resizes. Neither is logged. +- `terminal_output {terminal_id, after}` replies with the retained output (1 + MiB tail) from byte `after` of the stream named `stream`; a restarted server + starts a new stream at zero. Opening or reading subscribes the connection to + the terminal's output frames. +- `terminal_close {terminal_id}` kills the shell and its tmux session. +- Pushed `terminal_event {terminal_id, output, offset, end}` (output) or + `{terminal_id, terminal}` (opened, title, exited, closed). + +See `app/notes/terminal-panel.md`.