acpexec

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: Apache-2.0, Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package acpexec spawns a subprocess and wires its stdin/stdout together as a single io.ReadWriteCloser, the transport shape libacp's connections expect.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LockedBuffer

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

LockedBuffer is a concurrency-safe io.Writer around a bytes.Buffer, for use with WithStderr when a caller's String() read races the subprocess reader goroutine's writes.

func (*LockedBuffer) String

func (b *LockedBuffer) String() string

String returns everything written so far.

func (*LockedBuffer) Write

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

type Option

type Option func(*config)

Option configures Spawn. See WithStderr and WithKillGrace.

func WithKillGrace

func WithKillGrace(d time.Duration) Option

WithKillGrace overrides how long Close waits for the subprocess to exit on its own (after closing its stdin) before it escalates to Process.Kill; the default is 5 seconds.

func WithStderr

func WithStderr(w io.Writer) Option

WithStderr forwards the subprocess's stderr to w as it's written, instead of the default (io.Discard); use a *LockedBuffer if the caller reads w's contents from a different goroutine than the one writing it.

type Process

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

Process is a spawned subprocess wired up as an io.ReadWriteCloser: Read pulls from its stdout, Write pushes to its stdin, Close begins shutdown.

func Spawn

func Spawn(ctx context.Context, cmd *exec.Cmd, opts ...Option) (*Process, error)

Spawn starts cmd (claiming its Stdin/Stdout via exec.Cmd.StdinPipe/StdoutPipe, which callers must not set already) and returns it as a Process, tearing it down exactly as Close would if ctx is cancelled before the subprocess exits.

func (*Process) Close

func (p *Process) Close() error

func (*Process) Read

func (p *Process) Read(b []byte) (int, error)

Read reads from the subprocess's stdout, returning io.EOF like any closed pipe once the subprocess exits (on its own, or via Close/ctx cancellation).

func (*Process) Write

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

Write writes to the subprocess's stdin.

type Supervisor

type Supervisor struct {
	// Command builds a fresh *exec.Cmd for each (re)start; required, since an
	// exec.Cmd cannot be reused once started.
	Command func(ctx context.Context) *exec.Cmd

	// MaxRestarts caps how many times Serve respawns after the first attempt;
	// zero means one attempt with no restarts.
	MaxRestarts int

	// Backoff returns the delay before restart attempt n (1-based, nil means
	// no delay); Serve honors ctx cancellation while waiting.
	Backoff func(attempt int) time.Duration

	// OnRestart, if set, is called just before each genuine restart with the
	// 1-based restart number and the triggering error, but not for a startup
	// or non-retryable error (check libacp.IsStartupError).
	OnRestart func(attempt int, cause error)

	// SpawnOptions are forwarded to Spawn on every attempt (e.g. WithStderr).
	SpawnOptions []Option
}

Supervisor keeps an agent subprocess alive across transient crashes by respawning it with backoff and re-running a caller-supplied session. A startup error (IsStartupError) is never retried.

func (*Supervisor) Serve

func (s *Supervisor) Serve(ctx context.Context, session func(ctx context.Context, proc *Process, attempt int) error) error

Serve spawns the agent and runs session against each live Process, passing a zero-based attempt counter, until session succeeds, ctx is cancelled, or a failure that is not worth retrying (per libacp.IsRetryableError, up to MaxRestarts) occurs.

Jump to

Keyboard shortcuts

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