stagent

module
v0.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 3, 2026 License: MIT

README

stagent

Server-side companion of the SSH Term app. This repository holds its source code and its release binaries, so you can read exactly what the app installs on your server and check that the binaries were built from this code.

What it does

SSH Term's chat view shows the conversation of the coding agent (Claude Code, Codex, Oh My Pi) running in a terminal tab, also when it runs inside tmux, zellij, screen or herdr. For that the app runs stagent follow over an SSH exec channel on the terminal's own connection. It:

  • finds the tab's shell through the per-connection SSH server process, or among the user's terminal sessions below tailscaled (Tailscale SSH), or by the shell's SSH_CONNECTION, and the pane a multiplexer client in it shows,
  • finds the agent process in that shell or pane and the conversation file it writes (~/.claude, ~/.codex, ~/.omp),
  • streams new messages of that file as JSON lines until the app closes the channel.

It only reads the process list, on macOS the login records (/var/run/utmpx), and those conversation files, and asks the multiplexer which pane its client shows (tmux list-clients, …, run as the client's own binary). It only considers processes of the user it runs as: another user's processes are never taken for the tab, an agent or a multiplexer, so it never runs their binaries or passes on their environment. It opens no network port, starts no background process and changes no configuration. Input typed in the chat view goes to the terminal itself, not through stagent. The wire format and these rules are documented in PROTOCOL.md ("stagent follow").

The same binary also contains commands for an upcoming Agent Mode (run, daemon, bridge, hook, integrate); the chat view does not use them.

Persistent terminal sessions

When the app's persistent sessions are enabled, a terminal tab can run its shell inside stagent instead of directly on the SSH connection, so the shell (and whatever runs in it) survives a dropped connection, a sleeping phone or closing the app — without tmux. On the server this runs:

  • stagent bridge, started by the app over an SSH exec channel; it ends with the connection.
  • one stagent run --detached process per session (the holder): it owns the session's pseudo terminal, keeps an emulated copy of the screen and the session's recent output on disk (~/.ssh-term/agent/state/sessions/<id>/, 8 MiB per session by default), and serves the app over a socket only your user can open. It runs your login shell ($SHELL -l) and exits when the shell exits.
  • stagent daemon, the session list, started on demand. While a coding agent started inside a shell session runs, it reads that session's process tree every 2 s to notice when the agent quits, so the list shows the session as a plain shell again.

When the app reconnects it re-attaches the tab and receives only the output it missed (or a redraw of the screen when that is not possible). Sessions can be listed, re-opened and ended from the app, and from any terminal on the server:

  • stagent ls [--json] lists the sessions with their state, last activity and when someone last typed in a passthrough session's own terminal.
  • stagent attach [ID | --last] [--detach-key ctrl-]] attaches your terminal to a session; Ctrl-] detaches (press it twice to send it).
  • stagent run --handoff -- <command> runs a command in your terminal as a session that survives that terminal: when the terminal closes, the session continues detached and the app can pick it up. Before the command starts, stagent run tells you in one line how many agent sessions are held on the host (see stagent ls), if any.
  • Shell wrappers installed with stagent integrate --shell-wrapper make claude, codex and omp typed in an interactive terminal run as stagent run --handoff=auto -- …, so they survive the terminal by default. Non-interactive runs (claude -p, codex exec, omp -p, omp --mode json) are left alone. To have the agent end with the terminal instead, set STAGENT_HANDOFF=0 in that shell, or turn on disable_handoff in config.json (what the app's switch sets); STAGENT_HANDOFF=1 overrides the config.

Sessions also survive logging out. On Linux their sockets live in /tmp/stagent-<uid> (mode 0700), not in $XDG_RUNTIME_DIR, which is removed at logout. On systems where logind kills a user's processes at logout (KillUserProcesses=yes), or where lingering is on, stagent run and stagent daemon first move themselves into a new scope of your systemd user manager (the D-Bus call systemd-run --user --scope makes), so they and the agent leave the login session. That scope still ends with your last logout unless lingering is enabled (loginctl enable-linger, or stagent integrate --linger), and so do agents started from a terminal of a graphical (GNOME etc.) session. stagent doctor reports whether lingering is needed under persistence.linger_needed.

On macOS, sessions started from a terminal survive logging out of the GUI as they are, but the GUI session's Mach bootstrap port they inherit stops working then, and the tools an agent starts afterwards (curl, git, MCP servers) could no longer resolve host names. So stagent run and stagent daemon first switch to your per-user bootstrap port, as tmux does; the keychain stays usable while you are logged in. If that fails they start anyway and stagent doctor says why (shell_wrapper.last_run_bootstrap_error, daemon.bootstrap_error). The login keychain is unlocked only inside the GUI login session, not in SSH sessions, so while you are logged in to the GUI the app's sessions start through a one-off launchd job in that session (gui/<uid>), and claude uses the login it has there. Without a GUI login they start from the SSH session and claude asks you to /login; that login is kept in ~/.claude/.credentials.json. stagent integrate --terminal keeps Terminal.app from asking before it closes a window running an agent, or at logout (stagent in each profile's noWarnProcesses; removing it takes out only that entry).

Installation and removal

The app installs the binary to ~/.ssh-term/agent/bin/stagent the first time the chat view is opened: the server downloads the asset for its OS/arch from this repository's Releases page (or the app downloads and uploads it over SFTP) and it is verified against the SHA-256 pinned inside the app.

An app that pins a newer release updates the binary the same way: silently on a host prepared for it (shell wrapper in place), otherwise when you tap the host's button in the app. stagent install, which the app runs after placing the binary, then rewrites the shell wrapper blocks already in place and replaces a running daemon of the old version; the sessions keep running and stay listed.

Remove it from the chat view's menu (Remove stagent from the server), or on the server:

~/.ssh-term/agent/bin/stagent uninstall --level purge

Release assets

asset platform
stagent-linux-amd64 / stagent-linux-arm64 Linux
stagent-darwin-amd64 / stagent-darwin-arm64 macOS
stagent-windows-amd64.exe / stagent-windows-arm64.exe Windows 10 1809+

Each release also contains SHA256SUMS.

Building and verifying a release

Builds are reproducible (v0.2.0 and later; earlier releases predate the published source): CGO_ENABLED=0, -trimpath, no VCS stamp and an empty build id, so the same source and Go toolchain always produce the same bytes. To check a release, build the tag's source with the Go version named in its release notes and compare:

scripts/release.sh          # writes dist/ and dist/SHA256SUMS
diff dist/SHA256SUMS <(curl -sL https://github.com/obutora/stagent/releases/download/v0.3.0/SHA256SUMS)

Releases are published with scripts/release.sh --publish, which commits the exact source it built to this repository, rebuilds that commit and checks it gives the same bytes, and tags that commit — so a release's tag is always the source of its binaries.

Tests: go test ./... (the e2e tests use tmux, zellij and herdr when they are on PATH).

License

MIT

Directories

Path Synopsis
cmd
stagent command
Command stagent is the server-side half of SSH Term's agent features: the terminal chat view's transcript follower, and Agent Mode's PTY holders, daemon, app bridge, harness hooks and its own installer.
Command stagent is the server-side half of SSH Term's agent features: the terminal chat view's transcript follower, and Agent Mode's PTY holders, daemon, app bridge, harness hooks and its own installer.
stagent-bench command
Command stagent-bench is the load benchmark for stagent (not shipped).
Command stagent-bench is the load benchmark for stagent (not shipped).
internal
attachcli
Package attachcli implements the session commands for a terminal user on the server: `stagent ls` lists the daemon's sessions and `stagent attach` connects the local terminal to a session's holder, like `tmux attach`.
Package attachcli implements the session commands for a terminal user on the server: `stagent ls` lists the daemon's sessions and `stagent attach` connects the local terminal to a session's holder, like `tmux attach`.
bootstrap
Package bootstrap moves stagent's long-lived processes on macOS from the bootstrap namespace of the login session they were started in to the user's per-user one, as tmux does (compat/daemon-darwin.c).
Package bootstrap moves stagent's long-lived processes on macOS from the bootstrap namespace of the login session they were started in to the user's per-user one, as tmux does (compat/daemon-darwin.c).
bridge
Package bridge terminates the app protocol (PROTOCOL.md) on the stdin and stdout of `stagent bridge`, which the app runs over an SSH exec channel.
Package bridge terminates the app protocol (PROTOCOL.md) on the stdin and stdout of `stagent bridge`, which the app runs over an SSH exec channel.
daemon
Package daemon is `stagent daemon`: the per-user session registry, event log, hook integration (state, notifications, pending approvals), push notifications, transcript access and scrollback retention.
Package daemon is `stagent daemon`: the per-user session registry, event log, hook integration (state, notifications, pending approvals), push notifications, transcript access and scrollback retention.
daemonclient
Package daemonclient connects to the stagent daemon, starting it on demand.
Package daemonclient connects to the stagent daemon, starting it on demand.
detect
Package detect derives a session state from its terminal output alone: desktop-notification escapes (OSC 9, OSC 777;notify, OSC 99) and BEL mean the program wants the user, output means it is working, and silence for IdleAfter means it is idle.
Package detect derives a session state from its terminal output alone: desktop-notification escapes (OSC 9, OSC 777;notify, OSC 99) and BEL mean the program wants the user, output means it is working, and silence for IdleAfter means it is idle.
eventlog
Package eventlog is the daemon's append-only event log: one JSON event per line in segment files (events.log, events.log.1, … newest first), a monotonically increasing seq that survives restarts, and an in-memory ring of the latest events that answers `since` queries without touching disk.
Package eventlog is the daemon's append-only event log: one JSON event per line in segment files (events.log, events.log.1, … newest first), a monotonically increasing seq that survives restarts, and an in-memory ring of the latest events that answers `since` queries without touching disk.
follow
Package follow is `stagent follow`: it finds the coding agent running in the terminal that shares its SSH connection — in the terminal's shell, or in the tmux / zellij / screen / herdr pane that terminal shows — or, with --session, in a stagent session, and streams the agent's transcript to the app as JSON lines on stdout until stdin closes.
Package follow is `stagent follow`: it finds the coding agent running in the terminal that shares its SSH connection — in the terminal's shell, or in the tmux / zellij / screen / herdr pane that terminal shows — or, with --session, in a stagent session, and streams the agent's transcript to the app as JSON lines on stdout until stdin closes.
holder
Package holder implements `stagent run`: it starts a program on a PTY and owns everything derived from its output — the VT screen, the on-disk scrollback and the output-based state — while serving attach/input requests from the bridge on its own local IPC endpoint and reporting state changes (never output) to the daemon.
Package holder implements `stagent run`: it starts a program on a PTY and owns everything derived from its output — the VT screen, the on-disk scrollback and the output-based state — while serving attach/input requests from the bridge on its own local IPC endpoint and reporting state changes (never output) to the daemon.
hook
Package hook is `stagent hook <harness> [event] [json]`, the command harness hooks run.
Package hook is `stagent hook <harness> [event] [json]`, the command harness hooks run.
hostid
Package hostid keeps the host's random identifier (host_id): 128 random bits as 32 lowercase hex chars, created the first time stagent needs it and never changed afterwards.
Package hostid keeps the host's random identifier (host_id): 128 random bits as 32 lowercase hex chars, created the first time stagent needs it and never changed afterwards.
install
Package install implements stagent's ledger-based installer: `install` (layout + manifest; after an update, the running daemon and the shell wrapper blocks already in place), `integrate` (harness hooks, shell wrappers, login service; planned as unified diffs before anything is written), `doctor` (state report, orphans) and `uninstall` (stop / unhook / purge).
Package install implements stagent's ledger-based installer: `install` (layout + manifest; after an update, the running daemon and the shell wrapper blocks already in place), `integrate` (harness hooks, shell wrappers, login service; planned as unified diffs before anything is written), `doctor` (state report, orphans) and `uninstall` (stop / unhook / purge).
ipc
Package ipc provides the local, same-user-only transport between stagent processes: Unix domain sockets on Linux/macOS and named pipes on Windows.
Package ipc provides the local, same-user-only transport between stagent processes: Unix domain sockets on Linux/macOS and named pipes on Windows.
logind
Package logind asks systemd-logind whether processes a user leaves running survive the end of the login session they were started from, and moves stagent's long-lived processes where they do.
Package logind asks systemd-logind whether processes a user leaves running survive the end of the login session they were started from, and moves stagent's long-lived processes where they do.
notify
Package notify sends notifications to offline push channels: an ntfy topic and/or a generic webhook.
Package notify sends notifications to offline push channels: an ntfy topic and/or a generic webhook.
paths
Package paths resolves where stagent keeps its binary, state, sockets and scrollback on the current host.
Package paths resolves where stagent keeps its binary, state, sockets and scrollback on the current host.
proc
Package proc starts stagent processes that must outlive their caller: the daemon (auto-started by whoever needs it first) and detached holders.
Package proc starts stagent processes that must outlive their caller: the daemon (auto-started by whoever needs it first) and detached holders.
ptable
Package ptable takes snapshots of the host's process table: pid, parent, name, process group and terminal foreground group (unix) and start time of every process.
Package ptable takes snapshots of the host's process table: pid, parent, name, process group and terminal foreground group (unix) and start time of every process.
pty
Package pty runs a program on a pseudo terminal: openpty (creack/pty) on Linux and macOS, ConPTY on Windows 10 1809 and later.
Package pty runs a program on a pseudo terminal: openpty (creack/pty) on Linux and macOS, ConPTY on Windows 10 1809 and later.
rpc
Package rpc runs request/response/notification exchanges over a wire.Codec: a Client for the calling side and Serve for the serving side.
Package rpc runs request/response/notification exchanges over a wire.Codec: a Client for the calling side and Serve for the serving side.
screen
Package screen keeps the visible terminal screen of a session in a VT emulator (charmbracelet/x/vt) and renders it for attached clients: a full ANSI snapshot to resynchronize a client, and per-client diffs that redraw only the rows changed since that client's previous frame.
Package screen keeps the visible terminal screen of a session in a VT emulator (charmbracelet/x/vt) and renders it for attached clients: a full ANSI snapshot to resynchronize a client, and per-client diffs that redraw only the rows changed since that client's previous frame.
scrollback
Package scrollback keeps a session's raw PTY output on disk: append-only segment files of SegmentSize bytes under the session's data directory, oldest segments deleted once the per-session cap is reached.
Package scrollback keeps a session's raw PTY output on disk: append-only segment files of SegmentSize bytes under the session's data directory, oldest segments deleted once the per-session cap is reached.
transcript
Package transcript reads the conversation files coding agents write — Claude Code, Codex and omp JSONL — and normalizes them to wire.Message.
Package transcript reads the conversation files coding agents write — Claude Code, Codex and omp JSONL — and normalizes them to wire.Message.
version
Package version holds the stagent release and wire protocol versions.
Package version holds the stagent release and wire protocol versions.
wire
Package wire is the message envelope and codec shared by every stagent connection: app ↔ bridge (over the SSH exec channel) and the local IPC links (holder, daemon, hook, bridge).
Package wire is the message envelope and codec shared by every stagent connection: app ↔ bridge (over the SSH exec channel) and the local IPC links (holder, daemon, hook, bridge).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL