diff --git a/app/lib/features/settings/settings_terminal_section.dart b/app/lib/features/settings/settings_terminal_section.dart index 799508b2..bee5e91a 100644 --- a/app/lib/features/settings/settings_terminal_section.dart +++ b/app/lib/features/settings/settings_terminal_section.dart @@ -58,8 +58,10 @@ class SettingsTerminalSection extends StatelessWidget { children: [ const Text( 'Saved on this device. Ctrl+` shows or hides the terminal; on ' - 'a narrow window it opens full screen. Shells keep running ' - 'while it is hidden.', + 'a narrow window it opens full screen. A terminal starts at ' + 'the machine\'s login prompt: type that account\'s password ' + 'once, and it stays logged in while hidden. Terminals end ' + 'when the server restarts; run tmux inside one to keep work.', ), if (layout.saveFailed) Text( diff --git a/app/lib/features/terminal/terminal_body.dart b/app/lib/features/terminal/terminal_body.dart index 3e4aa1d0..063b64f9 100644 --- a/app/lib/features/terminal/terminal_body.dart +++ b/app/lib/features/terminal/terminal_body.dart @@ -40,10 +40,7 @@ class TerminalBody extends StatelessWidget { if (!tab.connected) '${tab.machineName} is not connected; the terminal resumes when it is.' else if (!tab.info.running) - tab.info.endedText - else if (!tab.info.persistent) - 'This shell ends if the server on ${tab.machineName} restarts ' - '(tmux is not installed there).', + tab.info.endedText, ?tab.error, ?hub.error, ]; diff --git a/app/lib/features/terminal/terminal_hub.dart b/app/lib/features/terminal/terminal_hub.dart index 40f85bc4..0c6485ed 100644 --- a/app/lib/features/terminal/terminal_hub.dart +++ b/app/lib/features/terminal/terminal_hub.dart @@ -29,6 +29,14 @@ class TerminalHub extends ChangeNotifier { /// The last refusal of an open or close, in the server's words. String? error; + /// Whether this app run has had its first terminal: the first time the + /// terminal is shown it starts one (`openTerminalIfNone`), later shows + /// only show it. Opening one by hand counts. + bool firstOpenDone = false; + + /// The first show's start, while it runs. + Future? firstOpening; + List get tabs => List.unmodifiable(_tabs); List get connected => machines.value; @@ -79,8 +87,10 @@ class TerminalHub extends ChangeNotifier { TerminalMachine? machineFor(String? id) => connected.where((machine) => machine.id == id).firstOrNull; - /// Opens a shell on [machineId] (the first connected machine when null) - /// in [cwd] (home when empty) and selects its tab. Returns the refusal. + /// Opens a terminal on [machineId] (the first connected machine when + /// null) and selects its tab: it shows that machine's login prompt, and + /// after the password the shell starts in [cwd] (home when empty). + /// Returns the refusal. Future open({String? machineId, String cwd = ''}) async { activate(); final machine = machineId == null @@ -96,6 +106,7 @@ class TerminalHub extends ChangeNotifier { final record = reply['terminal']; if (record is! Map) throw StateError('The server sent no terminal.'); final tab = _adopt(machine, TerminalInfo.fromJson(record)); + firstOpenDone = true; _selected = tab.key; error = null; _notify(); @@ -107,7 +118,7 @@ class TerminalHub extends ChangeNotifier { } } - /// Ends [tab]'s shell (and its tmux session) and removes the tab. A tab + /// Ends [tab]'s login or shell and removes the tab. A tab /// whose machine is not connected cannot be closed yet. Future close(TerminalTab tab) async { if (!tab.connected) { diff --git a/app/lib/features/terminal/terminal_info.dart b/app/lib/features/terminal/terminal_info.dart index ae264dbd..faae8f44 100644 --- a/app/lib/features/terminal/terminal_info.dart +++ b/app/lib/features/terminal/terminal_info.dart @@ -1,7 +1,8 @@ import '../../core/api_gateway.dart'; /// One machine terminal as the server records it (`terminal_list`, -/// `terminal_event`): a login shell on that machine, running or ended. +/// `terminal_event`): a login on that machine (its login prompt, then the +/// account's shell), running or ended. It ends with the server process. class TerminalInfo { const TerminalInfo({ required this.id, @@ -9,7 +10,6 @@ class TerminalInfo { required this.status, this.title = '', this.shell = '', - this.tmux = '', this.reason = '', this.exitCode, this.createdAt = 0, @@ -25,7 +25,6 @@ class TerminalInfo { status: '${json['status'] ?? ''}', title: '${json['title'] ?? ''}', shell: '${json['shell'] ?? ''}', - tmux: '${json['tmux'] ?? ''}', reason: '${json['reason'] ?? ''}', exitCode: (json['exit_code'] as num?)?.toInt(), createdAt: (json['created_at'] as num?)?.toInt() ?? 0, @@ -45,10 +44,6 @@ class TerminalInfo { final String title; final String shell; - /// The tmux session keeping the shell alive across server restarts; - /// empty when the shell ends with the server. - final String tmux; - /// Why an ended terminal ended, when it was not its own exit. final String reason; final int? exitCode; @@ -56,8 +51,8 @@ class TerminalInfo { final int cols; final int rows; - /// Names the server process's output stream: a restarted server starts - /// a new one, whose byte offsets begin again at zero. + /// Names the server process's output stream; another stream's byte + /// offsets begin again at zero. final String stream; final int outputEnd; @@ -68,7 +63,6 @@ class TerminalInfo { status: status, title: title, shell: shell, - tmux: tmux, reason: reason, exitCode: exitCode, createdAt: createdAt, @@ -80,7 +74,6 @@ class TerminalInfo { bool get running => status == 'running'; bool get closed => status == 'closed'; - bool get persistent => tmux.isNotEmpty; /// What an ended terminal shows instead of a prompt. String get endedText { diff --git a/app/lib/features/terminal/terminal_open_action.dart b/app/lib/features/terminal/terminal_open_action.dart index 4760ba27..f52f241b 100644 --- a/app/lib/features/terminal/terminal_open_action.dart +++ b/app/lib/features/terminal/terminal_open_action.dart @@ -7,12 +7,25 @@ import '../../ui/adaptive/segmented_bar_overflow.dart'; import 'terminal_hub.dart'; import 'terminal_scope.dart'; -/// Opening the terminal with none to show starts one, as New terminal -/// does, once every machine's list has arrived. -Future openTerminalIfNone(TerminalHub hub) async { +/// The first time the terminal is shown in an app run it starts one +/// terminal, as New terminal does (it shows the machine's login prompt), +/// unless one is already running once every machine's list has arrived. +/// Every later show only shows it: a hidden terminal stays logged in, and +/// a new one (each asking for the password) is opened by hand. +Future openTerminalIfNone(TerminalHub hub) { + if (hub.firstOpenDone) return Future.value(); + return hub.firstOpening ??= _openFirst( + hub, + ).whenComplete(() => hub.firstOpening = null); +} + +Future _openFirst(TerminalHub hub) async { hub.activate(); await hub.listed(); - if (hub.tabs.isNotEmpty || hub.connected.isEmpty) return; + // With no machine connected yet, a later show tries again. + if (hub.firstOpenDone || hub.connected.isEmpty) return; + hub.firstOpenDone = true; + if (hub.tabs.any((tab) => tab.info.running)) return; await hub.open(); } diff --git a/app/notes/terminal-panel.md b/app/notes/terminal-panel.md index 0deb4f82..44683a39 100644 --- a/app/notes/terminal-panel.md +++ b/app/notes/terminal-panel.md @@ -1,8 +1,11 @@ # Permanent terminal (bottom panel / floating window) -A full terminal available from every destination, independent of chats. It -keeps running when the panel is hidden, the app navigates or reloads, and -(where tmux is installed) across Pipelines server restarts. +A full terminal available from every destination, independent of chats. +Each terminal starts at the machine's login prompt (`su -l ` for the +server's own account), so the operator types that account's password before +getting a shell. It keeps running, logged in, when the panel is hidden or the +app navigates or reloads; it ends when the Pipelines server restarts (run +tmux inside it for work that must survive that). ## Behaviour @@ -10,8 +13,16 @@ keeps running when the panel is hidden, the app navigates or reloads, and menu): *Bottom panel* docks under the app (the app shrinks above it, never hidden under it); *Floating window* is a window over the app. Both are toggled by the bar's Terminal button (every destination's bar ends - with it) and the shortcuts below. Opening it with no terminal yet (once - every machine's list has arrived) starts one, as New terminal does. + with it) and the shortcuts below. +- **First open:** the first time the terminal is shown in an app run (once + every machine's list has arrived) it starts one terminal, as New terminal + does, unless one is already running; it shows the login prompt and the + operator types the password into the terminal (the PTY hides it). From + then on the button, shortcuts and the narrow page only hide and show it: + no toggle opens another terminal, even after every tab is closed (New + terminal does). Opening one by hand first counts as the first + (`TerminalHub.firstOpenDone`, `openTerminalIfNone`). Each new tab is its + own login and asks for the password again. - **Shortcuts** (`terminal_shortcuts.dart`, handled by the surface as a `FocusManager` early key handler, so they work wherever focus is, including inside the terminal and in the chat composer, and neither receives the key): @@ -67,7 +78,7 @@ keeps running when the panel is hidden, the app navigates or reloads, and why they ended with a Close tab button. With more than one machine connected (or tabs from several) each tab names its machine, and New terminal (+) asks which machine. ⨯ on a tab closes it, which kills the - shell (and its tmux session). + login or shell. - **Open in terminal:** the chat header's More menu (the chat's folder, on the chat's machine) and a folder's ⋮ menu in the Files panel open a new terminal there and show the surface. @@ -76,7 +87,7 @@ keeps running when the panel is hidden, the app navigates or reloads, and The layout (mode, shown, height, frame) is saved per device, so a reload reopens the terminal, lists the machine's terminals and resumes each from its retained output. A dropped connection resumes after the last - byte held; a restarted server (new stream) is read again from its start. + byte held. A restarted server lists the terminals as ended, with why. - **Keyboard:** a focused terminal gets every key (Ctrl-C, Esc, Tab, arrows, Ctrl-letters) except the shortcuts above (Ctrl+T, Alt+T, Cmd+T, Ctrl+`, and Ctrl+Tab while shown); paste is Cmd-V (Apple) or Ctrl-V/Ctrl-Shift-V. Clicking @@ -91,8 +102,9 @@ keeps running when the panel is hidden, the app navigates or reloads, and terminals"). Body `terminal_request {terminal_id?, cwd?, cols?, rows?, data?, after?}`. -- `terminal_open {cwd, cols, rows}` → `terminal` (subscribes the - connection to its output first). +- `terminal_open {cwd, cols, rows}` → `terminal` once su's password + prompt shows (subscribes the connection to its output first); refused + when the login cannot ask for a password (below). - `terminal_list` → `terminals` (running and ended, oldest first; live records carry their current title, size and `stream`). - `terminal_input {terminal_id, data}` (UTF-8, ≤ 16 KiB, 64-request queue), @@ -106,54 +118,73 @@ data?, after?}`. title, exited, closed) to every connection. A slow connection's queue (256 frames) drops output; the client sees the offset gap and re-reads. -## Persistence +## Login and lifetime (server) -- **tmux present** (looked up on PATH, then /opt/homebrew/bin, - /usr/local/bin, /usr/bin): each shell is `$SHELL -l` (else `/bin/sh`) as - the only pane of session `pipelines-` on a Pipelines-owned tmux - server, `tmux -L pipelines -f /dev/null` (never the operator's own server - or config; `TMUX`/`TMUX_PANE` are stripped). Options: status off, mouse - on (wheel scrolls tmux history), history-limit 10000, escape-time 10, - set-titles on with the pane title. The server shows it through an - ordinary `attach-session` client in a PTY (creack/pty, the chat shell's - `startChatShellTerminal`). On shutdown the client gets SIGHUP and the - session stays; on start `recover` lists the DB's running terminals and - reattaches each whose session is alive (a fresh output stream: tmux - redraws the screen). A client that exits while its session lives is - replaced (up to 5 times). -- **No tmux:** the shell is a direct child; it ends with the server and is - listed as ended with the reason ("tmux is not installed on this - machine"); running tabs say "This shell ends if the server on - restarts (tmux is not installed there)". -- DB table `terminals` (additive): id, title, cwd, shell, created_at, tmux, - status, ended_at, exit_code, reason, cols, rows. Ended records beyond 16 - are pruned. Output stays in memory (1 MiB tail per terminal). +- `internal/server/terminal_login.go`. A terminal runs, in its PTY, + `su -l -c '