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.SessionID, or a distinct Options.RunID when SessionID 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 ContextPressureDisplayStatus(pressure contextpolicy.ContextPressure) string
- func ContextPressureThresholdRatio(pressure contextpolicy.ContextPressure) float64
- func ContextPressureUsedRatio(pressure contextpolicy.ContextPressure) float64
- type BudgetMetrics
- type CompactionManager
- type CompactionRequest
- type CompletionPolicy
- type CompletionReason
- type Config
- 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)
- type ContinuationReason
- type ControlDisposition
- type ControlSignal
- type ControlSpec
- type Engine
- 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) SetApprover(approver tools.Approver)
- func (e *Engine) SetSink(sink event.Sink)
- func (e *Engine) SetStopHook(hook StopHook)
- func (e *Engine) WithOptions(options Options) (*Engine, error)
- type LocalCompactionManager
- type Options
- type PressureAnchorState
- type ProviderUsageContextStatus
- type RequestShapeHashes
- type Result
- type RunDecision
- type RunInput
- type RunLabels
- type RunMetrics
- type Status
- type StepMetrics
- type StepOutput
- type StopHook
- type StopHookContext
- type StopHookResult
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" )
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") ErrMixedControlTools = errors.New("provider returned control signal with ordinary tool calls") 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") )
var ErrContextWouldOverflow = errors.New("provider request would exceed context window")
Functions ¶
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 CompactionManager ¶
type CompactionManager interface {
Compact(context.Context, CompactionRequest) (compaction.Result, []session.Message, error)
}
type CompactionRequest ¶
type CompactionRequest struct {
RunID string
SessionID string
TraceID string
Step int
History []session.Message
Policy contextpolicy.Policy
Trigger compaction.Trigger
Reason compaction.Reason
Phase compaction.Phase
Provider provider.Provider
ProviderName string
Model string
PreviousCompactionID 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.Store
Prompt cache.Store
// Artifacts is required when a tool output policy preserves truncated full
// output. Runtime and harness callers provide a default store; direct engine
// callers may leave it nil only if their tools do not need preservation.
Artifacts artifact.Store
SystemPrompt string
Sink event.Sink
Approver tools.Approver
StopHook StopHook
Compactor CompactionManager
Options Options
}
type ContextPressureTracker ¶
type ContextPressureTracker struct {
// contains filtered or unexported fields
}
func NewContextPressureTracker ¶
func NewContextPressureTracker(sessionID 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)
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
// 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 Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
func (*Engine) SetApprover ¶
SetApprover replaces the tool approver for subsequent runs. It is a host wiring hook and must not be called concurrently with an active Run or RunTurn.
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 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 Options ¶
type Options struct {
RunID string
SessionID string
TraceID string
ProviderName string
Model string
Labels RunLabels
CacheNamespace string
CacheRetention cache.Retention
ContextPolicy contextpolicy.Policy
MaxEmptyProviderRetries int
NoProgressLimit int
DuplicateToolLimit int
WallTime time.Duration
MaxTotalTokens int64
MaxCostUSD float64
MaxToolCalls int
HostedToolDefinitions []provider.HostedToolDefinition
CompletionPolicy CompletionPolicy
ControlSpec ControlSpec
PreviousProviderState *provider.State
MaxLengthContinuations int
MaxStopHookContinuations int
// 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
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
}
type RunDecision ¶
type RunDecision struct {
CompletionReason CompletionReason
ContinuationReason ContinuationReason
FinishReason provider.FinishReason
RawFinishReason string
FinishInferred bool
Detail string
ControlSignal *ControlSignal
ProviderState *provider.State
}
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 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 StopHook ¶
type StopHook func(context.Context, StopHookContext) (StopHookResult, error)
type StopHookContext ¶
type StopHookResult ¶
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. |