terraphim_goal_alignment 1.0.0

Knowledge graph-based goal alignment system for multi-level goal management and conflict resolution
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# Terraphim Goal Alignment System

Knowledge graph-based goal alignment system for multi-level goal management and conflict resolution in the Terraphim AI ecosystem.

## Overview

The `terraphim_goal_alignment` crate provides a sophisticated goal alignment system that leverages Terraphim's knowledge graph infrastructure to ensure goal hierarchy consistency, detect conflicts, and propagate goals through role hierarchies. It integrates seamlessly with the agent registry and role graph systems to provide context-aware goal management.

## Key Features

- **Multi-level Goal Management**: Global, high-level, and local goal alignment with hierarchy validation
- **Knowledge Graph Integration**: Uses existing `extract_paragraphs_from_automata` and `is_all_terms_connected_by_path` for intelligent goal analysis
- **Conflict Detection**: Semantic, resource, temporal, and priority conflict detection with resolution strategies
- **Goal Propagation**: Intelligent goal distribution through role hierarchies with automatic agent assignment
- **Dynamic Alignment**: Real-time goal alignment as system state changes with incremental updates
- **Performance Optimization**: Efficient caching, incremental updates, and background processing

## Architecture

```
┌─────────────────────┐    ┌──────────────────────┐    ┌─────────────────────┐
│   Goal Aligner      │    │  Knowledge Graph     │    │  Conflict Detector  │
│   (Core Engine)     │◄──►│  Analyzer            │◄──►│  & Resolver         │
└─────────────────────┘    └──────────────────────┘    └─────────────────────┘
         │                           │                           │
         ▼                           ▼                           ▼
┌─────────────────────┐    ┌──────────────────────┐    ┌─────────────────────┐
│   Goal Hierarchy    │    │  Goal Propagation    │    │  Agent Registry     │
│   Management        │    │  Engine              │    │  Integration        │
└─────────────────────┘    └──────────────────────┘    └─────────────────────┘
```

## Core Concepts

### Goal Hierarchy

Goals are organized in a three-level hierarchy:

```rust
use terraphim_goal_alignment::{Goal, GoalLevel};

// Global strategic objectives
let global_goal = Goal::new(
    "increase_revenue".to_string(),
    GoalLevel::Global,
    "Increase company revenue by 20% this year".to_string(),
    1, // High priority
);

// High-level departmental objectives
let high_level_goal = Goal::new(
    "improve_product_quality".to_string(),
    GoalLevel::HighLevel,
    "Reduce product defects by 50%".to_string(),
    2,
);

// Local task-level objectives
let local_goal = Goal::new(
    "implement_testing".to_string(),
    GoalLevel::Local,
    "Implement automated testing for core modules".to_string(),
    3,
);
```

### Knowledge Graph Context

Goals operate within rich knowledge graph contexts:

```rust
use terraphim_goal_alignment::GoalKnowledgeContext;

let mut context = GoalKnowledgeContext::default();
context.domains = vec!["software_engineering".to_string(), "quality_assurance".to_string()];
context.concepts = vec!["testing".to_string(), "automation".to_string(), "quality".to_string()];
context.relationships = vec!["implements".to_string(), "improves".to_string()];
context.keywords = vec!["unit_test".to_string(), "integration_test".to_string()];

goal.knowledge_context = context;
```

### Goal Constraints

Goals can have various types of constraints:

```rust
use terraphim_goal_alignment::{GoalConstraint, ConstraintType};

let temporal_constraint = GoalConstraint {
    constraint_type: ConstraintType::Temporal,
    description: "Must complete by end of quarter".to_string(),
    parameters: {
        let mut params = HashMap::new();
        params.insert("deadline".to_string(), serde_json::json!("2024-03-31"));
        params
    },
    is_hard: true,
    priority: 1,
};

let resource_constraint = GoalConstraint {
    constraint_type: ConstraintType::Resource,
    description: "Requires 2 senior developers".to_string(),
    parameters: {
        let mut params = HashMap::new();
        params.insert("resource_type".to_string(), serde_json::json!("senior_developer"));
        params.insert("amount".to_string(), serde_json::json!(2));
        params
    },
    is_hard: true,
    priority: 2,
};

goal.add_constraint(temporal_constraint)?;
goal.add_constraint(resource_constraint)?;
```

## Quick Start

### 1. Create Goal Aligner

