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
- Variables
- func CompactionOperationID(runID string, step int, trigger compaction.Trigger, reason compaction.Reason, ...) string
- func ContextPressureDisplayStatus(pressure contextpolicy.ContextPressure) string
- func ContextPressureThresholdRatio(pressure contextpolicy.ContextPressure) float64
- func ContextPressureUsedRatio(pressure contextpolicy.ContextPressure) float64
- func WithFailureOrigin(err error, origin FailureOrigin) error
- type BudgetMetrics
- type CompactionCommitRequest
- type CompactionCommitter
- type CompactionManager
- type CompactionRequest
- type CompletionReason
- type Config
- type ContextCompactionResult
- type ContextPressureTracker
- func (t *ContextPressureTracker) ConsumePendingCompaction() (contextpolicy.ContextPressure, bool)
- func (t *ContextPressureTracker) ObserveSuccess(req provider.Request, history []session.Message, usage provider.Usage) (contextpolicy.ContextPressure, PressureAnchorState)
- func (t *ContextPressureTracker) Overflow(policy contextpolicy.Policy) contextpolicy.ContextPressure
- func (t *ContextPressureTracker) Project(req provider.Request, history []session.Message) contextpolicy.ContextPressure
- func (t *ContextPressureTracker) SetAnchor(anchor PressureAnchorState, request cache.ProviderRequestRecord)
- type ContinuationReason
- type ControlDisposition
- type ControlSignal
- type ControlSpec
- type EffectResultFinalizationRequest
- type EffectResultFinalizationResult
- type EffectResultFinalizer
- type Engine
- func (e *Engine) CompactContext(ctx context.Context, input RunInput, manual ManualCompactionRequest) (result ContextCompactionResult)
- func (e *Engine) Options() Options
- func (e *Engine) Run(ctx context.Context, userText string) Result
- func (e *Engine) RunTurn(ctx context.Context, input RunInput) Result
- func (e *Engine) SetSink(sink event.Sink)
- func (e *Engine) SetStopHook(hook StopHook)
- func (e *Engine) WithOptions(options Options) (*Engine, error)
- type ExecutionIdentity
- type FailureOrigin
- type LocalCompactionManager
- type ManualCompactionPollRequest
- type ManualCompactionRequest
- type ManualCompactionSource
- type Options
- type PressureAnchorState
- type ProviderToolSurface
- type ProviderUsageContextStatus
- type RequestShapeHashes
- type Result
- type RunDecision
- type RunInput
- type RunLabels
- type RunMetrics
- type Status
- type SteeringControl
- type StepMetrics
- type StepOutput
- type StopHook
- type StopHookContext
- type StopHookResult
- type ToolSurface
- type ToolSurfaceProvider
- type ToolSurfaceRequest
- type TurnSupplementalContextItem
Constants ¶
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" )
const ( MaxTurnSupplementalContextItems = 128 MaxTurnSupplementalContextKindRunes = 128 MaxTurnSupplementalContextTitleRunes = 256 MaxTurnSupplementalContextTextRunes = 16_384 MaxTurnSupplementalMetadataPairs = 32 MaxTurnSupplementalMetadataKeyBytes = 128 MaxTurnSupplementalMetadataValueRunes = 4_096 MaxTurnSupplementalPayloadBytes = 256 * 1024 )
Variables ¶
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") ErrInvalidContextBudget = errors.New("invalid context budget: output reservation must be smaller than the context window") ErrFixedContextOverBudget = errors.New("provider request fixed context overhead exceeds context budget") ErrCompactionNoop = errors.New("context compaction is not needed") )
var ErrContextWouldOverflow = errors.New("provider request would exceed context window")
Functions ¶
func CompactionOperationID ¶
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
func WithFailureOrigin ¶ added in v7.26.0
func WithFailureOrigin(err error, origin FailureOrigin) error
WithFailureOrigin preserves a source classification across Engine callbacks.
Types ¶
type BudgetMetrics ¶
type BudgetMetrics struct {
Type string `json:"type"`
Used float64 `json:"used"`
Limit float64 `json:"limit"`
Run RunMetrics `json:"run"`
}
type CompactionCommitRequest ¶
type CompactionCommitRequest struct {
CompactionRequest
Result compaction.Result
ActiveMessages []session.Message
Completion event.Event
RequestEstimate contextpolicy.RequestEstimate
ContextPressure contextpolicy.ContextPressure
}
type CompactionCommitter ¶
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 CompletionReason ¶
type CompletionReason string
const ( CompletionReasonNaturalStop CompletionReason = "natural_stop" CompletionReasonHookStop CompletionReason = "hook_stop" )
type ContextCompactionResult ¶
type ContextCompactionResult struct {
FailureOrigin FailureOrigin
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 (t *ContextPressureTracker) ObserveSuccess(req provider.Request, history []session.Message, usage provider.Usage) (contextpolicy.ContextPressure, PressureAnchorState)
func (*ContextPressureTracker) Overflow ¶
func (t *ContextPressureTracker) Overflow(policy contextpolicy.Policy) contextpolicy.ContextPressure
func (*ContextPressureTracker) Project ¶
func (t *ContextPressureTracker) Project(req provider.Request, history []session.Message) contextpolicy.ContextPressure
func (*ContextPressureTracker) SetAnchor ¶
func (t *ContextPressureTracker) SetAnchor(anchor PressureAnchorState, request cache.ProviderRequestRecord)
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" )
type ControlSignal ¶
type ControlSignal struct {
Disposition ControlDisposition
Name string
CallID string
ErrorCode string
Payload map[string]any
Activity *tools.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 []tools.ToolDefinition
Project func(provider.ToolCall) (ControlSignal, bool, error)
}
func DefaultControlSpec ¶
func DefaultControlSpec() ControlSpec
type EffectResultFinalizer ¶
type EffectResultFinalizer func(context.Context, EffectResultFinalizationRequest) (EffectResultFinalizationResult, error)
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
func (*Engine) CompactContext ¶
func (e *Engine) CompactContext(ctx context.Context, input RunInput, manual ManualCompactionRequest) (result ContextCompactionResult)
func (*Engine) SetSink ¶
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 ¶
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.
type ExecutionIdentity ¶
type FailureOrigin ¶
type FailureOrigin string
const ( FailureOriginNone FailureOrigin = "" FailureOriginCancelled FailureOrigin = "cancelled" FailureOriginProvider FailureOrigin = "provider" FailureOriginToolDispatch FailureOrigin = "tool_dispatch" FailureOriginControl FailureOrigin = "control" FailureOriginStorage FailureOrigin = "storage" FailureOriginContract FailureOrigin = "contract" )
func (FailureOrigin) Valid ¶
func (o FailureOrigin) Valid() bool
type LocalCompactionManager ¶
type LocalCompactionManager struct {
Generator compaction.SummaryGenerator
Now func() time.Time
}
func (LocalCompactionManager) Compact ¶
func (m LocalCompactionManager) Compact(ctx context.Context, req CompactionRequest) (compaction.Result, []session.Message, error)
type ManualCompactionRequest ¶
type ManualCompactionSource ¶
type ManualCompactionSource interface {
PollManualCompaction(context.Context, ManualCompactionPollRequest) (ManualCompactionRequest, bool, error)
}
type Options ¶
type Options struct {
Steering *SteeringControl
// RetrySourceEntryID is supplied only by the canonical harness retry admission.
RetrySourceEntryID string
RunID string
LogicalRequestID string
ThreadID string
TurnID string
TraceID string
PromptScopeID string
ProviderName string
Model string
StateCompatibilityKey string
Labels RunLabels
CacheNamespace string
CacheRetention cache.Retention
ContextPolicy contextpolicy.Policy
Reasoning provider.ReasoningSelection
ProviderRequestTimeoutPolicy providerattempt.RequestTimeoutPolicy
ProviderRetryPolicy providerretry.Policy
MaxEmptyProviderRetries int
NoProgressLimit int
DuplicateToolLimit int
WallTime time.Duration
MaxInputTokens int64
MaxTotalTokens int64
MaxCostUSD float64
MaxToolCalls int
HostedToolDefinitions []provider.HostedToolDefinition
ControlSpec ControlSpec
PreviousProviderState *provider.State
AdmitParentEvents func(context.Context) (session.Message, error)
AdmitModelContext func(context.Context, *session.ModelContextSnapshot) (session.Message, error)
CommitProviderContext func(context.Context, *provider.State, []session.Message, bool) error
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 ProviderToolSurface ¶ added in v7.18.0
type ProviderToolSurface struct {
SystemPrompt string
ToolDefinitions []tools.ToolDefinition
HostedToolDefinitions []provider.HostedToolDefinition
}
ProviderToolSurface is detached model-visible configuration from a checkpoint.
type ProviderUsageContextStatus ¶
type ProviderUsageContextStatus struct {
PayloadFingerprint string `json:"payload_fingerprint,omitempty"`
AttemptID string `json:"attempt_id,omitempty"`
AttemptEpoch int `json:"attempt_epoch,omitempty"`
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"`
}
func ProviderUsageContextStatusFromMetadata ¶
func ProviderUsageContextStatusFromMetadata(metadata any) (ProviderUsageContextStatus, bool)
ProviderUsageContextStatusFromMetadata reads the final provider-usage payload from the attempt-scoped event metadata emitted by Engine.
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
Metadata map[string]any
}
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 SteeringControl ¶ added in v7.36.0
type SteeringControl struct {
CanContinue func(error) bool
Begin func(context.Context, func() error) (context.Context, []session.Message, error)
Finish func(context.Context) (bool, error)
}
SteeringControl belongs to the thread actor. Begin installs one interruptible execution fragment and returns refreshed canonical history only after steering was applied. Finish atomically seals completion or asks the Engine to continue.
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 {
Sources []citation.Source
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
// contains filtered or unexported fields
}
type StopHook ¶
type StopHook func(context.Context, StopHookContext) (StopHookResult, error)
type StopHookContext ¶
type StopHookResult ¶
type ToolSurface ¶
type ToolSurface struct {
ModelContextSnapshot *session.ModelContextSnapshot
// RefreshProviderSurface opts into resolving system text and tool definitions
// at each provider request. Model, reasoning, context policy and historical
// checkpoints stay fixed. The zero value preserves the first Turn surface.
RefreshProviderSurface bool
Tools *tools.Registry
ToolDefinitions []tools.ToolDefinition
HostedToolDefinitions []provider.HostedToolDefinition
SystemPrompt string
HostContext map[string]string
Epoch string
Reason string
}
type ToolSurfaceProvider ¶
type ToolSurfaceProvider func(context.Context, ToolSurfaceRequest) (ToolSurface, error)
type ToolSurfaceRequest ¶
type ToolSurfaceRequest struct {
// InitialProviderSurface is the first checkpoint of this Turn, or nil before
// the first provider checkpoint. Hosts can retain capabilities unrelated to a live policy change.
// Mutating it cannot alter stored requests.
InitialProviderSurface *ProviderToolSurface
RunID string
ThreadID string
TurnID string
TraceID string
PromptScopeID string
Step int
Phase string
Labels RunLabels
HostContext map[string]string
}
type TurnSupplementalContextItem ¶
type TurnSupplementalContextItem struct {
Kind string
Title string
Text string
Metadata map[string]string
Sensitive bool
Truncated bool
}
func CloneTurnSupplementalContext ¶
func CloneTurnSupplementalContext(in []TurnSupplementalContextItem) []TurnSupplementalContextItem
func NormalizeAndValidateTurnSupplementalContext ¶
func NormalizeAndValidateTurnSupplementalContext(in []TurnSupplementalContextItem) ([]TurnSupplementalContextItem, error)
NormalizeAndValidateTurnSupplementalContext is the single normalization contract shared by runtime admission and Engine rendering.
Source Files
¶
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. |