engine

package
v2.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 8 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 engine defines workflow engine abstractions for durable agent execution. It provides pluggable interfaces so generated code and the runtime can target Temporal, in-memory, or custom backends without modification.

Core Abstractions

The package defines several key interfaces:

  • Engine: Registers workflows and activities, starts workflow executions. The runtime calls Engine methods during agent registration and run submission.

  • WorkflowContext: Provides deterministic operations inside workflow handlers. Generated workflow code uses this to schedule activities, handle signals, and start child workflows. Implementations must ensure replay-safe behavior.

  • WorkflowHandle: Represents a running workflow. Callers use handles to wait for completion, send signals, or cancel execution.

  • Future[T]: Represents a pending activity result. Enables parallel execution by allowing workflows to launch multiple activities and collect results later, with typed inputs and outputs.

  • Receiver[T]: Delivers typed signals to workflows in a deterministic way. Used for pause/resume, clarification answers, and external tool results.

Available Implementations

Two engine implementations ship with loom-mcp:

  • temporal: Production-grade durable execution backed by Temporal. Supports workflow replay, long-running execution, and distributed workers.

  • inmem: In-memory synchronous execution for development and testing. No durability, no workers, runs immediately in the caller's goroutine.

Determinism Requirements

Workflow handlers run in a deterministic environment where the same inputs and history must produce the same outputs. WorkflowContext enforces this by:

  • Providing Now() instead of time.Now() for workflow time
  • Requiring activities for all I/O operations
  • Using replay-safe signal channels

Activities (planner calls, tool execution) are NOT deterministic and can perform arbitrary I/O. The engine records activity inputs/outputs and replays them during workflow recovery.

Usage Pattern

// Create engine (Temporal for production)
eng, _ := temporal.NewWorker(temporal.Options{...})
defer eng.Close()

// Create runtime with engine
rt := runtime.New(runtime.WithEngine(eng))

// Register agents (registers workflows/activities on engine)
chat.RegisterChatAgent(ctx, rt, chat.ChatAgentConfig{...})

// Start runs (submits workflows to engine)
client := chat.NewClient(rt)
out, _ := client.Run(ctx, "session-1", messages)

Index

Constants

View Source
const QueryToolAwaits = "tool_awaits"

QueryToolAwaits names the read-only pending tool-handshake workflow query.

Variables

View Source
var (
	// ErrWorkflowNotFound indicates that no workflow execution exists for the given identifier.
	ErrWorkflowNotFound = errors.New("workflow not found")
	// ErrWorkflowCompleted indicates that a workflow exists but no longer accepts signals.
	ErrWorkflowCompleted = errors.New("workflow completed")
	// ErrWorkflowAlreadyStarted indicates that the workflow ID conflicts with an existing execution.
	// It does not imply ErrWorkflowStartRejected.
	ErrWorkflowAlreadyStarted = errors.New("workflow already started")
	// ErrWorkflowStartRejected indicates that a workflow start was definitely rejected before acceptance.
	ErrWorkflowStartRejected = errors.New("workflow start rejected before acceptance")
)
View Source
var ErrCanceled = errtypes.ErrCanceled

ErrCanceled marks canceled activities, workflows and timers. It shares context.Canceled's identity to preserve existing context cancellation checks.

Functions

func ActivityAttemptFromContext added in v2.1.0

func ActivityAttemptFromContext(ctx context.Context) int

ActivityAttemptFromContext returns the engine's current activity attempt. Attempts start at 1; zero means no engine activity attempt was attached.

func ActivityHeartbeatTimeout

func ActivityHeartbeatTimeout(ctx context.Context) time.Duration

ActivityHeartbeatTimeout returns the effective heartbeat timeout carried on ctx. Zero means the current activity does not use heartbeat-based failure detection.

func IsActivityContext

func IsActivityContext(ctx context.Context) bool

IsActivityContext reports whether ctx is marked as originating from an activity invocation.

func IsCanceled added in v2.1.0

func IsCanceled(err error) bool

IsCanceled reports whether err carries the engine cancellation marker. Mixed error graphs require the runtime's exact-leaf classifier to decide run status.

func IsTimeout added in v2.1.0

func IsTimeout(err error) bool

IsTimeout reports an engine timeout or a native context deadline.

func RecordActivityHeartbeat

func RecordActivityHeartbeat(ctx context.Context, details ...any) bool

RecordActivityHeartbeat emits a heartbeat on ctx when an engine-specific recorder is attached. It returns false when ctx is not an activity context or when the engine does not require heartbeats.

func WithActivityAttempt added in v2.1.0

func WithActivityAttempt(ctx context.Context, attempt int) context.Context

WithActivityAttempt returns a child context carrying the current activity's one-based attempt. Engine adapters set it on each activity invocation.

func WithActivityContext

func WithActivityContext(ctx context.Context) context.Context

