phi-kernel-tools 0.2.0

Kernel tools for phi-agent — file system, multi-agent, and shell tools
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
//! Multi-agent kernel tools.
//!
//! Six tools that the LLM uses to manage sub-agents:
//! spawn, send_message, followup_task, wait, list, close.
//!
//! Each tool holds an `Arc<MultiAgentRuntime>` and delegates to its methods.

use std::sync::Arc;

use agent_base::Tool;
use agent_works::multi_agent::MultiAgentRuntime;

mod close_agent;
mod followup_task;
mod list_agents;
mod send_message;
mod spawn_agent;
mod wait_agent;

pub use close_agent::{CloseAgentArgs, CloseAgentOutput, CloseAgentTool};
pub use followup_task::{FollowupTaskArgs, FollowupTaskOutput, FollowupTaskTool};
pub use list_agents::{ListAgentItem, ListAgentsArgs, ListAgentsTool};
pub use send_message::{SendMessageArgs, SendMessageOutput, SendMessageTool};
pub use spawn_agent::{SpawnAgentArgs, SpawnAgentOutput, SpawnAgentTool};
pub use wait_agent::{WaitAgentArgs, WaitAgentOutput, WaitAgentTool};

/// Create all 6 multi-agent tools, sharing the same runtime.
pub fn create_all_tools(runtime: Arc<MultiAgentRuntime>) -> Vec<Arc<dyn Tool>> {
    vec![
        Arc::new(SpawnAgentTool::new(runtime.clone())),
        Arc::new(SendMessageTool::new(runtime.clone())),
        Arc::new(FollowupTaskTool::new(runtime.clone())),
        Arc::new(WaitAgentTool::new(runtime.clone())),
        Arc::new(ListAgentsTool::new(runtime.clone())),
        Arc::new(CloseAgentTool::new(runtime)),
    ]
}

#[cfg(test)]
mod tests {
    use super::*;
    use agent_base::{Language, LlmClient, TypedTool};
    use agent_works::multi_agent::{ChildPermissionMode, MultiAgentConfig};
    use std::pin::Pin;
    use tokio_util::sync::CancellationToken;

    // ── Minimal mock LLM client ──

    struct StubClient;

    #[async_trait::async_trait]
    impl LlmClient for StubClient {
        async fn chat(
            &self,
            _messages: &[agent_base::ChatMessage],
            _tools: &[serde_json::Value],
            _reasoning: Option<&agent_base::ReasoningConfig>,
            _response_format: Option<&agent_base::ResponseFormat>,
        ) -> agent_base::AgentResult<serde_json::Value> {
            Ok(serde_json::json!({"choices": [{"message": {"content": "ok"}}]}))
        }

        async fn chat_stream(
            &self,
            _messages: &[agent_base::ChatMessage],
            _tools: &[serde_json::Value],
            _reasoning: Option<&agent_base::ReasoningConfig>,
            _response_format: Option<&agent_base::ResponseFormat>,
        ) -> agent_base::AgentResult<
            Pin<
                Box<
                    dyn futures_core::Stream<
                            Item = agent_base::AgentResult<agent_base::StreamChunk>,
                        > + Send,
                >,
            >,
        > {
            let chunks: Vec<agent_base::AgentResult<agent_base::StreamChunk>> = vec![
                Ok(agent_base::StreamChunk::Text("ok".to_string())),
                Ok(agent_base::StreamChunk::Stop {
                    finish_reason: Some("stop".to_string()),
                }),
            ];
            Ok(Box::pin(futures_util::stream::iter(chunks)))
        }

        fn capabilities(&self) -> agent_base::LlmCapabilities {
            agent_base::LlmCapabilities {
                supports_streaming: true,
                supports_tools: true,
                supports_vision: false,
                supports_thinking: false,
                max_context_tokens: None,
                max_output_tokens: None,
            }
        }
    }

    fn make_runtime() -> Arc<MultiAgentRuntime> {
        let client = agent_base::llm::adapt(Arc::new(StubClient));
        let cancel = CancellationToken::new();
        Arc::new(MultiAgentRuntime::new(
            MultiAgentConfig::enabled(),
            client,
            vec![],
            cancel,
            None,
            Language::En,
            None,
        ))
    }