```rust
use std::sync::Arc;
use terraphim_goal_alignment::{
    KnowledgeGraphGoalAligner, KnowledgeGraphGoalAnalyzer,
    AutomataConfig, SimilarityThresholds, AlignmentConfig,
};
use terraphim_rolegraph::RoleGraph;
use terraphim_agent_registry::AgentRegistry;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create knowledge graph analyzer
    let role_graph = Arc::new(RoleGraph::new());
    let kg_analyzer = Arc::new(KnowledgeGraphGoalAnalyzer::new(
        role_graph.clone(),
        AutomataConfig::default(),
        SimilarityThresholds::default(),
    ));

    // Create goal aligner
    let agent_registry = Arc::new(your_agent_registry);
    let config = AlignmentConfig::default();

    let aligner = KnowledgeGraphGoalAligner::new(
        kg_analyzer,
        agent_registry,
        role_graph,
        config,
    );

    Ok(())
}
```

### 2. Add Goals

```rust
use terraphim_goal_alignment::{Goal, GoalLevel};

// Create a strategic goal
let mut strategic_goal = Goal::new(
    "digital_transformation".to_string(),
    GoalLevel::Global,
    "Complete digital transformation initiative".to_string(),
    1,
);

// Set knowledge context
strategic_goal.knowledge_context.domains = vec![
    "digital_transformation".to_string(),
    "technology".to_string(),
    "business_process".to_string(),
];

strategic_goal.knowledge_context.concepts = vec![
    "automation".to_string(),
    "digitization".to_string(),
    "process_improvement".to_string(),
];

// Assign to roles
strategic_goal.assigned_roles = vec![
    "cto".to_string(),
    "digital_transformation_lead".to_string(),
];

// Add to aligner
aligner.add_goal(strategic_goal).await?;
```

### 3. Perform Goal Alignment

```rust
use terraphim_goal_alignment::{GoalAlignmentRequest, AlignmentType};

let request = GoalAlignmentRequest {
    goal_ids: Vec::new(), // All goals
    alignment_type: AlignmentType::FullAlignment,
    force_reanalysis: false,
    context: HashMap::new(),
};

let response = aligner.align_goals(request).await?;

println!("Alignment Score: {:.2}", response.summary.alignment_score_after);
println!("Conflicts Detected: {}", response.summary.conflicts_detected);
println!("Conflicts Resolved: {}", response.summary.conflicts_resolved);
println!("Goals Updated: {}", response.summary.goals_updated);

// Review recommendations
for recommendation in &response.summary.pending_recommendations {
    println!("Recommendation: {}", recommendation.description);
    println!("Priority: {}", recommendation.priority);
}
```

### 4. Propagate Goals

```rust
use terraphim_goal_alignment::{
    GoalPropagationEngine, GoalPropagationRequest, PropagationConfig,
};

let propagation_engine = GoalPropagationEngine::new(
    role_graph,
    agent_registry,
    PropagationConfig::default(),
);

let request = GoalPropagationRequest {
    source_goal: strategic_goal,
    target_roles: vec!["engineering_manager".to_string(), "product_manager".to_string()],
    max_depth: Some(3),
    context: HashMap::new(),
};

let result = propagation_engine.propagate_goal(request).await?;

println!("Goals Created: {}", result.summary.goals_created);
println!("Agents Assigned: {}", result.summary.agents_assigned);
println!("Roles Reached: {}", result.summary.roles_reached);
println!("Success Rate: {:.2}%", result.summary.success_rate * 100.0);
```

## Advanced Features

### Conflict Detection and Resolution

The system automatically detects various types of conflicts:

```rust
use terraphim_goal_alignment::{ConflictDetector, ConflictType};

let detector = ConflictDetector::new();

// Detect all conflicts
let conflicts = detector.detect_all_conflicts(&goals)?;

for conflict in &conflicts {
    println!("Conflict: {} vs {}", conflict.goal1, conflict.goal2);
    println!("Type: {:?}", conflict.conflict_type);
    println!("Severity: {:.2}", conflict.severity);
    println!("Description: {}", conflict.description);

    // Resolve conflict
    let resolution = detector.resolve_conflict(conflict, &mut goals)?;
    if resolution.success {
        println!("Resolution: {}", resolution.description);
    }
}
```

### Knowledge Graph Analysis

Deep integration with Terraphim's knowledge graph:

```rust
// Analyze goal connectivity
let analysis = GoalAlignmentAnalysis {
    goals: vec![goal1, goal2, goal3],
    analysis_type: AnalysisType::ConnectivityValidation,
    context: HashMap::new(),
};

let result = kg_analyzer.analyze_goal_alignment(analysis).await?;

// Check connectivity issues
for issue in &result.connectivity_issues {
    println!("Connectivity Issue: {}", issue.description);
    for fix in &issue.suggested_fixes {
        println!("  Suggested Fix: {}", fix);
    }
}
```

### Custom Propagation Strategies

Implement custom goal propagation logic:

```rust
use terraphim_goal_alignment::{PropagationStrategy, PropagationConfig};

let config = PropagationConfig {
    strategy: PropagationStrategy::SimilarityBased,
    min_role_similarity: 0.8,
    max_depth: 4,
    auto_assign_agents: true,
    max_agents_per_goal: 5,
};

let engine = GoalPropagationEngine::new(role_graph, agent_registry, config);
```

### Real-time Alignment Updates

Enable automatic alignment updates:

```rust
let config = AlignmentConfig {
    real_time_updates: true,
    auto_resolve_conflicts: false, // Manual review required
    max_alignment_iterations: 10,
    convergence_threshold: 0.95,
    ..AlignmentConfig::default()
};

let aligner = KnowledgeGraphGoalAligner::new(
    kg_analyzer,
    agent_registry,
    role_graph,
    config,
);

// Goals will be automatically re-aligned when updated
aligner.update_goal(modified_goal).await?;
```

## Integration with Terraphim Ecosystem

### With Agent Registry

Goals are automatically matched with suitable agents:

```rust
// Goals with assigned roles will automatically discover agents
strategic_goal.assigned_roles = vec!["senior_architect".to_string()];

// The system will find agents with the "senior_architect" role
// and assign them based on capability matching
aligner.add_goal(strategic_goal).await?;
```

### With Role Graph

Goal propagation follows role hierarchies:

```rust
// Goals propagate down the role hierarchy
// Global goals → High-level goals → Local goals
// Executive roles → Manager roles → Worker roles

let propagation_result = engine.propagate_goal(request).await?;

// Review propagation path
for step in &propagation_result.propagation_path {
    println!("Step {}: {} → {} ({})",
        step.step, step.from_role, step.to_role, step.reason);
}
```

### With Knowledge Graph

Semantic analysis guides all operations:

```rust
// Goals are analyzed for semantic consistency
// Concepts are extracted using extract_paragraphs_from_automata
// Connectivity is validated using is_all_terms_connected_by_path

let analysis_result = kg_analyzer.analyze_goal_alignment(analysis).await?;

println!("Overall Alignment Score: {:.2}", analysis_result.overall_alignment_score);

for (goal_id, analysis) in &analysis_result.goal_analyses {
    println!("Goal {}: Connectivity Score {:.2}",
        goal_id, analysis.connectivity.strength_score);
}
```

## Configuration

### Alignment Configuration

```rust
let config = AlignmentConfig {
    auto_resolve_conflicts: false,      // Manual conflict resolution
    max_alignment_iterations: 15,       // Maximum optimization iterations
    convergence_threshold: 0.98,        // High alignment threshold
    real_time_updates: true,            // Automatic re-alignment
    cache_ttl_secs: 3600,              // 1 hour cache TTL
    enable_monitoring: true,            // Performance monitoring
};
```

### Knowledge Graph Configuration

```rust
let automata_config = AutomataConfig {
    min_confidence: 0.8,               // High confidence threshold
    max_paragraphs: 20,                // More context extraction
    context_window: 2048,              // Larger context window
    language_models: vec!["advanced".to_string()],
};

let similarity_thresholds = SimilarityThresholds {
    concept_similarity: 0.85,          // High concept similarity
    domain_similarity: 0.8,            // High domain similarity
    relationship_similarity: 0.75,     // Moderate relationship similarity
    conflict_threshold: 0.6,           // Moderate conflict threshold
};
```

### Propagation Configuration

```rust
let propagation_config = PropagationConfig {
    max_depth: 6,                      // Deep propagation
    min_role_similarity: 0.75,         // Moderate role similarity
    auto_assign_agents: true,          // Automatic agent assignment
    max_agents_per_goal: 8,            // More agents per goal
    strategy: PropagationStrategy::HierarchicalCascade,
};
```

## Performance

The goal alignment system is optimized for performance:

- **Caching**: Analysis results cached with configurable TTL
- **Incremental Updates**: Only re-analyze affected goals
- **Background Processing**: Automatic cleanup and optimization
- **Efficient Algorithms**: Optimized conflict detection and resolution

### Benchmarks

Run benchmarks to see performance characteristics:

```bash
cargo bench --features benchmarks -p terraphim_goal_alignment
```

## Testing

Run the comprehensive test suite:

```bash
# Unit tests
cargo test -p terraphim_goal_alignment

# Integration tests
cargo test --test integration_tests -p terraphim_goal_alignment

# All tests with logging
RUST_LOG=debug cargo test -p terraphim_goal_alignment
```

## Examples

The crate includes comprehensive examples:

- **Basic Goal Management**: Creating, updating, and organizing goals
- **Conflict Detection**: Identifying and resolving goal conflicts
- **Goal Propagation**: Distributing goals through role hierarchies
- **Knowledge Graph Integration**: Semantic analysis and connectivity validation
- **Real-time Alignment**: Dynamic goal alignment with automatic updates

## 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.