ptyman

package
v0.154.2 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0 Imports: 13 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 ptyman owns pseudo-terminal sessions: spawn a process attached to a PTY (ConPTY on Windows), stream its bytes, write input, resize, signal, kill. The interface is ours so the backend is swappable (ADR-006); the first backend delegates to github.com/aymanbagabas/go-pty. Nothing in Phase 0 depends on this package — it exists for the T0 spike and Phase 1 (T11).

Index

Constants

This section is empty.

Variables

View Source
var ErrDetached = errors.New("ptyman: detached; the process keeps running in its pty-host")

ErrDetached is what Wait returns for a session that was let go of rather than ended: its process is still running, held by a pty-host, and the next daemon picks it back up. It is not an exit and must not be recorded as one.

View Source
var ErrNotSupported = errors.New("ptyman: signal not supported on this platform")

ErrNotSupported is returned for signals the platform cannot honour.

Functions

func KillPID added in v0.70.1

func KillPID(pid int) error

KillPID is TerminatePID's last resort: SIGKILL, group first.

func TerminatePID added in v0.70.1

func TerminatePID(pid int) error

TerminatePID asks a process Caprock started, by pid, to stop: SIGTERM to its process group when it leads one (go-pty starts every child in its own session, so Claude Code's own children go with it), else to the process.

For a process the daemon no longer holds a handle to — one an older daemon started and lost the terminal of. Callers must have established that the pid is a session Caprock owns (rule 7); this function cannot tell.

Types

type Detacher added in v0.70.1

type Detacher interface {
	Detach() error
}

Detacher is a session whose process lives outside the daemon. Detach drops the daemon's connection and leaves the process running, which is what a daemon restart or upgrade does to it.

type GoPTY

type GoPTY struct{}

GoPTY is the go-pty backed Manager (creack/pty on POSIX, ConPTY on Windows).

func (GoPTY) Spawn

func (GoPTY) Spawn(ctx context.Context, spec Spec) (Session, error)

Spawn starts spec.Command attached to a fresh PTY.

type Manager

type Manager interface {
	Spawn(ctx context.Context, spec Spec) (Session, error)
}

Manager spawns sessions.

func New

func New() Manager

New returns the default manager.

type Ringed added in v0.75.0

type Ringed interface {
	Ring() *termbuf.Ring
}

Ringed is a session that keeps its own scrollback ring, with offsets that continue across daemon restarts (a pty-host's). The daemon uses that ring rather than starting one at zero.

type SeqWriter added in v0.75.0

type SeqWriter interface {
	WriteSeq(client string, seq uint64, p []byte) (uint64, error)
}

SeqWriter is a session that applies sequenced input exactly once per client itself (terminal protocol v2), so the guarantee outlives the daemon. WriteSeq returns the client's last applied sequence; sequence 0 writes nothing and only asks for it. ErrNotSupported means the daemon must deduplicate on its own.

type Session

type Session interface {
	// Output streams the terminal bytes (what xterm.js renders).
	Output() io.Reader
	// Write sends bytes to the process's stdin (typed input).
	Write(p []byte) (int, error)
	// Resize changes the terminal size.
	Resize(cols, rows int) error
	// Signal pauses/resumes/kills the process. Paused sessions on Windows hold input.
	Signal(sig Signal) error
	// Wait blocks until the process exits and returns its exit error (nil = 0).
	Wait() error
	// PID returns the process id (0 before start / after close).
	PID() int
	// Paused reports the pause state (Windows input-hold or POSIX SIGSTOP).
	Paused() bool
	// Close releases the PTY; kills the process if still running.
	Close() error
}

Session is a running PTY-attached process.

type Signal

type Signal string

Signal is a control action on an owned session.

const (
	SignalPause  Signal = "pause"  // SIGSTOP on POSIX; input-hold on Windows (no process-level equivalent)
	SignalResume Signal = "resume" // SIGCONT on POSIX; releases the input-hold on Windows
	SignalKill   Signal = "kill"   // terminate the process tree
	// SignalTerm asks the process to stop and lets it clean up first. Claude
	// Code writes its transcript and releases its session on the way out, none
	// of which happens under SIGKILL — so a daemon restart used to take a
	// user's running work with it, silently.
	SignalTerm Signal = "term"
	// SignalHangup is what closing a terminal window sends: SIGHUP to the
	// child's process group. An interactive shell ignores SIGTERM but exits
	// on a hangup, and hangs up its own jobs on the way. Nothing on Windows,
	// where a console has no hangup and SignalTerm already ends the process.
	SignalHangup Signal = "hangup"
)

type Spec

type Spec struct {
	// ID names the session to a backend that keeps it outside this process
	// (internal/ptyhost files its registry entry under it). The in-process
	// backend ignores it.
	ID string
	// Meta is carried by a backend that keeps the session outside this
	// process and handed back when the session is reattached: what the
	// manager needs to know about a session it did not start in this run
	// (which agent, the port its server listens on). The in-process backend
	// ignores it.
	Meta    map[string]string
	Command string
	Args    []string
	Dir     string
	Env     []string // nil ⇒ inherit
	Cols    int      // 0 ⇒ 120
	Rows    int      // 0 ⇒ 40
}

Spec describes a process to spawn.

Jump to

Keyboard shortcuts

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