engine

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package engine implements the low-level prompt-first turn executor.

Engine instances execute turns from local run state. Each Run and RunTurn builds local turn state and may execute concurrently when shared dependencies are safe for concurrent use. The supported concurrent scope is a distinct Options.ThreadID, or a distinct Options.RunID when ThreadID is omitted. Providers, prompt cache stores, session stores, tool registries, event sinks, approvers, and compaction managers supplied by callers must honor their own concurrency contracts.

Hosts that need durable conversations should prefer runtime.NewHarness or agentharness.AgentHarness. Direct Engine use is intended for tests, eval runners, and specialized hosts that already own session persistence.

Construct engines with New(Config). Provider-visible local tool definitions are derived from tools.Registry so registry validation, permission policy, and deny-tool hiding remain the single local-tool boundary.

Index

Constants

View Source
const (
	ProviderUsagePhaseStreamUsage        = "stream_usage"
	ProviderUsagePhaseFinalContextStatus = "final_context_status"

	ContextStatusStable        = "stable"
	ContextStatusNearThreshold = "near_threshold"
	ContextStatusWillCompact   = "will_compact"
	ContextStatusHardLimit     = "hard_limit"
	ContextStatusEstimated     = "estimated"

	ContextCompactPhaseStart     = "start"
	ContextCompactPhaseComplete  = "complete"
	ContextCompactPhaseFailed    = "failed"
	ContextCompactPhaseCancelled = "cancelled"
	ContextCompactPhaseNoop      = "noop"

	ContextCompactDebugStageBegin                   = "begin"
	ContextCompactDebugStagePoll                    = "poll"
	ContextCompactDebugStagePreflight               = "preflight"
	ContextCompactDebugStageGenerateAttemptStart    = "generate_attempt_start"
	ContextCompactDebugStageGenerateAttemptComplete = "generate_attempt_complete"
	ContextCompactDebugStageRequestRebuildStart     = "request_rebuild_start"
	ContextCompactDebugStageRequestRebuildComplete  = "request_rebuild_complete"
	ContextCompactDebugStageRequestValidation       = "request_validation"
	ContextCompactDebugStageInstallStart            = "install_start"
	ContextCompactDebugStageInstallComplete         = "install_complete"

	ContextCompactDebugStatusRunning   = "running"
	ContextCompactDebugStatusOK        = "ok"
	ContextCompactDebugStatusRetrying  = "retrying"
	ContextCompactDebugStatusFailed    = "failed"
	ContextCompactDebugStatusCancelled = "cancelled"

	ContextCompactDebugNextActionProviderRequest        = "provider_request"
	ContextCompactDebugNextActionReturnCompactedContext = "return_compacted_context"
	ContextCompactDebugNextActionFailTurn               = "fail_turn"
)
View Source
const (
	MaxTurnSupplementalContextItems       = 128
	MaxTurnSupplementalContextKindRunes   = 128
	MaxTurnSupplementalContextTitleRunes  = 256
	MaxTurnSupplementalContextTextRunes   = 16_384
	MaxTurnSupplementalMetadataPairs      = 32
	MaxTurnSupplementalMetadataKeyBytes   = 128
	MaxTurnSupplementalMetadataValueRunes = 4_096
	MaxTurnSupplementalPayloadBytes       = 256 * 1024
)

Variables

View Source
var (
	ErrNoProgress                 = errors.New("agent loop made no progress")
	ErrDuplicateTools             = errors.New("agent loop repeated identical tool calls")
	ErrDuplicateToolCallID        = errors.New("provider returned duplicate tool call id")
	ErrProviderTruncated          = errors.New("provider output was truncated")
	ErrContentFiltered            = errors.New("provider output was content filtered")
	ErrProviderFinishError        = errors.New("provider returned error finish reason")
	ErrStopHookLoop               = errors.New("stop hook requested too many continuations")
	ErrInvalidTokenEstimate       = errors.New("provider token estimate missing source or method")
	ErrInputTokenBudgetExceeded   = errors.New("provider request exceeds input token budget")
	ErrCompactedRequestOverBudget = errors.New("compacted provider request still exceeds context budget")
	ErrFixedContextOverBudget     = errors.New("provider request fixed context overhead exceeds context budget")
	ErrCompactionNoop             = errors.New("context compaction is not needed")
)
View Source
var ErrContextWouldOverflow = errors.New("provider request would exceed context window")

