tasks

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package tasks manages long-running background shell commands and daemons with bounded memory+disk log streaming, stdin interaction, expect-regex waiting, session isolation, and recursive process-tree cleanup.

Index

Constants

View Source
const (
	DefaultMaxConcurrent   = 16
	DefaultMaxRetained     = 64
	DefaultMaxMemLogBytes  = 512 * 1024       // 512 KB rolling in-memory buffer per task
	DefaultMaxFileLogBytes = 50 * 1024 * 1024 // 50 MB max persisted log per task
	DefaultNormalWaitMs    = 5000
	DefaultDaemonWaitMs    = 1500
	MaxStartWaitMs         = 30000
	DefaultManageWaitMs    = 15000
	MaxManageWaitMs        = 60000
	DefaultNormalTimeout   = 30 * time.Minute
	DefaultLogTailLines    = 80
	MaxInlineLogBytes      = 24 * 1024
)

Variables

This section is empty.

Functions

func DefaultShellName

func DefaultShellName() string

DefaultShellName returns the default shell dialect for the current OS.

func FormatManageResult

func FormatManageResult(res *ManageResult) string

FormatManageResult renders a compact, LLM-friendly summary of a `task` tool invocation.

func FormatStartResult

func FormatStartResult(res *StartResult) string

FormatStartResult renders a compact, LLM-friendly summary of a `spawn` tool invocation.

func MergeEnv

func MergeEnv(extra map[string]string) []string

MergeEnv overlays extra environment variables on os.Environ(). On Windows, variable names are compared case-insensitively.

func ResolveShell

func ResolveShell(name string) (string, []string, error)

ResolveShell maps a shell dialect name to its executable path and command-invocation flags.

Types

type ManageRequest

type ManageRequest struct {
	Action        string `json:"action"` // list | status | logs | wait | input | kill | kill_all
	TaskID        string `json:"taskId,omitempty"`
	Tail          int    `json:"tail,omitempty"`
	Offset        *int64 `json:"offset,omitempty"`
	WaitMs        int    `json:"waitMs,omitempty"`
	Expect        string `json:"expect,omitempty"`
	Input         string `json:"input,omitempty"`
	AppendNewline *bool  `json:"appendNewline,omitempty"`
	CloseStdin    bool   `json:"closeStdin,omitempty"`
	SessionID     string `json:"-"`
}

ManageRequest operates on existing background tasks.

type ManageResult

type ManageResult struct {
	Action       string        `json:"action"`
	Tasks        []TaskSummary `json:"tasks,omitempty"`
	Task         *TaskSummary  `json:"task,omitempty"`
	Output       string        `json:"output,omitempty"`
	NextOffset   int64         `json:"nextOffset,omitempty"`
	TotalBytes   int64         `json:"totalBytes,omitempty"`
	Truncated    bool          `json:"truncated,omitempty"`
	Matched      bool          `json:"matched,omitempty"`
	MatchLine    string        `json:"matchLine,omitempty"`
	TimedOut     bool          `json:"timedOut,omitempty"`
	BytesWritten int           `json:"bytesWritten,omitempty"`
	KilledCount  int           `json:"killedCount,omitempty"`
}

ManageResult is the unified response for all task management actions.

type Manager

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

Manager coordinates background shell tasks and daemons.

func NewManager

func NewManager(dataDir string) *Manager

NewManager creates a TaskManager storing per-task logs under <dataDir>/tasks when dataDir != "".

func (*Manager) Close

func (m *Manager) Close()

Close shuts down the manager, terminates all running background process trees, and removes per-task log files.

func (*Manager) CloseSession

func (m *Manager) CloseSession(sessionID string) int

CloseSession kills all running tasks created by sessionID (used on HTTP DELETE /mcp).

func (*Manager) Manage

func (m *Manager) Manage(ctx context.Context, req ManageRequest) (*ManageResult, error)

Manage executes list, status, logs, wait, input, kill, or kill_all actions.

func (*Manager) SetEventHub

func (m *Manager) SetEventHub(hub *events.Hub)

SetEventHub attaches an optional real-time event hub for streaming live task output.

func (*Manager) Start

func (m *Manager) Start(ctx context.Context, pol *policy.Engine, req StartRequest) (*StartResult, error)

Start validates policy, launches a shell command, waits up to waitMs, and hands off to background if still running.

type StartRequest

type StartRequest struct {
	Command    string            `json:"command"`
	Cwd        string            `json:"cwd,omitempty"`
	Shell      string            `json:"shell,omitempty"`
	Env        map[string]string `json:"env,omitempty"`
	Daemon     bool              `json:"daemon,omitempty"`
	WaitMs     *int              `json:"waitMs,omitempty"`
	TimeoutSec *int              `json:"timeoutSec,omitempty"`
	Name       string            `json:"name,omitempty"`
	SessionID  string            `json:"-"`
}

StartRequest describes a command to run with hybrid sync-wait -> background handoff.

type StartResult

type StartResult struct {
	TaskID       string `json:"taskId"`
	Name         string `json:"name,omitempty"`
	PID          int    `json:"pid"`
	Status       Status `json:"status"`
	Daemon       bool   `json:"daemon"`
	Backgrounded bool   `json:"backgrounded"`
	Command      string `json:"command"`
	Cwd          string `json:"cwd"`
	ExitCode     *int   `json:"exitCode,omitempty"`
	DurationMs   int64  `json:"durationMs"`
	Output       string `json:"output"`
	LogFile      string `json:"logFile,omitempty"`
	NextOffset   int64  `json:"nextOffset"`
}

StartResult reports the initial outcome of starting a command.

type Status

type Status string

Status represents a background task's lifecycle state.

const (
	StatusRunning   Status = "running"
	StatusCompleted Status = "completed"
	StatusFailed    Status = "failed"
	StatusKilled    Status = "killed"
	StatusTimedOut  Status = "timed_out"
)

type TaskSummary

type TaskSummary struct {
	TaskID     string `json:"taskId"`
	Name       string `json:"name,omitempty"`
	PID        int    `json:"pid"`
	Status     Status `json:"status"`
	Daemon     bool   `json:"daemon"`
	Command    string `json:"command"`
	Cwd        string `json:"cwd"`
	ExitCode   *int   `json:"exitCode,omitempty"`
	StartedAt  string `json:"startedAt"`
	EndedAt    string `json:"endedAt,omitempty"`
	DurationMs int64  `json:"durationMs"`
	TotalBytes int64  `json:"totalBytes"`
	LogFile    string `json:"logFile,omitempty"`
}

TaskSummary is a compact metadata view returned by list/status.

Jump to

Keyboard shortcuts

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