terraphim_agent_registry 1.19.2

Knowledge graph-based agent registry for intelligent agent discovery and capability matching
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# Terraphim Agent Registry

Knowledge graph-based agent registry for intelligent agent discovery and capability matching in the Terraphim AI ecosystem.

## Overview

The `terraphim_agent_registry` crate provides a sophisticated agent registry that leverages Terraphim's knowledge graph infrastructure to enable intelligent agent discovery, capability matching, and role-based specialization. It integrates seamlessly with the existing automata and role graph systems to provide context-aware agent management.

## Key Features

- **Knowledge Graph Integration**: Uses existing `extract_paragraphs_from_automata` and `is_all_terms_connected_by_path` for intelligent agent discovery
- **Role-Based Specialization**: Leverages `terraphim_rolegraph` for agent role management and hierarchy
- **Capability Matching**: Semantic matching of agent capabilities to task requirements
- **Agent Metadata**: Rich metadata storage with knowledge graph context
- **Dynamic Discovery**: Real-time agent discovery based on evolving requirements
- **Performance Optimization**: Efficient indexing and caching for fast lookups
- **Multiple Discovery Algorithms**: Exact match, fuzzy match, semantic match, and hybrid approaches

## Architecture

```
┌─────────────────────┐    ┌──────────────────────┐    ┌─────────────────────┐
│   Agent Registry    │    │  Knowledge Graph     │    │  Role Graph         │
│   (Core)            │◄──►│  Integration         │◄──►│  Integration        │
└─────────────────────┘    └──────────────────────┘    └─────────────────────┘
         │                           │                           │
         ▼                           ▼                           ▼
┌─────────────────────┐    ┌──────────────────────┐    ┌─────────────────────┐
│   Agent Metadata    │    │  Discovery Engine    │    │  Capability         │
│   Management        │    │  (Multiple Algos)    │    │  Registry           │
└─────────────────────┘    └──────────────────────┘    └─────────────────────┘
```

## Core Concepts

### Agent Roles

Every agent in the registry has a **primary role** and can have multiple **secondary roles**. Roles are integrated with the `terraphim_rolegraph` system:

```rust
use terraphim_agent_registry::{AgentRole, AgentMetadata};

let role = AgentRole::new(
    "planner".to_string(),
    "Planning Agent".to_string(),
    "Responsible for task planning and coordination".to_string(),
);

// Roles support hierarchy and specialization
role.hierarchy_level = 2;
role.parent_roles = vec!["coordinator".to_string()];
role.child_roles = vec!["task_planner".to_string(), "resource_planner".to_string()];
role.knowledge_domains = vec!["project_management".to_string(), "scheduling".to_string()];
```

### Agent Capabilities

Agents have well-defined capabilities with performance metrics and resource requirements:

```rust
use terraphim_agent_registry::{AgentCapability, CapabilityMetrics, ResourceUsage};

let capability = AgentCapability {
    capability_id: "task_planning".to_string(),
    name: "Task Planning".to_string(),
    description: "Plan and organize complex tasks".to_string(),
    category: "planning".to_string(),
    required_domains: vec!["project_management".to_string()],
    input_types: vec!["requirements".to_string(), "constraints".to_string()],
    output_types: vec!["plan".to_string(), "timeline".to_string()],
    performance_metrics: CapabilityMetrics {
        avg_execution_time: Duration::from_secs(30),
        success_rate: 0.95,
        quality_score: 0.9,
        resource_usage: ResourceUsage {
            memory_mb: 256.0,
            cpu_percent: 15.0,
            network_kbps: 5.0,
            storage_mb: 100.0,
        },
        last_updated: Utc::now(),
    },
    dependencies: vec!["basic_reasoning".to_string()],
};
```

### Knowledge Graph Context

Agents operate within knowledge graph contexts that define their understanding:

```rust
use terraphim_agent_registry::KnowledgeContext;

let context = KnowledgeContext {
    domains: vec!["software_engineering".to_string(), "project_management".to_string()],
    concepts: vec!["agile".to_string(), "scrum".to_string(), "kanban".to_string()],
    relationships: vec!["implements".to_string(), "depends_on".to_string()],
    extraction_patterns: vec!["task_.*".to_string(), "requirement_.*".to_string()],
    similarity_thresholds: {
        let mut thresholds = HashMap::new();
        thresholds.insert("concept_similarity".to_string(), 0.8);
        thresholds.insert("domain_similarity".to_string(), 0.7);
        thresholds
    },
};
```

## Quick Start

### 1. Create a Registry