WithActivityContext returns a child context that is marked as an activity invocation context.

func WithActivityHeartbeatRecorder

func WithActivityHeartbeatRecorder(ctx context.Context, recorder ActivityHeartbeatRecorder) context.Context

WithActivityHeartbeatRecorder returns a child context carrying the given heartbeat recorder.

func WithActivityHeartbeatTimeout

func WithActivityHeartbeatTimeout(ctx context.Context, timeout time.Duration) context.Context

WithActivityHeartbeatTimeout returns a child context carrying the effective heartbeat timeout for the current activity attempt. Zero means the engine is not using heartbeat-based liveness detection for this activity.

func WithWorkflowContext

func WithWorkflowContext(ctx context.Context, wf WorkflowContext) context.Context

WithWorkflowContext returns a child context that carries the provided WorkflowContext. Engine adapters should use this when invoking activity handlers so downstream code can retrieve the workflow context if needed.

Types

type Activity added in v2.1.0

type Activity[I, O any] = execution.Activity[I, O]

Activity uses the shared Loom execution contract.

type ActivityCall added in v2.1.0

type ActivityCall = execution.ActivityCall

ActivityCall uses the shared Loom execution contract.

type ActivityDefinition added in v2.1.0

type ActivityDefinition = execution.ActivityDefinition

ActivityDefinition uses the shared Loom execution contract.

type ActivityExecutor added in v2.1.0

type ActivityExecutor = execution.ActivityExecutor

ActivityExecutor uses the shared Loom execution contract.

type ActivityFuture added in v2.1.0

type ActivityFuture = execution.ActivityFuture

ActivityFuture uses the shared Loom execution contract.

type ActivityHeartbeatRecorder

type ActivityHeartbeatRecorder interface {
	RecordHeartbeat(details ...any)
}

ActivityHeartbeatRecorder records liveness heartbeats for a running activity. Engine adapters attach implementations to activity contexts when the backend requires heartbeats to deliver cancellation or timeout updates promptly.

type ActivityOptions

type ActivityOptions = execution.ActivityOptions

ActivityOptions uses the shared Loom execution contract.

type ActivityRegistrar added in v2.1.0

type ActivityRegistrar = execution.ActivityRegistrar

ActivityRegistrar uses the shared Loom execution contract.

type AgentActivityRegistrations added in v2.1.0

type AgentActivityRegistrations struct {
	ActivityRegistrar
	// Defaults resolves agent-class defaults before generic backend registration.
	// A nil function leaves explicitly supplied options unchanged.
	Defaults func(class string, options ActivityOptions) ActivityOptions
}

AgentActivityRegistrations supplies agent-specific helpers above the backend registration boundary. Adapters embed it with their ActivityRegistrar.

func (AgentActivityRegistrations) RegisterChildDispatchActivity added in v2.1.0

func (a AgentActivityRegistrations) RegisterChildDispatchActivity(ctx context.Context, name string, opts ActivityOptions, fn func(context.Context, *api.ChildDispatchInput) (*api.ChildDispatchOutput, error)) error

RegisterChildDispatchActivity registers the child dispatch handler with its activity defaults.

func (AgentActivityRegistrations) RegisterExecuteToolActivity added in v2.1.0

func (a AgentActivityRegistrations) RegisterExecuteToolActivity(ctx context.Context, name string, opts ActivityOptions, fn func(context.Context, *api.ToolInput) (*api.ToolOutput, error)) error

RegisterExecuteToolActivity registers the tool handler with its activity defaults.

func (AgentActivityRegistrations) RegisterFinishRunActivity added in v2.1.0

func (a AgentActivityRegistrations) RegisterFinishRunActivity(ctx context.Context, name string, opts ActivityOptions, fn func(context.Context, *api.FinishRunInput) (*api.FinishRunOutput, error)) error

RegisterFinishRunActivity registers the finish handler with its activity defaults.

func (AgentActivityRegistrations) RegisterHookActivity added in v2.1.0

func (a AgentActivityRegistrations) RegisterHookActivity(ctx context.Context, name string, opts ActivityOptions, fn func(context.Context, *api.HookActivityInput) error) error

RegisterHookActivity registers the hook handler with its activity defaults.

func (AgentActivityRegistrations) RegisterPlannerActivity added in v2.1.0

func (a AgentActivityRegistrations) RegisterPlannerActivity(ctx context.Context, name string, opts ActivityOptions, fn func(context.Context, *api.PlanActivityInput) (*api.PlanActivityOutput, error)) error

RegisterPlannerActivity registers the planner handler with its activity defaults.

func (AgentActivityRegistrations) RegisterRecallActivity added in v2.1.0

func (a AgentActivityRegistrations) RegisterRecallActivity(ctx context.Context, name string, opts ActivityOptions, fn func(context.Context, *api.RecallActivityInput) (*api.RecallActivityOutput, error)) error