Functions

func CompactionOperationID added in v0.3.33

func CompactionOperationID(runID string, step int, trigger compaction.Trigger, reason compaction.Reason, requestID string) string

CompactionOperationID returns the engine identity used to correlate one logical compaction lifecycle across start, debug, complete, and failed events.

func ContextPressureDisplayStatus

func ContextPressureDisplayStatus(pressure contextpolicy.ContextPressure) string

func ContextPressureThresholdRatio

func ContextPressureThresholdRatio(pressure contextpolicy.ContextPressure) float64

func ContextPressureUsedRatio

func ContextPressureUsedRatio(pressure contextpolicy.ContextPressure) float64

Types

type BudgetMetrics

type BudgetMetrics struct {
	Type  string     `json:"type"`
	Used  float64    `json:"used"`
	Limit float64    `json:"limit"`
	Run   RunMetrics `json:"run"`
}

type CompactionCommitRequest added in v0.3.24

type CompactionCommitRequest struct {
	CompactionRequest
	Result         compaction.Result
	ActiveMessages []session.Message
}

type CompactionCommitter added in v0.3.24

type CompactionCommitter interface {
	CommitCompaction(context.Context, CompactionCommitRequest) (compaction.Result, []session.Message, error)
}

type CompactionManager

type CompactionManager interface {
	Compact(context.Context, CompactionRequest) (compaction.Result, []session.Message, error)
}

type CompactionRequest

type CompactionRequest struct {
	RunID                     string
	ThreadID                  string
	TurnID                    string
	TraceID                   string
	PromptScopeID             string
	Step                      int
	OperationID               string
	RequestID                 string
	Source                    string
	SupplementalAnchorEntryID string
	History                   []session.Message
	Policy                    contextpolicy.Policy
	Trigger                   compaction.Trigger
	Reason                    compaction.Reason
	Phase                     compaction.Phase
	Provider                  provider.Provider
	ProviderName              string
	Model                     string
	PreviousCompactionID      string
	PreviousGeneration        int
	PreviousWindowID          string
	PreviousSummary           string
	ContextUsage              contextpolicy.Usage
	Details                   map[string]string
}

type CompletionPolicy

type CompletionPolicy string
const (
	CompletionNaturalStop    CompletionPolicy = "natural_stop"
	CompletionExplicitSignal CompletionPolicy = "explicit_signal"
)

type CompletionReason

type CompletionReason string
const (
	CompletionReasonNaturalStop CompletionReason = "natural_stop"
	CompletionReasonToolSignal  CompletionReason = "tool_signal"
	CompletionReasonHookStop    CompletionReason = "hook_stop"
)

type Config

type Config struct {
	Provider     provider.Provider
	Tools        *tools.Registry
	Store        session.TranscriptStore
	Prompt       cache.Store
	SystemPrompt string
	Sink         event.Sink
	StopHook     StopHook
	Compactor    CompactionManager
	Options      Options
}

type ContextCompactionResult added in v0.3.27

type ContextCompactionResult struct {
	Status        Status
	Err           error
	Metrics       RunMetrics
	Messages      []session.Message
	Compaction    compaction.Result
	ProviderState *provider.State
}

type ContextPressureTracker

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

func NewContextPressureTracker

func NewContextPressureTracker(promptScopeID string) *ContextPressureTracker

func (*ContextPressureTracker) ConsumePendingCompaction

func (t *ContextPressureTracker) ConsumePendingCompaction() (contextpolicy.ContextPressure, bool)

func (*ContextPressureTracker) ObserveSuccess

func (*ContextPressureTracker) Overflow

func (*ContextPressureTracker) Project

func (*ContextPressureTracker) SetAnchor

func (t *ContextPressureTracker) SetAnchor(anchor PressureAnchorState)

