routine

package
v0.3.10 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package routine implements named, managed headless agent runs.

A routine is a directory under ~/.config/task/routines/<name>/ containing a prompt.md (the agent prompt, with optional frontmatter for model/project/ timeout) and an optional env.sh sourced before each run for secrets and fail-fast checks. Routines have no scheduler: anything — launchd, cron, a shell — triggers a run with `ty run <name>`. TaskYou owns the run itself: state, logs, history, and failure alerting.

Index

Constants

View Source
const (
	// DefaultModel keeps unattended runs on a cheap model unless the routine
	// explicitly opts into a bigger one.
	DefaultModel = "sonnet"

	// DefaultTimeout bounds a hung headless run so an unattended scheduler
	// invocation can't wedge forever.
	DefaultTimeout = 30 * time.Minute
)

Variables

This section is empty.

Functions

func Delete

func Delete(name string) error

Delete removes the routine's definition directory and its state directory (including logs). Run history in the database is the caller's to prune via db.DeleteRoutineRuns.

func Exists

func Exists(name string) bool

Exists reports whether a routine directory with a prompt.md exists.

func LoadSchedules

func LoadSchedules(names []string) (map[string]*Schedule, error)

LoadSchedules resolves live schedules for many routines with one pass over the LaunchAgents dir and one crontab read (the TUI and list call this).

func RemoveSchedule

func RemoveSchedule(name string) (bool, error)

RemoveSchedule removes any ty-managed scheduler entry for the routine. Idempotent: removing a routine that isn't scheduled returns (false, nil).

func RenderSchedule

func RenderSchedule(name string, opts ScheduleOptions) (backend, content string, err error)

RenderSchedule returns the scheduler config that InstallSchedule would install, for --print and for tests.

func RoutinesDir

func RoutinesDir() string

RoutinesDir returns the directory routine definitions live in.

func StateDir

func StateDir(name string) string

StateDir returns the per-routine state directory. It is exported to the agent as ROUTINE_STATE_DIR and used as the run's working directory, so routines keep cross-run state (seen IDs, cursors) without inventing paths.

func ValidateName

func ValidateName(name string) error

ValidateName rejects names that aren't safe as path components.

Types

type Routine

type Routine struct {
	Name           string
	Dir            string        // ~/.config/task/routines/<name>
	Prompt         string        // prompt.md body with frontmatter stripped
	Model          string        // claude model (frontmatter "model", default sonnet)
	Project        string        // project for emitted/alert tasks (frontmatter "project")
	PermissionMode string        // frontmatter "permission-mode", default dangerous (headless runs can't answer prompts)
	Timeout        time.Duration // frontmatter "timeout" (Go duration), default 30m
	WorkDir        string        // frontmatter "dir": run cwd override (for repo context / project-scoped MCP); default = state dir
	Disabled       bool          // presence of a "disabled" file in Dir
}

Routine is a loaded routine definition.

func List

func List() ([]*Routine, error)

List loads every routine under RoutinesDir, sorted by name. Directories without a prompt.md are skipped; unparseable routines are returned as an error so a typo doesn't silently hide a routine from the list.

func Load

func Load(name string) (*Routine, error)

Load reads a routine definition from disk.

func Scaffold

func Scaffold(name string) (*Routine, error)

Scaffold creates a new routine directory with a template prompt.md.

func (*Routine) EnvPath

func (r *Routine) EnvPath() (string, bool)

EnvPath returns the routine's env.sh path and whether it exists.

func (*Routine) SetDisabled

func (r *Routine) SetDisabled(disabled bool) error

SetDisabled creates or removes the routine's disabled marker file.

type RunResult

type RunResult struct {
	RunID    int64
	Status   string
	ExitCode int
	Output   string
	LogPath  string
	Duration time.Duration
}

RunResult describes a finished routine run.

type Runner

type Runner struct {
	DB      *db.DB
	Emitter *events.Emitter

	// ClaudeBin overrides the agent binary (tests use a stub). Empty = "claude".
	ClaudeBin string
	// Stdout, when set, receives a live copy of the run output (in addition to
	// the log file) so `ty run` invoked by hand shows progress.
	Stdout io.Writer
}

Runner executes routines and records their runs.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context, rt *Routine) (*RunResult, error)

Run executes the routine and records the run. A non-nil error means the run itself failed (agent exit != 0, env.sh failure, or timeout); the run row and failure alerting have already been handled when it returns.

type Schedule

type Schedule struct {
	Backend string // "launchd" or "cron"
	Detail  string // human summary, e.g. "every 30m" or "0 8 * * *"
	Path    string // plist path (launchd) or "crontab" (cron)
}

Schedule describes a live OS-scheduler entry for a routine.

func InstallSchedule

func InstallSchedule(name string, opts ScheduleOptions) (*Schedule, error)

InstallSchedule registers the routine with the OS scheduler and returns the live Schedule. Re-installing replaces any previous ty-managed entry.

func ScheduledFor

func ScheduledFor(name string) (*Schedule, error)

ScheduledFor returns the routine's live ty-managed schedule, or nil if none exists. It reads the OS scheduler directly — there is no cached state to disagree with reality.

type ScheduleOptions

type ScheduleOptions struct {
	Every time.Duration // interval cadence
	Cron  string        // five-field cron expression
}

ScheduleOptions describes the requested cadence. Exactly one of Every or Cron must be set: intervals install a launchd agent on macOS (cron elsewhere), cron expressions always install a crontab line.

Jump to

Keyboard shortcuts

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