RegisterRecallActivity registers the recall handler with its activity defaults.

func (AgentActivityRegistrations) RegisterRecordContentActivity added in v2.1.0

func (a AgentActivityRegistrations) RegisterRecordContentActivity(ctx context.Context, name string, opts ActivityOptions, fn func(context.Context, *api.RecordContentInput) (*api.RecordContentOutput, error)) error

RegisterRecordContentActivity registers the record content handler with its activity defaults.

func (AgentActivityRegistrations) RegisterSealRecordActivity added in v2.1.0

func (a AgentActivityRegistrations) RegisterSealRecordActivity(ctx context.Context, name string, opts ActivityOptions, fn func(context.Context, *api.SealRecordInput) (*api.SealRecordOutput, error)) error

RegisterSealRecordActivity registers the seal handler with its activity defaults.

func (AgentActivityRegistrations) RegisterSummarizeContextActivity added in v2.1.0

func (a AgentActivityRegistrations) RegisterSummarizeContextActivity(ctx context.Context, name string, opts ActivityOptions, fn func(context.Context, *api.SummarizeContextInput) (*api.SummarizeContextOutput, error)) error

RegisterSummarizeContextActivity registers the summary handler with its activity defaults.

type AgentWorkflowActivities added in v2.1.0

type AgentWorkflowActivities struct{ ActivityExecutor }

AgentWorkflowActivities supplies the runtime's typed agent operations above the backend execution boundary. Each workflow scope binds its own executor.

func (AgentWorkflowActivities) ExecuteChildDispatchActivity added in v2.1.0

func (a AgentWorkflowActivities) ExecuteChildDispatchActivity(ctx context.Context, call ChildDispatchActivityCall) (*api.ChildDispatchOutput, error)

ExecuteChildDispatchActivity executes the child dispatch activity with a typed result.

func (AgentWorkflowActivities) ExecuteFinishRunActivity added in v2.1.0

func (a AgentWorkflowActivities) ExecuteFinishRunActivity(ctx context.Context, call FinishRunActivityCall) (*api.FinishRunOutput, error)

ExecuteFinishRunActivity executes the finish activity with a typed result.

func (AgentWorkflowActivities) ExecutePlannerActivity added in v2.1.0

func (a AgentWorkflowActivities) ExecutePlannerActivity(ctx context.Context, call PlannerActivityCall) (*api.PlanActivityOutput, error)

ExecutePlannerActivity executes the planner activity with a typed result.

func (AgentWorkflowActivities) ExecuteRecallActivity added in v2.1.0

ExecuteRecallActivity executes the recall activity with a typed result.

func (AgentWorkflowActivities) ExecuteRecordContentActivity added in v2.1.0

func (a AgentWorkflowActivities) ExecuteRecordContentActivity(ctx context.Context, call RecordContentActivityCall) (*api.RecordContentOutput, error)

ExecuteRecordContentActivity executes the record content activity with a typed result.

func (AgentWorkflowActivities) ExecuteSealRecordActivity added in v2.1.0

func (a AgentWorkflowActivities) ExecuteSealRecordActivity(ctx context.Context, call SealRecordActivityCall) (*api.SealRecordOutput, error)

ExecuteSealRecordActivity executes the seal activity with a typed result.

func (AgentWorkflowActivities) ExecuteSummarizeContextActivity added in v2.1.0

func (a AgentWorkflowActivities) ExecuteSummarizeContextActivity(ctx context.Context, call SummarizeContextActivityCall) (*api.SummarizeContextOutput, error)

ExecuteSummarizeContextActivity executes the summary activity with a typed result.

func (AgentWorkflowActivities) ExecuteToolActivity added in v2.1.0

func (a AgentWorkflowActivities) ExecuteToolActivity(ctx context.Context, call ToolActivityCall) (*api.ToolOutput, error)

ExecuteToolActivity executes the tool activity with a typed result.

func (AgentWorkflowActivities) ExecuteToolActivityAsync added in v2.1.0

func (a AgentWorkflowActivities) ExecuteToolActivityAsync(ctx context.Context, call ToolActivityCall) (Future[*api.ToolOutput], error)

ExecuteToolActivityAsync starts a tool activity without blocking the workflow.

func (AgentWorkflowActivities) PublishHook added in v2.1.0

func (a AgentWorkflowActivities) PublishHook(ctx context.Context, call HookActivityCall) error

PublishHook executes the hook activity outside the workflow thread.

type ApplicationError added in v2.1.0

type ApplicationError = errtypes.ApplicationError

ApplicationError carries a portable activity or workflow failure. Every engine must stop retrying an activity that returns this error with NonRetryable set. Activity and workflow failures surfaced to workflow code must carry the engine error types so errors.As can inspect them in the runtime. Details is one serializable value; Cause retains the underlying failure.

func NewApplicationError added in v2.1.0

