Documentation
¶
Overview ¶
Package runtime claims queued runs, drives the agent and finishes runs.
Index ¶
Constants ¶
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 ¶
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 ¶
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.
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 ¶
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 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 ¶
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 ¶
Process claims a run and executes it synchronously. A run that is no longer queued is ignored (nil error).