```rust
use std::sync::Arc;
use terraphim_agent_registry::{RegistryBuilder, RegistryConfig};
use terraphim_rolegraph::RoleGraph;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create role graph (integrate with existing Terraphim role graph)
    let role_graph = Arc::new(RoleGraph::new());

    // Configure registry
    let config = RegistryConfig {
        max_agents: 1000,
        auto_cleanup: true,
        cleanup_interval_secs: 300,
        enable_monitoring: true,
        discovery_cache_ttl_secs: 3600,
    };

    // Build registry
    let registry = RegistryBuilder::new()
        .with_role_graph(role_graph)
        .with_config(config)
        .build()?;

    // Start background tasks
    registry.start_background_tasks().await?;

    Ok(())
}
```

### 2. Register Agents

```rust
use terraphim_agent_registry::{AgentRegistry, AgentMetadata, AgentRole};

// Create agent metadata
let agent_id = AgentPid::new();
let supervisor_id = SupervisorId::new();

let primary_role = AgentRole::new(
    "data_analyst".to_string(),
    "Data Analysis Agent".to_string(),
    "Specializes in data analysis and visualization".to_string(),
);

let mut metadata = AgentMetadata::new(agent_id.clone(), supervisor_id, primary_role);

// Add capabilities
let analysis_capability = AgentCapability {
    capability_id: "data_analysis".to_string(),
    name: "Data Analysis".to_string(),
    description: "Analyze datasets and generate insights".to_string(),
    category: "analysis".to_string(),
    required_domains: vec!["statistics".to_string(), "data_science".to_string()],
    input_types: vec!["csv".to_string(), "json".to_string()],
    output_types: vec!["report".to_string(), "visualization".to_string()],
    performance_metrics: CapabilityMetrics::default(),
    dependencies: Vec::new(),
};

metadata.add_capability(analysis_capability)?;

// Register with registry
registry.register_agent(metadata).await?;
```

### 3. Discover Agents

```rust
use terraphim_agent_registry::AgentDiscoveryQuery;

// Create discovery query
let query = AgentDiscoveryQuery {
    required_roles: vec!["data_analyst".to_string()],
    required_capabilities: vec!["data_analysis".to_string()],
    required_domains: vec!["statistics".to_string()],
    task_description: Some("Analyze customer behavior data and generate insights".to_string()),
    min_success_rate: Some(0.8),
    max_resource_usage: Some(ResourceUsage {
        memory_mb: 1024.0,
        cpu_percent: 50.0,
        network_kbps: 100.0,
        storage_mb: 500.0,
    }),
    preferred_tags: vec!["experienced".to_string()],
};

// Discover matching agents
let result = registry.discover_agents(query).await?;

println!("Found {} matching agents", result.matches.len());
for agent_match in result.matches {
    println!(
        "Agent: {} (Score: {:.2}) - {}",
        agent_match.agent.agent_id,
        agent_match.match_score,
        agent_match.explanation
    );
}
```

## Discovery Algorithms

The registry supports multiple discovery algorithms:

### Exact Match
```rust
use terraphim_agent_registry::{DiscoveryEngine, DiscoveryContext, DiscoveryAlgorithm};

let context = DiscoveryContext {
    algorithm: DiscoveryAlgorithm::ExactMatch,
    ..Default::default()
};
```

### Fuzzy Match
```rust
let context = DiscoveryContext {
    algorithm: DiscoveryAlgorithm::FuzzyMatch,
    ..Default::default()
};
```

### Semantic Match (Knowledge Graph)
```rust
let context = DiscoveryContext {
    algorithm: DiscoveryAlgorithm::SemanticMatch,
    ..Default::default()
};
```

### Hybrid Approach
```rust
let context = DiscoveryContext {
    algorithm: DiscoveryAlgorithm::Hybrid(vec![
        DiscoveryAlgorithm::ExactMatch,
        DiscoveryAlgorithm::FuzzyMatch,
        DiscoveryAlgorithm::SemanticMatch,
    ]),
    ..Default::default()
};
```

## Knowledge Graph Integration

The registry integrates deeply with Terraphim's knowledge graph infrastructure:

### Concept Extraction
```rust
// Uses extract_paragraphs_from_automata for intelligent context analysis
let task_description = "Plan a software development project using agile methodology";
let extracted_concepts = kg_integration.extract_concepts_from_text(task_description).await?;
// Returns: ["software", "development", "project", "agile", "methodology"]
```

### Connectivity Analysis
```rust
// Uses is_all_terms_connected_by_path for requirement validation
let requirements = vec!["planning", "agile", "software_development"];
let connectivity = kg_integration.analyze_term_connectivity(&requirements).await?;

if connectivity.all_connected {
    println!("All requirements are connected in the knowledge graph");
} else {
    println!("Disconnected terms: {:?}", connectivity.disconnected);
}
```

### Role Hierarchy Navigation
```rust
// Leverages terraphim_rolegraph for role-based discovery
let related_roles = kg_integration.find_related_roles("senior_developer").await?;
// Returns parent roles, child roles, and sibling roles
```

## Advanced Features

