terraphim_task_decomposition 1.0.0

Knowledge graph-based task decomposition system for intelligent task analysis and execution planning
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
//! Task decomposition engine using knowledge graph analysis
//!
//! This module provides intelligent task decomposition capabilities that leverage
//! Terraphim's knowledge graph infrastructure to break down complex tasks into
//! manageable subtasks with proper dependencies and execution ordering.

use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use async_trait::async_trait;
use log::{debug, info, warn};
use serde::{Deserialize, Serialize};

// use terraphim_automata::{extract_paragraphs_from_automata, is_all_terms_connected_by_path};
use terraphim_rolegraph::RoleGraph;
// use terraphim_types::Automata;

// Temporary mock functions until dependencies are fixed
fn extract_paragraphs_from_automata(
    _automata: &MockAutomata,
    text: &str,
    max_results: u32,
) -> Result<Vec<String>, String> {
    // Simple mock implementation
    let words: Vec<String> = text
        .split_whitespace()
        .take(max_results as usize)
        .map(|s| s.to_string())
        .collect();
    Ok(words)
}

fn is_all_terms_connected_by_path(
    _automata: &MockAutomata,
    terms: &[&str],
) -> Result<bool, String> {
    // Simple mock implementation - assume connected if terms share characters
    if terms.len() < 2 {
        return Ok(true);
    }
    let first = terms[0].to_lowercase();
    let second = terms[1].to_lowercase();
    Ok(first.chars().any(|c| second.contains(c)))
}

use crate::{Automata, MockAutomata};

use crate::{Task, TaskComplexity, TaskDecompositionError, TaskDecompositionResult, TaskId};

/// Task decomposition strategy
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum DecompositionStrategy {
    /// Decompose based on knowledge graph connectivity
    KnowledgeGraphBased,
    /// Decompose based on task complexity analysis
    ComplexityBased,
    /// Decompose based on role requirements
    RoleBased,
    /// Hybrid approach combining multiple strategies
    Hybrid,
}

/// Decomposition configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DecompositionConfig {
    /// Maximum decomposition depth
    pub max_depth: u32,
    /// Minimum subtask complexity threshold
    pub min_subtask_complexity: TaskComplexity,
    /// Maximum number of subtasks per task
    pub max_subtasks_per_task: u32,
    /// Strategy to use for decomposition
    pub strategy: DecompositionStrategy,
    /// Knowledge graph similarity threshold
    pub similarity_threshold: f64,
    /// Whether to preserve task dependencies during decomposition
    pub preserve_dependencies: bool,
    /// Whether to optimize for parallel execution
    pub optimize_for_parallelism: bool,
}

impl Default for DecompositionConfig {
    fn default() -> Self {
        Self {
            max_depth: 3,
            min_subtask_complexity: TaskComplexity::Simple,
            max_subtasks_per_task: 10,
            strategy: DecompositionStrategy::Hybrid,
            similarity_threshold: 0.7,
            preserve_dependencies: true,
            optimize_for_parallelism: true,
        }
    }
}

/// Result of task decomposition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DecompositionResult {
    /// Original task that was decomposed
    pub original_task: TaskId,
    /// Generated subtasks
    pub subtasks: Vec<Task>,
    /// Dependency relationships between subtasks
    pub dependencies: HashMap<TaskId, Vec<TaskId>>,
    /// Decomposition metadata
    pub metadata: DecompositionMetadata,
}

/// Metadata about the decomposition process
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DecompositionMetadata {
    /// Strategy used for decomposition
    pub strategy_used: DecompositionStrategy,
    /// Decomposition depth achieved
    pub depth: u32,
    /// Number of subtasks created
    pub subtask_count: u32,
    /// Knowledge graph concepts involved
    pub concepts_analyzed: Vec<String>,
    /// Roles identified for subtasks
    pub roles_identified: Vec<String>,
    /// Decomposition confidence score (0.0 to 1.0)
    pub confidence_score: f64,
    /// Estimated parallelism factor
    pub parallelism_factor: f64,
}

