xcodeai 2.1.0

Autonomous AI coding agent — zero human intervention, sbox sandboxed, OpenAI-compatible
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
// src/tools/spawn_task.rs
//
// SpawnTaskTool — lets the LLM orchestrate sub-agents.
//
// ── Two modes ─────────────────────────────────────────────────────────────────
//
// SINGLE-TASK mode  (only "description" is provided):
//
//   { "description": "Write a Fibonacci function in src/lib.rs" }
//
//   Creates one CoderAgent, runs it with a fresh message history built from the
//   agent's system prompt plus the description as the first user message.
//   Returns the agent's `final_message` as the tool output.
//
// MULTI-TASK mode  (a "tasks" array is provided):
//
//   {
//     "tasks": [
//       { "id": "write-lib",   "description": "...", "depends_on": [] },
//       { "id": "write-tests", "description": "...", "depends_on": ["write-lib"] }
//     ],
//     "parallel": true,
//     "max_concurrent": 4
//   }
//
//   Builds a TaskGraph from the provided list, runs a TaskExecutor that honours
//   the dependency ordering, and returns a JSON-like text summary of the results.
//
// ── Nesting guard ─────────────────────────────────────────────────────────────
//
// Sub-agents receive a clone of the parent's `ToolContext` with `nesting_depth`
// incremented by 1.  When `nesting_depth >= 3` the tool refuses to spawn so the
// call stack cannot grow without bound.
//
// ─────────────────────────────────────────────────────────────────────────────

use crate::agent::coder::CoderAgent;
use crate::agent::Agent;
use crate::config::AgentConfig;
use crate::llm::Message;
use crate::orchestrator::executor::TaskExecutor;
use crate::orchestrator::graph::{TaskGraph, TaskNode};
use crate::tools::{Tool, ToolContext, ToolResult};
use anyhow::Result;
use async_trait::async_trait;
use std::sync::Arc;

// ─── SpawnTaskTool ────────────────────────────────────────────────────────────

/// Tool that lets the LLM delegate work to one or more sub-agents.
///
/// This is the core of the "完全安全托管" (fully autonomous delegation) design:
/// the top-level LLM can hand off independent sub-tasks, run them in parallel,
/// collect their results, and incorporate the output into its own response —
/// all without human involvement.
///
/// The tool is stateless; all dynamic state comes from `ToolContext`.
pub struct SpawnTaskTool;

#[async_trait]
impl Tool for SpawnTaskTool {
    fn name(&self) -> &str {
        "spawn_task"
    }

    fn description(&self) -> &str {
        "Delegate work to one or more sub-agent(s). \
         In single-task mode, provide 'description' to run a CoderAgent on that task \
         and get back its final message. \
         In multi-task mode, provide a 'tasks' array with id/description/depends_on \
         fields to run tasks in parallel with dependency ordering and get back a \
         summary report. Nesting depth is capped at 3 levels."
    }

