agents-runtime 0.0.30

Async runtime orchestration for Rust deep agents.
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
mod tests {
    use super::*;
    use agents_core::agent::{PlannerDecision, PlannerHandle, ToolHandle, ToolResponse};
    use async_trait::async_trait;
    use serde_json::json;

    async fn send_user(agent: &DeepAgent, text: &str) -> AgentMessage {
        agent
            .handle_message(
                text,
                Arc::new(AgentStateSnapshot::default()),
            )
            .await
            .unwrap()
    }

    struct EchoPlanner;

    #[async_trait]
    impl PlannerHandle for EchoPlanner {
        async fn plan(
            &self,
            context: PlannerContext,
            _state: Arc<AgentStateSnapshot>,
        ) -> anyhow::Result<PlannerDecision> {
            let last_user = context
                .history
                .iter()
                .rev()
                .find(|msg| matches!(msg.role, MessageRole::User))
                .cloned()
                .unwrap_or(AgentMessage {
                    role: MessageRole::User,
                    content: MessageContent::Text("".into()),
                    metadata: None,
                });

            Ok(PlannerDecision {
                next_action: PlannerAction::Respond {
                    message: AgentMessage {
                        role: MessageRole::Agent,
                        content: last_user.content,
                        metadata: None,
                    },
                },
            })
        }
    }

    #[tokio::test]
    async fn deep_agent_echoes() {
        let planner = Arc::new(EchoPlanner);
        let agent = create_deep_agent_from_config(DeepAgentConfig::new("Be helpful", planner));

        let response = send_user(&agent, "hello").await;

        match response.content {
            MessageContent::Text(text) => assert_eq!(text, "hello"),
            other => panic!("expected text, got {other:?}"),
        }
    }

    struct LsPlanner;

    #[async_trait]
    impl PlannerHandle for LsPlanner {
        async fn plan(
            &self,
            _context: PlannerContext,
            _state: Arc<AgentStateSnapshot>,
        ) -> anyhow::Result<PlannerDecision> {
            Ok(PlannerDecision {
                next_action: PlannerAction::CallTool {
                    tool_name: "ls".into(),
                    payload: json!({}),
                },
            })
        }
    }

    #[tokio::test]
    async fn builtin_tools_can_be_filtered() {
        let planner = Arc::new(LsPlanner);
        // Allow only write_todos; ls should be filtered out
        let agent = create_deep_agent_from_config(
            DeepAgentConfig::new("Assist", planner).with_builtin_tools(["write_todos"]),
        );

        let response = send_user(&agent, "list files").await;

        if let MessageContent::Text(text) = response.content {
            assert!(text.contains("Tool 'ls' not available"));
        } else {
            panic!("expected text response");
        }
    }

    struct DelegatePlanner;

    #[async_trait]
    impl PlannerHandle for DelegatePlanner {
        async fn plan(
            &self,
            _context: PlannerContext,
            _state: Arc<AgentStateSnapshot>,
        ) -> anyhow::Result<PlannerDecision> {
            Ok(PlannerDecision {
                next_action: PlannerAction::CallTool {
                    tool_name: "task".into(),
                    payload: json!({
                        "description": "Handle delegation",
                        "subagent_type": "stub-agent"
                    }),
                },
            })
        }
    }

    struct StubSubAgent;

    #[async_trait]
    impl AgentHandle for StubSubAgent {
        async fn describe(&self) -> AgentDescriptor {
            AgentDescriptor {
                name: "stub-subagent".into(),
                version: "0.0.1".into(),
                description: None,
            }
        }

        async fn handle_message(
            &self,
            _input: AgentMessage,
            _state: Arc<AgentStateSnapshot>,
        ) -> anyhow::Result<AgentMessage> {
            Ok(AgentMessage {
                role: MessageRole::Agent,
                content: MessageContent::Text("delegated-result".into()),
                metadata: None,
            })
        }
    }