func NewApplicationError(message, errorType string, details any, cause error) *ApplicationError

NewApplicationError constructs a retryable application failure.

func NewNonRetryableApplicationError added in v2.1.0

func NewNonRetryableApplicationError(message, errorType string, details any, cause error) *ApplicationError

NewNonRetryableApplicationError constructs a failure that must not be retried.

type Canceler

type Canceler interface {
	// CancelByID requests cancellation of the workflow identified by
	// workflowID.
	CancelByID(ctx context.Context, workflowID string) error
}

Canceler provides workflow cancellation by workflow ID without requiring in-process workflow handles. Engines that support out-of-process cancellation (for example, Temporal) should implement this so callers can cancel runs across process restarts.

type ChildDispatchActivityCall added in v2.1.0

type ChildDispatchActivityCall struct {
	Name    string
	Input   *api.ChildDispatchInput
	Options ActivityOptions
}

ChildDispatchActivityCall schedules validation before starting a child workflow.

type ChildWorkflowHandle

type ChildWorkflowHandle interface {
	// Get waits for child completion and returns the typed result.
	Get(ctx context.Context) (*api.RunOutput, error)
	// IsReady returns true if the child workflow has completed (success or failure)
	// and Get() will not block.
	IsReady() bool
	// Cancel requests cancellation of the child workflow execution.
	Cancel(ctx context.Context) error
	// RunID returns the engine-assigned run identifier of the child.
	RunID() string
}

ChildWorkflowHandle allows a parent workflow to await/cancel a child workflow.

type ChildWorkflowRequest

type ChildWorkflowRequest struct {
	// ID is the child workflow identifier, unique within the engine scope.
	ID string
	// Workflow is the provider workflow name to execute.
	Workflow string
	// TaskQueue is the queue to schedule the child on.
	TaskQueue string
	// Input is the payload passed to the child workflow handler.
	Input *api.RunInput
	// RunTimeout bounds the total child workflow execution time.
	RunTimeout time.Duration
	// RetryPolicy controls start retries for the child workflow start attempt.
	RetryPolicy RetryPolicy
}

ChildWorkflowRequest describes a child workflow to start from within an existing workflow execution.

type CompletionQuerier

type CompletionQuerier interface {
	// QueryRunCompletion returns the workflow output for successful runs or the
	// terminal error for failed/timed-out/canceled runs, addressed by
	// workflowID.
	QueryRunCompletion(ctx context.Context, workflowID string) (*api.RunOutput, error)
}

CompletionQuerier allows runtimes to recover the same terminal output/error that WorkflowHandle.Wait would have returned, but by run identifier after a restart or detached starter. Callers should query lifecycle first and invoke this only once the engine reports a terminal status, because implementations may otherwise block waiting for completion.

type Engine

type Engine interface {
	ActivityRegistrar
	// RegisterWorkflow registers a workflow definition with the engine.
	RegisterWorkflow(ctx context.Context, def WorkflowDefinition) error

	// RegisterHookActivity registers a typed activity that publishes workflow-emitted
	// hook events outside of the deterministic workflow thread. The activity accepts
	// *api.HookActivityInput and returns an error.
	RegisterHookActivity(ctx context.Context, name string, opts ActivityOptions, fn func(context.Context, *api.HookActivityInput) error) error

	// RegisterPlannerActivity registers a typed planner activity (PlanStart or
	// PlanResume) that accepts *api.PlanActivityInput and returns *api.PlanActivityOutput.
	RegisterPlannerActivity(ctx context.Context, name string, opts ActivityOptions, fn func(context.Context, *api.PlanActivityInput) (*api.PlanActivityOutput, error)) error

	// RegisterExecuteToolActivity registers a typed execute_tool activity that
	// accepts *api.ToolInput and returns *api.ToolOutput.
	RegisterExecuteToolActivity(ctx context.Context, name string, opts ActivityOptions, fn func(context.Context, *api.ToolInput) (*api.ToolOutput, error)) error

	// RegisterSealRecordActivity registers terminal transcript sealing.
	RegisterSealRecordActivity(ctx context.Context, name string, opts ActivityOptions, fn func(context.Context, *api.SealRecordInput) (*api.SealRecordOutput, error)) error

	// RegisterFinishRunActivity registers terminal event storage and typed-result finishing.
	RegisterFinishRunActivity(ctx context.Context, name string, opts ActivityOptions, fn func(context.Context, *api.FinishRunInput) (*api.FinishRunOutput, error)) error

	// RegisterRecordContentActivity registers ordered workflow content storage.
	RegisterRecordContentActivity(ctx context.Context, name string, opts ActivityOptions, fn func(context.Context, *api.RecordContentInput) (*api.RecordContentOutput, error)) error

	// RegisterSummarizeContextActivity registers recorded summary folding.
	RegisterSummarizeContextActivity(ctx context.Context, name string, opts ActivityOptions, fn func(context.Context, *api.SummarizeContextInput) (*api.SummarizeContextOutput, error)) error

	// RegisterRecallActivity registers activity-only transcript lookup.
	RegisterRecallActivity(ctx context.Context, name string, opts ActivityOptions, fn func(context.Context, *api.RecallActivityInput) (*api.RecallActivityOutput, error)) error

	// RegisterChildDispatchActivity registers pre-child validation.
	RegisterChildDispatchActivity(ctx context.Context, name string, opts ActivityOptions, fn func(context.Context, *api.ChildDispatchInput) (*api.ChildDispatchOutput, error)) error

	// QueryRootRunID resolves the workflow run tree storage scope.
	QueryRootRunID(ctx context.Context, workflowID string) (string, error)

	// StartWorkflow initiates a new workflow execution and returns a handle for
	// interacting with it. The workflow ID in req must be unique for the engine
	// instance. Errors may describe a definite rejection or an uncertain outcome;
	// only errors matching ErrWorkflowStartRejected prove that this invocation was
	// not accepted. Duplicate IDs and transport errors must not match that sentinel.
	StartWorkflow(ctx context.Context, req WorkflowStartRequest) (WorkflowHandle, error)

	// QueryRunStatus returns the current lifecycle status for the workflow
	// identified by workflowID. The engine is the source of truth for durable
	// workflow status. Returns an error if the workflow does not exist or if
	// querying fails.
	QueryRunStatus(ctx context.Context, workflowID string) (RunStatus, error)
}