/// Knowledge graph-based task decomposer
#[async_trait]
pub trait TaskDecomposer: Send + Sync {
    /// Decompose a task into subtasks
    async fn decompose_task(
        &self,
        task: &Task,
        config: &DecompositionConfig,
    ) -> TaskDecompositionResult<DecompositionResult>;

    /// Analyze task complexity for decomposition planning
    async fn analyze_complexity(&self, task: &Task) -> TaskDecompositionResult<TaskComplexity>;

    /// Validate decomposition result
    async fn validate_decomposition(
        &self,
        result: &DecompositionResult,
    ) -> TaskDecompositionResult<bool>;
}

/// Knowledge graph-based task decomposer implementation
pub struct KnowledgeGraphTaskDecomposer {
    /// Knowledge graph automata
    automata: Arc<Automata>,
    /// Role graph for role-based decomposition
    role_graph: Arc<RoleGraph>,
    /// Decomposition cache for performance
    cache: Arc<tokio::sync::RwLock<HashMap<String, DecompositionResult>>>,
}

impl KnowledgeGraphTaskDecomposer {
    /// Create a new knowledge graph task decomposer
    pub fn new(automata: Arc<Automata>, role_graph: Arc<RoleGraph>) -> Self {
        Self {
            automata,
            role_graph,
            cache: Arc::new(tokio::sync::RwLock::new(HashMap::new())),
        }
    }

    /// Extract knowledge concepts from task description
    async fn extract_task_concepts(&self, task: &Task) -> TaskDecompositionResult<Vec<String>> {
        let text = format!(
            "{} {}",
            task.description,
            task.knowledge_context.keywords.join(" ")
        );

        match extract_paragraphs_from_automata(&self.automata, &text, 10) {
            Ok(paragraphs) => {
                let mut concepts: Vec<String> = paragraphs
                    .into_iter()
                    .flat_map(|p| {
                        p.split_whitespace()
                            .map(|s| s.to_lowercase())
                            .collect::<Vec<_>>()
                    })
                    .collect::<HashSet<_>>()
                    .into_iter()
                    .collect();

                // Enhance with role graph related concepts
                let mut related_concepts = HashSet::new();
                for concept in &concepts {
                    // Find related concepts through role graph connectivity
                    if self.role_graph.is_all_terms_connected_by_path(concept) {
                        let node_ids = self.role_graph.find_matching_node_ids(concept);
                        for _node_id in node_ids.iter().take(3) {
                            // Use role graph to find related documents
                            let documents = self.role_graph.find_document_ids_for_term(concept);
                            for doc_id in documents.iter().take(2) {
                                if let Some(document) = self.role_graph.get_document(doc_id) {
                                    // Extract concepts from document tags and ID
                                    for tag in &document.tags {
                                        if !tag.is_empty() && tag.len() > 2 {
                                            related_concepts.insert(tag.to_lowercase());
                                        }
                                    }
                                    // Extract concepts from document ID
                                    if !doc_id.is_empty() && doc_id.len() > 2 {
                                        related_concepts.insert(doc_id.to_lowercase());
                                    }
                                }
                            }
                        }
                    }
                }

                // Add related concepts to the main list
                concepts.extend(related_concepts);
                concepts.sort();
                concepts.dedup();

                debug!(
                    "Extracted {} concepts from task {}",
                    concepts.len(),
                    task.task_id
                );
                Ok(concepts)
            }
            Err(e) => {
                warn!(
                    "Failed to extract concepts from task {}: {}",
                    task.task_id, e
                );
                Err(TaskDecompositionError::KnowledgeGraphError(format!(
                    "Concept extraction failed: {}",
                    e
                )))
            }
        }
    }

