Documentation
¶
Overview ¶
Package subagents provides isolated execution contexts for subagent tasks.
ABOUTME: Context isolation for subagent execution ABOUTME: Ensures subagents run with separate conversation history and working memory
Index ¶
- func IsValid(t string) bool
- func ValidSubagentTypes() []string
- type Config
- type ContextManager
- func (m *ContextManager) CleanupOldContexts(maxAge time.Duration) int
- func (m *ContextManager) ContextCount() int
- func (m *ContextManager) CreateContext(parentID string, agentType SubagentType) *IsolatedContext
- func (m *ContextManager) DeleteContext(id string) error
- func (m *ContextManager) GetContext(id string) (*IsolatedContext, error)
- func (m *ContextManager) ListContexts() []string
- type DispatchRequest
- type DispatchResult
- type Dispatcher
- func (d *Dispatcher) DispatchBatch(ctx context.Context, requests []*DispatchRequest, batchSize int) []*DispatchResult
- func (d *Dispatcher) DispatchParallel(ctx context.Context, requests []*DispatchRequest) []*DispatchResult
- func (d *Dispatcher) DispatchSequential(ctx context.Context, requests []*DispatchRequest) []*DispatchResult
- func (d *Dispatcher) DispatchWithAggregation(ctx context.Context, requests []*DispatchRequest, ...) (interface{}, error)
- func (d *Dispatcher) WaitForAny(ctx context.Context, requests []*DispatchRequest) (*DispatchResult, error)
- type ExecutionContext
- type ExecutionRequest
- type Executor
- type HookEngine
- type IsolatedContext
- func (c *IsolatedContext) AddMessage(role, content string)
- func (c *IsolatedContext) Clear()
- func (c *IsolatedContext) GetMemory(key string) (interface{}, bool)
- func (c *IsolatedContext) GetMessages() []Message
- func (c *IsolatedContext) MessageCount() int
- func (c *IsolatedContext) SetMemory(key string, value interface{})
- type Message
- type Result
- type Statistics
- type SubagentType
- type ValidationError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ValidSubagentTypes ¶
func ValidSubagentTypes() []string
ValidSubagentTypes returns all valid subagent type strings
Types ¶
type Config ¶
type Config struct {
// Type is the subagent type (general-purpose, Explore, Plan, code-reviewer)
Type SubagentType
// Model is the Claude model to use (optional, inherits from parent if empty)
Model string
// Timeout is the maximum execution time (0 = use default)
Timeout time.Duration
// MaxTokens is the maximum response length (0 = use default)
MaxTokens int
// Temperature controls randomness (0.0-1.0, 0 = use default)
Temperature float64
// AllowedTools restricts which tools this subagent can use (nil = all tools)
AllowedTools []string
// SystemPrompt overrides the default system prompt for this type (optional)
SystemPrompt string
}
Config holds configuration for a subagent instance
func DefaultConfig ¶
func DefaultConfig(t SubagentType) *Config
DefaultConfig returns the default configuration for a subagent type
type ContextManager ¶
type ContextManager struct {
// contains filtered or unexported fields
}
ContextManager manages isolated contexts for multiple subagents
func NewContextManager ¶
func NewContextManager() *ContextManager
NewContextManager creates a new context manager
func (*ContextManager) CleanupOldContexts ¶
func (m *ContextManager) CleanupOldContexts(maxAge time.Duration) int
CleanupOldContexts removes contexts older than the specified duration
func (*ContextManager) ContextCount ¶
func (m *ContextManager) ContextCount() int
ContextCount returns the number of active contexts
func (*ContextManager) CreateContext ¶
func (m *ContextManager) CreateContext(parentID string, agentType SubagentType) *IsolatedContext
CreateContext creates a new isolated context
func (*ContextManager) DeleteContext ¶
func (m *ContextManager) DeleteContext(id string) error
DeleteContext removes a context by ID
func (*ContextManager) GetContext ¶
func (m *ContextManager) GetContext(id string) (*IsolatedContext, error)
GetContext retrieves a context by ID
func (*ContextManager) ListContexts ¶
func (m *ContextManager) ListContexts() []string
ListContexts returns all active context IDs
type DispatchRequest ¶
type DispatchRequest struct {
ID string // Unique identifier for this dispatch
Request *ExecutionRequest
}
DispatchRequest represents a single subagent to dispatch
type DispatchResult ¶
type DispatchResult struct {
ID string // Matches the DispatchRequest.ID
Result *Result // The execution result
Error error // Error if dispatch failed
}
DispatchResult contains the result of a dispatched subagent
type Dispatcher ¶
type Dispatcher struct {
// Executor is used to run individual subagents
Executor *Executor
// MaxConcurrent limits how many subagents can run in parallel
MaxConcurrent int
}
Dispatcher coordinates parallel execution of multiple subagents
func NewDispatcher ¶
func NewDispatcher(executor *Executor) *Dispatcher
NewDispatcher creates a new parallel dispatcher
func (*Dispatcher) DispatchBatch ¶
func (d *Dispatcher) DispatchBatch(ctx context.Context, requests []*DispatchRequest, batchSize int) []*DispatchResult
DispatchBatch executes subagents in batches of a specified size Useful for processing large numbers of subagents without overwhelming resources
func (*Dispatcher) DispatchParallel ¶
func (d *Dispatcher) DispatchParallel(ctx context.Context, requests []*DispatchRequest) []*DispatchResult
DispatchParallel executes multiple subagents concurrently and returns all results Results are returned in the order they complete, not request order
func (*Dispatcher) DispatchSequential ¶
func (d *Dispatcher) DispatchSequential(ctx context.Context, requests []*DispatchRequest) []*DispatchResult
DispatchSequential executes subagents one at a time in order Useful when order matters or when resources are constrained
func (*Dispatcher) DispatchWithAggregation ¶
func (d *Dispatcher) DispatchWithAggregation( ctx context.Context, requests []*DispatchRequest, aggregator func([]*Result) (interface{}, error), ) (interface{}, error)
DispatchWithAggregation executes subagents and aggregates their results The aggregator function is called with all successful results
func (*Dispatcher) WaitForAny ¶
func (d *Dispatcher) WaitForAny(ctx context.Context, requests []*DispatchRequest) (*DispatchResult, error)
WaitForAny waits for the first subagent to complete successfully Returns as soon as one subagent succeeds, cancelling others
type ExecutionContext ¶
type ExecutionContext struct {
context.Context
Isolated *IsolatedContext
}
ExecutionContext wraps a standard context.Context with subagent-specific data
func NewExecutionContext ¶
func NewExecutionContext(ctx context.Context, isolated *IsolatedContext) *ExecutionContext
NewExecutionContext creates a context for subagent execution
type ExecutionRequest ¶
type ExecutionRequest struct {
// Type is the subagent type to execute
Type SubagentType
// Prompt is the task description for the subagent
Prompt string
// Description is a human-readable summary of what this subagent will do
Description string
// Config overrides default configuration (optional)
Config *Config
// Context contains additional context to inject (optional)
Context map[string]string
}
ExecutionRequest contains all information needed to execute a subagent
func (*ExecutionRequest) Validate ¶
func (r *ExecutionRequest) Validate() error
Validate checks if the execution request is valid
type Executor ¶
type Executor struct {
// HexBinPath is the path to the hex binary (empty = search PATH)
HexBinPath string
// ContextManager manages isolated contexts
ContextManager *ContextManager
// DefaultTimeout is the default execution timeout
DefaultTimeout time.Duration
// MaxTimeout is the maximum allowed timeout
MaxTimeout time.Duration
}
Executor manages the execution of isolated subagent instances
func (*Executor) ExecuteWithHooks ¶
func (e *Executor) ExecuteWithHooks(ctx context.Context, req *ExecutionRequest, hookEngine HookEngine) (*Result, error)
ExecuteWithHooks runs a subagent and fires hooks at appropriate times This method integrates with the hooks system
type HookEngine ¶
type HookEngine interface {
FireSubagentStop(taskDescription, subagentType string, responseLength, tokensUsed int, success bool, executionTime float64) error
}
HookEngine is an interface for firing hooks This allows us to avoid a circular dependency on the hooks package
type IsolatedContext ¶
type IsolatedContext struct {
// ID is a unique identifier for this context
ID string
// ParentID is the ID of the parent context (if any)
ParentID string
// Type is the subagent type
Type SubagentType
// CreatedAt is when this context was created
CreatedAt time.Time
// ConversationHistory stores messages specific to this subagent
// This is isolated from the parent agent's history
ConversationHistory []Message
// WorkingMemory stores ephemeral data for this execution
WorkingMemory map[string]interface{}
// contains filtered or unexported fields
}
IsolatedContext represents an isolated execution context for a subagent Each subagent gets its own context that cannot see the parent's history
func NewIsolatedContext ¶
func NewIsolatedContext(parentID string, agentType SubagentType) *IsolatedContext
NewIsolatedContext creates a new isolated context for a subagent
func (*IsolatedContext) AddMessage ¶
func (c *IsolatedContext) AddMessage(role, content string)
AddMessage adds a message to this context's isolated conversation history
func (*IsolatedContext) Clear ¶
func (c *IsolatedContext) Clear()
Clear resets the context (useful for testing or cleanup)
func (*IsolatedContext) GetMemory ¶
func (c *IsolatedContext) GetMemory(key string) (interface{}, bool)
GetMemory retrieves a value from working memory
func (*IsolatedContext) GetMessages ¶
func (c *IsolatedContext) GetMessages() []Message
GetMessages returns a copy of the conversation history This prevents external code from modifying the internal state
func (*IsolatedContext) MessageCount ¶
func (c *IsolatedContext) MessageCount() int
MessageCount returns the number of messages in this context
func (*IsolatedContext) SetMemory ¶
func (c *IsolatedContext) SetMemory(key string, value interface{})
SetMemory stores a value in working memory
type Result ¶
type Result struct {
// Success indicates if the subagent completed successfully
Success bool
// Output is the subagent's response text
Output string
// Error contains error message if Success is false
Error string
// Type is the subagent type that was executed
Type SubagentType
// Metadata contains execution details (duration, tokens, etc.)
Metadata map[string]interface{}
// StartTime is when execution began
StartTime time.Time
// EndTime is when execution completed
EndTime time.Time
}
Result contains the outcome of a subagent execution
type Statistics ¶
Statistics provides metrics about dispatch operations
func CalculateStatistics ¶
func CalculateStatistics(results []*DispatchResult) *Statistics
CalculateStatistics computes statistics from dispatch results
type SubagentType ¶
type SubagentType string
SubagentType represents a predefined subagent type with specialized behavior
const ( // TypeGeneralPurpose is the default subagent for general tasks TypeGeneralPurpose SubagentType = "general-purpose" // TypeExplore is optimized for fast codebase exploration and research TypeExplore SubagentType = "Explore" // TypePlan is specialized for design and planning work TypePlan SubagentType = "Plan" // TypeCodeReviewer performs code review and quality checks TypeCodeReviewer SubagentType = "code-reviewer" )
type ValidationError ¶
ValidationError represents a validation failure
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
Error implements the error interface