    fn parameters_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "description": {
                    "type": "string",
                    "description": "Single-task mode: the task description to give to a CoderAgent."
                },
                "tasks": {
                    "type": "array",
                    "description": "Multi-task mode: list of tasks with dependency ordering.",
                    "items": {
                        "type": "object",
                        "properties": {
                            "id": {
                                "type": "string",
                                "description": "Unique identifier for this task (e.g. 'write-tests')."
                            },
                            "description": {
                                "type": "string",
                                "description": "What this sub-agent should do."
                            },
                            "depends_on": {
                                "type": "array",
                                "items": { "type": "string" },
                                "description": "IDs of tasks that must complete before this one starts.",
                                "default": []
                            }
                        },
                        "required": ["id", "description"]
                    }
                },
                "parallel": {
                    "type": "boolean",
                    "description": "Run independent tasks in parallel (default: true). \
                                    Set false to force serial execution.",
                    "default": true
                },
                "max_concurrent": {
                    "type": "integer",
                    "description": "Maximum tasks running at the same time (default: 4).",
                    "default": 4
                }
            }
        })
    }

    async fn execute(&self, args: serde_json::Value, ctx: &ToolContext) -> Result<ToolResult> {
        // ── 1. Nesting guard ──────────────────────────────────────────────────
        //
        // Each call to spawn_task increments nesting_depth in the child context.
        // We refuse to go deeper than 3 so we can never have unbounded recursion
        // (LLM A spawns LLM B which spawns LLM C which would try to spawn LLM D…).
        if ctx.nesting_depth >= 3 {
            return Ok(ToolResult {
                output: format!(
                    "spawn_task refused: maximum nesting depth (3) reached. \
                     Current depth: {}. Cannot spawn further sub-agents.",
                    ctx.nesting_depth
                ),
                is_error: true,
            });
        }

        // ── 2. Build child context ────────────────────────────────────────────
        //
        // Clone the full context and bump the depth counter.  The child agents
        // inherit the same working directory, LLM provider, tool registry, and
        // I/O channel, which lets them operate on the same project files.
        let mut sub_ctx = ctx.clone();
        sub_ctx.nesting_depth += 1;

        // ── 3. Dispatch to the right mode ─────────────────────────────────────
        let tasks_val = args.get("tasks");
        let description_val = args.get("description").and_then(|v| v.as_str());

        match (tasks_val, description_val) {
            // ─────────────────────────────────────────────────────────────────
            // MULTI-TASK MODE: caller provided a "tasks" array
            // ─────────────────────────────────────────────────────────────────
            (Some(tasks_arr), _) => {
                let tasks = match tasks_arr.as_array() {
                    Some(arr) => arr,
                    None => {
                        return Ok(ToolResult {
                            output: "spawn_task: 'tasks' must be a JSON array, not a scalar."
                                .to_string(),
                            is_error: true,
                        });
                    }
                };

                // Empty array is technically valid — nothing to do.
                if tasks.is_empty() {
                    return Ok(ToolResult {
                        output: "spawn_task: 'tasks' array is empty — nothing to execute."
                            .to_string(),
                        is_error: false,
                    });
                }

                // Parse optional concurrency controls.
                let parallel = args
                    .get("parallel")
                    .and_then(|v| v.as_bool())
                    .unwrap_or(true);
                let max_concurrent = args
                    .get("max_concurrent")
                    .and_then(|v| v.as_u64())
                    .unwrap_or(4) as usize;

                // Build the TaskGraph from the caller-supplied task list.
                // TaskGraph validates all dependency IDs and rejects cycles.
                let mut graph = TaskGraph::new();
                for task_val in tasks {
                    let id = match task_val.get("id").and_then(|v| v.as_str()) {
                        Some(s) => s,
                        None => {
                            return Ok(ToolResult {
                                output: "spawn_task: each task must have an 'id' string field."
                                    .to_string(),
                                is_error: true,
                            });
                        }
                    };

                    let desc = match task_val.get("description").and_then(|v| v.as_str()) {
                        Some(s) => s,
                        None => {
                            return Ok(ToolResult {
                                output: format!(
                                    "spawn_task: task '{}' must have a 'description' string field.",
                                    id
                                ),
                                is_error: true,
                            });
                        }
                    };

                    // Build the node, chaining on any declared dependencies.
                    let mut node = TaskNode::new(id, desc);
                    if let Some(deps) = task_val.get("depends_on").and_then(|v| v.as_array()) {
                        for dep_val in deps {
                            if let Some(dep_id) = dep_val.as_str() {
                                node = node.with_dependency(dep_id);
                            }
                        }
                    }

                    // add_task validates the dependency IDs against what is
                    // already in the graph, so tasks must be listed in
                    // topological order (dependencies before dependents).
                    graph.add_task(node)?;
                }

                // If parallel=false, override concurrency to 1 (serial execution).
                let concurrency = if parallel { max_concurrent } else { 1 };

                // Run the executor.  It takes ownership of the graph, the shared
                // Arc handles for LLM + tools, a clone of sub_ctx for each task,
                // and the I/O channel for progress output.
                let executor = TaskExecutor::new(graph).with_max_concurrent(concurrency);

                let report = executor
                    .run(
                        Arc::clone(&ctx.llm),
                        Arc::clone(&ctx.tools),
                        sub_ctx,
                        Arc::clone(&ctx.io),
                    )
                    .await?;

                // ── Format the report ─────────────────────────────────────────
                let completed_count = report.task_results.len();
                let failed_count = report.failed.len();
                let cancelled_count = report.cancelled.len();

                let mut output = format!(
                    "spawn_task completed: {} succeeded, {} failed, {} cancelled\n\
                     Duration: {:.1}s\n\n",
                    completed_count,
                    failed_count,
                    cancelled_count,
                    report.total_duration.as_secs_f64(),
                );

                if !report.task_results.is_empty() {
                    output.push_str("## Completed tasks\n");
                    // Sort by task ID for deterministic output.
                    let mut results: Vec<_> = report.task_results.iter().collect();
                    results.sort_by_key(|(id, _)| id.as_str());
                    for (id, result) in results {
                        // Truncate very long final messages to keep the tool
                        // output at a reasonable size.
                        let preview: String = result.final_message.chars().take(200).collect();
                        let ellipsis = if result.final_message.len() > 200 {
                            ""
                        } else {
                            ""
                        };
                        output.push_str(&format!(
                            "- **{}**: {}{} (iters={}, tools={})\n",
                            id, preview, ellipsis, result.iterations, result.tool_calls_total,
                        ));
                    }
                    output.push('\n');
                }

                if !report.failed.is_empty() {
                    output.push_str(&format!(
                        "## Failed tasks\n{}\n\n",
                        report.failed.join(", ")
                    ));
                }

                if !report.cancelled.is_empty() {
                    output.push_str(&format!(
                        "## Cancelled tasks\n{}\n\n",
                        report.cancelled.join(", ")
                    ));
                }

                // Mark the result as an error when any tasks failed, so the
                // calling LLM knows it should investigate the failures.
                Ok(ToolResult {
                    output,
                    is_error: !report.failed.is_empty(),
                })
            }

            // ─────────────────────────────────────────────────────────────────
            // SINGLE-TASK MODE: caller provided only a "description" string
            // ─────────────────────────────────────────────────────────────────
            (None, Some(description)) => {
                // Create a fresh CoderAgent with default configuration.
                // AgentConfig::default() gives 25 max iterations and
                // reasonable tool-call limits — tuned for autonomous coding tasks.
                let agent = CoderAgent::new(AgentConfig::default());

                // Build the initial message history:
                //   [0] system  — agent's hard-coded system prompt
                //   [1] user    — the task description from the caller
                let mut messages = vec![
                    Message::system(agent.system_prompt().as_str()),
                    Message::user(description),
                ];

                // Run the agent to completion.  It will loop until it emits
                // [TASK_COMPLETE], hits max_iterations, or encounters an error.
                let result = agent
                    .run(
                        &mut messages,
                        ctx.tools.as_ref(), // &ToolRegistry
                        ctx.llm.as_ref(),   // &dyn LlmProvider
                        &sub_ctx,
                    )
                    .await?;

                Ok(ToolResult {
                    output: result.final_message,
                    is_error: false,
                })
            }

            // ─────────────────────────────────────────────────────────────────
            // USAGE ERROR: neither "description" nor "tasks" was provided
            // ─────────────────────────────────────────────────────────────────
            (None, None) => Ok(ToolResult {
                output: "spawn_task: provide either 'description' (single-task mode) \
                         or a 'tasks' array (multi-task mode)."
                    .to_string(),
                is_error: true,
            }),
        }
    }
}

