agentpool

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package agentpool manages a pool of discovered P2P agents with health checking, weighted selection, and capability-based filtering.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoAgents    = errors.New("no agents available")
	ErrAgentExists = errors.New("agent already registered")
	ErrNotFound    = errors.New("agent not found")
)

Sentinel errors for pool operations.

Functions

This section is empty.

Types

type Agent

type Agent struct {
	DID          string            `json:"did"`
	Name         string            `json:"name"`
	PeerID       string            `json:"peerId"`
	Capabilities []string          `json:"capabilities"`
	Metadata     map[string]string `json:"metadata,omitempty"`
	Status       AgentStatus       `json:"status"`
	TrustScore   float64           `json:"trustScore"`
	PricePerCall float64           `json:"pricePerCall"`
	Available    bool              `json:"available"`
	Performance  AgentPerformance  `json:"performance"`
	Latency      time.Duration     `json:"latency"`
	LastSeen     time.Time         `json:"lastSeen"`
	LastHealthy  time.Time         `json:"lastHealthy"`
	FailCount    int               `json:"failCount"`
}

Agent represents a discovered P2P agent in the pool.

func (*Agent) HasCapability

func (a *Agent) HasCapability(cap string) bool

HasCapability reports whether the agent advertises the given capability.

type AgentPerformance

type AgentPerformance struct {
	AvgLatencyMs float64 `json:"avgLatencyMs"`
	SuccessRate  float64 `json:"successRate"`
	TotalCalls   int     `json:"totalCalls"`
}

AgentPerformance tracks runtime performance metrics for a pooled agent.

type AgentStatus

type AgentStatus string

AgentStatus represents the health status of a pooled agent.

const (
	StatusHealthy   AgentStatus = "healthy"
	StatusDegraded  AgentStatus = "degraded"
	StatusUnhealthy AgentStatus = "unhealthy"
	StatusUnknown   AgentStatus = "unknown"
)

type DynamicAgentInfo

type DynamicAgentInfo struct {
	Name         string
	DID          string
	PeerID       string
	Description  string
	Capabilities []string
	TrustScore   float64
	PricePerCall float64
}

DynamicAgentInfo describes a discovered agent for routing purposes. It is a lightweight descriptor that avoids importing ADK agent types.

type DynamicAgentProvider

type DynamicAgentProvider interface {
	// AvailableAgents returns all healthy agents currently in the pool.
	AvailableAgents() []DynamicAgentInfo

	// FindForCapability returns agents that match the given capability.
	FindForCapability(capability string) []DynamicAgentInfo
}

DynamicAgentProvider discovers remote agents dynamically at runtime. The orchestrator queries this interface to integrate P2P agents into its routing table without requiring them to implement adk_agent.Agent.

type HealthCheckFunc

type HealthCheckFunc func(ctx context.Context, agent *Agent) (time.Duration, error)

HealthCheckFunc pings an agent and returns its latency if reachable.

type HealthChecker

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

HealthChecker periodically checks agent health.

func NewHealthChecker

func NewHealthChecker(pool *Pool, checkFn HealthCheckFunc, interval time.Duration, logger *zap.SugaredLogger) *HealthChecker

NewHealthChecker creates a health checker for the given pool.

func (*HealthChecker) Start

func (hc *HealthChecker) Start(wg *sync.WaitGroup)

Start begins periodic health checking.

func (*HealthChecker) Stop

func (hc *HealthChecker) Stop()

Stop halts the health checker.

type Pool

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

Pool manages a set of P2P agents with thread-safe access.

func New

func New(logger *zap.SugaredLogger) *Pool

New creates an empty agent pool.

func (*Pool) Add

func (p *Pool) Add(agent *Agent) error

Add registers an agent in the pool. Returns ErrAgentExists if the DID is already registered.

func (*Pool) EvictStale

func (p *Pool) EvictStale(threshold time.Duration) int

EvictStale removes agents not seen within the given threshold.

func (*Pool) FindByCapability

func (p *Pool) FindByCapability(cap string) []*Agent

FindByCapability returns all healthy agents that advertise the given capability.

func (*Pool) Get

func (p *Pool) Get(did string) *Agent

Get returns an agent by DID or nil if not found.

func (*Pool) List

func (p *Pool) List() []*Agent

List returns all agents in the pool.

func (*Pool) MarkHealthy

func (p *Pool) MarkHealthy(did string, latency time.Duration)

MarkHealthy updates an agent's status and records the health check time.

func (*Pool) MarkUnhealthy

func (p *Pool) MarkUnhealthy(did string)

MarkUnhealthy updates an agent's status after a failed health check.

func (*Pool) Remove

func (p *Pool) Remove(did string)

Remove removes an agent from the pool by DID.

func (*Pool) Size

func (p *Pool) Size() int

Size returns the number of agents in the pool.

func (*Pool) Update

func (p *Pool) Update(agent *Agent)

Update replaces an agent in the pool. The agent is matched by DID.

func (*Pool) UpdatePerformance

func (p *Pool) UpdatePerformance(did string, latencyMs float64, success bool)

UpdatePerformance records a call outcome and recalculates running averages.

type PoolProvider

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

PoolProvider adapts an agentpool.Pool into a DynamicAgentProvider.

func NewPoolProvider

func NewPoolProvider(pool *Pool, selector *Selector) *PoolProvider

NewPoolProvider creates a DynamicAgentProvider backed by a Pool.

func (*PoolProvider) AvailableAgents

func (p *PoolProvider) AvailableAgents() []DynamicAgentInfo

AvailableAgents returns all healthy agents in the pool.

func (*PoolProvider) FindForCapability

func (p *PoolProvider) FindForCapability(capability string) []DynamicAgentInfo

FindForCapability returns agents matching the given capability.

type Selector

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

Selector picks agents from the pool using weighted scoring.

func NewSelector

func NewSelector(pool *Pool, weights SelectorWeights) *Selector

NewSelector creates a weighted selector for the given pool.

func (*Selector) ScoreWithCaps

func (s *Selector) ScoreWithCaps(a *Agent, requiredCaps []string) float64

ScoreWithCaps computes a weighted score considering required capabilities.

func (*Selector) Select

func (s *Selector) Select(capability string) (*Agent, error)

Select picks the best agent for the given capability. Returns ErrNoAgents if no suitable agent is found.

func (*Selector) SelectBest

func (s *Selector) SelectBest(agents []*Agent, requiredCaps []string, n int) []*Agent

SelectBest picks the top N agents for the given required capabilities.

func (*Selector) SelectN

func (s *Selector) SelectN(capability string, n int) ([]*Agent, error)

SelectN picks the top N agents for the given capability.

func (*Selector) SelectRandom

func (s *Selector) SelectRandom(capability string) (*Agent, error)

SelectRandom picks a random agent from healthy candidates for the given capability.

type SelectorWeights

type SelectorWeights struct {
	Trust        float64 // weight for trust score [0,1]
	Capability   float64 // weight for capability match breadth
	Performance  float64 // weight for success rate / latency
	Price        float64 // weight for price (lower is better)
	Availability float64 // weight for availability / health status
	// Legacy aliases (used if the new fields are zero).
	Latency float64 // weight for latency (lower is better)
	Health  float64 // weight for health status
}

SelectorWeights configures the relative importance of selection criteria.

func DefaultWeights

func DefaultWeights() SelectorWeights

DefaultWeights returns production-default selector weights.

Jump to

Keyboard shortcuts

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