hooks

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package hooks defines lifecycle hook interfaces for the ReAcTree execution engine.

Hooks are observer-style callbacks invoked at well-defined points during tree execution. They allow external packages (audit, telemetry, debugging) to react to execution lifecycle events without modifying the core loop.

Key design decisions:

  • Event structs use primitive types only — no reactree imports allowed, preventing circular dependencies.
  • Hooks are fire-and-forget (no return values that influence control flow). Control-flow decisions like reflection-based halting belong in the reflector interface, not in hooks.
  • All methods must be safe to call concurrently.
  • Implementations should be cheap; expensive work should be dispatched asynchronously.

Index

Constants

This section is empty.

Variables

View Source
var CompactionTrackerKey = compactionTrackerKey{}

CompactionTrackerKey is the context key for passing a CompactionTracker.

Functions

func EstimateHistoryTokens added in v0.1.6

func EstimateHistoryTokens(prompt string) int

EstimateHistoryTokens returns a rough token count for the prompt / message history string.

func EstimatePersonaTokens added in v0.1.6

func EstimatePersonaTokens(personaText string) int

EstimatePersonaTokens returns a rough token count for the persona text. Uses the common heuristic of ~4 chars per token.

func EstimateToolSchemaTokens added in v0.1.6

func EstimateToolSchemaTokens(names []string, descriptions []string) int

EstimateToolSchemaTokens returns a rough token count for a set of tool declarations by summing name + description lengths.

Types

type ChainHook

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

ChainHook fans out lifecycle events to multiple ExecutionHook implementations. This allows composing audit hooks, telemetry hooks, debug hooks, etc. into a single injectable dependency.

func NewChainHook

func NewChainHook(hooks ...ExecutionHook) *ChainHook

NewChainHook creates a ChainHook from the provided hooks. Nil entries are silently skipped.

func (*ChainHook) OnCompactionMiss added in v0.1.6

func (c *ChainHook) OnCompactionMiss(ctx context.Context, event CompactionMissEvent)

func (*ChainHook) OnContextBudget added in v0.1.6

func (c *ChainHook) OnContextBudget(ctx context.Context, event ContextBudgetEvent)

func (*ChainHook) OnDryRun

func (c *ChainHook) OnDryRun(ctx context.Context, event DryRunEvent)

func (*ChainHook) OnIterationEnd

func (c *ChainHook) OnIterationEnd(ctx context.Context, event IterationEndEvent)

func (*ChainHook) OnIterationStart

func (c *ChainHook) OnIterationStart(ctx context.Context, event IterationStartEvent)

func (*ChainHook) OnPlanExecution

func (c *ChainHook) OnPlanExecution(ctx context.Context, event PlanExecutionEvent)

func (*ChainHook) OnReflection

func (c *ChainHook) OnReflection(ctx context.Context, event ReflectionEvent)

func (*ChainHook) OnToolValidation

func (c *ChainHook) OnToolValidation(ctx context.Context, event ToolValidationEvent)

type CompactionMissEvent added in v0.1.6

type CompactionMissEvent struct {
	ToolName       string
	OriginalSize   int
	CompressedSize int
}

CompactionMissEvent is fired when a tool is re-invoked identically after its prior output was compressed.

type CompactionTracker added in v0.1.6

type CompactionTracker interface {
	MarkCompressed(toolName string, originalSize, compressedSize int)
	// GetChunkBoost returns a multiplier (>1) for max_chunks on a tool that
	// previously caused a compaction miss, or 0 if no boost is needed.
	GetChunkBoost(toolName string) int
}

CompactionTracker allows middlewares to signal that compaction occurred and to query adaptive chunk-boost hints set by the loop on compaction misses.

type ContextBudgetEvent added in v0.1.6

type ContextBudgetEvent struct {
	PersonaTokens    int
	ToolSchemaTokens int
	HistoryTokens    int
	TotalTokens      int
	MaxTokens        int
	UtilizationPct   float64
}

ContextBudgetEvent is fired before making a model request to report token usage. Use NewContextBudgetEvent to construct one from raw inputs; the helper functions EstimatePersonaTokens / EstimateToolSchemaTokens / EstimateHistoryTokens perform the heuristic word-to-token conversion.

func NewContextBudgetEvent added in v0.1.6

func NewContextBudgetEvent(personaTokens, toolSchemaTokens, historyTokens int) ContextBudgetEvent