type ContinuationReason

type ContinuationReason string
const (
	ContinueToolResults       ContinuationReason = "tool_results"
	ContinueCompaction        ContinuationReason = "compaction"
	ContinueProviderTruncated ContinuationReason = "provider_truncated"
	ContinueRetryEmpty        ContinuationReason = "retry_empty"
	ContinueNoProgress        ContinuationReason = "no_progress"
	ContinueHook              ContinuationReason = "hook"
)

type ControlDisposition

type ControlDisposition string
const (
	// ControlContinue asks the engine to append OutputText as a provider-visible
	// synthetic tool result and continue the run.
	ControlContinue ControlDisposition = "continue"
	ControlWaiting  ControlDisposition = "waiting"
	ControlTerminal ControlDisposition = "terminal"
)

type ControlSignal

type ControlSignal struct {
	Disposition ControlDisposition
	Name        string
	CallID      string
	Payload     map[string]any
	Activity    *observation.ActivityPresentation
	// OutputText is the human-readable control result. For ControlContinue it is
	// provider-visible; host-only details must stay in Payload.
	OutputText string
	ArgsHash   string
	Labels     map[string]string
}

type ControlSpec

type ControlSpec struct {
	Definitions []provider.ToolDefinition
	Project     func(provider.ToolCall) (ControlSignal, bool, error)
}

func DefaultControlSpec

func DefaultControlSpec(policy CompletionPolicy) ControlSpec

type EffectResultFinalizationRequest added in v0.18.0

type EffectResultFinalizationRequest struct {
	RunID      string
	ThreadID   string
	TurnID     string
	ToolCallID string
	Message    session.Message
	FullOutput *artifact.FullOutput
}

type EffectResultFinalizationResult added in v0.18.0

type EffectResultFinalizationResult struct {
	Handled          bool
	Message          session.Message
	Replayed         bool
	CanonicalEntryID string
}

type Engine

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

func New

func New(cfg Config) (*Engine, error)

func (*Engine) CompactContext added in v0.3.27

func (e *Engine) CompactContext(ctx context.Context, input RunInput, manual ManualCompactionRequest) ContextCompactionResult

func (*Engine) Options

func (e *Engine) Options() Options

func (*Engine) Run

func (e *Engine) Run(ctx context.Context, userText string) Result

func (*Engine) RunTurn

func (e *Engine) RunTurn(ctx context.Context, input RunInput) Result

func (*Engine) SetSink

func (e *Engine) SetSink(sink event.Sink)

SetSink replaces the event sink for subsequent runs. It is a host wiring hook and must not be called concurrently with an active Run or RunTurn.

func (*Engine) SetStopHook

func (e *Engine) SetStopHook(hook StopHook)

SetStopHook replaces the stop hook for subsequent runs. It is a host wiring hook and must not be called concurrently with an active Run or RunTurn.

func (*Engine) WithOptions

func (e *Engine) WithOptions(options Options) (*Engine, error)

type ExecutionIdentity

type ExecutionIdentity struct {
	RunID         string
	ThreadID      string
	TurnID        string
	TraceID       string
	PromptScopeID string
}

type FailureOrigin added in v0.20.0

type FailureOrigin string
const (
	FailureOriginNone         FailureOrigin = ""
	FailureOriginCancelled    FailureOrigin = "cancelled"
	FailureOriginProvider     FailureOrigin = "provider"
	FailureOriginToolDispatch FailureOrigin = "tool_dispatch"
	FailureOriginStorage      FailureOrigin = "storage"
	FailureOriginContract     FailureOrigin = "contract"
)

func (FailureOrigin) Valid added in v0.20.0

func (o FailureOrigin) Valid() bool

type LocalCompactionManager

type LocalCompactionManager struct {
	Generator compaction.SummaryGenerator
	Now       func() time.Time
}

func (LocalCompactionManager) Compact

type ManualCompactionPollRequest added in v0.3.27

type ManualCompactionPollRequest struct {
	RunID         string
	ThreadID      string
	TurnID        string
	TraceID       string
	PromptScopeID string
	Step          int
}

