startup

package
v0.12.49 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package startup provides process startup resilience: EADDRINUSE recovery, pre-flight port cleanup, URL matcher setup, and auto-restart registration.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DetectEADDRINUSE

func DetectEADDRINUSE(output string) int

DetectEADDRINUSE checks process output for EADDRINUSE errors. Returns the port number if found, 0 otherwise.

func ExtractPortFromCommand

func ExtractPortFromCommand(command string, args []string) int

ExtractPortFromCommand extracts a port number from a command and its arguments.

func ExtractPortFromProxyConfig

func ExtractPortFromProxyConfig(proxyConfig *config.ProxyConfig) int

ExtractPortFromProxyConfig gets the expected port from a proxy configuration.

func GetExpectedPortsForScript

func GetExpectedPortsForScript(
	scriptName string,
	script *config.ScriptConfig,
	proxyConfigs map[string]*config.ProxyConfig,
	projectPath string,
	command string,
	args []string,
) []int

GetExpectedPortsForScript determines the ports a script uses. Checks: explicit config ports, linked proxy, command args, package.json.

func LastLines

func LastLines(text string, n int) string

LastLines returns the last n non-empty lines from text.

func WaitForPortFree

func WaitForPortFree(port int, timeout time.Duration)

WaitForPortFree polls until the given port is free or timeout expires. Checks both IPv4 and IPv6 loopback since the process may bind on either.

Types

type AutoRestartConfig

type AutoRestartConfig struct {
	Enabled       bool          // Whether auto-restart is enabled
	MaxRestarts   int           // Max restarts within window (0 = unlimited)
	RestartWindow time.Duration // Time window for restart limit (default 1 minute)
	RestartDelay  time.Duration // Delay before restart (default 1 second)
	OnlyOnError   bool          // Only restart if exit code != 0
}

AutoRestartConfig holds auto-restart settings for a process.

func DefaultAutoRestartConfig

func DefaultAutoRestartConfig() AutoRestartConfig

DefaultAutoRestartConfig returns sensible defaults. Auto-restart is disabled by default -- users restart manually from the overlay or via MCP tools. Enable explicitly in .agnt.kdl with `auto-restart true`.

type AutoRestarterView

type AutoRestarterView interface {
	Register(processID string, config AutoRestartConfig, command string, args []string, env []string, expectedPorts []int, projectPath, workingDir string)
}

AutoRestarterView exposes the auto-restart registration needed by startup resilience.

type Deps

type Deps interface {
	ProcessManager() ProcessManagerView
	ScriptRegistry() ScriptRegistryView
	URLTracker() URLTrackerView
	AutoRestarter() AutoRestarterView
	StartupLog() *startuplog.StartupLogStore
}

Deps provides the daemon capabilities needed by startup resilience.

type ProcessManagerView

type ProcessManagerView interface {
	StartOrReuse(ctx context.Context, cfg process.ProcessConfig) (*process.StartOrReuseResult, error)
	StopProcess(ctx context.Context, proc *process.ManagedProcess) error
	RemoveByPath(id string, projectPath string) bool
	KillProcessByPort(ctx context.Context, port int) ([]int, error)
}

ProcessManagerView exposes the process manager operations needed by startup resilience.

type ScriptRegistryView

type ScriptRegistryView interface {
	Register(name, projectPath string, cfg *script.Config) (*script.Entry, error)
	Get(name, projectPath string) (*script.Entry, bool)
	GetByProcessID(processID string) (*script.Entry, bool)
}

ScriptRegistryView exposes the script registry operations needed by startup resilience.

type StartScriptConfig

type StartScriptConfig struct {
	// ProcessID is the unique identifier for the process
	ProcessID string
	// ProjectPath is the root project directory (where .agnt.kdl is located).
	// Used for session association and proxy event handling.
	// If empty, defaults to WorkingDir.
	ProjectPath string
	// WorkingDir is the working directory for the process (may differ from ProjectPath
	// when script has cwd configured)
	WorkingDir string
	// Command is the executable to run
	Command string
	// Args are the command arguments
	Args []string
	// Env are environment variables (KEY=VALUE format)
	Env []string
	// ExpectedPorts are the ports the process is expected to use (for pre-flight cleanup and EADDRINUSE recovery)
	ExpectedPorts []int
	// URLMatchers are patterns for URL detection in output
	URLMatchers []string
	// AutoRestart enables automatic restart on process exit
	AutoRestart bool
	// AutoRestartConfig is optional custom restart configuration.
	// If nil, default config is used.
	AutoRestartConfig *AutoRestartConfig
	// StripProcessPrefix extracts the script name from a process ID.
	// If nil, the process ID is used as-is.
	StripProcessPrefix func(processID string) string
}

StartScriptConfig holds configuration for starting a script/process. This unified config is used by both autostartScript and hub handlers to ensure consistent behavior (EADDRINUSE recovery, URL tracking, auto-restart).

type StartScriptResult

type StartScriptResult struct {
	Process *process.ManagedProcess
	Reused  bool // True if an existing process was reused
}

StartScriptResult holds the result of starting a script.

func StartScript

func StartScript(ctx context.Context, d Deps, cfg StartScriptConfig) (*StartScriptResult, error)

StartScript starts a script/process with unified behavior:

  • Pre-flight port cleanup and EADDRINUSE recovery
  • URL matcher setup for proxy auto-creation
  • Auto-restart registration for crash recovery

This is the canonical way to start processes. Both autostartScript and hub handlers should use this.

type StartupError

type StartupError struct {
	ProcessID string
	Port      int
	ErrorType string // "EADDRINUSE", "start_failed", "cleanup_failed", "retry_failed", "startup_failed"
	Message   string
	Output    string // Last lines of process output (for user-facing diagnostics)
	Retried   bool
}

StartupError represents a startup failure with recovery information.

func StartScriptWithRetry

func StartScriptWithRetry(
	ctx context.Context,
	d Deps,
	processID string,
	projectPath string,
	workingDir string,
	command string,
	args []string,
	env []string,
	expectedPorts []int,
) (*process.ManagedProcess, *StartupError)

StartScriptWithRetry starts a script with automatic EADDRINUSE recovery. It monitors the process output for startup failures and retries once after cleanup. projectPath is the root project directory (for session association). workingDir is the actual working directory for the process (may differ when script has cwd).

This is exported for use by hub_proc.go and process_autorestart.go which call it directly to avoid re-registering for auto-restart.

func (*StartupError) Error

func (e *StartupError) Error() string

type URLTrackerView

type URLTrackerView interface {
	SetURLMatchers(processID string, matchers []string)
	ClearProcess(processID string)
}

URLTrackerView exposes the URL tracker operations needed by startup resilience.

Jump to

Keyboard shortcuts

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