tasks

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 15 Imported by: 0 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 tasks provides the daemon's background task management and execution, used to run workloads (notably the `bash` tool) that may outlive the synchronous wait window of a single tool call.

Core Types

A Task is a smart unit of work defined by a Func workload plus metadata (ID, Kind, Title, Command, and a Wait window). It owns its execution lifecycle directly via Task.Run, Task.Kill, Task.WriteInput, and Task.Cancel, and implements io.Writer for streaming process output.

A Store (created via New) is an interface that manages the task registry for a session and exposes Store.Add, Store.Get, Store.List, and Store.Close.

Foreground vs. Background

Task.Run consults Task.Wait to decide how long to block before returning:

  • When Wait is zero or negative, the task is backgrounded immediately and Run returns nil so the caller can proceed without waiting.
  • When Wait is positive, Run blocks until the task finishes, the caller's context is canceled, or the timer elapses. If the timer elapses first, the task is backgrounded and Run returns nil; otherwise Run returns the task's terminal error.

Backgrounded tasks are the only ones returned by Store.List, which returns them sorted by start time ascending, then ID ascending.

Lifecycle & Status

A task transitions through github.com/masterkeysrd/tasksmith/api.TaskStatus values: pending (default) → running → one of completed, failed, or canceled. A task is marked canceled when it is explicitly killed or when its context is canceled; a nonzero exit code or a non-nil error otherwise yields failed. Callers inspect terminal state via Task.Snapshot, Task.Status, Task.ExitCode, Task.Err, Task.Duration, and Task.LogPath.

Output Capture

Every write to the task (Task.Write) is fanned out to three sinks:

  1. An in-memory rolling buffer capped at MaxLogMemBytes (64 KB), used for fast recent-history access and live tailing.
  2. A disk log at `<OutputsDir>/<id>.log` capped at MaxLogDiskBytes (16 MB); once the cap is reached a truncation marker is appended and no further bytes are written.
  3. Live output subscribers registered via Task.SubscribeOutput, each receiving streamed chunks over a buffered channel that is closed when the task finishes.

Task.Tail returns the last N lines from the disk log (falling back to the in-memory buffer), and Task.SubscribeOutput returns the initial buffered content alongside the live channel and an unsubscribe function.

Input & Cancellation

Each running task is given a stdin pipe, but only tasks declaring Task.AcceptsInput — whose workload actually reads that pipe — accept interactive data. Task.WriteInput streams data into the pipe while such a task is running, returns ErrInputUnsupported for tasks that fixed their standard input at launch (for example custom tools, which receive a JSON payload or nothing), and ErrNotRunning for tasks that are not running. Task.Cancel closes the stdin pipe and cancels the task's context; Task.Kill validates that the task is currently running or pending before canceling; and Store.Close cancels every tracked task.

Integration

github.com/masterkeysrd/tasksmith/internal/daemon/session.Session creates one Store per session (via New) with the session's background context, per-session outputs directory, and an update callback that publishes task transitions. The builtin `bash` and `tasks` tools (github.com/masterkeysrd/tasksmith/internal/daemon/tools/builtin) consume the Store to register tasks and to list, inspect, feed, and kill background tasks.

Index

Constants

View Source
const (
	MaxLogDiskBytes = 16 * 1024 * 1024 // 16 MB cap on task log file
	MaxLogMemBytes  = 64 * 1024        // 64 KB rolling window for in-memory buffer
)

Variables

View Source
var (
	ErrTaskNotFound     = errors.New("tasks: task not found")
	ErrNotRunning       = errors.New("tasks: task is not running")
	ErrInputUnsupported = errors.New("tasks: task does not read standard input")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	Context    context.Context
	OutputsDir string
	OnUpdate   func(api.Task)
}

type Func

type Func func(ctx context.Context, output io.Writer, stdin io.Reader) (exitCode int, err error)

Func is the execution workload of a Task. ctx is the task's execution context; output receives combined stdout/stderr; stdin supplies input.

type Store

type Store interface {
	Add(task *Task) error
	Get(id string) (*Task, bool)
	List() []api.Task
	Close() error
}

Store manages the background task registry for a session.

func New

func New(cfg Config) Store

type Task

type Task struct {
	ID      string
	Kind    string
	Title   string
	Command string
	Wait    time.Duration
	Fn      Func
	// AcceptsInput declares that the task's workload consumes its stdin
	// reader interactively, so [Task.WriteInput] may feed it data. Tasks
	// that fix their standard input at launch (a payload, or nothing)
	// leave it false, and WriteInput rejects them instead of writing into
	// a pipe no one reads.
	AcceptsInput bool
	// contains filtered or unexported fields
}

func (*Task) Backgrounded

func (t *Task) Backgrounded() bool

func (*Task) Cancel

func (t *Task) Cancel()

func (*Task) Done

func (t *Task) Done() <-chan struct{}

func (*Task) Duration

func (t *Task) Duration() time.Duration

func (*Task) Err

func (t *Task) Err() error

func (*Task) ExitCode

func (t *Task) ExitCode() int

func (*Task) Kill

func (t *Task) Kill() error

func (*Task) LogPath

func (t *Task) LogPath() string

func (*Task) Run

func (t *Task) Run(ctx context.Context) error

func (*Task) Snapshot

func (t *Task) Snapshot() api.Task

func (*Task) Status

func (t *Task) Status() api.TaskStatus

func (*Task) SubscribeOutput

func (t *Task) SubscribeOutput() (<-chan string, string, func())

func (*Task) Tail

func (t *Task) Tail(maxLines int) (string, error)

func (*Task) Write

func (t *Task) Write(p []byte) (n int, err error)

func (*Task) WriteInput

func (t *Task) WriteInput(data string) error

Jump to

Keyboard shortcuts

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