jobs

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 10 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.

README

jobs

Extension. Ejectable — the loop never names it. Without it every tool is synchronous: a long crawl, build, render, or export blocks the run until it finishes, and a run that has to wait cannot also think.

Model Experience

A tool goes asynchronous

The tool calls agentcore.LaunchBackground(ctx, tool, label, run) and returns immediately with the JSON receipt (id, tool, label, state, started_at). What the model sees in the tool result is whatever that tool chose to say; the convention is to include the receipt.

Token effect

Zero-direct at the moment of the call. The plugin's cost arrives later, in the completion notice.

KV cache effect

Append-only.

Jobs finished since the last turn
What the model sees

A synthetic user message at the top of the next turn, before the model reasons.

Verbatim text for this field
Background job update:
- job_7f2a (crawl_site, succeeded, 4.21s):
<up to 2 KB of the result inline>
- job_91bc (render_pdf, succeeded, 61.4s): 184320 bytes of output — call job_status with id "job_91bc" to read it.
- job_c3d1 (crawl_site, failed, 0.98s): dial tcp: connection refused

A small successful result rides inline so the common case costs no extra tool call; a large one is named and left for job_status.

Token effect

Capped. Inline output is bounded at 2 KB per job (noticeInlineLimit), and a job's stored result is bounded at the run's MaxToolResultLen (16 KB when unset) at completion time, before it can be read at all.

KV cache effect

Append-only. The notice is appended after the previous turn's messages and never rewritten.

The model inspects or stops work
What the model sees

Four tools: job_list, job_status, job_wait, job_cancel. job_wait blocks for at most 120s and then returns the job's current state, so the model cannot park the run forever on work that never finishes.

A running job is not a black box. Its work publishes output as it goes (agentcore.PublishJobOutput(ctx, text)); the board keeps the first 2 KiB and the last 8 KiB and counts the rest, so memory stays bounded however much the job prints. job_status on a running job returns its state and the newest 2 KiB of output (output_bytes asks for more, never beyond the run's tool-result limit); job_list adds the newest output line under each running job; a job_wait that times out carries the newest 1 KiB. A job that fails or is cancelled keeps up to 4 KiB of its last output beside its error. A job that never publishes renders exactly as before. The run_shell / computer_use background mode streams the command's output into its job, and an async spawn_subagent publishes a start line and its current tool or progress note.

A work function that runs its tool under another context's values (to keep the call's policy and session) must build it with agentcore.BackgroundValues(jobCtx, values); a wrapper that hides jobCtx's values silences PublishJobOutput and HandleJobMessages.

Optional: steering a running sub-agent (Plugin{AllowSend: true})

Adds job_send {id, message}. The message (at most 4 KiB, at most 8 unread per child) is queued for a running background sub-agent and reaches it at its next step boundary as a user message, persisted in the child's own transcript (and so in its checkpoint); a message that arrives while the child composes its final answer holds the finish for one more step. It is refused with a reason when the job is unknown, already finished, or not a sub-agent (any job whose work did not register a handler with agentcore.HandleJobMessages). The child keeps its depth cap, its policy intersection and its budgets: steering is a user message, not a capability. Only spawn_subagent self-forks register a handler; a named delegate has no step boundary to deliver to. A message that arrives after the child's last step is returned in the job result as undelivered. Off by default: it changes the tool list the model sees.

Optional: work that outlives the run (Plugin{Survive: &Survival{...}})

Adds job_keep {id}. Only a job the model keeps survives; every other job is cancelled when the run ends, exactly as without the option, and a kept job does not hold up the final answer. See Survival for what the host owns. In short:

hold := jobs.NewHold(func(e jobs.Ended) { /* start a run of e.Conversation carrying e.Notice */ })
plugin := jobs.Plugin{Survive: &jobs.Survival{Hold: hold, MaxAge: time.Hour, MaxJobs: 4}}
ctx := jobs.WithConversation(ctx, conversationID) // per run, passed to RunNative

State machine of one job:

running --job_keep--> running(kept)
running(kept) --run ends--> detached in the Hold (goroutine keeps running)
detached --next run of the same conversation begins--> running(kept) on that board
running/running(kept) --work returns / job_cancel / MaxAge--> succeeded | failed | cancelled
ended while a run is attached --> announced by that run's next step, once
ended while detached          --> announced through Hold's callback, once (the next run then lists it as announced)
process restart               --> Hold is empty; the restored checkpoint reports it interrupted

The job's context is detached from the run's cancellation but keeps the run's values, so it runs under the policy of the run that started it; a later run only lists, reads and cancels it, and cannot widen it. Kept jobs are found by conversation identity only: another conversation sees nothing. MaxAge (default 1 h, counted from the job's start) cancels a kept job; MaxJobs (default 4) caps the kept jobs running per conversation. Model-backed work cannot be kept: spawn_subagent jobs (and any tool named in Survival.Exclude) are refused by job_keep with that reason, because a child's token usage is folded into the Agent that spawned it, which has finished by the time a surviving child would.

Token effect

Capped, by the same result bound applied at completion.

KV cache effect

Append-only.

Impact on the agent

  • Adds four tools that bypass the permission gate. They are SelfGated: every run has its own board, so they observe and cancel only work this run launched — a capability the agent already exercised by starting the job.
  • Binds a launcher onto the run context (RunContext) through agentcore.WithBackgroundLauncher. agentcore.LaunchBackground fails when the plugin is absent, so a background-capable tool degrades to synchronous.
  • The job's context derives from the run's, not the calling tool's, so work survives the call that started it and still dies with the run.
  • CloseRun cancels everything this run started. A job outliving its run would be unobservable and uncancellable — nothing left to look at it.
  • The board holds at most MaxConcurrent running jobs (15 by default, -1 for no ceiling) and 16 finished, announced records; older announced records are evicted. A record no notice has carried yet is never evicted.
  • The board survives resume through the native plugin checkpoint (NativeState / RestoreNativeState). Finished results stay readable with job_status, unannounced completions are announced on the first resumed step, and ids keep counting so a new job never reuses an id the transcript names.

Known limitations

  • Running work does not survive the run, unless the host opts in (see Survival). Even then only the process's memory keeps it; a goroutine cannot be checkpointed. The checkpoint is taken before CloseRun cancels the work, so a job still running then is recorded as cancelled with the error "interrupted: the run ended before this job finished; start it again if it is still needed", and the resumed run's first step announces it. Durable host tasks belong on the consumer's own task queue.
  • No completion notice for a run that has already ended. Jobs finishing after the last turn are cancelled by CloseRun; the resumed run learns they were interrupted, not what they would have produced. (A kept job's end is delivered through the Hold's callback.)
  • The Hold's callback is once per process. A crash between the callback and the host acting on it loses the notice; the restored checkpoint then reports the job interrupted.

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 agentcore.LaunchBackground, returns a job receipt 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 run gets its own board, so a job can be neither observed nor cancelled by another run, and every job is cancelled when its run ends. The board's records are checkpointed: a resumed run can still read the results of jobs that finished, and learns which ones the end of the previous run interrupted.

A running job is not a black box: its work publishes output as it goes (agentcore.PublishJobOutput), and job_status / job_list show the newest part of it. Two opt-ins extend the board: Plugin.AllowSend adds job_send, which steers a running background sub-agent, and Plugin.Survive lets a job the model marks with job_keep outlive the run that started it (see Survival).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithConversation added in v0.2.2

func WithConversation(ctx context.Context, id string) context.Context

WithConversation names the conversation a run belongs to, for Survive. The host sets it on the context it passes to RunNative; it is the identity under which kept jobs are held and found again. A durable session id is used when the context carries none.

Types

type Ended added in v0.2.2

type Ended struct {
	Conversation string
	JobID        string
	Tool         string
	Label        string
	// State is "succeeded", "failed" or "cancelled".
	State string
	// Notice is the completion notice (the text the next run's first step would
	// have carried), ready to be sent as a user message of a new run.
	Notice string
}

Ended tells the host that a kept job ended while no run of its conversation was attached.

type Hold added in v0.2.2

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

Hold is the host-owned home of kept jobs between runs, keyed by conversation. It owns no storage and starts nothing: it only keeps the goroutines of jobs the model chose to keep reachable. Create one per process with NewHold.

func NewHold added in v0.2.2

func NewHold(onEnd func(Ended)) *Hold

NewHold creates a Hold. onEnd, when set, is called once for each kept job that ends while no run of its conversation is attached, and for a kept job that had ended unannounced when its run closed. It runs on the job's goroutine: return quickly and start the run elsewhere. It is never called for a job a run is attached to; that run announces the job itself.

func (*Hold) CancelAll added in v0.2.2

func (h *Hold) CancelAll()

CancelAll cancels every detached job still running, for host shutdown.

type Plugin

type Plugin struct {
	// MaxConcurrent bounds running jobs per run. Zero uses 15; -1 disables the ceiling.
	MaxConcurrent int
	// AllowSend adds job_send, which delivers a message to a running
	// background sub-agent at its next step. Off by default: it changes the
	// tool list the model sees.
	AllowSend bool
	// Survive, when set, adds job_keep and lets kept jobs outlive their run.
	// Nil keeps every job bound to its run, exactly as without the option.
	Survive *Survival
}

Plugin installs background jobs.

func (Plugin) BeginRun

func (p Plugin) BeginRun(ctx context.Context, info agentcore.RunInfo) (agentcore.Extension, error)

BeginRun gives the run its own board.

func (Plugin) Name

func (Plugin) Name() string

Name identifies the plugin and the extension it installs.

func (Plugin) Register

func (p Plugin) Register(r *agentcore.Registry) error

Register adds the plugin as a run extension.

type Survival added in v0.2.2

type Survival struct {
	// Hold is the host-owned home of kept jobs between runs. Required.
	Hold *Hold
	// MaxAge bounds a kept job's lifetime, counted from its start; at that age it
	// is cancelled and reported cancelled. Zero uses one hour.
	MaxAge time.Duration
	// MaxJobs bounds how many kept jobs one conversation may have running. Zero
	// uses four.
	MaxJobs int
	// Exclude names more tools whose jobs job_keep refuses, besides
	// spawn_subagent.
	Exclude []string
}

Survival is the opt-in that lets a background job outlive the run that started it. Set it as Plugin.Survive.

The model chooses: only a job it marks with job_keep survives; every other job is cancelled when the run ends, exactly as without this option. A kept job keeps running after the run's final answer. It is listed, readable and cancellable (job_list, job_status, job_wait, job_cancel) from the next run of the SAME conversation and from no other. Its result is announced exactly once: in that next run's first step, or, when it ends while no run of the conversation is attached, through Hold's callback.

What the host must do:

  • Create one Hold for the process (NewHold) and give every run the same one.
  • Tell the library which conversation a run belongs to: WithConversation on the context passed to RunNative, or a durable session id (RunInfo.SessionID). A run with neither cannot keep jobs.
  • Implement the Hold's callback to start a new run of that conversation carrying Ended.Notice as a user message.
  • Persist the native checkpoint as it already does. A kept job lives in the process: when the process restarts the Hold is empty, and the restored checkpoint reports the job as interrupted.

A job started under one run keeps that run's context values, so its policy, credentials and session stay those of the run that started it; the next run only observes and cancels it.

Model-backed work cannot be kept. spawn_subagent folds its child's token usage into the Agent that spawned it, and that Agent's run has ended by the time a surviving child would finish: the usage would be lost or charged to the wrong run. job_keep refuses it with that reason. Shell commands and host tools can be kept; Exclude refuses more tools that call a model.

Jump to

Keyboard shortcuts

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