orchestrator

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package orchestrator decides each step of a session, the same way on every runtime.

Run drives one session from its first wake to its close. Given the session's State and each wake (a prompt, a resume, a cancel, a job's end), it decides the next step: commit the prompt into the window, ask the model, run the reply's calls and start the jobs they ask for, park on an interrupt, commit the round, and end the turn. It makes no network call and touches no store: it only calls its Runtime.

Runtime is the port to a durable runtime (Temporal, Durable Objects): it runs each step once (Infer, Tools, Commit), reads the session's next wake (Next), publishes events (Emit), and runs jobs. A runtime records each step's result, so a replay of Run gets the recorded result instead of running the step again. The work of a step is the harness's (tacklr.Harness); the runtime only carries it out.

State is the session's state across its turns: the open round, the inbox, the jobs, and the last commit's revision. Carry is the part of it that a runtime keeps across a suspend or a continue-as-new.

Index

Constants

View Source
const ToolTimeoutMargin = time.Minute

ToolTimeoutMargin is what a runtime's Tool step adds to its tool's own timeout: the step also builds or reads the harness around the call.

Variables

View Source
var ErrSuspended = errors.New("session suspended")

ErrSuspended is Run's error when a suspend stopped the session: the runtime restarts it from the State as Run left it.

Functions

func JobEnd

func JobEnd(id session.ID, result string, err error) session.JobEnd

JobEnd is how a task session that Run ended tells its parent: its result, or why it failed.

func Run

func Run(rt Runtime, t *State) (string, error)

Run drives one session lifetime over d from the state in t, which the caller keeps reading (a status query) while Run changes it. A task session (Identity.Task) runs its first prompt, its task, and returns once that turn ended and is no longer parked; any other session returns when it is closed (t.Closed). Either way Run then stops the jobs that still run. A suspend returns ErrSuspended, and the jobs go on.

func TakeCancel

func TakeCancel(queue []session.Wake) (kept []session.Wake, taken []int, ok bool)

TakeCancel takes the first cancel in queue, with the prompts queued before it: they steered the turn the cancel ends. It returns the wakes that stay, in order, the positions of those it took, and whether it found a cancel. A runtime calls it while a step runs, so the cancel stops the step.

func UserMessage

func UserMessage(text string, msg *message.Message) *message.Message

UserMessage is the prompt text, or the host-supplied user message.

Types

type Carry

type Carry struct {
	Mounts    []session.MountRecipe `json:"mounts,omitempty"`
	MCP       []session.MCPServer   `json:"mcp,omitempty"`
	Inbox     []*message.Message    `json:"inbox,omitempty"`
	NextMCP   []session.MCPServer   `json:"nextMcp,omitempty"`
	State     map[string]any        `json:"state,omitempty"`
	Round     *tacklr.Round         `json:"round,omitempty"`
	Rev       session.Revision      `json:"rev,omitempty"`
	Result    string                `json:"result,omitempty"`
	Terminal  session.State         `json:"terminal,omitempty"`
	Yielded   bool                  `json:"yielded,omitempty"`
	Finished  bool                  `json:"finished,omitempty"`
	Suspended bool                  `json:"suspended,omitempty"`
	Jobs      []Job                 `json:"jobs,omitempty"`
	Held      []session.Wake        `json:"held,omitempty"`
	Trace     telemetry.Carrier     `json:"trace,omitempty"`
	Span      telemetry.Carrier     `json:"span,omitempty"`
}

Carry is a session's state across a suspend: everything the State holds that is not in the snapshot. A runtime keeps it when Run returns ErrSuspended (on Temporal, in the continue-as-new input) and starts the next Run from it with NewState. The session goes on with no step running: idle, or in a turn that waits on jobs (Suspended). Jobs carry by id and, when one already ended, its result.

type Identity

type Identity struct {
	Specialist string
	// Worker, when set, means this session is a named JobHandler child.
	Worker string
	Parent session.ID
	Mounts []session.MountRecipe
	// Task says the session runs its first prompt, its task, then ends: a
	// child a tool started. Its runtime puts the task first in its mailbox.
	Task bool `json:",omitempty"`
}

Identity is who a session is and what it mounts. The session's runtime holds it and gives it to each step, which builds the turn's harness from it; the snapshot store does not keep it.

type Job

type Job struct {
	ID     session.ID
	Name   string
	Call   string `json:",omitempty"`
	Ended  bool   `json:",omitempty"`
	Result session.JobResult
}

Job is one job the session started, by a deterministic id. Call is the key of the call that waits on it; empty for a background job. A waited job's end is kept (Ended, Result) until its call takes it.

type Runtime

type Runtime interface {
	// Begin starts a turn, or the part of one that a resume or a restart
	// runs: the runtime starts its span (telemetry.StartTurn) once, records
	// the span's context durably, and returns it; a replay of the turn
	// returns the recorded context and starts nothing. Each step of the
	// turn runs in that context.
	Begin(TurnStart) telemetry.Carrier
	// End ends the turn's span, when this run started it.
	End()
	Commit(base session.Revision, in tacklr.CommitIn) (session.Revision, error)
	Infer() (tacklr.Reply, error)
	Tools(calls []tacklr.Call) []tacklr.Call
	Emit(session.Event)
	Job(name, task string) (string, error)

	Next(block bool) session.Wake

	Start(tacklr.JobStart) error
	Cancel(id session.ID)
}

Runtime is how one runtime runs a session: its steps, its mailbox and its children. Every runtime implements it; Run is the same for all of them.

Steps. Each non-deterministic action is one step: Infer is one model request, Tool one tool call, Job one worker body. A runtime records each completed step and its result, and a replay returns the recorded result instead of running the step again; neither the harness nor the snapshot records which steps ran. A runtime may run a step more than once before it records it (a worker dies before it reports). Infer and Tool change nothing a later step reads, so running them again is safe; a tool with external side effects uses its call id (CallContext.CurrentToolCallID) as the idempotency key.

Commit is the only step that writes the snapshot, and it is idempotent: base is the revision the previous Commit returned, and Commit applies in only when the snapshot is still at base, so a Commit that already saved does not apply it again. A cancel does not stop a Commit, so the turn always learns the revision it saved. Tools runs one Tool step per run, all at once (the runtime decides how: one activity each, or goroutines), and returns a result per run in run order.

Steps take only what changes per step. The session's identity and MCP servers are the runtime's: it holds them for the session and builds each step's harness from them. Begin and End bracket a turn so the implementation can pin turn-scoped resources. A Begin of kind session.WakePrompt starts a new turn: the runtime then trims the event log and the call bodies of the turns before it, which committed. A resume or a continue goes on with the turn it parked or suspended.

Mailbox. Next is the one read of the mailbox: prompts, resumes, cancels, closes, and job ends, in the order they arrived. With block it waits for one; without, it returns a zero Wake when none is queued. // orchestrator (Run) decides what each does. A step that runs while a cancel is queued stops early; the cancel keeps its place.

Jobs. The session owns each job's lifecycle (State.Jobs): it starts a job by its deterministic id, records the call that waits on it, cancels it by id, and turns its end into that call's result or an inbox message. The runtime only runs it: Start starts the job (a job already started under that id is not started again) and Cancel stops it. A job's end reaches the session one way, as a WakeJob from Next.

Suspend. Next with block may return WakeSuspend so the runtime can restart the session from the same state (on Temporal, continue-as-new), while idle or while a turn waits on jobs. The running jobs carry over with the turn.

type State

type State struct {
	SessionID session.ID
	Identity

	MCP     []session.MCPServer
	Inbox   []*message.Message
	NextMCP []session.MCPServer
	// State is host values not yet committed. The next Commit writes them
	// into the session's user state.
	State map[string]any
	// Round is the open round: each call's last outcome.
	Round *tacklr.Round
	// Rev is the snapshot revision of the last Commit.
	Rev session.Revision

	Result   string
	Terminal session.State
	Err      error
	Yielded  bool
	// Finished says the model finished the turn, which waits on children.
	Finished bool
	// Suspended says the runtime stopped the turn mid-way to restart the
	// session, which goes on with it in the restarted session.
	Suspended bool
	// Jobs are the session's running jobs, in start order: the one record
	// of their lifecycle (started, running, ended).
	Jobs []Job
	// Closed says the session was closed: Run returned for good.
	Closed bool

	// Trace is the trace of the turn that runs or is parked: the context
	// of the request that sent its prompt. A resume, a restart or a
	// continue-as-new goes on in the same trace.
	Trace telemetry.Carrier
	// Span is the context of the turn's running span (Begin's): each step
	// and each job the turn starts runs in it.
	Span telemetry.Carrier
	// contains filtered or unexported fields
}

State is one session's scheduler state. The snapshot holds what committed; Round is the reply whose calls do not all have results yet, rebuilt from recorded step results on replay. Run drives it: a turn is one prompt or resume, and ends when it parks, finishes, or fails.

func NewState

func NewState(id session.ID, ident Identity, mcp []session.MCPServer, state map[string]any, c *Carry) *State

NewState is the State a session's Run starts from: its identity, the MCP servers and host state it was created with, and, when an earlier Run suspended, that Run's carry. Its mounts hold no credentials; each turn applies its own.

func (*State) Carry

func (t *State) Carry() Carry

Carry is t's state for the next Run.

func (*State) Outcome

func (t *State) Outcome() string

Outcome is how the turn that ran last ended, for its span and metrics: parked (yield), cancelled, failed (error), or ok.

func (*State) Status

func (t *State) Status() session.Status

Status is the session's status as the State holds it: running (waiting while parked), or the terminal state its turn or a close set. A runtime answers a status query with it.

func (*State) Steer

func (t *State) Steer(p session.Prompt)

Steer queues a prompt onto this turn with its auth and state.

type TurnStart

type TurnStart struct {
	Kind      string
	PromptLen int
	Resumes   int
	Parent    telemetry.Carrier
	Link      telemetry.Carrier
}

TurnStart is what Begin starts: the turn's kind (prompt, resume, or continue for a turn a runtime restarted), the prompt's length and the answers it resumes with, for its log, and its trace. Parent is the context of the turn's trace (the request that sent its prompt, or the parent session's turn); Link is the request that resumed it, which the span links to.

Jump to

Keyboard shortcuts

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