    /// Analyze knowledge graph connectivity for decomposition
    async fn analyze_connectivity(
        &self,
        concepts: &[String],
        _threshold: f64,
    ) -> TaskDecompositionResult<Vec<Vec<String>>> {
        let mut concept_groups = Vec::new();
        let mut processed = HashSet::new();

        for concept in concepts {
            if processed.contains(concept) {
                continue;
            }

            let mut group = vec![concept.clone()];
            processed.insert(concept.clone());

            // Find connected concepts
            for other_concept in concepts {
                if processed.contains(other_concept) {
                    continue;
                }

                match is_all_terms_connected_by_path(&self.automata, &[concept, other_concept]) {
                    Ok(connected) => {
                        if connected {
                            group.push(other_concept.clone());
                            processed.insert(other_concept.clone());
                        }
                    }
                    Err(e) => {
                        debug!(
                            "Connectivity check failed for {} -> {}: {}",
                            concept, other_concept, e
                        );
                    }
                }
            }

            if group.len() > 1 {
                concept_groups.push(group);
            }
        }

        debug!("Found {} concept groups", concept_groups.len());
        Ok(concept_groups)
    }

    /// Generate subtasks from concept groups
    async fn generate_subtasks_from_concepts(
        &self,
        _original_task: &Task,
        concept_groups: &[Vec<String>],
        config: &DecompositionConfig,
    ) -> TaskDecompositionResult<Vec<Task>> {
        let mut subtasks = Vec::new();
        let base_priority = _original_task.priority;

        for (i, group) in concept_groups.iter().enumerate() {
            if subtasks.len() >= config.max_subtasks_per_task as usize {
                break;
            }

            let subtask_id = format!("{}_{}", _original_task.task_id, i + 1);
            let description = format!(
                "Subtask of '{}' focusing on: {}",
                _original_task.description,
                group.join(", ")
            );

            let mut subtask = Task::new(
                subtask_id,
                description,
                config.min_subtask_complexity.clone(),
                base_priority,
            );

            // Set knowledge context
            subtask.knowledge_context.domains = _original_task.knowledge_context.domains.clone();
            subtask.knowledge_context.concepts = group.clone();
            subtask.knowledge_context.relationships =
                _original_task.knowledge_context.relationships.clone();
            subtask.knowledge_context.keywords = group.clone();
            subtask.knowledge_context.input_types =
                _original_task.knowledge_context.input_types.clone();
            subtask.knowledge_context.output_types =
                _original_task.knowledge_context.output_types.clone();
            subtask.knowledge_context.similarity_thresholds = _original_task
                .knowledge_context
                .similarity_thresholds
                .clone();

            // Inherit some constraints
            for constraint in &_original_task.constraints {
                use crate::TaskConstraintType;
                if matches!(
                    constraint.constraint_type,
                    TaskConstraintType::Quality | TaskConstraintType::Security
                ) {
                    subtask.add_constraint(constraint.clone())?;
                }
            }

            // Set parent goal
            subtask.parent_goal = _original_task.parent_goal.clone();

            // Estimate effort (distribute original effort)
            let effort_fraction = 1.0 / concept_groups.len() as f64;
            subtask.estimated_effort = _original_task.estimated_effort.mul_f64(effort_fraction);

            subtasks.push(subtask);
        }

        info!(
            "Generated {} subtasks for task {}",
            subtasks.len(),
            _original_task.task_id
        );
        Ok(subtasks)
    }