// ─── Tests ────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::io::NullIO;
    use crate::llm::NullLlmProvider;
    use std::path::PathBuf;
    use tokio::sync::Mutex;

    /// Minimal ToolContext for unit-testing spawn_task itself.
    ///
    /// Uses `NullLlmProvider` (returns empty responses) and an empty
    /// `ToolRegistry` so agent runs inside these tests terminate quickly
    /// (they hit max_iterations rather than [TASK_COMPLETE]).
    fn make_ctx() -> ToolContext {
        let llm = Arc::new(NullLlmProvider);
        let tools = Arc::new(crate::tools::ToolRegistry::new());
        ToolContext {
            working_dir: PathBuf::from("/tmp"),
            sandbox_enabled: false,
            io: Arc::new(NullIO),
            compact_mode: false,
            lsp_client: Arc::new(Mutex::new(None)),
            mcp_client: None,
            nesting_depth: 0,
            llm,
            tools,
            permissions: vec![],
            formatters: std::collections::HashMap::new(),
        }
    }

    // ── Metadata tests ────────────────────────────────────────────────────────

    #[test]
    fn test_spawn_task_name() {
        assert_eq!(SpawnTaskTool.name(), "spawn_task");
    }

    #[test]
    fn test_spawn_task_description_non_empty() {
        assert!(!SpawnTaskTool.description().is_empty());
    }

    #[test]
    fn test_spawn_task_schema_has_required_properties() {
        let schema = SpawnTaskTool.parameters_schema();
        let props = &schema["properties"];
        // All four parameters must be documented.
        assert!(
            props["description"].is_object(),
            "schema missing 'description'"
        );
        assert!(props["tasks"].is_object(), "schema missing 'tasks'");
        assert!(props["parallel"].is_object(), "schema missing 'parallel'");
        assert!(
            props["max_concurrent"].is_object(),
            "schema missing 'max_concurrent'"
        );
    }

    // ── Nesting guard ─────────────────────────────────────────────────────────

    #[tokio::test]
    async fn test_nesting_depth_at_limit_is_refused() {
        // When nesting_depth == 3, spawn_task must refuse.
        let mut ctx = make_ctx();
        ctx.nesting_depth = 3;
        let result = SpawnTaskTool
            .execute(serde_json::json!({ "description": "test" }), &ctx)
            .await
            .unwrap();
        assert!(result.is_error, "should be an error at depth 3");
        assert!(
            result.output.contains("maximum nesting depth"),
            "error message should mention nesting depth: {}",
            result.output
        );
    }

    #[tokio::test]
    async fn test_nesting_depth_below_limit_is_allowed_to_attempt() {
        // depth 2 is below the limit → the tool should proceed (and may fail
        // for other reasons — NullLlmProvider returns empty responses —
        // but it should NOT refuse with a nesting error).
        let mut ctx = make_ctx();
        ctx.nesting_depth = 2;
        let result = SpawnTaskTool
            .execute(serde_json::json!({ "tasks": [] }), &ctx)
            .await
            .unwrap();
        // Empty tasks array → succeeds with a "nothing to execute" message.
        assert!(!result.is_error);
        assert!(result.output.contains("empty"));
    }

    // ── Usage / argument errors ────────────────────────────────────────────────

    #[tokio::test]
    async fn test_no_args_returns_usage_error() {
        let ctx = make_ctx();
        let result = SpawnTaskTool
            .execute(serde_json::json!({}), &ctx)
            .await
            .unwrap();
        assert!(result.is_error, "missing args should be an error");
        // The error message should mention both modes so the caller knows what to do.
        let msg = result.output.to_lowercase();
        assert!(
            msg.contains("description") || msg.contains("tasks"),
            "error message should mention required fields: {}",
            result.output
        );
    }

    #[tokio::test]
    async fn test_empty_tasks_array_succeeds() {
        let ctx = make_ctx();
        let result = SpawnTaskTool
            .execute(serde_json::json!({ "tasks": [] }), &ctx)
            .await
            .unwrap();
        assert!(!result.is_error);
        assert!(result.output.contains("empty"));
    }

    #[tokio::test]
    async fn test_tasks_not_array_returns_error() {
        let ctx = make_ctx();
        let result = SpawnTaskTool
            .execute(serde_json::json!({ "tasks": "not-an-array" }), &ctx)
            .await
            .unwrap();
        assert!(result.is_error, "non-array tasks should be an error");
    }

    #[tokio::test]
    async fn test_task_missing_id_returns_error() {
        let ctx = make_ctx();
        let result = SpawnTaskTool
            .execute(
                serde_json::json!({ "tasks": [{ "description": "no id here" }] }),
                &ctx,
            )
            .await
            .unwrap();
        assert!(result.is_error, "task without 'id' should be an error");
    }

    #[tokio::test]
    async fn test_task_missing_description_returns_error() {
        let ctx = make_ctx();
        let result = SpawnTaskTool
            .execute(serde_json::json!({ "tasks": [{ "id": "t1" }] }), &ctx)
            .await
            .unwrap();
        assert!(
            result.is_error,
            "task without 'description' should be an error"
        );
    }

    // ── Graph construction / validation ───────────────────────────────────────

    #[tokio::test]
    async fn test_unknown_dependency_returns_error() {
        // The graph validator should reject a dependency on a non-existent task ID.
        let ctx = make_ctx();
        let result = SpawnTaskTool
            .execute(
                serde_json::json!({
                    "tasks": [
                        {
                            "id": "t1",
                            "description": "first task",
                            "depends_on": ["does-not-exist"]
                        }
                    ]
                }),
                &ctx,
            )
            .await;
        // add_task propagates an error for unknown dep IDs.
        assert!(
            result.is_err() || result.unwrap().is_error,
            "dependency on unknown task should fail"
        );
    }

    // ── parallel / max_concurrent ─────────────────────────────────────────────

    #[tokio::test]
    async fn test_parallel_false_accepted_as_serial() {
        // When parallel=false, we clamp concurrency to 1.
        // The test just checks the executor runs without crashing on empty input.
        let ctx = make_ctx();
        let result = SpawnTaskTool
            .execute(serde_json::json!({ "tasks": [], "parallel": false }), &ctx)
            .await
            .unwrap();
        assert!(!result.is_error);
    }

    #[tokio::test]
    async fn test_max_concurrent_respected() {
        // max_concurrent=1 is equivalent to serial=true; just ensure no crash.
        let ctx = make_ctx();
        let result = SpawnTaskTool
            .execute(
                serde_json::json!({ "tasks": [], "max_concurrent": 1 }),
                &ctx,
            )
            .await
            .unwrap();
        assert!(!result.is_error);
    }
}