runtime

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 14 Imported by: ? 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 runtime claims queued runs, drives the agent and finishes runs.

Index

Constants

View Source
const InterruptedMessage = "The run was interrupted (the runtime stopped). Send your message again to continue."

InterruptedMessage is the RUN_ERROR message of interrupted runs.

Variables

View Source
var (
	// ErrCancelled is the cancel cause of a run cancelled by the user.
	ErrCancelled = errors.New("run cancelled by the user")
)

Functions

This section is empty.

Types

type ErrorReporter

type ErrorReporter interface {
	CaptureError(err error)
	CapturePanic(recovered any)
}

ErrorReporter receives what the runtime logs as internal failures.

type Executor

type Executor interface {
	Run(ctx context.Context, in agent.Input, w eventlog.Writer) (agent.Result, error)
}

Executor runs the agent loop of one run.

type Finish

type Finish struct {
	Run    *store.Run
	Status string
	Result agent.Result
	// contains filtered or unexported fields
}

Finish describes a run that is ending.

func (*Finish) AddUsage

func (f *Finish) AddUsage(u agui.Usage)

AddUsage counts a hook's model call in the run's RUN_FINISHED usage (A-25).

func (*Finish) Emit

func (f *Finish) Emit(ctx context.Context, ev agui.Event) error

Emit appends an event to the run's log (before its terminal event).

type Hook

type Hook interface {
	// Finishing runs after the run's work, before its final status is recorded;
	// it may emit events. The returned func (optional) runs once the status is
	// recorded, before the terminal event (e.g. to start the next run).
	Finishing(ctx context.Context, f *Finish) func(ctx context.Context)
}

Hook runs at the end of every run whose log this runtime still owns (harness v2: generated titles, goal evaluation).

type Job

type Job struct {
	Name  string
	Every time.Duration
	Run   func(ctx context.Context) error
}

Job is a periodic maintenance job run by the sweeper under a Redis lock (work:job:<Name>:lock, held for Every), like the retention purge.

type Locks

type Locks interface {
	// SetNX stores key=value for ttl unless the key exists.
	SetNX(ctx context.Context, key, value string, ttl time.Duration) (bool, error)
	// Set stores key=value for ttl.
	Set(ctx context.Context, key, value string, ttl time.Duration) error
	// Del removes key.
	Del(ctx context.Context, key string) (bool, error)
	// Exists reports whether key is set.
	Exists(ctx context.Context, key string) (bool, error)
}

Locks is the small shared state the runtime needs to cooperate with other runtimes: the sweeper and job locks, and the liveness marks of runs. The Redis implementation is redisx.Coordinator; nil means a single process.

type Options

type Options struct {
	ID string
	// Reporter receives internal errors and panics (an error tracker such as Sentry); nil: logs only.
	Reporter      ErrorReporter
	Concurrency   int
	Heartbeat     time.Duration // default 10 s
	SweepEvery    time.Duration // default 30 s
	QueuedAge     time.Duration // default 30 s
	StaleAge      time.Duration // default 60 s
	ShutdownGrace time.Duration // default 15 s: in-flight runs may finish normally
	InterruptWait time.Duration // default 8 s: then interrupted runs get this long to write RUN_ERROR
	FinishRetry   time.Duration // default 30 s: retry budget for recording the final status
	ReadBlock     time.Duration // default 2 s (bounds shutdown latency: a blocking Redis read ignores ctx cancel)
	// ConsumerIdle: queue consumers idle for longer, without pending entries,
	// are deleted by the sweeper (default 1 h); entries pending for longer than
	// QueuedAge on a consumer are reclaimed.
	ConsumerIdle time.Duration
	// StuckAfter: Health fails when the queue loop has not completed a read for
	// that long while it has free slots (default 60 s, at least 5 × ReadBlock).
	StuckAfter time.Duration
	// Hooks run at the end of each run, in order, before its terminal event
	// (end-of-run artifact snapshot first, then titles, goals).
	Hooks []Hook
	// Purger enforces the retention (purge of deleted threads and their blobs),
	// run by the sweeper at most every PurgeEvery (default 1 h) across runtimes.
	Purger     Purger
	PurgeEvery time.Duration
	// Jobs are periodic maintenance jobs of the sweeper (memory consolidation),
	// each run at most every Job.Every across runtimes.
	Jobs []Job
}

Options tunes the runtime.

type Purger

type Purger interface {
	Purge(ctx context.Context) error
}

Purger hard-deletes data past its retention.

type RunQueue

type RunQueue interface {
	Enqueue(ctx context.Context, runID string) error
	EnsureGroup(ctx context.Context) error
	Read(ctx context.Context, consumer string, count int, block time.Duration) ([]queue.Message, error)
	Ack(ctx context.Context, streamID string) error
	Reclaim(ctx context.Context, consumer string, minIdle time.Duration) ([]queue.Message, error)
	PruneConsumers(ctx context.Context, keep string, idle time.Duration) (int, error)
}

RunQueue is the queue side used by the runtime.

type Runtime

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

Runtime is a run worker.

func New

func New(opts Options, st store.Full, log eventlog.Log, q RunQueue, bus queue.Bus, exec Executor, locks Locks) *Runtime

New builds a runtime. locks is used for the sweeper lock and run liveness (nil: single process).

func (*Runtime) Health

func (r *Runtime) Health() error

Health reports whether the runtime consumes its queue: an error when the consumer group could not be created yet, or when the queue loop has free slots but no read completed for StuckAfter (stuck connection, Redis down).

func (*Runtime) Process

func (r *Runtime) Process(ctx context.Context, runID string) error

Process claims a run and executes it synchronously. A run that is no longer queued is ignored (nil error).

func (*Runtime) Run

func (r *Runtime) Run(ctx context.Context) error

Run consumes the queue until ctx is done, then drains in-flight runs: they get ShutdownGrace to finish, then are cancelled and finished as interrupted.

func (*Runtime) Sweep

func (r *Runtime) Sweep(ctx context.Context)

Sweep re-enqueues stale queued runs and interrupts runs whose runtime stopped heartbeating.

Jump to

Keyboard shortcuts

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