    fn make_tool_ctx() -> agent_base::ToolContext {
        agent_base::ToolContext::for_test()
    }

    // ── name / description / schema ──

    #[test]
    fn test_spawn_agent_tool_metadata() {
        let t = SpawnAgentTool::new(make_runtime());
        assert_eq!(agent_base::TypedTool::name(&t), "spawn_agent");
        assert!(!agent_base::TypedTool::description(&t).is_empty());
        let schema = t.schema();
        assert_eq!(schema["type"], "object");
        assert!(
            schema["required"]
                .as_array()
                .unwrap()
                .contains(&"task_name".into())
        );
        assert!(
            schema["required"]
                .as_array()
                .unwrap()
                .contains(&"message".into())
        );
    }

    #[test]
    fn test_send_message_tool_metadata() {
        let t = SendMessageTool::new(make_runtime());
        assert_eq!(agent_base::TypedTool::name(&t), "send_message");
        assert!(!agent_base::TypedTool::description(&t).is_empty());
        let schema = t.schema();
        assert_eq!(schema["type"], "object");
        assert!(
            schema["required"]
                .as_array()
                .unwrap()
                .contains(&"agent_path".into())
        );
    }

    #[test]
    fn test_followup_task_tool_metadata() {
        let t = FollowupTaskTool::new(make_runtime());
        assert_eq!(agent_base::TypedTool::name(&t), "followup_task");
        assert!(!agent_base::TypedTool::description(&t).is_empty());
        let schema = t.schema();
        assert!(
            schema["required"]
                .as_array()
                .unwrap()
                .contains(&"agent_path".into())
        );
    }

    #[test]
    fn test_wait_agent_tool_metadata() {
        let t = WaitAgentTool::new(make_runtime());
        assert_eq!(agent_base::TypedTool::name(&t), "wait_agent");
        assert!(!agent_base::TypedTool::description(&t).is_empty());
        let schema = t.schema();
        assert_eq!(schema["type"], "object");
        // All fields are optional, so schemars omits the `required` key.
        assert!(schema.get("required").is_none());
    }

    #[test]
    fn test_list_agents_tool_metadata() {
        let t = ListAgentsTool::new(make_runtime());
        assert_eq!(agent_base::TypedTool::name(&t), "list_agents");
        assert!(!agent_base::TypedTool::description(&t).is_empty());
        let schema = t.schema();
        assert_eq!(schema["type"], "object");
    }

    #[test]
    fn test_close_agent_tool_metadata() {
        let t = CloseAgentTool::new(make_runtime());
        assert_eq!(agent_base::TypedTool::name(&t), "close_agent");
        assert!(!agent_base::TypedTool::description(&t).is_empty());
        let schema = t.schema();
        assert!(
            schema["required"]
                .as_array()
                .unwrap()
                .contains(&"agent_path".into())
        );
    }

    // ── format_output ──

    #[test]
    fn test_spawn_agent_format_output() {
        let t = SpawnAgentTool::new(make_runtime());
        let out = t.format_output(SpawnAgentOutput {
            agent_path: "root/w1".into(),
            message: "ok".into(),
        });
        let text = agent_base::tool::content_text(&[out]);
        let v: serde_json::Value = serde_json::from_str(&text).unwrap();
        assert_eq!(v["agent_path"], "root/w1");
        assert_eq!(v["message"], "ok");
    }

    #[test]
    fn test_send_message_format_output() {
        let t = SendMessageTool::new(make_runtime());
        let out = t.format_output(SendMessageOutput { delivered: true });
        let text = agent_base::tool::content_text(&[out]);
        let v: serde_json::Value = serde_json::from_str(&text).unwrap();
        assert_eq!(v["delivered"], true);
    }

    #[test]
    fn test_followup_task_format_output() {
        let t = FollowupTaskTool::new(make_runtime());
        let out = t.format_output(FollowupTaskOutput {
            accepted: true,
            agent_path: "root/w1".into(),
        });
        let text = agent_base::tool::content_text(&[out]);
        let v: serde_json::Value = serde_json::from_str(&text).unwrap();
        assert_eq!(v["accepted"], true);
    }