type ManualCompactionRequest added in v0.3.27

type ManualCompactionRequest struct {
	RequestID string
	Source    string
}

type ManualCompactionSource added in v0.3.27

type ManualCompactionSource interface {
	PollManualCompaction(context.Context, ManualCompactionPollRequest) (ManualCompactionRequest, bool, error)
}

type Options

type Options struct {
	RunID                    string
	ThreadID                 string
	TurnID                   string
	TraceID                  string
	PromptScopeID            string
	ProviderName             string
	Model                    string
	Labels                   RunLabels
	CacheNamespace           string
	CacheRetention           cache.Retention
	ContextPolicy            contextpolicy.Policy
	Reasoning                provider.ReasoningSelection
	MaxEmptyProviderRetries  int
	NoProgressLimit          int
	DuplicateToolLimit       int
	WallTime                 time.Duration
	MaxInputTokens           int64
	MaxTotalTokens           int64
	MaxCostUSD               float64
	MaxToolCalls             int
	HostedToolDefinitions    []provider.HostedToolDefinition
	CompletionPolicy         CompletionPolicy
	ControlSpec              ControlSpec
	PreviousProviderState    *provider.State
	MaxLengthContinuations   int
	MaxStopHookContinuations int
	ManualCompactions        ManualCompactionSource
	ToolSurfaceProvider      ToolSurfaceProvider
	EffectBatchPreflight     tools.EffectBatchPreflight
	EffectDispatcher         tools.EffectDispatcher
	EffectResultFinalizer    EffectResultFinalizer
	ProviderRequestGate      func(context.Context) (func(), error)
	SupplementalContext      []TurnSupplementalContextItem
	// contains filtered or unexported fields
}

type PressureAnchorState

type PressureAnchorState = cache.PressureAnchorState

type ProviderUsageContextStatus

type ProviderUsageContextStatus struct {
	Phase                string                        `json:"phase"`
	RequestID            string                        `json:"request_id,omitempty"`
	LogicalRequestID     string                        `json:"logical_request_id,omitempty"`
	Attempt              int                           `json:"attempt,omitempty"`
	Usage                provider.Usage                `json:"usage"`
	RequestEstimate      contextpolicy.RequestEstimate `json:"request_estimate"`
	ContextPressure      contextpolicy.ContextPressure `json:"context_pressure"`
	UsedRatio            float64                       `json:"used_ratio,omitempty"`
	ThresholdRatio       float64                       `json:"threshold_ratio,omitempty"`
	Status               string                        `json:"status"`
	CompactionGeneration int                           `json:"compaction_generation,omitempty"`
	CompactionWindowID   string                        `json:"compaction_window_id,omitempty"`
}

type RequestShapeHashes

type RequestShapeHashes = cache.RequestShapeHashes

type Result

type Result struct {
	Status             Status
	FailureOrigin      FailureOrigin
	Output             string
	Err                error
	Metrics            RunMetrics
	Messages           []session.Message
	CompletionReason   CompletionReason
	ContinuationReason ContinuationReason
	FinishReason       provider.FinishReason
	RawFinishReason    string
	FinishInferred     bool
	ControlSignal      *ControlSignal
	ProviderState      *provider.State
	ProviderStateFresh bool
}

type RunDecision

type RunDecision struct {
	CompletionReason   CompletionReason
	ContinuationReason ContinuationReason
	FinishReason       provider.FinishReason
	RawFinishReason    string
	FinishInferred     bool
	Detail             string
	ControlSignal      *ControlSignal
	ProviderState      *provider.State
	ProviderStateFresh bool
	Metadata           map[string]any
}

type RunInput

type RunInput struct {
	RunID                 string
	ThreadID              string
	TurnID                string
	TraceID               string
	PromptScopeID         string
	Labels                RunLabels
	PreviousProviderState *provider.State
	History               []session.Message
	SupplementalContext   []TurnSupplementalContextItem
}

type RunLabels

type RunLabels struct {
	Correlation map[string]string
	Host        map[string]string
}

type RunMetrics