### Capability Dependencies
```rust
let mut capability_registry = CapabilityRegistry::new();

// Register capabilities with dependencies
let advanced_planning = AgentCapability {
    capability_id: "advanced_planning".to_string(),
    dependencies: vec!["basic_planning".to_string(), "risk_assessment".to_string()],
    // ... other fields
};

capability_registry.register_capability(advanced_planning)?;

// Check if agent has all required dependencies
let agent_capabilities = vec!["basic_planning".to_string(), "risk_assessment".to_string()];
let can_use = capability_registry.check_dependencies("advanced_planning", &agent_capabilities);
```

### Performance Monitoring
```rust
// Agents track performance metrics automatically
agent_metadata.record_task_completion(Duration::from_secs(45), true);
agent_metadata.record_resource_usage(ResourceUsage {
    memory_mb: 512.0,
    cpu_percent: 25.0,
    network_kbps: 20.0,
    storage_mb: 200.0,
});

let success_rate = agent_metadata.get_success_rate(); // 0.0 to 1.0
```

### Dynamic Role Assignment
```rust
// Agents can assume multiple roles
let secondary_role = AgentRole::new(
    "code_reviewer".to_string(),
    "Code Review Specialist".to_string(),
    "Reviews code for quality and standards".to_string(),
);

agent_metadata.add_secondary_role(secondary_role)?;

// Check if agent can fulfill a role
if agent_metadata.has_role("code_reviewer") {
    println!("Agent can perform code reviews");
}
```

## Integration with Terraphim Ecosystem

### With Agent Supervisor
```rust
use terraphim_agent_supervisor::{Supervisor, AgentSpec};

// Registry integrates with supervision system
let supervisor = Supervisor::new(supervisor_id, RestartStrategy::OneForOne);

// Agents found through registry can be supervised
for agent_match in discovery_result.matches {
    let agent_spec = AgentSpec::new(
        agent_match.agent.agent_id,
        agent_match.agent.primary_role.role_id,
        serde_json::json!({}),
    );
    supervisor.start_agent(agent_spec).await?;
}
```

### With Messaging System
```rust
use terraphim_agent_messaging::{MessageSystem, AgentMessage};

// Discovered agents can communicate through messaging system
let message_system = MessageSystem::new();
for agent_match in discovery_result.matches {
    let message = AgentMessage::new(
        "task_assignment".to_string(),
        serde_json::json!({"task": "analyze_data"}),
    );
    message_system.send_message(agent_match.agent.agent_id, message).await?;
}
```

### With GenAgent Framework
```rust
use terraphim_gen_agent::{GenAgentFactory, GenAgentRuntime};

// Registry works with GenAgent runtime system
let factory = GenAgentFactory::new(state_manager, runtime_config);

for agent_match in discovery_result.matches {
    // Create runtime for discovered agents
    let runtime = factory.get_runtime(&agent_match.agent.agent_id).await;
    // ... interact with agent through runtime
}
```

## Configuration

### Registry Configuration
```rust
let config = RegistryConfig {
    max_agents: 10000,              // Maximum agents to register
    auto_cleanup: true,             // Automatically remove terminated agents
    cleanup_interval_secs: 300,     // Cleanup every 5 minutes
    enable_monitoring: true,        // Enable performance monitoring
    discovery_cache_ttl_secs: 3600, // Cache discovery results for 1 hour
};
```

### Knowledge Graph Configuration
```rust
let automata_config = AutomataConfig {
    min_confidence: 0.7,           // Minimum confidence for concept extraction
    max_paragraphs: 10,            // Maximum paragraphs to extract
    context_window: 512,           // Context window size
    language_models: vec!["default".to_string()],
};

let similarity_thresholds = SimilarityThresholds {
    role_similarity: 0.8,          // Role matching threshold
    capability_similarity: 0.75,   // Capability matching threshold
    domain_similarity: 0.7,        // Domain matching threshold
    concept_similarity: 0.65,      // Concept matching threshold
};
```

## Performance

The registry is optimized for high performance:

- **Efficient Indexing**: Agents indexed by role, capability, and domain
- **Caching**: Query results cached with configurable TTL
- **Background Processing**: Automatic cleanup and monitoring
- **Concurrent Access**: Thread-safe operations with minimal locking

### Benchmarks

Run benchmarks to see performance characteristics:

```bash
cargo bench --features benchmarks
```

## Testing

Run the comprehensive test suite:

```bash
# Unit tests
cargo test

# Integration tests
cargo test --test integration_tests

# All tests with logging
RUST_LOG=debug cargo test
```

## Examples

See the `tests/` directory for comprehensive examples:

- Basic agent registration and discovery
- Knowledge graph integration
- Role-based specialization
- Capability matching
- Performance monitoring
- Multi-algorithm discovery

## Contributing

Contributions are welcome! Please see the main Terraphim repository for contribution guidelines.

## License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.