NewContextBudgetEvent constructs a fully populated ContextBudgetEvent from the three token components.

func (ContextBudgetEvent) IsOverBudget added in v0.1.6

func (e ContextBudgetEvent) IsOverBudget() bool

IsOverBudget reports whether context utilization exceeds the warning threshold (85%).

type DryRunEvent

type DryRunEvent struct {
	PlannedSteps  int
	ToolsUsed     []string
	EstimatedCost string
}

DryRunEvent is fired when a dry run simulation completes.

type ExecutionHook

type ExecutionHook interface {
	// OnIterationStart is called before each adaptive loop iteration executes.
	OnIterationStart(ctx context.Context, event IterationStartEvent)

	// OnIterationEnd is called after each adaptive loop iteration completes,
	// with the iteration's result status and tool usage.
	OnIterationEnd(ctx context.Context, event IterationEndEvent)

	// OnReflection is called after a RAR reflection step completes.
	OnReflection(ctx context.Context, event ReflectionEvent)

	// OnToolValidation is called when the critic middleware evaluates a tool call.
	OnToolValidation(ctx context.Context, event ToolValidationEvent)

	// OnDryRun is called when a dry run simulation completes.
	OnDryRun(ctx context.Context, event DryRunEvent)

	// OnPlanExecution is called when the orchestrator starts executing a plan.
	OnPlanExecution(ctx context.Context, event PlanExecutionEvent)

	// OnContextBudget is called before each model execution to report token utilization.
	OnContextBudget(ctx context.Context, event ContextBudgetEvent)

	// OnCompactionMiss is called when the runner detects a likely infinite loop caused by output compaction.
	OnCompactionMiss(ctx context.Context, event CompactionMissEvent)
}

ExecutionHook defines lifecycle callbacks for ReAcTree execution. All methods are optional — implementations may embed NoOpHook to get default no-op behavior and override only the hooks they care about.

type IterationEndEvent

type IterationEndEvent struct {
	Iteration      int
	Status         string         // "running", "success", "failure"
	ToolCallCounts map[string]int // per-tool call counts for this iteration
	TaskCompleted  bool
	Output         string
}

IterationEndEvent is fired at the end of each adaptive loop iteration.

type IterationStartEvent

type IterationStartEvent struct {
	Goal          string
	Iteration     int
	MaxIterations int
}

IterationStartEvent is fired at the beginning of each adaptive loop iteration.

type NoOpHook

type NoOpHook struct{}

NoOpHook is a default implementation that does nothing. Embed this in custom hook implementations to avoid implementing every method.

func (NoOpHook) OnCompactionMiss added in v0.1.6

func (NoOpHook) OnCompactionMiss(_ context.Context, _ CompactionMissEvent)

func (NoOpHook) OnContextBudget added in v0.1.6

func (NoOpHook) OnContextBudget(_ context.Context, _ ContextBudgetEvent)

func (NoOpHook) OnDryRun

func (NoOpHook) OnDryRun(_ context.Context, _ DryRunEvent)

func (NoOpHook) OnIterationEnd

func (NoOpHook) OnIterationEnd(_ context.Context, _ IterationEndEvent)

func (NoOpHook) OnIterationStart

func (NoOpHook) OnIterationStart(_ context.Context, _ IterationStartEvent)

func (NoOpHook) OnPlanExecution

func (NoOpHook) OnPlanExecution(_ context.Context, _ PlanExecutionEvent)

func (NoOpHook) OnReflection

func (NoOpHook) OnReflection(_ context.Context, _ ReflectionEvent)

func (NoOpHook) OnToolValidation

func (NoOpHook) OnToolValidation(_ context.Context, _ ToolValidationEvent)

type PlanExecutionEvent

type PlanExecutionEvent struct {
	Flow      string // "sequence", "parallel", "fallback"
	StepCount int
	StepNames []string
}

PlanExecutionEvent is fired when the orchestrator starts executing a plan.

type ReflectionEvent

type ReflectionEvent struct {
	Iteration     int
	Monologue     string
	ShouldProceed bool
}

ReflectionEvent is fired after a RAR reflection step completes.

type ToolValidationEvent

type ToolValidationEvent struct {
	ToolName string
	Allowed  bool
	Reason   string // non-empty when Allowed=false
}

ToolValidationEvent is fired when the critic middleware validates a tool call.

Jump to

Keyboard shortcuts

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