ptyhost

package
v2.10.2 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 32 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ptyhost is Lectern's own terminal keeper: one small process per user per machine that owns pseudo-terminals, so agents and shells outlive the Lectern server, its restarts and its upgrades, with nothing but the lectern binary installed (docs/ptyhost.md).

The host keeps each session's screen in a terminal emulator, so a client can read it back (capture), and any number of clients can attach to the live stream. Clients reach it over a Unix socket (an AF_UNIX socket on Windows too) in a directory only the user can enter.

Index

Constants

View Source
const (
	Protocol    = 1
	MinProtocol = 1
)

Protocol is the version this build speaks; MinProtocol the oldest it still accepts. New operations are additive and announced in the handshake, so a newer client can keep using an older host for everything that host knows.

View Source
const DefaultHistory = 5000

DefaultHistory is how many lines of scrollback a session keeps unless LECTERN_PTY_HISTORY says otherwise.

View Source
const PollEnd = "ADK-POLL-END-v2"

PollEnd ends a batched poll or probe; it is the framing of internal/sessions/backend, and a test there keeps the two equal.

View Source
const SocketEnv = "LECTERN_PTYHOST_SOCKET"

SocketEnv overrides where the host listens (tests, several instances).

View Source
const TestGuardEnv = "LECTERN_PTYHOST_TEST_GUARD"

TestGuardEnv marks a process started by a test, directly or through any number of children. Under it the default socket is refused: a test names its own (SocketEnv), so it never starts, reaches or leaves behind a host on the user's real socket — where a running Lectern server would count its sessions as its own. The test runners set it; every Go test binary sets it for itself below, so a `go test` run by hand is guarded too.

Variables

View Source
var ErrDefaultSocketInTest = errors.New("refusing the default PTY host socket in a test: set " + SocketEnv + " to a private path")

ErrDefaultSocketInTest is SocketPath's answer under TestGuardEnv when no socket was named.

View Source
var ErrRunning = errors.New("a PTY host is already running for this socket")

ErrRunning means another host already owns the socket.

Functions

func AttachTerminal

func AttachTerminal(cl *Client, name string, in, out *os.File) error

AttachTerminal shows a session in this process's terminal until the session ends, the host goes away, or the user detaches.

func Command

func Command(args []string) int

Command is `lectern pty …`, the tmux-language client of this user's PTY host (docs/md).

func HostCommand

func HostCommand(args []string, build string) int

HostCommand is `lectern ptyhost serve|status|stop`; build labels the host.

func IsNotRunning

func IsNotRunning(err error) bool

IsNotRunning reports whether err means no host is listening.

func Serve

func Serve(opts Options) error

Serve runs a host until it is idle or told to stop. It returns ErrRunning without touching the socket when another host holds the lock.

func SocketPath

func SocketPath() (string, error)

SocketPath is where this user's PTY host listens on this machine.

The directory is private to the user: the socket is a raw input channel into every session, with the user's own permissions behind it.

Types

type CLI

type CLI struct {
	Socket string
	// Bin is the lectern binary that starts a host when a session is made.
	Bin    string
	Stdin  io.Reader
	Stdout io.Writer
	Stderr io.Writer
	// Attach runs an interactive attachment; the command layer supplies it
	// because it needs the real terminal.
	Attach func(c *Client, name string) error
}

CLI is `lectern pty …`: the subset of tmux's command language Lectern's session code uses, answered by the PTY host, with tmux's output and error texts (docs/ptyhost.md §3).

func (*CLI) Run

func (c *CLI) Run(args []string) int

Run executes one command and returns its exit status.

type Client

type Client struct {
	Hello hello
	// contains filtered or unexported fields
}

Client is one connection to a host.

func Dial

func Dial(socket string) (*Client, error)

Dial connects to the host on socket and shakes hands.

func Ensure

func Ensure(socket, bin string) (*Client, error)

Ensure connects to the host on socket, starting one from bin when none is running. Only a caller that is about to create a session should start one; everything else treats "not running" as "no sessions".

func (*Client) Attach

func (c *Client) Attach(name string, cols, rows int) (*Stream, error)

Attach turns the connection into a stream of the named session. The size, when given, becomes the session's size.

func (*Client) Close

func (c *Client) Close() error

Close ends the connection.

func (*Client) Do

func (c *Client) Do(req Request) (Response, error)

Do sends one request and reads its answer.

func (*Client) Supports

func (c *Client) Supports(op string) bool

Supports reports whether the host announced op.

type Host

type Host struct {
	// contains filtered or unexported fields
}

Host owns the sessions of one user on one machine.

type Info

