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
- Variables
- func Attach(conn Conn, id string, in io.Reader, out io.Writer, sizes <-chan [2]int, ...) (int, error)
- func BundledConpty(string) (dll, host string, ok bool)
- func LinkTarget(raw string) (string, bool)
- func Serve(conn Conn, find func(id string) (*Session, error), openLink func(url string)) error
- type Capture
- type Conn
- type Env
- type Info
- type Manager
- type Options
- type Session
- type Signal
- type Spec
- type State
Constants ¶
const ( ReasonBell = "bell" ReasonNotification = "notification" ReasonIdle = "idle" )
Why a tab is Waiting.
Variables ¶
var ErrClosed = errors.New("the terminal tabs are closed")
ErrClosed is returned once the Manager was closed.
Functions ¶
func Attach ¶ added in v0.5.0
func Attach(conn Conn, id string, in io.Reader, out io.Writer, sizes <-chan [2]int, problems func(string)) (int, error)
Attach shows a tab that runs elsewhere (hopsesh serve on another machine, over conn) in the terminal in and out belong to: the tab's scrollback and output go to out, the keys read from in go to the program, and each size from sizes resizes it. It returns the program's exit code once it ends, or an error when the connection ends first (the tab keeps running there; attaching again shows it). problems is told what the far end refused.
func BundledConpty ¶
BundledConpty is Windows only: elsewhere a tab always uses the system's pseudo-terminal.
func LinkTarget ¶
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 ¶
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.
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).
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 ¶
NewManager is a Manager with opts (defaults filled in).
func (*Manager) BundledConsole ¶
BundledConsole reports whether tabs run on the newer pseudoconsole the app carries (Windows; false elsewhere).
func (*Manager) Close ¶
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).
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) 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.
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" )
Source Files
¶
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). |