    #[test]
    fn test_wait_agent_format_output() {
        let t = WaitAgentTool::new(make_runtime());
        let out = t.format_output(WaitAgentOutput {
            status: "timeout".into(),
            result: None,
            agent_path: None,
            has_more: false,
            denied_tools: vec![],
        });
        let text = agent_base::tool::content_text(&[out]);
        let v: serde_json::Value = serde_json::from_str(&text).unwrap();
        assert_eq!(v["status"], "timeout");
        assert_eq!(v["has_more"], false);
    }

    #[test]
    fn test_close_agent_format_output() {
        let t = CloseAgentTool::new(make_runtime());
        let out = t.format_output(CloseAgentOutput {
            closed: true,
            previous_status: "idle".into(),
            message: "done".into(),
        });
        let text = agent_base::tool::content_text(&[out]);
        let v: serde_json::Value = serde_json::from_str(&text).unwrap();
        assert_eq!(v["closed"], true);
    }

    // ── call_typed for tools that don't need spawn ──

    #[tokio::test]
    async fn test_list_agents_call_empty() {
        let rt = make_runtime();
        let t = ListAgentsTool::new(rt);
        let ctx = make_tool_ctx();
        let result = t.call_typed(ListAgentsArgs {}, &ctx).await.unwrap();
        assert!(result.is_empty());
    }

    #[tokio::test]
    async fn test_close_agent_nonexistent() {
        let rt = make_runtime();
        let t = CloseAgentTool::new(rt);
        let ctx = make_tool_ctx();
        let result = t
            .call_typed(
                CloseAgentArgs {
                    agent_path: "root/ghost".into(),
                },
                &ctx,
            )
            .await
            .unwrap();
        assert!(!result.closed);
        assert_eq!(result.previous_status, "unknown");
    }

    #[tokio::test]
    async fn test_send_message_nonexistent() {
        let rt = make_runtime();
        let t = SendMessageTool::new(rt);
        let ctx = make_tool_ctx();
        let result = t
            .call_typed(
                SendMessageArgs {
                    agent_path: "root/ghost".into(),
                    message: "hi".into(),
                },
                &ctx,
            )
            .await
            .unwrap();
        assert!(!result.delivered);
    }

    #[tokio::test]
    async fn test_followup_task_nonexistent() {
        let rt = make_runtime();
        let t = FollowupTaskTool::new(rt);
        let ctx = make_tool_ctx();
        let result = t
            .call_typed(
                FollowupTaskArgs {
                    agent_path: "root/ghost".into(),
                    task: "do".into(),
                    interrupt: true,
                },
                &ctx,
            )
            .await
            .unwrap();
        assert!(!result.accepted);
    }

    // ── send_message + followup_task + wait result round-trip ──

    #[tokio::test]
    async fn test_send_message_and_followup_task_roundtrip() {
        let rt = make_runtime();

        // Spawn a child first
        let path = rt
            .spawn_child("worker", "you are a worker".into(), 1, 0, false, vec![])
            .await
            .unwrap();

        // Send a message (no execution trigger)
        let t = SendMessageTool::new(rt.clone());
        let ctx = make_tool_ctx();
        let result = t
            .call_typed(
                SendMessageArgs {
                    agent_path: path.clone(),
                    message: "context info".into(),
                },
                &ctx,
            )
            .await
            .unwrap();
        assert!(result.delivered);

        // Send a task (triggers execution, drains pending messages)
        let t2 = FollowupTaskTool::new(rt.clone());
        let result2 = t2
            .call_typed(
                FollowupTaskArgs {
                    agent_path: path.clone(),
                    task: "do work".into(),
                    interrupt: true,
                },
                &ctx,
            )
            .await
            .unwrap();
        assert!(result2.accepted);

        // Wait for result
        let t3 = WaitAgentTool::new(rt.clone());
        let result3 = t3
            .call_typed(
                WaitAgentArgs {
                    agent_path: Some(path.clone()),
                    timeout_ms: 5000,
                },
                &ctx,
            )
            .await
            .unwrap();
        assert_eq!(result3.status, "ok");
        assert!(result3.result.is_some());

        // Close
        let t4 = CloseAgentTool::new(rt.clone());
        let result4 = t4
            .call_typed(
                CloseAgentArgs {
                    agent_path: path.clone(),
                },
                &ctx,
            )
            .await
            .unwrap();
        assert!(result4.closed);
    }

