subprocess

package
v0.2.12 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// MaxMessageSize bounds the encoded JSON-RPC message size accepted by the transport.
	MaxMessageSize = 10 * 1024 * 1024
)

Variables

This section is empty.

Functions

func ConfigureCommandProcessGroup added in v0.2.11

func ConfigureCommandProcessGroup(cmd *exec.Cmd) error

ConfigureCommandProcessGroup prepares cmd for process-tree lifecycle control.

func ForceTerminateCommandProcessTree added in v0.2.11

func ForceTerminateCommandProcessTree(cmd *exec.Cmd) error

ForceTerminateCommandProcessTree forcefully terminates cmd and its children.

func MergeEnvironment

func MergeEnvironment(base map[string]string, extra map[string]string) []string

MergeEnvironment returns the current environment with base and extra assignments appended.

func NormalizeWaitError

func NormalizeWaitError(prefix string, err error) error

NormalizeWaitError wraps subprocess wait errors with a caller-provided prefix.

func TerminateCommandProcessTree added in v0.2.11

func TerminateCommandProcessTree(cmd *exec.Cmd) error

TerminateCommandProcessTree requests graceful termination for cmd and its children.

func ValidateInitializeResponse

func ValidateInitializeResponse(request InitializeRequest, response InitializeResponse) error

ValidateInitializeResponse checks the negotiated protocol version and capability subset.

Types

type InitializeCanceledError added in v0.2.11

type InitializeCanceledError struct {
	Cause error
}

InitializeCanceledError reports context cancellation during the initialize handshake.

func (*InitializeCanceledError) Error added in v0.2.11

func (e *InitializeCanceledError) Error() string

func (*InitializeCanceledError) Unwrap added in v0.2.11

func (e *InitializeCanceledError) Unwrap() error

type InitializeRequest

type InitializeRequest struct {
	ProtocolVersion           string   `json:"protocol_version"`
	SupportedProtocolVersions []string `json:"supported_protocol_versions,omitempty"`
	GrantedCapabilities       []string `json:"granted_capabilities,omitempty"`
}

InitializeRequest describes the protocol version and capability contract offered by the host.

type InitializeResponse

type InitializeResponse struct {
	ProtocolVersion      string   `json:"protocol_version"`
	AcceptedCapabilities []string `json:"accepted_capabilities,omitempty"`
}

InitializeResponse describes the negotiated protocol version and accepted capability contract.

func Initialize

func Initialize(
	ctx context.Context,
	transport *Transport,
	requestID json.RawMessage,
	request InitializeRequest,
) (InitializeResponse, error)

Initialize sends an initialize request and validates the selected protocol version.

type LaunchConfig

type LaunchConfig struct {
	Command           []string
	Env               []string
	WorkingDir        string
	WaitDelay         time.Duration
	WaitErrorPrefix   string
	SignalGracePeriod time.Duration
}

LaunchConfig describes how to start and manage a subprocess.

type LockedBuffer

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

LockedBuffer is a concurrency-safe stderr capture buffer.

func (*LockedBuffer) String

func (b *LockedBuffer) String() string

String returns the current buffer contents as a string.

func (*LockedBuffer) Write

func (b *LockedBuffer) Write(p []byte) (int, error)

Write appends bytes to the buffer.

type Message

type Message struct {
	JSONRPC string           `json:"jsonrpc"`
	ID      *json.RawMessage `json:"id,omitempty"`
	Method  string           `json:"method,omitempty"`
	Params  json.RawMessage  `json:"params,omitempty"`
	Result  json.RawMessage  `json:"result,omitempty"`
	Error   *RequestError    `json:"error,omitempty"`
}

Message is a line-framed JSON-RPC 2.0 envelope.

type Process

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

Process manages a spawned subprocess and its lifetime.

func Launch

func Launch(ctx context.Context, cfg LaunchConfig) (*Process, error)

Launch starts a subprocess with the provided configuration.

func (*Process) CloseInput

func (p *Process) CloseInput()

CloseInput closes the subprocess stdin pipe.

func (*Process) Done

func (p *Process) Done() <-chan struct{}

Done returns a channel that closes when the subprocess exits.

func (*Process) Forced

func (p *Process) Forced() bool

Forced reports whether shutdown escalated beyond cooperative stdin closure.

func (*Process) Kill

func (p *Process) Kill() error

Kill force-terminates the subprocess immediately.

func (*Process) Shutdown

func (p *Process) Shutdown(timeout time.Duration) error

Shutdown attempts cooperative exit first, then escalates to SIGTERM and SIGKILL.

func (*Process) StderrBuffer

func (p *Process) StderrBuffer() *LockedBuffer

StderrBuffer returns the subprocess stderr capture buffer.

func (*Process) Stdin

func (p *Process) Stdin() io.WriteCloser

Stdin returns the subprocess stdin pipe.

func (*Process) Stdout

func (p *Process) Stdout() io.ReadCloser

Stdout returns the subprocess stdout pipe.

func (*Process) Wait

func (p *Process) Wait() error

Wait blocks until the subprocess exits and returns the normalized wait error.

type RequestError

type RequestError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Data    any    `json:"data,omitempty"`
}

RequestError represents a JSON-RPC error object.

func NewInternalError

func NewInternalError(data any) *RequestError

NewInternalError creates a JSON-RPC internal error.

func NewInvalidParams

func NewInvalidParams(data any) *RequestError

NewInvalidParams creates a JSON-RPC invalid params error.

func NewInvalidRequest

func NewInvalidRequest(data any) *RequestError

NewInvalidRequest creates a JSON-RPC invalid request error.

func NewMethodNotFound

func NewMethodNotFound(method string) *RequestError

NewMethodNotFound creates a JSON-RPC method-not-found error.

func NewParseError

func NewParseError(data any) *RequestError

NewParseError creates a JSON-RPC parse error.

func (*RequestError) Error

func (e *RequestError) Error() string

Error returns a stable human-readable JSON-RPC error string.

type Transport

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

Transport reads and writes line-delimited JSON-RPC messages.

func NewTransport

func NewTransport(reader io.Reader, writer io.Writer) *Transport

NewTransport constructs a line-delimited JSON-RPC transport.

func (*Transport) Close added in v0.2.11

func (t *Transport) Close() error

Close releases transport endpoints that expose close semantics.

func (*Transport) ReadMessage

func (t *Transport) ReadMessage() (Message, error)

ReadMessage reads the next non-empty JSON-RPC message from the transport.

func (*Transport) WriteMessage

func (t *Transport) WriteMessage(message Message) error

WriteMessage writes one JSON-RPC message and appends a single trailing newline.

Jump to

Keyboard shortcuts

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