Engine abstracts workflow registration and execution so adapters (Temporal, in-memory, or custom) can be swapped without touching generated code. Implementations translate these generic types into backend-specific primitives.

type FinishRunActivityCall added in v2.1.0

type FinishRunActivityCall struct {
	Name    string
	Input   *api.FinishRunInput
	Options ActivityOptions
}

FinishRunActivityCall schedules the runtime terminal activity.

type Future

type Future[T any] = execution.Future[T]

Future uses the shared Loom execution contract.

type HookActivityCall

type HookActivityCall struct {
	// Name identifies the registered hook activity.
	Name string

	// Input is the typed payload passed to the activity handler.
	Input *api.HookActivityInput

	// Options overrides the registered activity defaults for this invocation.
	Options ActivityOptions
}

HookActivityCall describes a single invocation of the runtime hook publishing activity from inside workflow code.

type PlannerActivityCall

type PlannerActivityCall struct {
	// Name identifies the registered planner activity.
	Name string

	// Input is the typed payload passed to the activity handler.
	Input *api.PlanActivityInput

	// Options overrides the registered activity defaults for this invocation.
	Options ActivityOptions
}

PlannerActivityCall describes a single invocation of a PlanStart/PlanResume activity from inside workflow code.

type RecallActivityCall added in v2.1.0

type RecallActivityCall struct {
	Name    string
	Input   *api.RecallActivityInput
	Options ActivityOptions
}

RecallActivityCall schedules a bounded transcript lookup.

type Receiver

type Receiver[T any] interface {
	// Receive blocks until a signal value is delivered and returns it.
	// Implementations should respect ctx when possible; for engines that do not
	// support context cancellation, Receive may ignore ctx and rely on workflow
	// cancellation semantics instead.
	Receive(ctx context.Context) (T, error)

	// ReceiveWithTimeout blocks until a signal value is delivered or the timeout
	// elapses and returns an error. A non-positive timeout must return
	// context.DeadlineExceeded immediately.
	//
	// This method is required so workflow code can enforce global run deadlines
	// while awaiting external signals (pause/resume, confirmations, clarifications,
	// external tool results) without relying on engine-level timeouts.
	ReceiveWithTimeout(ctx context.Context, timeout time.Duration) (T, error)

	// ReceiveAsync attempts to receive a signal without blocking.
	ReceiveAsync() (T, bool)

	// Pending returns the number of buffered signals not yet received. It is
	// side-effect free and deterministic under replay: it reflects only signals
	// already delivered to the workflow in history order. It is safe to call
	// inside an Await predicate.
	Pending() int
}

Receiver exposes typed workflow signal delivery in an engine-agnostic way. Implementations wrap engine-specific channels (Temporal signal channels, in-process Go channels, etc.) and provide blocking and non-blocking receive helpers so workflow code can react to external events deterministically.

type RecordContentActivityCall added in v2.1.0

type RecordContentActivityCall struct {
	Name    string
	Input   *api.RecordContentInput
	Options ActivityOptions
}

RecordContentActivityCall schedules the runtime content storage activity.

type RegistrationSealer

type RegistrationSealer interface {
	// SealRegistration closes the registration phase and activates any staged
	// workers before the runtime starts serving traffic. Successful calls must
	// be idempotent and implementations must honor ctx as the activation
	// deadline. If activation fails because ctx ended, callers may retry.
	SealRegistration(ctx context.Context) error
}