    // ── spawn_agent call_typed ──

    #[tokio::test]
    async fn test_spawn_agent_call() {
        let rt = make_runtime();
        let t = SpawnAgentTool::new(rt.clone());
        let ctx = make_tool_ctx();

        let result = t
            .call_typed(
                SpawnAgentArgs {
                    task_name: "helper".into(),
                    message: "do something".into(),
                    agent_type: None,
                    system_prompt: Some("you are a helper".into()),
                    model: None,
                    reasoning_effort: None,
                    fork_history: None,
                    depth: 1,
                    full_permission: false,
                },
                &ctx,
            )
            .await
            .unwrap();

        assert_eq!(result.agent_path, "root/helper");
        assert!(result.message.contains("spawned"));

        // Verify the agent shows up in list
        let t2 = ListAgentsTool::new(rt);
        let list = t2.call_typed(ListAgentsArgs {}, &ctx).await.unwrap();
        assert_eq!(list.len(), 1);
        assert_eq!(list[0].agent_path, "root/helper");
    }

    // ── spawn with auto-generated system prompt from agent_type ──

    #[tokio::test]
    async fn test_spawn_agent_with_agent_type() {
        let rt = make_runtime();
        let t = SpawnAgentTool::new(rt);
        let ctx = make_tool_ctx();

        let result = t
            .call_typed(
                SpawnAgentArgs {
                    task_name: "searcher".into(),
                    message: "search for info".into(),
                    agent_type: Some("researcher".into()),
                    system_prompt: None,
                    model: None,
                    reasoning_effort: None,
                    fork_history: None,
                    depth: 1,
                    full_permission: false,
                },
                &ctx,
            )
            .await
            .unwrap();

        assert_eq!(result.agent_path, "root/searcher");
    }

    // ── spawn limit exceeded ──

    #[tokio::test]
    async fn test_spawn_agent_limit_exceeded() {
        let client = agent_base::llm::adapt(Arc::new(StubClient));
        let cancel = CancellationToken::new();
        let config = MultiAgentConfig {
            enabled: true,
            max_sub_agents: 1,
            max_agent_depth: 1,
            child_permission_mode: ChildPermissionMode::Full,
        };
        let rt = Arc::new(MultiAgentRuntime::new(
            config,
            client,
            vec![],
            cancel,
            None,
            Language::En,
            None,
        ));

        let t = SpawnAgentTool::new(rt.clone());
        let ctx = make_tool_ctx();

        // First spawn succeeds
        let r1 = t
            .call_typed(
                SpawnAgentArgs {
                    task_name: "first".into(),
                    message: "task".into(),
                    agent_type: None,
                    system_prompt: None,
                    model: None,
                    reasoning_effort: None,
                    fork_history: None,
                    depth: 1,
                    full_permission: false,
                },
                &ctx,
            )
            .await
            .unwrap();
        assert_eq!(r1.agent_path, "root/first");

        // Second spawn fails
        let r2 = t
            .call_typed(
                SpawnAgentArgs {
                    task_name: "second".into(),
                    message: "task".into(),
                    agent_type: None,
                    system_prompt: None,
                    model: None,
                    reasoning_effort: None,
                    fork_history: None,
                    depth: 1,
                    full_permission: false,
                },
                &ctx,
            )
            .await
            .unwrap();

        assert!(r2.agent_path.is_empty());
        assert!(r2.message.contains("Failed"));
    }

    // ── create_all_tools ──

    #[test]
    fn test_create_all_tools_returns_six() {
        let rt = make_runtime();
        let tools = create_all_tools(rt);
        assert_eq!(tools.len(), 6);

        let names: Vec<&str> = tools.iter().map(|t| t.name()).collect();
        assert!(names.contains(&"spawn_agent"));
        assert!(names.contains(&"send_message"));
        assert!(names.contains(&"followup_task"));
        assert!(names.contains(&"wait_agent"));
        assert!(names.contains(&"list_agents"));
        assert!(names.contains(&"close_agent"));
    }
}