    #[tokio::test]
    async fn deep_agent_delegates_to_subagent() {
        let planner = Arc::new(DelegatePlanner);
        let config = DeepAgentConfig::new("Use tools", planner).with_subagent(
            SubAgentDescriptor {
                name: "stub-agent".into(),
                description: "Stub Agent".into(),
            },
            Arc::new(StubSubAgent),
        );
        let agent = create_deep_agent_from_config(config);

        let response = send_user(&agent, "delegate").await;

        assert!(matches!(response.role, MessageRole::Tool));
        match response.content {
            MessageContent::Text(text) => assert_eq!(text, "delegated-result"),
            other => panic!("expected text, got {other:?}"),
        }
    }

    struct AlwaysTextPlanner(&'static str);

    #[async_trait]
    impl PlannerHandle for AlwaysTextPlanner {
        async fn plan(
            &self,
            _context: PlannerContext,
            _state: Arc<AgentStateSnapshot>,
        ) -> anyhow::Result<PlannerDecision> {
            Ok(PlannerDecision {
                next_action: PlannerAction::Respond {
                    message: AgentMessage {
                        role: MessageRole::Agent,
                        content: MessageContent::Text(self.0.to_string()),
                        metadata: None,
                    },
                },
            })
        }
    }

    #[allow(dead_code)]
    struct GpDelegatePlanner;

    #[async_trait]
    impl PlannerHandle for GpDelegatePlanner {
        async fn plan(
            &self,
            _context: PlannerContext,
            _state: Arc<AgentStateSnapshot>,
        ) -> anyhow::Result<PlannerDecision> {
            Ok(PlannerDecision {
                next_action: PlannerAction::CallTool {
                    tool_name: "task".into(),
                    payload: json!({
                        "description": "Ask GP agent",
                        "subagent_type": "general-purpose"
                    }),
                },
            })
        }
    }

    #[tokio::test]
    async fn default_general_purpose_subagent_is_available() {
        // Main agent delegates to general-purpose; GP uses AlwaysTextPlanner to respond
        // let main_planner = Arc::new(GpDelegatePlanner);
        let gp_planner = Arc::new(AlwaysTextPlanner("gp-ok"));
        // Build agent but override planner for the GP by setting it as the main planner
        // and ensuring GP inherits it
        let agent = create_deep_agent_from_config(DeepAgentConfig::new("Assist", gp_planner));

        let response = send_user(&agent, "delegate to gp").await;

        match response.content {
            MessageContent::Text(text) => assert_eq!(text, "gp-ok"),
            other => panic!("expected text, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn subagent_convenience_builder_registers_and_delegates() {
        let main_planner = Arc::new(DelegatePlanner);
        let custom_planner = Arc::new(AlwaysTextPlanner("custom-ok"));
        let agent = create_deep_agent_from_config(
            DeepAgentConfig::new("Assist", main_planner).with_subagent_config(SubAgentConfig {
                name: "stub-agent".into(),
                description: "Stub Agent".into(),
                instructions: "Custom".into(),
                tools: None,
                planner: Some(custom_planner),
            }),
        );

        let response = send_user(&agent, "delegate").await;

        match response.content {
            MessageContent::Text(text) => assert_eq!(text, "custom-ok"),
            other => panic!("expected text, got {other:?}"),
        }
    }

    struct AlwaysRespondPlanner;

    #[async_trait]
    impl PlannerHandle for AlwaysRespondPlanner {
        async fn plan(
            &self,
            context: PlannerContext,
            _state: Arc<AgentStateSnapshot>,
        ) -> anyhow::Result<PlannerDecision> {
            Ok(PlannerDecision {
                next_action: PlannerAction::Respond {
                    message: AgentMessage {
                        role: MessageRole::Agent,
                        content: MessageContent::Text(
                            context
                                .history
                                .last()
                                .and_then(|m| m.content.as_text())
                                .unwrap_or("")
                                .to_string(),
                        ),
                        metadata: None,
                    },
                },
            })
        }
    }

    #[tokio::test]
    async fn deep_agent_applies_summarization() {
        let planner = Arc::new(AlwaysRespondPlanner);
        let agent = create_deep_agent_from_config(
            DeepAgentConfig::new("Assist", planner).with_summarization(SummarizationConfig {
                messages_to_keep: 1,
                summary_note: "Summary".into(),
            }),
        );

        send_user(&agent, "first").await;
        let response = send_user(&agent, "second").await;

        if let MessageContent::Text(text) = response.content {
            assert_eq!(text, "second");
        }
    }

    struct SensitiveTool;

    #[async_trait]
    impl ToolHandle for SensitiveTool {
        fn name(&self) -> &str {
            "sensitive"
        }

        async fn invoke(&self, _invocation: ToolInvocation) -> anyhow::Result<ToolResponse> {
            Ok(ToolResponse::Message(AgentMessage {
                role: MessageRole::Tool,
                content: MessageContent::Text("tool-output".into()),
                metadata: None,
            }))
        }
    }

    struct ToolPlanner;

    #[async_trait]
    impl PlannerHandle for ToolPlanner {
        async fn plan(
            &self,
            _context: PlannerContext,
            _state: Arc<AgentStateSnapshot>,
        ) -> anyhow::Result<PlannerDecision> {
            Ok(PlannerDecision {
                next_action: PlannerAction::CallTool {
                    tool_name: "sensitive".into(),
                    payload: json!({}),
                },
            })
        }
    }

    #[tokio::test]
    async fn deep_agent_requires_hitl() {
        let planner = Arc::new(ToolPlanner);
        let config = DeepAgentConfig::new("Assist", planner)
            .with_tool(Arc::new(SensitiveTool))
            .with_tool_interrupt(
                "sensitive",
                HitlPolicy {
                    allow_auto: false,
                    note: Some("Needs approval".into()),
                },
            );
        let agent = create_deep_agent_from_config(config);

        let response = send_user(&agent, "call tool").await;

        match response.content {
            MessageContent::Text(text) => assert!(text.contains("HITL_REQUIRED")),
            other => panic!("expected text, got {other:?}"),
        }
        assert!(matches!(
            agent.current_interrupt(),
            Some(AgentInterrupt::HumanInLoop(_))
        ));
    }

    struct NoopTool;

    #[async_trait]
    impl ToolHandle for NoopTool {
        fn name(&self) -> &str {
            "noop"
        }
        async fn invoke(&self, _invocation: ToolInvocation) -> anyhow::Result<ToolResponse> {
            Ok(ToolResponse::Message(AgentMessage {
                role: MessageRole::Tool,
                content: MessageContent::Text("edited-ok".into()),
                metadata: None,
            }))
        }
    }

    #[tokio::test]
    async fn hitl_edit_changes_tool_and_args() {
        // Planner calls 'sensitive' which requires approval; we then edit to call 'noop'.
        let planner = Arc::new(ToolPlanner);
        let config = DeepAgentConfig::new("Assist", planner)
            .with_tool(Arc::new(SensitiveTool))
            .with_tool(Arc::new(NoopTool))
            .with_tool_interrupt(
                "sensitive",
                HitlPolicy {
                    allow_auto: false,
                    note: Some("Needs approval".into()),
                },
            );
        let agent = create_deep_agent_from_config(config);

        let response = send_user(&agent, "call tool").await;
        match response.content {
            MessageContent::Text(text) => assert!(text.contains("HITL_REQUIRED")),
            other => panic!("expected text, got {other:?}"),
        }
        assert!(matches!(
            agent.current_interrupt(),
            Some(AgentInterrupt::HumanInLoop(_))
        ));

        let edited = agent
            .resume_hitl(HitlAction::Edit {
                action: "noop".into(),
                args: json!({}),
            })
            .await
            .unwrap();
        match edited.content {
            MessageContent::Text(text) => assert_eq!(text, "edited-ok"),
            other => panic!("expected text, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn agent_builder_supports_prompt_caching() {
        let planner = Arc::new(EchoPlanner);
        let agent = ConfigurableAgentBuilder::new("test prompt caching")
            .with_planner(planner)
            .with_prompt_caching(true)
            .build()
            .unwrap();

        let response = send_user(&agent, "hello").await;
        // Echo planner just returns the input, so this verifies the agent works with caching enabled
        assert_eq!(response.content.as_text().unwrap(), "hello");
    }

    #[tokio::test]
    async fn agent_builder_supports_checkpointer() {
        use agents_core::persistence::InMemoryCheckpointer;

        let planner = Arc::new(EchoPlanner);
        let checkpointer = Arc::new(InMemoryCheckpointer::new());
        let agent = ConfigurableAgentBuilder::new("test checkpointer")
            .with_planner(planner)
            .with_checkpointer(checkpointer)
            .build()
            .unwrap();

        // Test that we can save and load state
        let thread_id = "test-thread".to_string();
        agent.save_state(&thread_id).await.unwrap();

        // Load should return true (state was found and loaded)
        let loaded = agent.load_state(&thread_id).await.unwrap();
        assert!(loaded);

        // Test listing threads
        let threads = agent.list_threads().await.unwrap();
        assert!(threads.contains(&thread_id));

        // Clean up
        agent.delete_thread(&thread_id).await.unwrap();
        let threads_after = agent.list_threads().await.unwrap();
        assert!(!threads_after.contains(&thread_id));
    }

    #[tokio::test]
    async fn agent_builder_with_model_mirrors_python_api() {
        use agents_core::llm::{LlmRequest, LlmResponse};
        use async_trait::async_trait;

        // Mock model that mirrors Python API usage
        struct MockLanguageModel;

        #[async_trait]
        impl LanguageModel for MockLanguageModel {
            async fn generate(&self, _request: LlmRequest) -> anyhow::Result<LlmResponse> {
                Ok(LlmResponse {
                    message: AgentMessage {
                        role: MessageRole::Agent,
                        content: MessageContent::Text("model response".into()),
                        metadata: None,
                    },
                })
            }
        }

        // This mirrors Python: create_deep_agent(model=some_model, ...)
        let model = Arc::new(MockLanguageModel);
        let agent = ConfigurableAgentBuilder::new("test model")
            .with_model(model) // ← This mirrors Python's model= parameter
            .build()
            .unwrap();

        let response = send_user(&agent, "hello").await;
        assert_eq!(response.content.as_text().unwrap(), "model response");
    }

    #[test]
    fn test_get_default_model_requires_api_key() {
        // Save current state
        let original_key = std::env::var("ANTHROPIC_API_KEY").ok();

        // Remove the key to test error case
        std::env::remove_var("ANTHROPIC_API_KEY");
        let result = get_default_model();
        assert!(result.is_err());
        let err_msg = format!("{}", result.err().unwrap());
        assert!(err_msg.contains("ANTHROPIC_API_KEY"));

        // Restore original state
        if let Some(key) = original_key {
            std::env::set_var("ANTHROPIC_API_KEY", key);
        }
    }

    #[test]
    fn test_get_default_model_with_api_key() {
        // Save current state
        let original_key = std::env::var("ANTHROPIC_API_KEY").ok();

        // Set test key
        std::env::set_var("ANTHROPIC_API_KEY", "test-key");
        let result = get_default_model();
        assert!(result.is_ok());

        // Restore original state
        match original_key {
            Some(key) => std::env::set_var("ANTHROPIC_API_KEY", key),
            None => std::env::remove_var("ANTHROPIC_API_KEY"),
        }
    }

    #[tokio::test]
    async fn test_configurable_agent_with_default_model() {
        // Save current state
        let original_key = std::env::var("ANTHROPIC_API_KEY").ok();

        // Test using get_default_model with the builder pattern
        std::env::set_var("ANTHROPIC_API_KEY", "test-key");

        let model = get_default_model().unwrap();
        let agent = ConfigurableAgentBuilder::new("test instructions")
            .with_model(model) // Use the default model
            .build()
            .unwrap();

        // Agent should be created successfully
        let descriptor = agent.describe().await;
        assert_eq!(descriptor.name, "deep-agent");

        // Restore original state
        match original_key {
            Some(key) => std::env::set_var("ANTHROPIC_API_KEY", key),
            None => std::env::remove_var("ANTHROPIC_API_KEY"),
        }
    }
}