pty

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: Apache-2.0 Imports: 21 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 pty runs the programs of the app's terminal tabs: each in a pseudo-terminal of its own (a Unix PTY, or a Windows pseudoconsole: Microsoft's newer conpty.dll and OpenConsole.exe when the app carries them, else the system's), with its output kept in memory for the tab's window and passed to it with flow control (see Serve).

The rules (docs/design.md §15, "The app's terminal"):

  • A tab's program is started from its argument list, never typed into a shell.
  • Its input comes only from the user: the keys, pastes and terminal answers the tab's window sends over its stream (Serve). Nothing else in hopsesh writes into it; this package has no exported way to.
  • What it prints lives in memory only: the tab's scrollback (for the window to draw it again) and, for a step hopsesh started (Spec.Capture), a bounded capture that only the module's reader looks at once the program ended (Session.StepOutput). Neither is ever logged, journaled or written to disk; both are zeroed when the tab closes.
  • A sign-in tab (Spec.Private) keeps no capture and reads nothing of the output, not even the notifications that mark a tab as waiting.

Index

Constants

View Source
const (
	ReasonBell         = "bell"
	ReasonNotification = "notification"
	ReasonIdle         = "idle"
)

Why a tab is Waiting.

Variables

View Source
var ErrClosed = errors.New("the terminal tabs are closed")

ErrClosed is returned once the Manager was closed.

Functions

func BundledConpty

func BundledConpty(string) (dll, host string, ok bool)

BundledConpty is Windows only: elsewhere a tab always uses the system's pseudo-terminal.

func LinkTarget

func LinkTarget(raw string) (string, bool)

LinkTarget is the address a tab's link may open: an http or https URL with a host, at most 2,048 bytes and without control characters, in a canonical form (so the dialog shows what will open). Anything else (javascript:, file:, an app's own scheme) is refused.

func Serve

func Serve(conn Conn, find func(id string) (*Session, error), openLink func(url string)) error

Serve runs one window's connection to a tab: the first frame names the tab, which find returns (the app checks that the window may show it); then output and state go to the window and its input, sizes and acks to the tab until either side ends. openLink is asked to open a link the user clicked, already checked to be http(s) (nil: links are refused). Serve closes conn.

Types

type Capture

type Capture uint8

Capture is what hopsesh keeps of a tab's output besides its scrollback.

const (
	// CaptureNone keeps nothing for hopsesh: a resumed session, a shell, a sign-in.
	CaptureNone Capture = iota
	// CaptureStep keeps the last term.MaxCapture bytes for the module's step reader (the
	// session link a claude --cloud step prints), read once the program ended.
	CaptureStep
)

type Conn

type Conn interface {
	Send([]byte) error
	Receive() ([]byte, error)
	Context() context.Context
	Close() error
}

Conn is a tab window's connection: a Wails stream (*application.StreamConn) in the app, anything that sends and receives whole frames in tests.

type Env

type Env struct {
	// Base is the environment to start from (nil: this process's).
	Base []string
	// Strict keeps only the allowlist (the system's essentials, locale, temporary
	// folders, proxies and certificate settings) and the names in Keep.
	Strict bool
	// Keep are more names Strict keeps (an agent's login variables, such as
	// CLAUDE_CONFIG_DIR).
	Keep []string
	// Unset are names removed after the rest.
	Unset []string
	// Set are KEY=VALUE pairs added last; they win over anything before.
	Set []string
}

Env is how a tab's environment is made. By default a tab gets the user's environment (what a terminal they opened would have), less the variables that tell a program it runs inside another terminal, multiplexer or editor; Strict keeps only a short allowlist instead. Either way the terminal's identity is set (TERM=xterm-256color, COLORTERM=truecolor, TERM_PROGRAM=hopsesh), then Unset is removed and Set added. Nothing here injects a credential: hopsesh never adds one, and a cloud step's Unset removes the ones a vendor's program must not see (ANTHROPIC_API_KEY, CLAUDE_CODE_CHILD_SESSION).

func (Env) Build

func (e Env) Build(version string) []string

Build is the environment a tab's program gets (see Env). version is hopsesh's, for TERM_PROGRAM_VERSION.

type Info

type Info struct {
	PID     int       `json:"pid,omitempty"`
	ID      string    `json:"id"`
	Title   string    `json:"title"`
	Program string    `json:"program"` // the program's file name
	Dir     string    `json:"dir"`
	State   State     `json:"state"`
	Reason  string    `json:"reason,omitempty"` // when Waiting
	Code    int       `json:"code"`             // when Exited: the exit code (-1 unknown; 128+n for signal n)
	Cols    int       `json:"cols"`
	Rows    int       `json:"rows"`
	Capture bool      `json:"capture"` // hopsesh reads this tab's output once it ends (a step)
	Private bool      `json:"private"` // a sign-in: nothing of it is read
	Backend string    `json:"backend"` // "pty", "conpty (bundled)" or "conpty (system)"
	Started time.Time `json:"started"`
}