RegistrationSealer allows an engine to stage workflow/activity registrations until the runtime has finished building its full local registry. Worker-capable engines use this to avoid polling with partially registered handlers.

type RetryPolicy

type RetryPolicy = execution.RetryPolicy

RetryPolicy uses the shared Loom execution contract.

type RunStatus

type RunStatus string

RunStatus represents the lifecycle state of a workflow execution.

const (
	// RunStatusPending indicates the workflow has been accepted but not started yet.
	RunStatusPending RunStatus = "pending"
	// RunStatusRunning indicates the workflow is actively executing.
	RunStatusRunning RunStatus = "running"
	// RunStatusCompleted indicates the workflow finished successfully.
	RunStatusCompleted RunStatus = "completed"
	// RunStatusTimedOut indicates the workflow exceeded its run deadline.
	RunStatusTimedOut RunStatus = "timed_out"
	// RunStatusFailed indicates the workflow failed permanently.
	RunStatusFailed RunStatus = "failed"
	// RunStatusCanceled indicates the workflow was canceled externally.
	RunStatusCanceled RunStatus = "canceled"
	// RunStatusPaused indicates execution is paused awaiting external intervention.
	RunStatusPaused RunStatus = "paused"
)

type SealRecordActivityCall added in v2.1.0

type SealRecordActivityCall struct {
	// Name is the registered agent seal activity name.
	Name string
	// Input fixes the terminal entries and run-log event identity.
	Input *api.SealRecordInput
	// Options overrides registered scheduling options when non-zero.
	Options ActivityOptions
}

SealRecordActivityCall schedules terminal transcript sealing.

type Signaler

type Signaler interface {
	// SignalByID sends a signal to the given workflow identified by workflowID
	// and optional runID. The payload is engine-specific and must be
	// serializable by the engine client.
	SignalByID(ctx context.Context, workflowID, runID, name string, payload any) error
}

Signaler provides direct signaling by workflow ID/run ID without relying on in-process workflow handles. Engines that support out-of-process signaling (e.g., Temporal) should implement this interface so the runtime can deliver Provide*/Pause/Resume signals across process restarts.

type SummarizeContextActivityCall added in v2.1.0

type SummarizeContextActivityCall struct {
	Name    string
	Input   *api.SummarizeContextInput
	Options ActivityOptions
}

SummarizeContextActivityCall schedules a recorded summary at an immutable cut.

type TimeoutError added in v2.1.0

type TimeoutError = errtypes.TimeoutError

TimeoutError marks an engine execution timeout and retains its underlying cause. Engines must surface this type to workflow code for execution timeouts.

type ToolActivityCall

type ToolActivityCall struct {
	// Name identifies the registered execute_tool activity.
	Name string

	// Input is the typed payload passed to the activity handler.
	Input *api.ToolInput

	// Options overrides the registered activity defaults for this invocation.
	Options ActivityOptions
}

ToolActivityCall describes a single invocation of a tool execution activity from inside workflow code.

type ToolAwaitQuerier added in v2.1.0

type ToolAwaitQuerier interface {
	QueryToolAwaits(ctx context.Context, workflowID string) ([]planner.AwaitExternalTools, error)
}

ToolAwaitQuerier exposes pending tool handshakes to result-submitting clients. Engines must implement this interface to support Runtime.ProvideToolResults.

type WorkflowContext

