nika 0.35.4

Semantic YAML workflow engine for AI tasks - DAG execution, MCP integration, multi-provider LLM support
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
//! Spawn Agent Tool (MVP 8 Phase 2)
//!
//! Internal tool for recursive agent spawning. Allows an agent to delegate
//! subtasks to child agents, enabling hierarchical task decomposition.
//!
//! ## Depth Limit
//!
//! To prevent infinite recursion, each spawn tracks depth and enforces limits:
//! - Default limit: 3 levels
//! - Maximum limit: 10 levels
//! - Spawning at max depth returns an error
//!
//! ## Events
//!
//! Spawning emits an `AgentSpawned` event with:
//! - `parent_task_id`: The spawning agent's task ID
//! - `child_task_id`: The new agent's task ID
//! - `depth`: Current recursion depth
//!
//! ## rig::ToolDyn Integration
//!
//! SpawnAgentTool implements `rig::tool::ToolDyn` for seamless integration
//! with `RigAgentLoop`. When an agent has `depth_limit > current_depth`,
//! the spawn_agent tool is automatically added to its tool list.
//!
//! ## Example
//!
//! ```json
//! {
//!   "task_id": "subtask-1",
//!   "prompt": "Generate the header section",
//!   "context": {"entity": "qr-code"},
//!   "max_turns": 5
//! }
//! ```

use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use rig::completion::ToolDefinition;
use rig::tool::{ToolDyn, ToolError};
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};

use crate::ast::AgentParams;
use crate::event::{EventKind, EventLog};
use crate::mcp::McpClient;

/// Parameters for spawning a child agent
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SpawnAgentParams {
    /// Unique identifier for the child task
    pub task_id: String,
    /// Prompt/goal for the child agent
    pub prompt: String,
    /// Optional context data to pass to child
    #[serde(default)]
    pub context: Option<Value>,
    /// Optional max turns override for child
    #[serde(default)]
    pub max_turns: Option<u32>,
}

/// Internal tool for spawning sub-agents
///
/// This tool is automatically added to agents that have depth_limit > current_depth.
/// It allows recursive task decomposition with safety limits.
///
/// Implements `rig::tool::ToolDyn` for integration with RigAgentLoop.
#[derive(Clone)]
pub struct SpawnAgentTool {
    /// Current recursion depth (1 = root agent)
    current_depth: u32,
    /// Maximum allowed depth
    max_depth: u32,
    /// Parent task ID for event linking
    parent_task_id: Arc<str>,
    /// Event log for emitting AgentSpawned events
    event_log: EventLog,
    /// MCP clients for child agent tool access
    mcp_clients: FxHashMap<String, Arc<McpClient>>,
    /// MCP server names for child agents (from parent AgentParams.mcp)
    mcp_names: Vec<String>,
}

impl SpawnAgentTool {
    /// Create a new SpawnAgentTool (minimal - for testing without MCP)
    ///
    /// # Arguments
    /// * `current_depth` - Current recursion depth (starts at 1 for root)
    /// * `max_depth` - Maximum allowed depth (default 3)
    /// * `parent_task_id` - ID of the parent task
    /// * `event_log` - Shared event log for observability
    pub fn new(
        current_depth: u32,
        max_depth: u32,
        parent_task_id: Arc<str>,
        event_log: EventLog,
    ) -> Self {
        Self {
            current_depth,
            max_depth,
            parent_task_id,
            event_log,
            mcp_clients: FxHashMap::default(),
            mcp_names: Vec::new(),
        }
    }

