session

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 15 Imported by: ? 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 session manages persistent SSH PTY shell sessions for the MCP terminal tool.

Ownership: the Manager owns every managedSession it stores and the PTY channel inside it. A session is closed when it is replaced (generation change), idles past its TTL, its reader observes EOF, or the Manager shuts down. The idle reaper goroutine is started lazily with the first session and joined by Shutdown.

Index

Constants

View Source
const (
	DefaultTimeout          = 5 * time.Second
	DefaultIdleTTL          = 30 * time.Minute
	DefaultMaxSessions      = 16
	DefaultMaxTotalSessions = 64
)

Variables

View Source
var (
	// ErrSessionBusy is returned when a new command is submitted while the
	// previous command has not yet reported completion.
	ErrSessionBusy = errors.New("terminal session is still running a previous command; poll with an empty cmd or send SIGINT/EOF/SIGTERM/SIGKILL first")
	// ErrSessionInUse is returned when another Execute call is actively waiting
	// on the session; callers should wait for that call to return or send a
	// control signal (SIGINT/EOF/SIGTERM/SIGKILL) to interrupt it.
	ErrSessionInUse = errors.New("another call is currently waiting on this terminal session; wait for it to return or send SIGINT/EOF/SIGTERM/SIGKILL to interrupt")
	// ErrSessionNotFound is returned when a poll or signal targets a session
	// that has not been created or has already closed/expired.
	ErrSessionNotFound = errors.New("terminal session does not exist or has expired")
	// ErrTooManySessions is returned when the live session limit is reached.
	ErrTooManySessions = errors.New("too many persistent terminal sessions")
	// ErrManagerClosed is returned after Shutdown.
	ErrManagerClosed = errors.New("session manager is shut down")
)

Functions

func IsSignalCommand

func IsSignalCommand(cmd string) bool

IsSignalCommand reports whether the command string represents a terminal control signal.

Types

type ExecuteRequest

type ExecuteRequest struct {
	Key Key
	// Generation identifies the admitted target configuration. A session
	// created under a different generation is closed and replaced so that
	// configuration or credential changes are never bypassed by reuse.
	// Generation is compared by equality only; callers must ensure that no
	// requests carrying a stale generation are admitted after a configuration
	// update (enforced by the state.Coordinator update barrier).
	Generation string
	Dialect    ssh.LaunchDialect
	Dial       func(ctx context.Context) (ports.PTYSession, error)
	Command    string
	Cwd        string
	Timeout    time.Duration
}

ExecuteRequest describes a command, signal, or poll operation on a persistent session.

type ExecuteResult

type ExecuteResult struct {
	Output     string
	ExitCode   *uint32
	State      State
	Truncated  bool
	NewSession bool
}

ExecuteResult contains incremental terminal output and completion state.

type Key

type Key struct {
	Scope       string
	NodeID      string
	SessionName string
}

Key uniquely identifies a persistent terminal session within an isolation scope.

type Manager

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

Manager maintains active PTY sessions indexed by Key.

func NewManager

func NewManager(idleTTL time.Duration) *Manager

NewManager creates a persistent PTY session manager with the given idle TTL.

func (*Manager) Execute

func (m *Manager) Execute(ctx context.Context, req ExecuteRequest) (ExecuteResult, error)

Execute runs a command, sends a control signal, or polls incremental output on the named session.

func (*Manager) Shutdown

func (m *Manager) Shutdown(ctx context.Context) error

Shutdown closes all active PTY sessions and joins the idle reaper.

func (*Manager) SweepIdle

func (m *Manager) SweepIdle(now time.Time)

SweepIdle closes and removes any sessions that have had no client activity longer than idleTTL (2*idleTTL while a command is still running) or have exceeded maxSessionLifetime.

type State

type State int

State describes what is known about the command after an Execute call.

const (
	// StateIdle means no command was in flight (plain poll or signal).
	StateIdle State = iota
	// StateCompleted means the completion sentinel was observed; ExitCode is set.
	StateCompleted
	// StateRunning means the wait window elapsed before completion.
	StateRunning
	// StateLost means the session ended before the completion sentinel was
	// observed; the command outcome is unknown.
	StateLost
)

Jump to

Keyboard shortcuts

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