type WorkflowContext interface {
	ActivityExecutor
	// Context returns the Go context for the workflow. In deterministic engines
	// (like Temporal), this is a special replay-aware context. Use this for activity
	// execution and cancellation propagation.
	Context() context.Context
	// SetQueryHandler registers a read-only query handler that can be invoked by
	// external clients to retrieve workflow state. Handlers must be deterministic
	// and side-effect free. Engines that do not support queries may implement
	// this as a no-op.
	SetQueryHandler(name string, handler any) error

	// WorkflowID returns the unique identifier for this workflow execution.
	WorkflowID() string

	// RunID returns the engine-assigned run identifier, used for observability
	// and run-level correlation.
	RunID() string

	// PublishHook schedules the runtime hook activity and waits for completion.
	// Implementations must run hook publishing outside of the deterministic workflow
	// thread (e.g., via activities in Temporal) so subscribers can perform I/O.
	PublishHook(ctx context.Context, call HookActivityCall) error

	// ExecutePlannerActivity schedules a planner activity (PlanStart/PlanResume)
	// and blocks until it completes. Planner activities are executed outside the
	// deterministic workflow thread and may perform I/O.
	ExecutePlannerActivity(ctx context.Context, call PlannerActivityCall) (*api.PlanActivityOutput, error)

	// ExecuteSealRecordActivity stores pending index entries and the terminal seal.
	ExecuteSealRecordActivity(ctx context.Context, call SealRecordActivityCall) (*api.SealRecordOutput, error)

	// ExecuteFinishRunActivity stores terminal events outside workflow execution.
	ExecuteFinishRunActivity(ctx context.Context, call FinishRunActivityCall) (*api.FinishRunOutput, error)

	// ExecuteRecordContentActivity stores ordered content outside workflow execution.
	ExecuteRecordContentActivity(ctx context.Context, call RecordContentActivityCall) (*api.RecordContentOutput, error)

	// ExecuteSummarizeContextActivity folds eligible evidence outside workflows.
	ExecuteSummarizeContextActivity(ctx context.Context, call SummarizeContextActivityCall) (*api.SummarizeContextOutput, error)

	// ExecuteRecallActivity recalls eligible evidence outside the workflow.
	ExecuteRecallActivity(ctx context.Context, call RecallActivityCall) (*api.RecallActivityOutput, error)

	// ExecuteChildDispatchActivity validates a child before it starts.
	ExecuteChildDispatchActivity(ctx context.Context, call ChildDispatchActivityCall) (*api.ChildDispatchOutput, error)

	// ExecuteToolActivity schedules a tool execution activity and blocks until it
	// completes. This is useful for sequential execution (finalizers, single tools).
	ExecuteToolActivity(ctx context.Context, call ToolActivityCall) (*api.ToolOutput, error)

	// ExecuteToolActivityAsync schedules a tool execution activity and returns a Future
	// so workflows can run multiple tools concurrently and collect results later.
	ExecuteToolActivityAsync(ctx context.Context, call ToolActivityCall) (Future[*api.ToolOutput], error)

	// PauseRequests returns a typed receiver for pause signals.
	PauseRequests() Receiver[*api.PauseRequest]

	// ResumeRequests returns a typed receiver for resume signals.
	ResumeRequests() Receiver[*api.ResumeRequest]

	// ClarificationAnswers returns a typed receiver for clarification answers.
	ClarificationAnswers() Receiver[*api.ClarificationAnswer]

	// ExternalToolResults returns a typed receiver for external tool results.
	ExternalToolResults() Receiver[*api.ToolResultsSet]

	// ConfirmationDecisions returns a typed receiver for tool confirmation decisions.
	ConfirmationDecisions() Receiver[*api.ConfirmationDecision]

	// TypedInputAnswers returns a typed receiver for schema-typed human input answers.
	TypedInputAnswers() Receiver[*api.TypedInputAnswer]

	// Now returns the current workflow time in a deterministic manner. Implementations
	// must return a time source that is replay-safe (e.g., Temporal's workflow.Now).
	Now() time.Time

	// NewTimer returns a Future that becomes ready after the given duration elapses
	// in workflow time. This is the engine-agnostic primitive for waking up on time
	// without polling.
	//
	// Implementations must schedule a deterministic timer (e.g., Temporal's
	// workflow.NewTimer). A non-positive duration should produce a Future that is
	// already ready.
	NewTimer(ctx context.Context, d time.Duration) (Future[time.Time], error)

	// Await blocks until condition returns true or the workflow scope is canceled.
	//
	// ctx must be this WorkflowContext's Context(), or a context derived from it
	// only to add values. Deterministic adapters may not observe arbitrary Go-context
	// cancellation after Await yields. Use WithCancel to create a workflow scope
	// that can be canceled while waiting.
	//
	// Condition must be deterministic and side-effect free. A typical use is to
	// wait on a set of Futures using IsReady() without draining them in a fixed
	// order (e.g., "wait until any tool future completes").
	Await(ctx context.Context, condition func() bool) error

	// StartChildWorkflow starts a child workflow execution and returns a handle
	// to await its completion or cancel it. Implementations should honor the
	// provided workflow name, task queue and timeouts without requiring local
	// registration lookups in the parent process.
	StartChildWorkflow(ctx context.Context, req ChildWorkflowRequest) (ChildWorkflowHandle, error)

	// Detached returns a derived WorkflowContext whose cancellation is disconnected
	// from the parent workflow scope.
	//
	// This is intended for cleanup/terminal work (e.g., emitting RunCompleted
	// hooks) that should still be attempted even when the main workflow context is
	// canceled.
	Detached() WorkflowContext

	// WithCancel returns a derived WorkflowContext whose cancellation can be
	// triggered independently of the parent workflow scope. This is used to
	// cooperatively cancel in-flight activities/child workflows when the runtime
	// needs to finalize (e.g., time budget reached).
	//
	// In deterministic engines, this must map to a workflow-level cancel scope
	// (e.g., Temporal's workflow.WithCancel).
	WithCancel() (WorkflowContext, func())
}

WorkflowContext exposes engine operations to workflow handlers within the deterministic execution environment of a workflow. It wraps engine-specific contexts (Temporal workflow.Context, in-memory contexts, etc.) and provides a uniform API for activity execution, signal handling, and observability.

