korterm/docs/PLUGINS.md
2026-11-14 01:16:38 +03:00

22 KiB

Plugins

Four features were planned as separate modules. Quick commands, the SSH manager and session recording are implemented; the daemon is not. This file records what each one is for, where it attaches to the code, and what still needs a decision. It is a design record, not a commitment to a schedule.

The daemon changes how sessions are owned, so it comes last.

Decision: how a plugin is loaded

Plugins are built-in modules: the logic lives in a korterm-* crate without GTK code, the window code in korterm-ui, and a setting turns the feature on or off. Simple, type-checked, no ABI to maintain. Dynamic loading (shared libraries or scripts) would need a stable interface, versioning and a way to report failures, and is not planned.

The SSH manager is the first plugin and sets the pattern: korterm-ssh (reading and patching the SSH config, group metadata, command line) plus hosts_panel.rs and hosts_dialogs.rs in korterm-ui. A plugin is registered in plugins.rs, which is where Preferences → Plugins gets its list of switches: a title, a description, and the setting that holds its state. Adding a plugin means adding an entry there and a setting for it.

1. Quick commands with a button bar

Status. Implemented.

Purpose. Named commands, shown as buttons in a thin bar under the terminal (not under the SSH panel), as in XShell. A click types the command into the focused pane.

Where the data lives. ~/.config/korterm/quick-commands.yaml, one list for every tab and profile, in the order of the bar:

version: 2
items:
  - type: command
    name: Disk usage
    text: "df -h\n"          # ends with a line break: a click runs it
  - type: group
    name: Maintenance
    commands:
      - name: Deploy
        text: |
          cd /srv/app
          git pull
        confirm: true        # ask before running
  - type: command
    name: cd logs
    text: "cd /var/log/"     # no line break: a click only types it

name is required (at most 64 characters, no control characters); text is not empty, at most 64 KiB, and of the control characters holds only line breaks and tabs. Unknown keys are refused. A missing file is an empty list; one that cannot be read is reported on the bar and never rewritten. Writes are atomic and private (korterm_core::files::write_private, shared with hosts.yaml). The legacy commands/groups format is migrated in memory and is only replaced after a real edit; its first replacement is backed up as quick-commands.yaml.v1.bak. A change that leaves the list as it is writes nothing. The file is watched: a change made in one window, or by hand, shows at once in every window. config.yaml holds the plugin switch (quick-commands, off by default), the initial bar state (quick-commands-bar-visible, off by default), and its two keys (toggle-quick-bar and quick-commands-list in shortcuts:).

Behaviour.

  • The text is sent as it was written. A final line break is Enter: a text that ends with one runs, one that does not is only typed and Enter is left to the user.
  • It is sent as a paste: in bracketed-paste mode (bash, zsh, fish turn it on) the whole text, however many lines, arrives as one block and nothing in it runs before the Enter that follows it. A program without bracketed paste gets it typed line by line (\n as \r), so there every line but an unfinished last one runs. The Advanced Paste dialog is not used: it is the user's own text.
  • The first button lists every command with a search (name or text); Enter or a click sends one. It is also how commands that do not fit in the width are reached (the row scrolls with the wheel). quick-commands-list (F3 by default) opens the same list and search on its own, whether or not the bar is shown: the popover is parented to the terminal area, not to the button, so the key works even while the bar is hidden.
  • Right-click a command: Run (always with Enter), Type without Enter, Edit…, Duplicate, Move to group…, Add command…, Delete… (asked). Right-click the bar: Add command…, New group….
  • Groups are top-level items mixed freely with command items. Each group owns its ordered command list and gets one button whose popover contains those commands. Deleting a group expands its commands in place as top-level commands without changing their order. Right-click a group: Rename… or Delete….
  • The editor has a name (left empty, the first line of the command is used), the text in the terminal's font with a note under it saying what a click will do, and "Ask before running", which asks only when Enter is about to be sent. Ctrl+Enter saves.
  • An edit made from a stale view (the file changed in between) finds the command where it went, or refuses, so it never lands on another command.
  • The header button, "Quick commands bar" in the main menu and toggle-quick-bar show or hide the bar; the shortcut has no key until one is given (the F-keys alone are taken from programs such as mc). Hidden that way, it stays hidden until it is shown again. New windows use quick-commands-bar-visible (off by default). quick-commands-list does not depend on this: it works whether the bar is shown, hidden or has never been opened.
  • Settings of the plugin (the gear): the flattened command list (add, edit, duplicate, delete), initial bar visibility and both keys.
  • quick-commands: false hides the bar, turns both keys and the menu item off.