    /// Create a new SpawnAgentTool with MCP clients (for production use)
    ///
    /// # Arguments
    /// * `current_depth` - Current recursion depth (starts at 1 for root)
    /// * `max_depth` - Maximum allowed depth (default 3)
    /// * `parent_task_id` - ID of the parent task
    /// * `event_log` - Shared event log for observability
    /// * `mcp_clients` - Connected MCP clients for child agent tools
    /// * `mcp_names` - MCP server names to pass to child agents
    pub fn with_mcp(
        current_depth: u32,
        max_depth: u32,
        parent_task_id: Arc<str>,
        event_log: EventLog,
        mcp_clients: FxHashMap<String, Arc<McpClient>>,
        mcp_names: Vec<String>,
    ) -> Self {
        Self {
            current_depth,
            max_depth,
            parent_task_id,
            event_log,
            mcp_clients,
            mcp_names,
        }
    }

    /// Get the tool name
    pub fn name(&self) -> &str {
        "spawn_agent"
    }

    /// Get the JSON Schema definition for this tool
    ///
    /// Note: OpenAI's strict mode requires ALL properties in `required` and
    /// `additionalProperties: false`. We keep the schema simple with only
    /// required fields - context and max_turns use sensible defaults.
    pub fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: "spawn_agent".to_string(),
            description: "Spawn a sub-agent to handle a delegated subtask. The child agent \
                         runs independently with max 10 turns and returns its result."
                .to_string(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "task_id": {
                        "type": "string",
                        "description": "Unique identifier for the child task (e.g., 'subtask-1')"
                    },
                    "prompt": {
                        "type": "string",
                        "description": "Goal/prompt describing what the child agent should accomplish"
                    }
                },
                "required": ["task_id", "prompt"],
                "additionalProperties": false
            }),
        }
    }

    /// Execute the spawn_agent tool
    ///
    /// Creates and runs a child `RigAgentLoop` with inherited MCP clients.
    /// The child agent runs to completion and its result is returned.
    ///
    /// # Errors
    /// Returns an error if:
    /// - Current depth >= max depth (depth limit reached)
    /// - Invalid arguments
    /// - Child agent execution fails
    pub async fn call(&self, args: String) -> Result<String, SpawnAgentError> {
        // Parse arguments
        let params: SpawnAgentParams =
            serde_json::from_str(&args).map_err(|e| SpawnAgentError::InvalidArgs(e.to_string()))?;

        // Check depth limit
        if self.current_depth >= self.max_depth {
            return Err(SpawnAgentError::DepthLimitReached {
                current: self.current_depth,
                max: self.max_depth,
            });
        }

        // Emit AgentSpawned event
        let child_depth = self.current_depth + 1;
        self.event_log.emit(EventKind::AgentSpawned {
            parent_task_id: self.parent_task_id.clone(),
            child_task_id: Arc::from(params.task_id.as_str()),
            depth: child_depth,
        });

        // If no MCP clients, return placeholder (for compatibility with tests)
        if self.mcp_clients.is_empty() {
            return Ok(json!({
                "status": "spawned",
                "child_task_id": params.task_id,
                "depth": child_depth,
                "note": "Child agent execution requires MCP client context"
            })
            .to_string());
        }

        // Build child AgentParams
        // Calculate remaining depth from PARENT's current_depth, not child_depth.
        // With depth_limit=3:
        //   - Root (depth=1, max=3): remaining = 3-1 = 2 → child gets max=2
        //   - Child (depth=1, max=2): remaining = 2-1 = 1 → grandchild gets max=1
        //   - Grandchild (depth=1, max=1): 1 >= 1, cannot spawn ✓
        let remaining_depth = self.max_depth.saturating_sub(self.current_depth);
        let child_params = AgentParams {
            prompt: params.prompt,
            system: params.context.as_ref().map(|ctx| {
                format!(
                    "Context from parent agent:\n{}",
                    serde_json::to_string_pretty(ctx).unwrap_or_default()
                )
            }),
            mcp: self.mcp_names.clone(),
            max_turns: params.max_turns.or(Some(10)),
            depth_limit: Some(remaining_depth),
            ..Default::default()
        };

        // Create child RigAgentLoop
        let mut child_loop = super::RigAgentLoop::new(
            params.task_id.clone(),
            child_params,
            self.event_log.clone(),
            self.mcp_clients.clone(),
        )
        .map_err(|e| SpawnAgentError::ExecutionFailed(e.to_string()))?;

        // Execute child agent with auto-detected provider (production mode)
        // Uses ANTHROPIC_API_KEY or OPENAI_API_KEY from environment
        let result = child_loop
            .run_auto()
            .await
            .map_err(|e| SpawnAgentError::ExecutionFailed(e.to_string()))?;

        // Return child's result
        Ok(json!({
            "status": "completed",
            "child_task_id": params.task_id,
            "depth": child_depth,
            "result": result.final_output,
            "turns": result.turns,
            "total_tokens": result.total_tokens
        })
        .to_string())
    }

    /// Check if spawning is allowed at current depth
    pub fn can_spawn(&self) -> bool {
        self.current_depth < self.max_depth
    }

    /// Get the depth that child agents would have
    pub fn child_depth(&self) -> u32 {
        self.current_depth + 1
    }
}