type Info struct {
	Name     string            `json:"name"`
	ID       int               `json:"id"`
	Created  int64             `json:"created"`
	Activity int64             `json:"activity"`
	PID      int               `json:"pid"`
	TTY      string            `json:"tty,omitempty"`
	Cwd      string            `json:"cwd,omitempty"`
	Current  string            `json:"current,omitempty"` // the foreground command
	StartDir string            `json:"start_dir,omitempty"`
	Cols     int               `json:"cols"`
	Rows     int               `json:"rows"`
	Clients  int               `json:"clients"`
	Options  map[string]string `json:"options,omitempty"`
	Env      []string          `json:"env,omitempty"`
}

Info describes one session.

type MissingError

type MissingError struct{ Name string }

MissingError is tmux's "can't find session".

func (*MissingError) Error

func (e *MissingError) Error() string

type Options

type Options struct {
	Socket string
	// History is the scrollback per session; 0 means DefaultHistory.
	History int
	// Idle is how long a host with no sessions and no clients waits before
	// exiting; 0 means ten seconds.
	Idle  time.Duration
	Build string
	Log   *slog.Logger
}

Options configure a host.

type Probe

type Probe struct {
	Name     string   `json:"name"`
	Found    bool     `json:"found"`
	RootArgs string   `json:"root_args,omitempty"`
	Current  string   `json:"current,omitempty"`
	TTYArgs  []string `json:"tty_args,omitempty"`
}

Probe is what the agent-exit probe needs about one session: the arguments of its root process and its foreground command, and the arguments of every process on its terminal.

type Process

type Process struct {
	// contains filtered or unexported fields
}

Process is one program on a pseudo-terminal of its own, with no screen model and no host: what the web terminal runs for an attachment that is not a PTY-host session (a `tmux attach`, an ssh to another machine).

func StartProcess

func StartProcess(argv []string, cols, rows int) (*Process, error)

StartProcess starts argv on a new pseudo-terminal, in this process's environment with TERM set for an xterm-compatible client.

func (*Process) Close

func (p *Process) Close() error

Close hangs the terminal up, as a closed terminal window does, and kills whatever is left of the program a moment later.

func (*Process) Done

func (p *Process) Done() <-chan struct{}

Done closes when the program has exited.

func (*Process) Read

func (p *Process) Read(b []byte) (int, error)

Read returns the program's output; io.EOF-like errors once it has exited.

func (*Process) Resize

func (p *Process) Resize(cols, rows int) error

Resize changes the terminal's size.

func (*Process) Write

func (p *Process) Write(b []byte) (int, error)

Write types into the program.

type Request

type Request struct {
	Op   string `json:"op"`
	Name string `json:"name,omitempty"`

	// new: Argv runs as it is; Shell, when Argv is empty, is one command
	// line for the host's shell.
	Argv       []string `json:"argv,omitempty"`
	Shell      string   `json:"shell,omitempty"`
	Dir        string   `json:"dir,omitempty"`
	Env        []string `json:"env,omitempty"`         // the program's whole environment
	SessionEnv []string `json:"session_env,omitempty"` // -e entries, read back by show-environment
	Cols       int      `json:"cols,omitempty"`
	Rows       int      `json:"rows,omitempty"`

	// write: Data is typed as it is; with Paste it is bracketed when the
	// program asked for bracketed paste, as tmux's paste-buffer -p does.
	Data  []byte `json:"data,omitempty"`
	Paste bool   `json:"paste,omitempty"`

	// capture
	Lines int `json:"lines,omitempty"`

	// set-option
	Key         string `json:"key,omitempty"`
	Value       string `json:"value,omitempty"`
	OnlyIfUnset bool   `json:"only_if_unset,omitempty"`

	// info, list: Detail also asks for the foreground process and the
	// working directory, which can cost a process-table read per session.
	Detail bool `json:"detail,omitempty"`

	// probe
	Names []string `json:"names,omitempty"`

	// shutdown
	Force bool `json:"force,omitempty"`
}

Request is one operation. Unused fields are omitted.

type Response

type Response struct {
	OK      bool   `json:"ok"`
	Error   string `json:"error,omitempty"`
	Missing bool   `json:"missing,omitempty"` // no such session

	Info     *Info   `json:"info,omitempty"`
	Sessions []Info  `json:"sessions,omitempty"`
	Text     string  `json:"text,omitempty"`
	Set      bool    `json:"set,omitempty"`
	Probes   []Probe `json:"probes,omitempty"`
}

Response answers a Request.

type Session

type Session struct {
	// contains filtered or unexported fields
}

Session is one program on one pseudo-terminal.

type Stream

type Stream struct {

	// Exited is set once the host said the session ended.
	Exited bool
	// contains filtered or unexported fields
}

Stream is an attached session.

func (*Stream) Close

func (s *Stream) Close() error

Close detaches.

func (*Stream) Read

func (s *Stream) Read() ([]byte, error)

Read returns terminal output. It returns io.EOF when the session ends or the host goes away.

func (*Stream) Resize

func (s *Stream) Resize(cols, rows int) error

Resize changes the session's size.

func (*Stream) Write

func (s *Stream) Write(b []byte) error

Write sends keyboard input.

Jump to

Keyboard shortcuts

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