Documentation
¶
Overview ¶
Package jobs lets a tool go asynchronous.
Without it every tool is synchronous: a long crawl, build, render, or export blocks the whole run until it finishes, and a run that has to wait cannot also think. With it a tool calls jobs.From(ctx).Start(...), returns a job id immediately, and the model keeps working; the built-in job_list / job_status / job_wait / job_cancel tools observe and stop the work, and jobs that finished since the last turn are announced at the top of the next one.
Every job is fenced to the run that started it and cancelled when that run ends, so a job can neither outlive its owner nor be observed by another run.
Index ¶
- Variables
- type Job
- type JobSpec
- type JobState
- type JobStore
- type Launcher
- type LocalJobStore
- func (s *LocalJobStore) Cancel(owner, id string) error
- func (s *LocalJobStore) CancelAll(owner string)
- func (s *LocalJobStore) DrainCompleted(owner string) []Job
- func (s *LocalJobStore) Get(owner, id string) (Job, error)
- func (s *LocalJobStore) List(owner string) []Job
- func (s *LocalJobStore) Start(ctx context.Context, owner string, spec JobSpec) (Job, error)
- func (s *LocalJobStore) Wait(ctx context.Context, owner, id string) (Job, error)
- type Plugin
Constants ¶
This section is empty.
Variables ¶
var ErrJobNotFound = errors.New("agentcore: job not found")
ErrJobNotFound is returned for an unknown job, and for a job owned by another session — the two are deliberately indistinguishable so a job id is not an existence oracle across sessions.
var ToolNames = []string{"job_list", "job_status", "job_wait", "job_cancel"}
ToolNames are the tools this plugin contributes, for a consumer that wants to name them in a policy or a UI.
Functions ¶
This section is empty.
Types ¶
type Job ¶
type Job struct {
ID string `json:"id"`
Tool string `json:"tool"`
Label string `json:"label"`
State JobState `json:"state"`
// Result is the work's output once it succeeds, bounded by the store.
Result string `json:"result,omitempty"`
// Err is the failure message once it fails.
Err string `json:"error,omitempty"`
StartedAt time.Time `json:"started_at"`
EndedAt time.Time `json:"ended_at,omitempty"`
}
Job is one unit of background work.
type JobSpec ¶
type JobSpec struct {
// Tool is the tool that started the job (descriptive; used in notices).
Tool string
// Label is a short human description ("export 2.3M rows").
Label string
// Run performs the work. Its context is cancelled when the job is cancelled
// or the run ends. The returned string is the job's result.
Run func(ctx context.Context) (string, error)
}
JobSpec describes work to launch.
type JobStore ¶
type JobStore interface {
// Start launches fn and returns immediately with a Running job.
Start(ctx context.Context, owner string, spec JobSpec) (Job, error)
// Get returns one job, or ErrJobNotFound.
Get(owner, id string) (Job, error)
// List returns the owner's jobs, newest first.
List(owner string) []Job
// Cancel requests cancellation. Cancelling a finished job is a no-op.
Cancel(owner, id string) error
// Wait blocks until the job is terminal or the context ends, returning the
// job as of that moment.
Wait(ctx context.Context, owner, id string) (Job, error)
// DrainCompleted returns jobs that reached a terminal state since the last
// drain for this owner, clearing the pending-notice set.
DrainCompleted(owner string) []Job
// CancelAll stops every running job for an owner. The loop calls it when a
// run ends so background work cannot outlive the run that started it.
CancelAll(owner string)
}
JobStore owns background work for a run. Implementations must fence every operation by owner: an id from another owner behaves exactly like an unknown id. LocalJobStore is the in-process default.
type Launcher ¶
type Launcher struct {
// contains filtered or unexported fields
}
Launcher is what a tool receives from the run context: a store already bound to this run's owner, so a tool can start background work without being handed — or being able to forge — another session's owner token.
func From ¶
From returns the background-job launcher for the current run, if this plugin is installed. A tool that wants to go asynchronous calls this and degrades gracefully when it is absent; a tool that does not is unaffected. That is the whole ejection contract for jobs: the ok result is the plugin's presence.
func (Launcher) Start ¶
func (l Launcher) Start(tool, label string, run func(ctx context.Context) (string, error)) (Job, error)
Start launches background work and returns immediately. The job's context is derived from the RUN's context, not the calling tool's, so the work is not cancelled when the tool call that started it returns — that is the entire point — but still dies with the run.
type LocalJobStore ¶
type LocalJobStore struct {
// contains filtered or unexported fields
}
LocalJobStore runs jobs as goroutines in this process, fenced by owner.
func NewLocalJobStore ¶
func NewLocalJobStore() *LocalJobStore
NewLocalJobStore builds an empty in-process store.
func (*LocalJobStore) Cancel ¶
func (s *LocalJobStore) Cancel(owner, id string) error
Cancel requests cancellation; cancelling a finished job is a no-op.
func (*LocalJobStore) CancelAll ¶
func (s *LocalJobStore) CancelAll(owner string)
CancelAll stops every running job for an owner.
func (*LocalJobStore) DrainCompleted ¶
func (s *LocalJobStore) DrainCompleted(owner string) []Job
DrainCompleted returns and clears the owner's finished-since-last-drain set.
func (*LocalJobStore) Get ¶
func (s *LocalJobStore) Get(owner, id string) (Job, error)
Get returns a snapshot of one job, fenced by owner.
func (*LocalJobStore) List ¶
func (s *LocalJobStore) List(owner string) []Job
List returns the owner's jobs, newest first.
type Plugin ¶
type Plugin struct {
// MaxConcurrent bounds running jobs per owner in the default store. Zero uses 15; -1 disables the ceiling.
MaxConcurrent int
// Store owns the work. nil uses a fresh LocalJobStore for the run.
Store JobStore
// MaxResultBytes bounds a job result held in the store, so a completed job's
// output cannot blow the context when the model reads it. 0 uses the run's
// limits.MaxToolResultLen.
MaxResultBytes int
// MaxWaitSeconds bounds one job_wait call so the model cannot park the run
// forever on a job that never finishes. 0 uses defaultJobMaxWait.
MaxWaitSeconds int
// SuppressCompletionNotice turns OFF the synthetic user message that lists
// jobs finished since the last turn. The notice is on by default because
// without it the model must poll job_status, burning a turn per check — and
// a model that forgets to poll never learns its work finished at all.
SuppressCompletionNotice bool
}
Plugin installs background jobs.
func Local ¶
func Local() Plugin
Local installs the in-process job store — the right default for a single server. A custom store must preserve run ownership and cancellation; durable host tasks belong on the consumer's existing task queue.
func (Plugin) BeginRun ¶
BeginRun resolves the policy and binds a launcher to THIS run.
info.Owner is the fence: it is the session id on a durable run and a unique token otherwise, so a job started here is invisible to every other run and a tool cannot forge another run's owner — it never sees one.