Documentation
¶
Overview ¶
Package ptyhost keeps an owned session's terminal alive across daemon restarts (ADR-033).
A session Caprock starts used to live inside the daemon: the daemon held the PTY master, so stopping the daemon — an upgrade, `launchctl kickstart -k`, `caprock down` — closed the terminal and took the session with it. Here the PTY belongs to a small process of its own, `caprock pty-host`, one per session: it starts the agent, keeps the PTY master and a scrollback ring, and serves one daemon at a time over a token-guarded loopback socket. The daemon is a client. It can go away and a new one, even a newer binary, can connect to the same holder and carry on.
The holder never signals or types into anything but its own child (rule 7), exits when the child does, and removes its registry entry on the way out.
Wire protocol ¶
Every message is a frame: one type byte, a 4-byte big-endian payload length, the payload. The first frame on a connection is the client's Hello; the holder answers Welcome and then a Snapshot of its ring, and from then on streams Output and, once, Exit.
The protocol is versioned by Proto, carried in Hello and Welcome. A holder started by one release must keep working with the daemon of the next, so the rule is additive: frame types are never renumbered or repurposed, and a side that receives a frame type it does not know ignores it. A change that cannot be made that way raises Proto, and a daemon keeps speaking every older version for as long as a holder of that version can still be running.
Index ¶
Constants ¶
const Proto = 1
Proto is the protocol version this binary speaks.
const ReadyTimeout = 15 * time.Second
ReadyTimeout is how long a new holder gets to start its child and report.
const SeqAckTimeout = 5 * time.Second
SeqAckTimeout is how long sequenced input waits for the holder's answer. Past it the input counts as not applied, and the browser resends it.
Variables ¶
This section is empty.
Functions ¶
func Main ¶
func Main(stdin io.Reader, stdout io.WriteCloser) int
Main runs a holder: read the LaunchSpec from stdin, start the child, report on stdout, then serve until the child exits. Returns the process exit code.
After the ready line stdout is closed. The holder outlives the daemon that started it, and a write to a pipe whose reader has gone is a SIGPIPE.
Types ¶
type ExitError ¶
type ExitError struct{ Code int }
ExitError is a non-zero exit reported by a holder.
type ExitRecord ¶
type ExitRecord struct {
SessionID string `json:"session_id"`
Code int `json:"code"`
At time.Time `json:"at"`
}
ExitRecord is left behind when a session ends while no daemon is connected, so the next one can record how it ended rather than only that it did.
type LaunchSpec ¶
type LaunchSpec struct {
Proto int `json:"proto"`
SessionID string `json:"session_id"`
Dir string `json:"dir"` // registry directory
Token string `json:"token"`
Version string `json:"version,omitempty"`
Spec ptyman.Spec `json:"spec"`
}
LaunchSpec is what the daemon hands a new holder on its stdin. Stdin rather than argv or a file: argv is readable by every process on the machine, and the spec carries the token and the child's environment (which can hold an API key), none of which should touch the disk.
type Manager ¶
type Manager struct {
// Exe is the binary to run as the holder; Args the arguments that select
// the holder (default: "pty-host").
Exe string
Args []string
// Env is the holder process's own environment (nil: this process's).
// The child's environment is the Spec's, as it always was.
Env []string
DataDir string
Version string
Log *slog.Logger
// Fallback starts a session in-process when a holder cannot be started.
// Such a session works as before and ends with the daemon, which beats
// refusing to start it.
Fallback ptyman.Manager
}
Manager starts sessions inside pty-host processes and reattaches to the ones a previous daemon left running. It is a ptyman.Manager, so the agents package spawns through it exactly as it spawns in-process.
func (*Manager) Reattach ¶
func (m *Manager) Reattach() ([]Attached, []ExitRecord)
Reattach connects to every holder in the registry. It also returns the exit codes of sessions that ended while no daemon was connected, and removes the files it has read. A registry entry nothing answers for is a holder that died without cleaning up, and is removed; one that answers but refuses is left alone and logged.
type Record ¶
type Record struct {
Proto int `json:"proto"`
SessionID string `json:"session_id"`
HostPID int `json:"host_pid"`
ChildPID int `json:"child_pid"`
Addr string `json:"addr"`
Token string `json:"token"`
Cwd string `json:"cwd"`
Command string `json:"command"`
StartedAt time.Time `json:"started_at"`
// Version is the Caprock release that started the holder, for the log
// line a mismatch is diagnosed from. Compatibility is Proto's job.
Version string `json:"version,omitempty"`
// Meta is the spawn's ptyman.Spec.Meta, kept for the daemon that
// reattaches: which agent it is, and anything the daemon needs to keep
// watching it.
Meta map[string]string `json:"meta,omitempty"`
}
Record is a running holder's registry entry, <data>/ptyhost/<session>.json. It is how a daemon that did not start the holder finds it: where it listens, the token it wants, and enough about the session to show it.
The file is the holder's to write and remove. The daemon only deletes one whose holder is gone (nothing listens at Addr any more).