    /// Generate dependencies between subtasks
    async fn generate_subtask_dependencies(
        &self,
        subtasks: &[Task],
        _original_task: &Task,
        config: &DecompositionConfig,
    ) -> TaskDecompositionResult<HashMap<TaskId, Vec<TaskId>>> {
        let mut dependencies = HashMap::new();

        if !config.preserve_dependencies {
            return Ok(dependencies);
        }

        // Analyze concept relationships to determine dependencies
        for (i, subtask) in subtasks.iter().enumerate() {
            let mut deps = Vec::new();

            // Check if this subtask's concepts depend on previous subtasks' concepts
            for (j, other_subtask) in subtasks.iter().enumerate() {
                if i == j {
                    continue;
                }

                let has_dependency = self
                    .check_concept_dependency(
                        &subtask.knowledge_context.concepts,
                        &other_subtask.knowledge_context.concepts,
                    )
                    .await?;

                if has_dependency && j < i {
                    deps.push(other_subtask.task_id.clone());
                }
            }

            if !deps.is_empty() {
                dependencies.insert(subtask.task_id.clone(), deps);
            }
        }

        debug!("Generated {} dependency relationships", dependencies.len());
        Ok(dependencies)
    }

    /// Check if one set of concepts depends on another
    async fn check_concept_dependency(
        &self,
        dependent_concepts: &[String],
        prerequisite_concepts: &[String],
    ) -> TaskDecompositionResult<bool> {
        // Simple heuristic: check if any dependent concept is connected to prerequisite concepts
        for dep_concept in dependent_concepts {
            for prereq_concept in prerequisite_concepts {
                match is_all_terms_connected_by_path(&self.automata, &[prereq_concept, dep_concept])
                {
                    Ok(connected) => {
                        if connected {
                            return Ok(true);
                        }
                    }
                    Err(_) => {
                        // Ignore connectivity check errors
                        continue;
                    }
                }
            }
        }

        Ok(false)
    }

    /// Calculate decomposition confidence score
    fn calculate_confidence_score(
        &self,
        original_task: &Task,
        subtasks: &[Task],
        concept_groups: &[Vec<String>],
    ) -> f64 {
        let mut score = 0.0;

        // Factor 1: Concept coverage (how well subtasks cover original concepts)
        let original_concepts: HashSet<String> = original_task
            .knowledge_context
            .concepts
            .iter()
            .cloned()
            .collect();
        let subtask_concepts: HashSet<String> = subtasks
            .iter()
            .flat_map(|t| t.knowledge_context.concepts.iter().cloned())
            .collect();

        let coverage = if original_concepts.is_empty() {
            1.0
        } else {
            subtask_concepts.intersection(&original_concepts).count() as f64
                / original_concepts.len() as f64
        };

        score += coverage * 0.4;

        // Factor 2: Decomposition balance (how evenly concepts are distributed)
        let concept_distribution = concept_groups.iter().map(|g| g.len()).collect::<Vec<_>>();

        let mean_size =
            concept_distribution.iter().sum::<usize>() as f64 / concept_distribution.len() as f64;
        let variance = concept_distribution
            .iter()
            .map(|&size| (size as f64 - mean_size).powi(2))
            .sum::<f64>()
            / concept_distribution.len() as f64;

        let balance_score = 1.0 / (1.0 + variance);
        score += balance_score * 0.3;

        // Factor 3: Complexity appropriateness
        let complexity_score = if original_task.complexity.requires_decomposition() {
            if subtasks.len() > 1 { 1.0 } else { 0.5 }
        } else if subtasks.len() <= 2 {
            1.0
        } else {
            0.7
        };

        score += complexity_score * 0.3;

        score.clamp(0.0, 1.0)
    }

    /// Calculate parallelism factor
    fn calculate_parallelism_factor(&self, dependencies: &HashMap<TaskId, Vec<TaskId>>) -> f64 {
        if dependencies.is_empty() {
            return 1.0; // All tasks can run in parallel
        }

        // Simple heuristic: ratio of independent tasks to total tasks
        let total_tasks = dependencies.keys().len();
        let independent_tasks = dependencies.values().filter(|deps| deps.is_empty()).count();

        if total_tasks == 0 {
            1.0
        } else {
            independent_tasks as f64 / total_tasks as f64
        }
    }
}