Implementations must ensure deterministic replay: operations that interact with the workflow engine (planner/tool activities and signal receivers) must produce deterministic results when replayed. Direct I/O, random number generation, or system time access within workflows violates determinism and causes workflow failures.

Thread-safety: WorkflowContext is bound to a single workflow execution and must not be shared across goroutines. Activity and signal operations are serialized by the workflow engine.

Lifecycle: Created by the engine when a workflow starts and remains valid until the workflow completes or fails. Do not cache WorkflowContext outside the workflow function scope.

func WorkflowContextFromContext

func WorkflowContextFromContext(ctx context.Context) WorkflowContext

WorkflowContextFromContext extracts a WorkflowContext from ctx if present. Returns nil if the context does not carry a workflow context. Engine adapters are responsible for attaching the workflow context via WithWorkflowContext.

type WorkflowDefinition

type WorkflowDefinition struct {
	// Name is the logical identifier registered with the engine (e.g., "AgentWorkflow").
	Name string
	// TaskQueue is the default queue used when starting new workflows. Workers
	// subscribe to this queue to receive workflow tasks.
	TaskQueue string
	// Handler is the workflow function invoked by the engine when the workflow executes.
	Handler WorkflowFunc
}

WorkflowDefinition binds a workflow handler to a logical name and default queue.

type WorkflowFunc

type WorkflowFunc func(ctx WorkflowContext, input *api.RunInput) (*api.RunOutput, error)

WorkflowFunc is the generated workflow entry point. It receives a WorkflowContext and a typed RunInput, returning a typed RunOutput. Implementations must be deterministic with respect to activity results.

type WorkflowHandle

type WorkflowHandle interface {
	// Wait blocks until the workflow completes and returns the typed result.
	// Returns an error if the workflow fails or is cancelled.
	Wait(ctx context.Context) (*api.RunOutput, error)

	// Signal sends an asynchronous message to the workflow. The workflow can listen
	// for signals using engine-specific APIs. Returns an error if the signal cannot
	// be delivered (e.g., workflow already completed).
	Signal(ctx context.Context, name string, payload any) error

	// Cancel requests cancellation of the workflow. The workflow's context will be
	// cancelled, and in-flight activities may be cancelled depending on the engine.
	// Returns an error if cancellation fails.
	Cancel(ctx context.Context) error
}

WorkflowHandle allows callers to interact with a running workflow. Returned by Engine.StartWorkflow, it provides methods to wait for completion, send signals, or cancel execution.

type WorkflowStartRequest

type WorkflowStartRequest struct {
	// ID is the workflow identifier, which must be unique within the engine scope.
	// Typically derived from the agent ID and a UUID.
	ID string
	// Workflow names the registered workflow definition to execute. Engines that
	// support multiple workflows (one per agent) require this field.
	Workflow string
	// TaskQueue selects the queue to schedule the workflow on. Workers listening
	// on this queue will pick up the workflow.
	TaskQueue string
	// Input is the typed payload passed to the workflow handler.
	Input *api.RunInput
	// RunTimeout bounds the total workflow execution time at the engine level.
	// Zero disables the engine-level timeout. Engines may map a positive value to
	// their native execution timeout/TTL (Temporal: WorkflowRunTimeout/ExecutionTimeout).
	RunTimeout time.Duration
	// Memo stores small diagnostic payloads alongside the workflow execution.
	// Engines like Temporal persist these for queries/visibility. Nil means no memo.
	Memo map[string]any
	// SearchAttributes captures indexed metadata used for visibility queries.
	// Nil means no attributes are set.
	SearchAttributes map[string]any
	// RetryPolicy controls automatic restarts of the workflow start attempt if
	// scheduling fails. Not to be confused with activity retries.
	RetryPolicy RetryPolicy
}

WorkflowStartRequest describes how to launch a workflow execution. Generated code constructs these when agents are invoked.

Directories

Path Synopsis
Package enginetest provides reusable behavioral tests for workflow engines.
Package enginetest provides reusable behavioral tests for workflow engines.
Package errtypes owns engine error values without activity or workflow dependencies.
Package errtypes owns engine error values without activity or workflow dependencies.
Package inmem provides an in-memory workflow engine implementation for tests and local development.
Package inmem provides an in-memory workflow engine implementation for tests and local development.
internal
activity
Package activity preserves the agent adapter's plain JSON payload contract.
Package activity preserves the agent adapter's plain JSON payload contract.
Package selvedge implements the engine contract on a Selvedge App backed by SQLite or PostgreSQL.
Package selvedge implements the engine contract on a Selvedge App backed by SQLite or PostgreSQL.
Package temporal implements the loom-mcp workflow engine adapter backed by Temporal (https://temporal.io).
Package temporal implements the loom-mcp workflow engine adapter backed by Temporal (https://temporal.io).

Jump to

Keyboard shortcuts

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