Integration.

  • korterm-quick: the list, its checks, the file, split_run and how a command is sent (Send).
  • korterm-ui: quick_bar.rs (the bar, the list and its own popup, the menus, the file watch), quick_dialogs.rs (editor, delete confirmation), input::quick_bytes (the bytes), Ui::send_quick (focused pane, bracketed-paste mode, confirmation through Ui::ask).

Later, if wanted.

  • Sending to every pane or tab at once.
  • Escape sequences (Ctrl+C, a pause) in the text.

2. SSH connection manager

Status. Implemented.

Purpose. A list of hosts; choosing one opens a tab that runs ssh to it.

Where the data lives. ~/.ssh/config is the source of truth. The list is its Host blocks (without wildcard patterns); Match blocks and Include are not read. What an SSH config cannot hold lives in ~/.config/korterm/hosts.yaml: groups (name, colour, collapsed, in display order), hosts (name to group) and panel-width (the panel's width in pixels, 200 to 900, as the user last dragged the divider; 300 until then). An entry there for a host that is not in the SSH config is ignored. A first-version hosts.yaml (a full host list) is migrated when the panel is first opened: the application lists the hosts the SSH config lacks and adds them as Host blocks only after you agree (until then the file is left as it is, and changing groups is refused so that nothing is lost). Its groups are kept, and the old file stays as hosts.yaml.bak. Hosts whose names cannot stand on a Host line are reported and not added. A hosts.yaml that cannot be read does not hide the hosts of the SSH config; its error is shown above them.

Behaviour.

  • A side panel, opened from the header button, lists hosts as cards (name, user@host:port, ↪ jump) in colour-coded groups, with a search field. A double click (or Enter) opens a new tab that runs ssh and is named after the host. Typing [user@]host[:port] and pressing Enter connects to an address that is not in the list.
  • Right-click menus: for a host Connect, Edit, Duplicate, Move to group, Copy address, Copy ssh command, Delete (confirmed); for a group New host here, Edit group (name, colour), Move up/down and Delete group (its hosts stay, without a group). Cards can be dragged onto a group (joins it, position unspecified), or directly onto another card (joins that card's group, if different, and takes the position right next to it — ssh_config::reorder relocates just that host's own Host block).
  • A failed connection's tab stays open to show ssh's error text instead of closing with it (holds_open_on_exit in lib.rs): only the tab's first pane, with a non-zero exit, from a tab opened by the panel (ssh_pinned). Any key, or the tab's own close, dismisses it; the title gains "— connection ended (exit N)" while it is held open.
  • Group colours are picked by the user, or automatic: stable by name, and different from the other groups' colours while the palette lasts.
  • Both files are watched; a change made elsewhere shows at once, in every window.
  • Hosts that share a Host a b line are listed but not editable: changing one would change all.
  • Connecting. A host of the SSH config is connected to as ssh -- <name>, as one would type ssh server: ssh applies the whole block (SetEnv, RemoteCommand, ForwardAgent, ProxyJump, key, port), and the session behaves like any other, including the title the remote shell sends. An address typed in the search field has no block and gets explicit options.
  • Before it connects by name, the panel asks ssh what the name resolves to (ssh -G -- <name>): the account and host ssh will really use after every Host, Match and Include, and whether the route goes through a jump host or a proxy command. The tab's user@host and the password prompt that may be answered are those, not what one entry of the config says.
  • Names of hosts that ssh would read as something else (-x, user@host, ssh://…, with /, : or =) are not listed (the import says so).
  • SFTP. "Open SFTP in file manager" opens sftp://<name>/ (for a host that is not in the config, sftp://[user@]host[:port]/) with the command in ssh-file-manager (a program and its options, split as a shell would split them but run without one, ~/ meaning the home directory; the address is added last), or with the system's default for sftp:// links, and where none is registered (Cinnamon registers Nemo only for directories) with the default handler of inode/directory. gvfs and KIO run ssh themselves, so the key, port and jump hosts of the block apply. When nothing can open it, a message points to the plugin's settings.
  • Settings of the plugin (the gear in Preferences → Plugins): the file manager, and the key that shows or hides the host list (F2 by default, like About on F1: both are ordinary entries of shortcuts:; an empty value turns one off). The gear is the place for any plugin's own settings: a plugin registers a function that builds them (plugins::Plugin::configure).
  • A tab opened from the panel is titled user@host (then what the remote shell calls itself) and has a 3 px stripe along its bottom edge in the colour its group had at connection time (#88c0d0 for a host without a group), so ssh tabs stand out among ordinary ones.
  • Tab rows are not rebuilt when only their position or width changes (resizing the window or the panel); they are moved in place.
  • ssh-manager: false (Preferences → Plugins) hides the button and the panel.

Passwords. A host may have a password, set in its dialog. It is never in ~/.ssh/config or hosts.yaml; hosts.yaml only records where it is (password: keyring or password: file), so a connection to a host without one never touches the keyring (which may ask to be unlocked).

  • Saving is all or nothing where it can be: a password that follows a renamed host moves before the host is renamed, and goes back if the host cannot be saved; deleting a host forgets its password first and keeps the host if that fails. A keyring that exists but refuses (unlocking dismissed, timeout) is reported as that, and plain text is not offered for it.
  • Where: the freedesktop Secret Service over the session bus (keyring.rs, written on gio: an item per host with the attributes application=korterm, kind=ssh-password, host=<name>; a locked collection is unlocked through the service's own prompt). If there is no such service or no default collection, the dialog asks before saving the password as plain text in ~/.config/korterm/secrets.yaml (korterm-ssh::secrets: 0600 in a 0700 directory, atomic writes, wiped from memory when dropped, never printed). Renaming a host moves its password, deleting a host or pressing Forget removes it from both places.
  • Typing it: the tab keeps the password (passwords::PendingPassword) for 30 seconds. It is typed once, with Enter, and dropped, when all of these hold: ssh is in the foreground of the pane the tab was opened with; the terminal is set the way a password prompt sets it (echo off, a line at a time: Session::echo_off_line_input), which an interactive session is not, so lines that merely look like a prompt (a banner, a remote sudo) are not answered; and the line at the cursor is a password prompt for this account and host (user@host's password:, (user@host) Password:, Password for user:). A prompt that names another host, as a jump host's does, or another account is left alone; one that names nobody (Password:) is answered only when the connection does not go through a jump host. Passphrases, new or retyped passwords, sudo and the host key question are never answered. It never goes into an argument or the environment of ssh, or the clipboard.
  • Limits: one password per host; key passphrases are not filled in; a password typed after a wrong one is not retried; the D-Bus messages and the service's memory are out of this program's reach (the buffers it holds itself are wiped).

Writing the SSH config. Only the block of the changed host is touched, and only the keys HostName, User, Port, IdentityFile and ProxyJump. Comments, other keys, other blocks, the line ending and the indentation stay. A value the form could not show (a relative or %d key path, ProxyJump none) is never removed by an edit that did not touch it. A new host is written before the first wildcard block (Host *), because ssh takes the first value it finds. Renaming a host follows the ProxyJump lines of other hosts that go through it; deleting one warns which hosts depend on it. Include lines end a block and are never changed. If the second file (hosts.yaml) cannot be written, the first is put back. The file is replaced atomically (temporary file, fsync, rename), keeps its permissions and, if it is a symlink, stays one. The first write in a run leaves a copy as config.korterm.bak next to it. Every edit reads the file again first.

Integration.

  • Not profiles: Ui::new_tab_with starts a tab from any command, so a host does not need a profile. The tab name is set like a rename, so a remote shell's title does not replace it.
  • The address is always passed after --, and the command is run without a shell. Addresses, users and jump hops are limited to a set of safe characters.

Security.

  • Authentication uses ssh, ssh-agent and key files, with optional saved passwords as described above.
  • The system keyring is the primary password store.
  • Plaintext storage requires explicit consent when Secret Service or its default collection is unavailable.
  • File permissions do not protect plaintext passwords from programs running as your account.
  • Backups and synchronization can copy secrets.yaml; exclude it or protect those copies.
  • The host list contains password-store references, not password values.

Open questions.

  • Is ssh-agent forwarding a per-host option?
  • Follow Include files?

3. Daemon that keeps sessions when the window closes

Purpose. Closing a window does not end its shells. Reopening KorTerm attaches to the running sessions.

Why it is hard. Today each Session owns its PTY and its event loop inside the GUI process (Session::spawn in korterm-core). Closing the process closes the PTY. To keep sessions alive, something else must own the PTY and keep the terminal state (the VT grid and scrollback) while no window is attached.

Options.

  • A. Use tmux or screen. Each session runs inside tmux. KorTerm attaches to it. Little code, but it depends on an external program and its behaviour (keys, colours, scrollback) differs from KorTerm's own.
  • B. Own daemon (korterm-daemon). The daemon owns the PTYs and the VT state and serves attached clients over a Unix socket in $XDG_RUNTIME_DIR. A client attaches, receives the current screen, then the live output stream, and sends input. Full control, much more code (protocol, reconnect, resize, versioning, cleanup).

Recommendation. Decide between A and B before any design work. If B, start with a protocol document and a daemon that can list and attach to a session without a GUI.

Integration points.

  • Session gains a transport (in-process or socket). Output is already a stream of bytes read by the event loop; the daemon would forward that stream.
  • Single-instance handling (GApplication, --toggle) must learn about the daemon.
  • The close-confirmation (confirm-close) reports sessions that stay alive in the daemon, instead of asking whether they will be lost.

4. Session recording

Purpose. Record what a session shows, with timing, for later replay and search.

Status. Implemented. KorTerm writes and reads recordings itself, in korecord's own on-disk format — the same .rec archive, the same SQLite schema and encryption format. KorTerm uses its own recording directory, index and configuration, so installing or configuring korec is not required.

Decisions taken.

  • The format is kept, the subprocess is not. The first version started korec ingest per recorded pane and piped asciicast v2 into its standard input. korterm-record/src/format/ now implements the same archive layout (archive.rs), the same zstd+AES-GCM packing (compress.rs, crypto.rs), and the same SQLite schema (index.rs) — checked against the real korecord Python package with tests that shell out to it. format::session::repair is the native equivalent of korec repair: finishing off a session a past crash left pending, run once per process (record::repair_once in korterm-ui), only against KorTerm's independent index.
  • Independent storage. Recording data defaults to $XDG_DATA_HOME/korterm/recordings (~/.local/share/korterm/recordings). Configuration lives at $XDG_CONFIG_HOME/korterm/recordings/config.json (~/.config/korterm/recordings/config.json). Only KORTERM_RECORDINGS_DATA_DIR and KORTERM_RECORDINGS_PASSWORD override those settings. KorTerm never implicitly reads korec's configuration or adopts its recordings. Existing files in the old shared directory stay there. Do not point the new data directory at korec's store.
  • One recording per pane. A recording is one terminal stream, and panes cannot be merged into one. Recording a tab, a session or everything means recording each pane in it.
  • Output only. KorTerm records terminal output, not keystrokes. There is no input tap. A password normally stays absent only when the program does not echo or otherwise print it. Recordings keep any secrets in output, including tokens, command arguments and file contents.
  • Encryption limits. KorTerm writes a plaintext intermediate file even when encryption is enabled. It encrypts the archive when recording finishes, not each output chunk as it arrives. A crash, write error or queue overflow can leave the plaintext intermediate file behind. Backups and synchronization can copy active intermediate files. File permissions restrict access but do not encrypt the recording directory.
  • Mid-session start. New recordings preserve the active screen's colours and text attributes in their initial ANSI frame. Old plain-text initial frames cannot recover discarded colours. This is a screen snapshot, not a full checkpoint of every terminal mode or application state.
  • Back-pressure. At most 64 MiB of output waits to be written; output faster than that (yes) stops the recording with an error rather than filling memory.
  • No txt transcript. korecord's "txt" archive member (a searchable plaintext transcript, built by replaying through pyte) is not built by KorTerm's own recordings; its recordings browser reads the "cast" member directly instead. korec cat/grep on a KorTerm recording wait for that member forever unless korec's own tooling is run on it separately.
  • Scopes. A tab's switch is a field of the tab, so it moves with the tab between groups, sessions and windows. A session's is in its window's Terminal (recorded_sessions), the "everything" switch is shared by all windows, and the record-always setting holds it on. record::sync runs from Ui::refresh, which every change of panes, tabs and sessions ends in, and starts or stops recorders to match.

Integration points.

  • Output tap: korterm-core wraps alacritty's PTY (TappedPty) so the reader thread hands every chunk, and every resize, to an OutputTap set with Session::set_output_tap. Setting a tap also hands it the screen as it is, which becomes the first frame of a recording begun midway.
  • korterm-record (no GTK): format/ is the on-disk format (above). Recorder::start inserts a pending index row and starts a writer thread; the thread turns the tap's data into asciicast v2 lines in a local raw file (UTF-8 split across reads kept whole, repeated resizes dropped), then on End packs that file into the session's .rec and finalizes its index row — all covered by wait_for_writers, since there is no longer a subprocess to finish the job once KorTerm has moved on. list_sessions/read_session_output/delete_session serve the recordings browser; repair_orphaned_sessions is format::session::repair.
  • korterm-ui: record.rs (scopes, sync, repair_once), recordings.rs (the browser window, win.show-recordings), the header's record menu (win.record-tab, win.record-session, win.record-all), the plugin entry and its settings in plugins.rs.