#[async_trait]
impl TaskDecomposer for KnowledgeGraphTaskDecomposer {
    async fn decompose_task(
        &self,
        task: &Task,
        config: &DecompositionConfig,
    ) -> TaskDecompositionResult<DecompositionResult> {
        info!("Starting decomposition of task: {}", task.task_id);

        // Check cache first
        let cache_key = format!("{}_{:?}", task.task_id, config.strategy);
        {
            let cache = self.cache.read().await;
            if let Some(cached_result) = cache.get(&cache_key) {
                debug!("Using cached decomposition for task {}", task.task_id);
                return Ok(cached_result.clone());
            }
        }

        // Extract concepts from task
        let concepts = self.extract_task_concepts(task).await?;

        if concepts.is_empty() {
            return Err(TaskDecompositionError::DecompositionFailed(
                task.task_id.clone(),
                "No concepts could be extracted from task".to_string(),
            ));
        }

        // Analyze concept connectivity
        let concept_groups = self
            .analyze_connectivity(&concepts, config.similarity_threshold)
            .await?;

        if concept_groups.is_empty() || concept_groups.len() == 1 {
            // Task doesn't need decomposition or can't be meaningfully decomposed
            let result = DecompositionResult {
                original_task: task.task_id.clone(),
                subtasks: vec![task.clone()],
                dependencies: HashMap::new(),
                metadata: DecompositionMetadata {
                    strategy_used: config.strategy.clone(),
                    depth: 0,
                    subtask_count: 1,
                    concepts_analyzed: concepts,
                    roles_identified: Vec::new(),
                    confidence_score: 0.8,
                    parallelism_factor: 1.0,
                },
            };

            return Ok(result);
        }

        // Generate subtasks
        let subtasks = self
            .generate_subtasks_from_concepts(task, &concept_groups, config)
            .await?;

        // Generate dependencies
        let dependencies = self
            .generate_subtask_dependencies(&subtasks, task, config)
            .await?;

        // Calculate metadata
        let confidence_score = self.calculate_confidence_score(task, &subtasks, &concept_groups);
        let parallelism_factor = self.calculate_parallelism_factor(&dependencies);

        let result = DecompositionResult {
            original_task: task.task_id.clone(),
            subtasks: subtasks.clone(),
            dependencies,
            metadata: DecompositionMetadata {
                strategy_used: config.strategy.clone(),
                depth: 1, // For now, we only do single-level decomposition
                subtask_count: subtasks.len() as u32,
                concepts_analyzed: concepts,
                roles_identified: Vec::new(), // TODO: Implement role identification
                confidence_score,
                parallelism_factor,
            },
        };

        // Cache the result
        {
            let mut cache = self.cache.write().await;
            cache.insert(cache_key, result.clone());
        }

        info!(
            "Completed decomposition of task {} into {} subtasks",
            task.task_id,
            result.subtasks.len()
        );

        Ok(result)
    }

    async fn analyze_complexity(&self, task: &Task) -> TaskDecompositionResult<TaskComplexity> {
        // Extract concepts to analyze complexity
        let concepts = self.extract_task_concepts(task).await?;

        let complexity = match concepts.len() {
            0..=2 => TaskComplexity::Simple,
            3..=5 => TaskComplexity::Moderate,
            6..=10 => TaskComplexity::Complex,
            _ => TaskComplexity::VeryComplex,
        };

        debug!(
            "Analyzed complexity for task {}: {:?} (based on {} concepts)",
            task.task_id,
            complexity,
            concepts.len()
        );

        Ok(complexity)
    }

    async fn validate_decomposition(
        &self,
        result: &DecompositionResult,
    ) -> TaskDecompositionResult<bool> {
        // Basic validation checks
        if result.subtasks.is_empty() {
            return Ok(false);
        }

        // Check for circular dependencies
        let mut visited = HashSet::new();
        let mut rec_stack = HashSet::new();

        for subtask in &result.subtasks {
            if has_circular_dependency(
                &subtask.task_id,
                &result.dependencies,
                &mut visited,
                &mut rec_stack,
            ) {
                return Ok(false);
            }
        }

        // Check confidence score threshold
        if result.metadata.confidence_score < 0.5 {
            return Ok(false);
        }

        Ok(true)
    }
}