/// Errors that can occur when spawning agents
#[derive(Debug, thiserror::Error)]
pub enum SpawnAgentError {
    #[error("spawn_agent: depth limit reached (current: {current}, max: {max})")]
    DepthLimitReached { current: u32, max: u32 },

    #[error("spawn_agent: invalid arguments - {0}")]
    InvalidArgs(String),

    #[error("spawn_agent: execution failed - {0}")]
    ExecutionFailed(String),
}

impl std::fmt::Debug for SpawnAgentTool {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SpawnAgentTool")
            .field("current_depth", &self.current_depth)
            .field("max_depth", &self.max_depth)
            .field("parent_task_id", &self.parent_task_id)
            .finish()
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// rig::ToolDyn implementation (for integration with RigAgentLoop)
// ═══════════════════════════════════════════════════════════════════════════════

/// Type alias for boxed future (required by ToolDyn)
type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;

impl ToolDyn for SpawnAgentTool {
    fn name(&self) -> String {
        "spawn_agent".to_string()
    }

    fn definition(&self, _prompt: String) -> BoxFuture<'_, ToolDefinition> {
        // Note: OpenAI's strict mode requires ALL properties in `required` and
        // `additionalProperties: false`. Keep schema simple with only required fields.
        let def = ToolDefinition {
            name: "spawn_agent".to_string(),
            description: "Spawn a sub-agent to handle a delegated subtask. The child agent \
                         runs independently with max 10 turns and returns its result."
                .to_string(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "task_id": {
                        "type": "string",
                        "description": "Unique identifier for the child task (e.g., 'subtask-1')"
                    },
                    "prompt": {
                        "type": "string",
                        "description": "Goal/prompt describing what the child agent should accomplish"
                    }
                },
                "required": ["task_id", "prompt"],
                "additionalProperties": false
            }),
        };
        Box::pin(async move { def })
    }

    fn call(&self, args: String) -> BoxFuture<'_, Result<String, ToolError>> {
        Box::pin(async move {
            self.call(args).await.map_err(|e| {
                ToolError::ToolCallError(Box::new(std::io::Error::other(e.to_string())))
            })
        })
    }
}

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

    #[test]
    fn spawn_agent_tool_name() {
        let tool = SpawnAgentTool::new(1, 3, "parent".into(), EventLog::new());
        assert_eq!(tool.name(), "spawn_agent");
    }

    #[test]
    fn spawn_agent_tool_can_spawn() {
        let tool = SpawnAgentTool::new(1, 3, "parent".into(), EventLog::new());
        assert!(tool.can_spawn());

        let at_limit = SpawnAgentTool::new(3, 3, "parent".into(), EventLog::new());
        assert!(!at_limit.can_spawn());
    }

    #[test]
    fn spawn_agent_tool_child_depth() {
        let tool = SpawnAgentTool::new(1, 3, "parent".into(), EventLog::new());
        assert_eq!(tool.child_depth(), 2);
    }

    #[test]
    fn spawn_agent_params_deserializes() {
        let json = json!({
            "task_id": "child-1",
            "prompt": "Do something",
            "context": {"key": "value"},
            "max_turns": 5
        });

        let params: SpawnAgentParams = serde_json::from_value(json).unwrap();
        assert_eq!(params.task_id, "child-1");
        assert_eq!(params.prompt, "Do something");
        assert!(params.context.is_some());
        assert_eq!(params.max_turns, Some(5));
    }

    #[test]
    fn spawn_agent_params_minimal() {
        let json = json!({
            "task_id": "child-1",
            "prompt": "Do something"
        });

        let params: SpawnAgentParams = serde_json::from_value(json).unwrap();
        assert_eq!(params.task_id, "child-1");
        assert!(params.context.is_none());
        assert!(params.max_turns.is_none());
    }

    #[tokio::test]
    async fn spawn_agent_at_max_depth_fails() {
        let tool = SpawnAgentTool::new(3, 3, "parent".into(), EventLog::new());

        let args = json!({
            "task_id": "child-1",
            "prompt": "Do something"
        })
        .to_string();

        let result = tool.call(args).await;
        assert!(result.is_err());

        let err = result.unwrap_err();
        assert!(err.to_string().contains("depth limit"));
    }

    #[tokio::test]
    async fn spawn_agent_below_max_depth_succeeds() {
        let tool = SpawnAgentTool::new(2, 3, "parent".into(), EventLog::new());

        let args = json!({
            "task_id": "child-1",
            "prompt": "Do something"
        })
        .to_string();

        let result = tool.call(args).await;
        assert!(result.is_ok());

        let response: Value = serde_json::from_str(&result.unwrap()).unwrap();
        assert_eq!(response["status"], "spawned");
        assert_eq!(response["child_task_id"], "child-1");
        assert_eq!(response["depth"], 3);
    }

    #[tokio::test]
    async fn spawn_agent_emits_event() {
        let event_log = EventLog::new();
        let tool = SpawnAgentTool::new(1, 3, "parent".into(), event_log.clone());

        let args = json!({
            "task_id": "child-1",
            "prompt": "Do something"
        })
        .to_string();

        let _ = tool.call(args).await;

        // Check that AgentSpawned event was emitted
        let events = event_log.events();
        let spawned_events: Vec<_> = events
            .iter()
            .filter(|e| matches!(e.kind, EventKind::AgentSpawned { .. }))
            .collect();

        assert_eq!(spawned_events.len(), 1);

        if let EventKind::AgentSpawned {
            parent_task_id,
            child_task_id,
            depth,
        } = &spawned_events[0].kind
        {
            assert_eq!(&**parent_task_id, "parent");
            assert_eq!(&**child_task_id, "child-1");
            assert_eq!(*depth, 2);
        }
    }

    #[test]
    fn tool_definition_has_required_params() {
        let tool = SpawnAgentTool::new(1, 3, "parent".into(), EventLog::new());
        let def = tool.definition();

        let required = def
            .parameters
            .get("required")
            .and_then(|v| v.as_array())
            .expect("required should be an array");

        // OpenAI strict mode: all properties in required + additionalProperties: false
        // We keep schema simple with only task_id and prompt (context/max_turns use defaults)
        assert!(required.iter().any(|v| v == "task_id"));
        assert!(required.iter().any(|v| v == "prompt"));
        assert_eq!(required.len(), 2);

        // Check additionalProperties is false (required for OpenAI strict mode)
        let additional = def
            .parameters
            .get("additionalProperties")
            .expect("additionalProperties should exist");
        assert_eq!(additional, false);
    }

    // =========================================================================
    // rig::ToolDyn implementation tests
    // =========================================================================

    #[test]
    fn spawn_agent_implements_tool_dyn() {
        use rig::tool::ToolDyn;

        let tool = SpawnAgentTool::new(1, 3, "parent".into(), EventLog::new());

        // Test ToolDyn::name()
        let name: String = ToolDyn::name(&tool);
        assert_eq!(name, "spawn_agent");
    }

    #[tokio::test]
    async fn spawn_agent_tool_dyn_definition_returns_correct_schema() {
        use rig::tool::ToolDyn;

        let tool = SpawnAgentTool::new(1, 3, "parent".into(), EventLog::new());

        // Test ToolDyn::definition()
        let def = ToolDyn::definition(&tool, "test".to_string()).await;

        assert_eq!(def.name, "spawn_agent");
        assert!(def.description.contains("sub-agent"));
        assert!(def.parameters.get("required").is_some());
    }

    #[tokio::test]
    async fn spawn_agent_tool_dyn_call_enforces_depth_limit() {
        use rig::tool::ToolDyn;

        let tool = SpawnAgentTool::new(3, 3, "parent".into(), EventLog::new());

        let args = json!({
            "task_id": "child-1",
            "prompt": "Do something"
        })
        .to_string();

        // Test ToolDyn::call() - should fail at max depth
        let result = ToolDyn::call(&tool, args).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("depth limit"));
    }

    #[test]
    fn spawn_agent_with_mcp_creates_correctly() {
        let event_log = EventLog::new();
        let mcp_clients = FxHashMap::default();
        let mcp_names = vec!["novanet".to_string()];

        let tool = SpawnAgentTool::with_mcp(
            1,
            3,
            "parent".into(),
            event_log,
            mcp_clients,
            mcp_names.clone(),
        );

        assert_eq!(tool.name(), "spawn_agent");
        assert!(tool.can_spawn());
        assert_eq!(tool.child_depth(), 2);
    }

    // =========================================================================
    // Depth calculation regression tests
    // =========================================================================

    #[test]
    fn depth_calculation_allows_three_levels() {
        // With depth_limit=3, we should allow:
        // - Root (depth=1) → can spawn child
        // - Child (depth=2) → can spawn grandchild
        // - Grandchild (depth=3) → cannot spawn

        // Root agent: current=1, max=3
        let root = SpawnAgentTool::new(1, 3, "root".into(), EventLog::new());
        assert!(root.can_spawn(), "Root should be able to spawn");
        assert_eq!(root.child_depth(), 2);

        // Simulate what child receives: remaining = max - current = 3 - 1 = 2
        // So child sees: current=1, max=2
        let child = SpawnAgentTool::new(1, 2, "child".into(), EventLog::new());
        assert!(
            child.can_spawn(),
            "Child should be able to spawn grandchild"
        );
        assert_eq!(child.child_depth(), 2);

        // Simulate what grandchild receives: remaining = max - current = 2 - 1 = 1
        // So grandchild sees: current=1, max=1
        let grandchild = SpawnAgentTool::new(1, 1, "grandchild".into(), EventLog::new());
        assert!(
            !grandchild.can_spawn(),
            "Grandchild should NOT be able to spawn"
        );
    }

    #[test]
    fn remaining_depth_calculation_formula() {
        // Verify the formula: remaining_depth = max_depth - current_depth
        // NOT: max_depth - child_depth (old buggy formula)

        // Root: current=1, max=3
        let root_current = 1_u32;
        let root_max = 3_u32;
        let child_will_receive = root_max.saturating_sub(root_current); // 3-1=2 ✓
        assert_eq!(child_will_receive, 2, "Child should receive depth_limit=2");

        // Child (simulated): current=1, max=2
        let child_current = 1_u32;
        let child_max = child_will_receive; // 2
        let grandchild_will_receive = child_max.saturating_sub(child_current); // 2-1=1 ✓
        assert_eq!(
            grandchild_will_receive, 1,
            "Grandchild should receive depth_limit=1"
        );

        // Grandchild (simulated): current=1, max=1
        let grandchild_current = 1_u32;
        let grandchild_max = grandchild_will_receive; // 1
        let can_spawn = grandchild_current < grandchild_max; // 1 < 1 = false ✓
        assert!(!can_spawn, "Grandchild should not be able to spawn");
    }
}