ptyrunner

package
v0.196.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 8 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 ptyrunner is the worker-side runner variant for interactive (pty-attached) jobs (WEB-03 design §5/§6, D1). PtyRunner satisfies runner.Runner and is selected by the job service ONLY for interactive jobs (submit routing), so a plain job keeps running on the local runner unchanged (G023). Run starts a pty child via internal/pty, wraps it in a PtySession state machine, registers the session, and blocks until the child exits.

The package imports internal/pty (backend) + internal/runner (the interface); it does NOT import internal/job (dependency stays one-way, G022/G024): the job package reaches PtyRunner only through the runner.Runner interface via the runners map, and NEVER holds the PtySession — fd ownership/teardown lives here.

Index

Constants

View Source
const (
	StateStarting   = "starting"
	StateRunning    = "running"
	StateCancelling = "cancelling" // cancel requested; teardown about to run
	StateExiting    = "exiting"    // ordered close sequence executing
	StateClosed     = "closed"
)

Session state names (design §5 state machine). Kept as strings so the test hook can assert an exact transition + teardown-step ORDER.

View Source
const Name = "pty"

Name is the runner identifier the job service routes interactive jobs to. It is registered into the runners map (like "local") ONLY when Available() so the job package can select it by key without importing this package (G024).

Variables

This section is empty.

Functions

func Available

func Available() bool

Available reports whether a pty backend is usable on this build/host. core registers PtyRunner only when true, so "pty runner present in the map" is the capability signal the job service keys on (it never calls pty.IsAvailable).

Types

type PtyRunner

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

PtyRunner runs interactive jobs under a pty. It satisfies runner.Runner.

func New

func New() *PtyRunner

New builds a PtyRunner with its own session registry.

func (*PtyRunner) Name

func (r *PtyRunner) Name() string

Name implements runner.Runner.

func (*PtyRunner) Run

func (r *PtyRunner) Run(ctx context.Context, req runner.Request) runner.Result

Run starts req under a pty, registers the PtySession and blocks until the child exits (or ctx cancels → ordered teardown). Output is drained off the pty (in P1+ it feeds the serve relay/cast — design §11 "pty output只入 cast/attach", NOT stdout.log — so req.Stdout is intentionally not wired here).

func (*PtyRunner) Sessions

func (r *PtyRunner) Sessions() *registry

Sessions exposes the live session registry (P1+ relay wiring / cancel; the spike uses it to assert a running job's session is discoverable).

func (*PtyRunner) SetInitialInputMaxWait

func (r *PtyRunner) SetInitialInputMaxWait(d time.Duration)

SetInitialInputMaxWait overrides how long priming waits for a quiet terminal before writing anyway (design §9.1 B). Tests use it to prove the bounded path without a 10s wait; <= 0 restores the default.

func (*PtyRunner) SetObserver

func (r *PtyRunner) SetObserver(o SessionObserver)

SetObserver injects the session observer. Worker uses it to pump remote interactive jobs to the hub; serve uses it to expose local interactive jobs via the same relay/attach stack. Leaving it nil keeps the default discard drain.

type PtySession

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

PtySession is one pty execution's handle + state machine. It owns the master fd lifecycle: teardown (Close on the pty + reap) runs here in a fixed order, exactly once (unified CAS close). The job's finish never closes the fd — it only observes the terminal result via Run's return (design §5 "finish 不拥有 fd close").

func (*PtySession) Done

func (ps *PtySession) Done() <-chan struct{}

Done is closed when the session is fully closed (post publish-exit).

func (*PtySession) ExitCode

func (ps *PtySession) ExitCode() int

ExitCode returns the reaped child's exit code (valid after Done is closed).

func (*PtySession) Read

func (ps *PtySession) Read(b []byte) (int, error)

Read exposes the raw pty output stream (the relay/recorder consumes it). It also stamps the output-activity clock the priming wait uses (see WaitOutputQuiet) — the reader is the only place that sees when the child last painted, and there is exactly ONE reader (design §9.1 B).

func (*PtySession) Resize

func (ps *PtySession) Resize(cols, rows int) error

Resize forwards a window-size change to the pty.

func (*PtySession) State

func (ps *PtySession) State() string

State returns the current state name.

func (*PtySession) WaitOutputQuiet

func (ps *PtySession) WaitOutputQuiet(ctx context.Context, quiet, limit time.Duration) bool

WaitOutputQuiet reports that the child's terminal is ready to be typed into: it has produced output and then stayed silent for quiet, OR limit has elapsed since the call (a TUI that never stops repainting must not hold the message forever — it is written anyway). It is how path B's first message waits for the TUI to draw its prompt (design §9.1 B). false = ctx ended first.

It polls rather than blocking on a channel: the wait is bounded by 10s, the cadence is coarse on purpose (nothing here is latency-sensitive), and a poll cannot be lost the way a coalescing signal can.

func (*PtySession) WriteInput

func (ps *PtySession) WriteInput(b []byte) (int, error)

WriteInput forwards stdin bytes to the child. It is refused once the teardown "stop input" step has run (input is the FIRST thing severed on close).

type SessionObserver

type SessionObserver interface {
	OnSessionStart(jobID string, sess *PtySession)
}

SessionObserver is notified once, synchronously, right after a PtySession is registered and BEFORE PtyRunner.Run blocks on the child — the observer then becomes the SOLE reader of the session's pty output (design §11 / D-P2-3). The implementation MUST be non-blocking (only hand off / start a goroutine internally); a blocking OnSessionStart would stall PtyRunner.Run → job.execute.

Jump to

Keyboard shortcuts

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