type RunMetrics struct {
	Usage       provider.Usage `json:"usage"`
	Steps       int            `json:"steps"`
	LLMRequests int            `json:"llm_requests"`
	ToolCalls   int            `json:"tool_calls"`
	Compactions int            `json:"compactions"`
	Retries     int            `json:"retries"`
	WallTimeMS  int64          `json:"wall_time_ms,omitempty"`
}

func (*RunMetrics) AddUsage

func (m *RunMetrics) AddUsage(usage provider.Usage)

type Status

type Status string
const (
	Completed Status = "completed"
	Waiting   Status = "waiting"
	Failed    Status = "failed"
	Cancelled Status = "cancelled"
)

type StepMetrics

type StepMetrics struct {
	Step               int            `json:"step"`
	Provider           string         `json:"provider,omitempty"`
	Model              string         `json:"model,omitempty"`
	Usage              provider.Usage `json:"usage"`
	ProviderLatencyMS  int64          `json:"provider_latency_ms,omitempty"`
	ToolLatencyMS      int64          `json:"tool_latency_ms,omitempty"`
	ToolCalls          int            `json:"tool_calls,omitempty"`
	Retries            int            `json:"retries,omitempty"`
	FinishReason       string         `json:"finish_reason,omitempty"`
	RawFinishReason    string         `json:"raw_finish_reason,omitempty"`
	FinishInferred     bool           `json:"finish_inferred,omitempty"`
	CompletionReason   string         `json:"completion_reason,omitempty"`
	ContinuationReason string         `json:"continuation_reason,omitempty"`
}

type StepOutput

type StepOutput struct {
	Text            string
	Reasoning       string
	Calls           []provider.ToolCall
	Usage           provider.Usage
	ResponseID      string
	Retry           bool
	Truncated       bool
	FinishReason    provider.FinishReason
	RawFinishReason string
	FinishInferred  bool
	ResponseState   *provider.State
}

type StopHookContext

type StopHookContext struct {
	RunID         string
	ThreadID      string
	TurnID        string
	TraceID       string
	PromptScopeID string
	Step          int

	LastAssistant   session.Message
	Messages        []session.Message
	FinishReason    provider.FinishReason
	RawFinishReason string
	FinishInferred  bool
	Metrics         RunMetrics
}

type StopHookResult

type StopHookResult struct {
	Continue bool
	Prompt   string
	Reason   string
}

type ToolSurface added in v0.3.40

type ToolSurface struct {
	Tools                 *tools.Registry
	ToolDefinitions       []provider.ToolDefinition
	HostedToolDefinitions []provider.HostedToolDefinition
	SystemPrompt          string
	HostContext           map[string]string
	Epoch                 string
	Reason                string
}

type ToolSurfaceProvider added in v0.3.40

type ToolSurfaceProvider func(context.Context, ToolSurfaceRequest) (ToolSurface, error)

type ToolSurfaceRequest added in v0.3.40

type ToolSurfaceRequest struct {
	RunID         string
	ThreadID      string
	TurnID        string
	TraceID       string
	PromptScopeID string
	Step          int
	Phase         string
	Labels        RunLabels
	HostContext   map[string]string
}

type TurnSupplementalContextItem added in v0.3.89

type TurnSupplementalContextItem struct {
	Kind      string
	Title     string
	Text      string
	Metadata  map[string]string
	Sensitive bool
	Truncated bool
}

func CloneTurnSupplementalContext added in v0.3.89

func CloneTurnSupplementalContext(in []TurnSupplementalContextItem) []TurnSupplementalContextItem

func NormalizeAndValidateTurnSupplementalContext added in v0.20.0

func NormalizeAndValidateTurnSupplementalContext(in []TurnSupplementalContextItem) ([]TurnSupplementalContextItem, error)

NormalizeAndValidateTurnSupplementalContext is the single normalization contract shared by runtime admission and Engine rendering.

Directories

Path Synopsis
Package compaction adapts provider-backed summary generation for engine context compaction.
Package compaction adapts provider-backed summary generation for engine context compaction.

Jump to

Keyboard shortcuts

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