impl KnowledgeGraphTaskDecomposer {}

/// Check for circular dependencies using DFS - standalone function
fn has_circular_dependency(
    task_id: &str,
    dependencies: &HashMap<TaskId, Vec<TaskId>>,
    visited: &mut HashSet<String>,
    rec_stack: &mut HashSet<String>,
) -> bool {
    visited.insert(task_id.to_string());
    rec_stack.insert(task_id.to_string());

    if let Some(deps) = dependencies.get(task_id) {
        for dep in deps {
            if !visited.contains(dep) {
                if has_circular_dependency(dep, dependencies, visited, rec_stack) {
                    return true;
                }
            } else if rec_stack.contains(dep) {
                return true;
            }
        }
    }

    rec_stack.remove(task_id);
    false
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;

    use crate::decomposition::Automata;
    use terraphim_rolegraph::RoleGraph;

    fn create_test_automata() -> Arc<Automata> {
        // Create a simple test automata
        Arc::new(Automata::default())
    }

    async fn create_test_role_graph() -> Arc<RoleGraph> {
        use terraphim_automata::{AutomataPath, load_thesaurus};
        use terraphim_types::RoleName;

        // Use the existing test pattern from rolegraph crate
        let role_name = RoleName::new("test_role");
        let thesaurus = load_thesaurus(&AutomataPath::local_example())
            .await
            .unwrap();

        let role_graph = RoleGraph::new(role_name, thesaurus).await.unwrap();

        Arc::new(role_graph)
    }

    #[tokio::test]
    async fn test_task_decomposer_creation() {
        let automata = create_test_automata();
        let role_graph = create_test_role_graph().await;

        let decomposer = KnowledgeGraphTaskDecomposer::new(automata, role_graph);

        // Test that decomposer was created successfully
        assert!(decomposer.cache.read().await.is_empty());
    }

    #[tokio::test]
    async fn test_simple_task_decomposition() {
        let automata = create_test_automata();
        let role_graph = create_test_role_graph().await;
        let decomposer = KnowledgeGraphTaskDecomposer::new(automata, role_graph);

        let task = Task::new(
            "test_task".to_string(),
            "Simple test task".to_string(),
            TaskComplexity::Simple,
            1,
        );

        let config = DecompositionConfig::default();
        let result = decomposer.decompose_task(&task, &config).await;

        assert!(result.is_ok());
        let decomposition = result.unwrap();
        assert_eq!(decomposition.original_task, "test_task");
        assert!(!decomposition.subtasks.is_empty());
    }

    #[tokio::test]
    async fn test_complexity_analysis() {
        let automata = create_test_automata();
        let role_graph = create_test_role_graph().await;
        let decomposer = KnowledgeGraphTaskDecomposer::new(automata, role_graph);

        let simple_task = Task::new(
            "simple".to_string(),
            "Simple task".to_string(),
            TaskComplexity::Simple,
            1,
        );

        let result = decomposer.analyze_complexity(&simple_task).await;
        assert!(result.is_ok());
    }

    #[test]
    fn test_decomposition_config_defaults() {
        let config = DecompositionConfig::default();

        assert_eq!(config.max_depth, 3);
        assert_eq!(config.min_subtask_complexity, TaskComplexity::Simple);
        assert_eq!(config.max_subtasks_per_task, 10);
        assert_eq!(config.strategy, DecompositionStrategy::Hybrid);
        assert_eq!(config.similarity_threshold, 0.7);
        assert!(config.preserve_dependencies);
        assert!(config.optimize_for_parallelism);
    }
}