|
| 1 | +# AGENT_1: Multi-Agent System & Subagents |
| 2 | + |
| 3 | +## Mission |
| 4 | + |
| 5 | +Implémenter un système complet de multi-agents permettant l'orchestration de subagents parallèles avec communication inter-agents, inspiré de Claude Code et OpenAI Codex. |
| 6 | + |
| 7 | +## Contexte |
| 8 | + |
| 9 | +**Source de référence:** AMELIORATIONS.md Sections 2.1, 5.5, 16.1.1, 21.1.10 |
| 10 | +**Priorité:** 🔴 Critique |
| 11 | +**Effort estimé:** 6-8 jours développeur |
| 12 | +**Dépendances internes:** cortex-agents/ (existant mais incomplet) |
| 13 | + |
| 14 | +## État Actuel dans cortex-cli |
| 15 | + |
| 16 | +Le crate `cortex-agents/` existe avec: |
| 17 | +- `AgentControl` - Contrôle basique des threads agents |
| 18 | +- `AgentThread` - Représentation d'un thread agent |
| 19 | +- `AgentInfo` - Configuration d'un agent |
| 20 | +- `@mention` support pour invoquer des subagents |
| 21 | + |
| 22 | +**Manquant:** |
| 23 | +- Outils de collaboration (spawn_agent, send_input, wait, close_agent) |
| 24 | +- DAG de dépendances entre tâches |
| 25 | +- Communication asynchrone entre agents |
| 26 | +- Gestion des limites de profondeur |
| 27 | +- Swarms multi-agents |
| 28 | + |
| 29 | +## Objectifs Spécifiques |
| 30 | + |
| 31 | +### 1. Outils de Collaboration Multi-Agent |
| 32 | + |
| 33 | +Créer `cortex-agents/src/collab/` avec: |
| 34 | + |
| 35 | +```rust |
| 36 | +// collab/mod.rs |
| 37 | +pub mod spawn; |
| 38 | +pub mod send_input; |
| 39 | +pub mod wait; |
| 40 | +pub mod close; |
| 41 | + |
| 42 | +// Constantes |
| 43 | +pub const MAX_THREAD_SPAWN_DEPTH: i32 = 1; |
| 44 | +pub const MIN_WAIT_TIMEOUT_MS: i64 = 10_000; |
| 45 | +pub const DEFAULT_WAIT_TIMEOUT_MS: i64 = 30_000; |
| 46 | +pub const MAX_WAIT_TIMEOUT_MS: i64 = 300_000; |
| 47 | +``` |
| 48 | + |
| 49 | +### 2. spawn_agent Tool |
| 50 | + |
| 51 | +```rust |
| 52 | +// collab/spawn.rs |
| 53 | +#[derive(Debug, Deserialize)] |
| 54 | +pub struct SpawnAgentArgs { |
| 55 | + pub message: String, |
| 56 | + pub agent_type: Option<AgentRole>, |
| 57 | +} |
| 58 | + |
| 59 | +#[derive(Debug, Serialize)] |
| 60 | +pub struct SpawnAgentResult { |
| 61 | + pub agent_id: String, |
| 62 | +} |
| 63 | + |
| 64 | +pub async fn handle( |
| 65 | + session: &Session, |
| 66 | + args: SpawnAgentArgs, |
| 67 | +) -> Result<SpawnAgentResult, CollabError> { |
| 68 | + // 1. Valider le message non-vide |
| 69 | + // 2. Vérifier la profondeur d'imbrication |
| 70 | + // 3. Créer config pour le nouvel agent |
| 71 | + // 4. Spawn via AgentControl |
| 72 | + // 5. Retourner l'ID du nouvel agent |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +### 3. send_input Tool |
| 77 | + |
| 78 | +```rust |
| 79 | +// collab/send_input.rs |
| 80 | +#[derive(Debug, Deserialize)] |
| 81 | +pub struct SendInputArgs { |
| 82 | + pub id: String, |
| 83 | + pub message: String, |
| 84 | + #[serde(default)] |
| 85 | + pub interrupt: bool, |
| 86 | +} |
| 87 | + |
| 88 | +pub async fn handle( |
| 89 | + control: &AgentControl, |
| 90 | + args: SendInputArgs, |
| 91 | +) -> Result<SendInputResult, CollabError> { |
| 92 | + // 1. Parser l'ID de l'agent |
| 93 | + // 2. Optionnellement interrompre avant envoi |
| 94 | + // 3. Envoyer le prompt |
| 95 | + // 4. Retourner le submission_id |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +### 4. wait Tool |
| 100 | + |
| 101 | +```rust |
| 102 | +// collab/wait.rs |
| 103 | +#[derive(Debug, Deserialize)] |
| 104 | +pub struct WaitArgs { |
| 105 | + pub ids: Vec<String>, |
| 106 | + pub timeout_ms: Option<i64>, |
| 107 | +} |
| 108 | + |
| 109 | +#[derive(Debug, Serialize)] |
| 110 | +pub struct WaitResult { |
| 111 | + pub status: HashMap<String, AgentThreadStatus>, |
| 112 | + pub timed_out: bool, |
| 113 | +} |
| 114 | + |
| 115 | +pub async fn handle( |
| 116 | + control: &AgentControl, |
| 117 | + args: WaitArgs, |
| 118 | +) -> Result<WaitResult, CollabError> { |
| 119 | + // 1. Valider les IDs non-vides |
| 120 | + // 2. Clamper le timeout entre MIN et MAX |
| 121 | + // 3. S'abonner aux status |
| 122 | + // 4. Attendre avec FuturesUnordered + timeout |
| 123 | + // 5. Retourner les status finaux |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +### 5. close_agent Tool |
| 128 | + |
| 129 | +```rust |
| 130 | +// collab/close.rs |
| 131 | +pub async fn handle( |
| 132 | + control: &AgentControl, |
| 133 | + agent_id: &str, |
| 134 | +) -> Result<CloseAgentResult, CollabError> { |
| 135 | + // 1. Parser l'ID |
| 136 | + // 2. Récupérer le status actuel |
| 137 | + // 3. Shutdown si pas déjà fermé |
| 138 | + // 4. Retourner le status final |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +### 6. Système de DAG pour Tâches |
| 143 | + |
| 144 | +Créer `cortex-agents/src/task/`: |
| 145 | + |
| 146 | +```rust |
| 147 | +// task/mod.rs |
| 148 | +pub mod dag; |
| 149 | +pub mod persistence; |
| 150 | +pub mod hydration; |
| 151 | + |
| 152 | +// task/dag.rs |
| 153 | +pub struct TaskDag { |
| 154 | + tasks: HashMap<TaskId, Task>, |
| 155 | + dependencies: HashMap<TaskId, HashSet<TaskId>>, |
| 156 | +} |
| 157 | + |
| 158 | +impl TaskDag { |
| 159 | + pub fn new() -> Self; |
| 160 | + pub fn add_task(&mut self, task: Task) -> TaskId; |
| 161 | + pub fn add_dependency(&mut self, task: TaskId, depends_on: TaskId); |
| 162 | + pub fn get_ready_tasks(&self) -> Vec<&Task>; |
| 163 | + pub fn mark_completed(&mut self, task_id: TaskId); |
| 164 | + pub fn topological_sort(&self) -> Result<Vec<TaskId>>; |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | +### 7. Routing Decision Framework |
| 169 | + |
| 170 | +```rust |
| 171 | +// routing.rs |
| 172 | +pub enum DispatchMode { |
| 173 | + Parallel, // 3+ tâches non-liées, pas d'état partagé |
| 174 | + Sequential, // Dépendances entre tâches, fichiers partagés |
| 175 | + Background, // Recherche/analyse, pas de modifications |
| 176 | +} |
| 177 | + |
| 178 | +pub struct RoutingDecision { |
| 179 | + pub mode: DispatchMode, |
| 180 | + pub reason: String, |
| 181 | + pub affected_files: Vec<PathBuf>, |
| 182 | +} |
| 183 | + |
| 184 | +pub fn decide_routing(tasks: &[Task]) -> RoutingDecision { |
| 185 | + // Analyser les fichiers modifiés |
| 186 | + // Détecter les dépendances implicites |
| 187 | + // Recommander le mode de dispatch |
| 188 | +} |
| 189 | +``` |
| 190 | + |
| 191 | +## Fichiers à Créer |
| 192 | + |
| 193 | +``` |
| 194 | +cortex-agents/src/ |
| 195 | +├── collab/ |
| 196 | +│ ├── mod.rs |
| 197 | +│ ├── spawn.rs |
| 198 | +│ ├── send_input.rs |
| 199 | +│ ├── wait.rs |
| 200 | +│ ├── close.rs |
| 201 | +│ └── error.rs |
| 202 | +├── task/ |
| 203 | +│ ├── mod.rs |
| 204 | +│ ├── dag.rs |
| 205 | +│ ├── persistence.rs |
| 206 | +│ └── hydration.rs |
| 207 | +├── routing.rs |
| 208 | +└── swarm.rs (optionnel phase 2) |
| 209 | +``` |
| 210 | + |
| 211 | +## Fichiers à Modifier |
| 212 | + |
| 213 | +1. `cortex-agents/src/lib.rs` - Ajouter exports des nouveaux modules |
| 214 | +2. `cortex-agents/src/control.rs` - Étendre AgentControl |
| 215 | +3. `cortex-agents/Cargo.toml` - Ajouter dépendances (futures, uuid, tokio) |
| 216 | + |
| 217 | +## Tests Requis |
| 218 | + |
| 219 | +```rust |
| 220 | +#[cfg(test)] |
| 221 | +mod tests { |
| 222 | + // Test spawn avec validation de profondeur |
| 223 | + #[tokio::test] |
| 224 | + async fn test_spawn_depth_limit() { ... } |
| 225 | + |
| 226 | + // Test communication inter-agents |
| 227 | + #[tokio::test] |
| 228 | + async fn test_send_input_to_agent() { ... } |
| 229 | + |
| 230 | + // Test wait avec timeout |
| 231 | + #[tokio::test] |
| 232 | + async fn test_wait_with_timeout() { ... } |
| 233 | + |
| 234 | + // Test DAG topological sort |
| 235 | + #[test] |
| 236 | + fn test_dag_topological_sort() { ... } |
| 237 | + |
| 238 | + // Test routing decision |
| 239 | + #[test] |
| 240 | + fn test_routing_decision_parallel() { ... } |
| 241 | +} |
| 242 | +``` |
| 243 | + |
| 244 | +## Sécurité |
| 245 | + |
| 246 | +### À Implémenter |
| 247 | + |
| 248 | +1. **Limite de profondeur** - MAX_THREAD_SPAWN_DEPTH = 1 (pas de récursion infinie) |
| 249 | +2. **Limite de threads** - Max concurrent agents par session |
| 250 | +3. **Timeout obligatoire** - MIN_WAIT_TIMEOUT_MS pour éviter le polling |
| 251 | +4. **Isolation de contexte** - Chaque agent a son propre contexte |
| 252 | +5. **RAII Guards** - Cleanup automatique des resources |
| 253 | + |
| 254 | +### Vérifications de Sécurité |
| 255 | + |
| 256 | +```rust |
| 257 | +// Avant chaque spawn |
| 258 | +fn validate_spawn(session: &Session) -> Result<()> { |
| 259 | + let depth = get_current_depth(session); |
| 260 | + if depth > MAX_THREAD_SPAWN_DEPTH { |
| 261 | + bail!("Agent depth limit reached"); |
| 262 | + } |
| 263 | + let active = session.agent_control.active_count(); |
| 264 | + if active >= MAX_CONCURRENT_AGENTS { |
| 265 | + bail!("Maximum concurrent agents reached"); |
| 266 | + } |
| 267 | + Ok(()) |
| 268 | +} |
| 269 | +``` |
| 270 | + |
| 271 | +## Ce Qu'il Ne Faut PAS Faire |
| 272 | + |
| 273 | +1. ❌ Permettre la récursion infinie d'agents |
| 274 | +2. ❌ Ignorer les timeouts |
| 275 | +3. ❌ Partager l'état mutable entre agents sans synchronisation |
| 276 | +4. ❌ Laisser des agents orphelins sans cleanup |
| 277 | +5. ❌ Logger les prompts utilisateur (peuvent contenir des secrets) |
| 278 | + |
| 279 | +## Critères de Succès |
| 280 | + |
| 281 | +- [ ] `spawn_agent` fonctionne avec limite de profondeur |
| 282 | +- [ ] `send_input` permet la communication bidirectionnelle |
| 283 | +- [ ] `wait` respecte les timeouts avec clamping |
| 284 | +- [ ] `close_agent` fait un cleanup propre |
| 285 | +- [ ] DAG gère correctement les dépendances |
| 286 | +- [ ] Tests unitaires passent |
| 287 | +- [ ] Pas de memory leaks (agents cleanup) |
| 288 | +- [ ] Documentation Rustdoc complète |
| 289 | + |
| 290 | +## Estimation |
| 291 | + |
| 292 | +| Tâche | Durée | |
| 293 | +|-------|-------| |
| 294 | +| Outils collab (spawn, send, wait, close) | 3 jours | |
| 295 | +| DAG de tâches | 2 jours | |
| 296 | +| Routing framework | 1 jour | |
| 297 | +| Tests et documentation | 2 jours | |
| 298 | +| **Total** | **8 jours** | |
| 299 | + |
| 300 | +--- |
| 301 | + |
| 302 | +*Agent autonome - Pas de dépendance externe* |
0 commit comments