Info is a tab as the window shows it.

type Manager

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

Manager holds the open tabs.

func NewManager

func NewManager(opts Options) *Manager

NewManager is a Manager with opts (defaults filled in).

func (*Manager) BundledConsole

func (m *Manager) BundledConsole() bool

BundledConsole reports whether tabs run on the newer pseudoconsole the app carries (Windows; false elsewhere).

func (*Manager) Close

func (m *Manager) Close(id string) error

Close ends tab id's program if it still runs, waits for it (a few seconds at most), and forgets the tab and everything it kept.

func (*Manager) CloseAll

func (m *Manager) CloseAll()

CloseAll closes every tab and refuses new ones (the app is quitting).

func (*Manager) Get

func (m *Manager) Get(id string) (*Session, bool)

Get is the open tab id.

func (*Manager) List

func (m *Manager) List() []Info

List is every open tab, oldest first.

func (*Manager) Running

func (m *Manager) Running() int

Running is how many tabs' programs have not ended (the app asks before quitting them).

func (*Manager) Start

func (m *Manager) Start(spec Spec) (*Session, error)

Start opens a tab running spec's program.

type Options

type Options struct {
	// Version is hopsesh's version, for TERM_PROGRAM_VERSION.
	Version string
	// Scrollback is how many bytes of output a tab keeps for its window (0: 1 MiB).
	Scrollback int
	// IdleAfter is how long a program that printed something since the user last typed
	// must stay quiet to count as waiting (0: 3 seconds; negative: never).
	IdleAfter time.Duration
	// MaxTabs is how many tabs may be open at once (0: 16).
	MaxTabs int
	// OnChange is told every change of a tab's state, and its removal (State ""). It is
	// called without locks held, from the goroutine that saw the change, and must not
	// block.
	OnChange func(Info)
	// ConptyDir is where Windows looks for a bundled conpty.dll (with OpenConsole.exe in
	// its x64 or arm64 folder): "" is the "conpty" folder beside the program. Elsewhere
	// it is unused.
	ConptyDir string
}

Options set up a Manager.

type Session

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

Session is one tab: its program, its terminal and what it printed.

func (*Session) Done

func (s *Session) Done() <-chan struct{}

Done is closed once the program ended and its output was read.

func (*Session) ID

func (s *Session) ID() string

ID is the tab's id.

func (*Session) Info

func (s *Session) Info() Info

Info is the tab as it is now.

func (*Session) StepOutput

func (s *Session) StepOutput() (agent.StepOutput, bool)

StepOutput is what a CaptureStep tab's program printed, for the module's step reader (agent.CloudStepReader), once it ended: the capture as plain text, the terminal's width and the exit code. It forgets the capture; a second call, or one before the program ended or on a tab without capture, returns false.

func (*Session) Wait

func (s *Session) Wait(ctx context.Context) (int, error)

Wait waits for the program to end (or ctx) and returns its exit code.

type Signal

type Signal uint8

Signal is what the notifier saw.

const (
	SignalNone Signal = iota
	SignalBell
	SignalNotification
)

type Spec

type Spec struct {
	// Argv is the program and its arguments, run as they are (never through a shell).
	Argv []string
	// Dir is the folder it runs in (absolute; "" is the user's home folder).
	Dir string
	// Title is what the tab says it runs ("claude --cloud in hopsesh's hand-off folder for
	// demo"); "" is the program's name.
	Title string
	Env   Env
	// Cols and Rows are the terminal's starting size (0: 100 by 30).
	Cols, Rows int
	Capture    Capture
	// Private marks a sign-in (claude auth login, codex login, gh auth login): nothing of
	// the output is read, not even for the waiting state. It cannot capture.
	Private bool
	// SystemConsole runs the tab on the system's pseudoconsole even when the app carries
	// Microsoft's newer one (Windows; the user's choice, for a security tool that blocks
	// the bundled OpenConsole.exe). Elsewhere it is unused.
	SystemConsole bool
}

Spec is a tab to open.

type State

type State string

State is where a tab's program is.

const (
	// Running: the program runs (or hopsesh cannot tell that it waits).
	Running State = "running"
	// Waiting: the program asked for the user (a bell or a notification), or printed
	// something and went quiet since the user last typed (Reason says which).
	Waiting State = "waiting"
	// Exited: the program ended (Info.Code has how).
	Exited State = "exited"
)

Directories

Path Synopsis
Package ptytest is a tab's window for tests: it connects to a tab through pty.Serve over an in-memory stream, draws the output in a terminal emulator (charmbracelet/x/vt) that answers the program's queries as xterm.js does in the app, acks what it drew, and types what the test types (as the user would; hopsesh never does).
Package ptytest is a tab's window for tests: it connects to a tab through pty.Serve over an in-memory stream, draws the output in a terminal emulator (charmbracelet/x/vt) that answers the program's queries as xterm.js does in the app, acks what it drew, and types what the test types (as the user would; hopsesh never does).

Jump to

Keyboard shortcuts

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