agy-bridge 0.1.0

Rust bridge for the Google Antigravity SDK (Python) via PyO3
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
//! Mock runtime for agent unit testing.

use std::{
    sync::{
        Arc,
        atomic::{AtomicBool, Ordering},
    },
    time::Duration,
};

use super::{AgentHandle, AgentId, Runtime};
use crate::{
    config::AgentConfig,
    content::Content,
    error::Error,
    streaming::{self, ChatResponseHandle},
    types::{ConversationMessage, MessageRole, UsageMetadata},
};

/// A mock runtime that simulates tool calls on the first `chat()` call
/// and returns text on subsequent calls, enabling tests of the `chat_text()`
/// agentic loop without a live Python runtime.
pub struct ToolAwareMockRuntime {
    /// Counts how many times `chat()` has been called per agent.
    /// First call → tool call; subsequent calls → text response.
    chat_count: std::sync::Mutex<std::collections::HashMap<AgentId, u32>>,
    /// If true, `create_agent` will fail.
    fail_create: AtomicBool,
    /// If true, first `chat` will return `QuotaExceeded` (then resets).
    fail_quota: AtomicBool,
    /// Tracks whether `try_shutdown_agent` was called (from Drop).
    pub(crate) try_shutdown_called: AtomicBool,
    /// Per-runtime quota registry.
    quota_registry: crate::quota::QuotaRegistry,
}

impl ToolAwareMockRuntime {
    pub(crate) fn new() -> Self {
        Self {
            chat_count: std::sync::Mutex::new(std::collections::HashMap::new()),
            fail_create: AtomicBool::new(false),
            fail_quota: AtomicBool::new(false),
            try_shutdown_called: AtomicBool::new(false),
            quota_registry: crate::quota::QuotaRegistry::new(),
        }
    }

    pub(crate) fn with_create_failure() -> Self {
        let rt = Self::new();
        rt.fail_create.store(true, Ordering::SeqCst);
        rt
    }
}

impl Runtime for ToolAwareMockRuntime {
    async fn create_agent(
        &self,
        _agent_id: AgentId,
        _config: AgentConfig,
        _bridge_state: Arc<crate::runtime::AgentBridgeState>,
    ) -> Result<(), Error> {
        if self.fail_create.load(Ordering::SeqCst) {
            return Err(Error::BackendError {
                message: "invalid config: missing system instructions".to_owned(),
            });
        }
        Ok(())
    }

    /// # Panics
    ///
    /// Panics if the internal `chat_count` mutex is poisoned. This is
    /// acceptable because `ToolAwareMockRuntime` is only compiled under
    /// `#[cfg(test)]`.
    async fn chat(
        &self,
        agent_id: AgentId,
        _content: &Content,
    ) -> Result<ChatResponseHandle, Error> {
        if self.fail_quota.load(Ordering::SeqCst) {
            self.fail_quota.store(false, Ordering::SeqCst);
            return Err(Error::QuotaExceeded {
                retry_after: Duration::from_millis(10),
            });
        }

        let call_num = {
            let mut counts = self.chat_count.lock().unwrap();
            let entry = counts.entry(agent_id).or_insert(0);
            *entry += 1;
            *entry
        };

        let (writer, handle) = streaming::channel();
        if call_num == 1 {
            tokio::spawn(async move {
                if let Err(e) = writer
                    .tool_call_tx
                    .send(crate::streaming::ToolCallEvent {
                        name: "add_numbers".to_owned(),
                        args: serde_json::json!({"x": 2, "y": 3}),
                        id: Some("call_1".to_owned()),
                        canonical_path: None,
                    })
                    .await
                {
                    tracing::error!("Mock tool_call send failed: {e}");
                }
                if let Err(e) = writer.text_tx.send("Mock text response".to_owned()).await {
                    tracing::error!("Mock text send failed: {e}");
                }
            });
        } else {
            tokio::spawn(async move {
                if let Err(e) = writer
                    .text_tx
                    .send("Tool result received, final answer: 5".to_owned())
                    .await
                {
                    tracing::error!("Mock text send failed: {e}");
                }
            });
        }
        Ok(handle)
    }

    async fn shutdown_agent(&self, _agent_id: AgentId) -> Result<(), Error> {
        Ok(())
    }

    async fn cancel(&self, _agent_id: AgentId) -> Result<(), Error> {
        Ok(())
    }

    async fn wait_for_idle(&self, _agent_id: AgentId) -> Result<(), Error> {
        Ok(())
    }

    async fn send(&self, _agent_id: AgentId, _content: &Content) -> Result<(), Error> {
        Ok(())
    }

    async fn signal_idle(&self, _agent_id: AgentId) -> Result<(), Error> {
        Ok(())
    }

    async fn wait_for_wakeup(&self, _agent_id: AgentId, _timeout: Duration) -> Result<bool, Error> {
        // Mock always times out (returns false).
        Ok(false)
    }

    async fn wait_for_quota(&self) {}

    async fn record_quota_hit(&self, _retry_after: Duration) {}

    fn quota_registry(&self) -> &crate::quota::QuotaRegistry {
        &self.quota_registry
    }

    async fn history(&self, _agent_id: AgentId) -> Result<Vec<ConversationMessage>, Error> {
        Ok(vec![
            ConversationMessage {
                role: MessageRole::User,
                content: "Hello".to_string(),
            },
            ConversationMessage {
                role: MessageRole::Model,
                content: "Hi there!".to_string(),
            },
        ])
    }

    async fn turn_count(&self, agent_id: AgentId) -> Result<u32, Error> {
        let counts = self.chat_count.lock().unwrap();
        Ok(*counts.get(&agent_id).unwrap_or(&0))
    }

    async fn total_usage(&self, _agent_id: AgentId) -> Result<UsageMetadata, Error> {
        Ok(UsageMetadata {
            prompt_token_count: Some(500),
            cached_content_token_count: None,
            candidates_token_count: Some(200),
            thoughts_token_count: Some(100),
            total_token_count: Some(800),
        })
    }

    async fn last_turn_usage(&self, _agent_id: AgentId) -> Result<UsageMetadata, Error> {
        Ok(UsageMetadata {
            prompt_token_count: Some(100),
            cached_content_token_count: None,
            candidates_token_count: Some(50),
            thoughts_token_count: Some(20),
            total_token_count: Some(170),
        })
    }

    async fn clear_history(&self, agent_id: AgentId) -> Result<(), Error> {
        {
            let mut counts = self.chat_count.lock().unwrap();
            counts.insert(agent_id, 0);
        }
        Ok(())
    }

    async fn last_response(&self, _agent_id: AgentId) -> Result<Option<String>, Error> {
        Ok(Some("Hi there!".to_string()))
    }

    async fn compaction_indices(&self, _agent_id: AgentId) -> Result<Vec<u32>, Error> {
        Ok(vec![3, 7])
    }

    async fn delete(&self, _agent_id: AgentId) -> Result<(), Error> {
        Ok(())
    }

    async fn disconnect(&self, _agent_id: AgentId) -> Result<(), Error> {
        Ok(())
    }

    async fn is_idle(&self, _agent_id: AgentId) -> Result<bool, Error> {
        Ok(true)
    }

    fn try_shutdown_agent(&self, _agent_id: AgentId) {
        self.try_shutdown_called.store(true, Ordering::SeqCst);
    }
}

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

    fn test_config() -> AgentConfig {
        AgentConfig::default()
    }

    // ── Agent lifecycle tests ────────────────────────────────────────

    #[tokio::test]
    async fn create_chat_shutdown_lifecycle() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(rt, test_config(), None, None, None)
            .await
            .expect("create should succeed");

        assert!(agent.is_started());
        assert!(agent.id() > 0);

        {
            let mut response = agent.chat("Hello").await.expect("chat should succeed");
            if let Some(mut rx) = response.take_tool_call_stream() {
                let call = rx.recv().await.expect("should get tool call");
                assert_eq!(call.name, "add_numbers");
            }
        }

        agent.shutdown().await.expect("shutdown should succeed");
        assert!(!agent.is_started());
    }

    #[tokio::test]
    async fn create_with_invalid_config_returns_error() {
        let rt = Arc::new(ToolAwareMockRuntime::with_create_failure());
        let result = AgentHandle::new(rt, test_config(), None, None, None).await;

        match result {
            Err(Error::BackendError { message }) => {
                assert!(message.contains("invalid config"));
            }
            Err(other) => panic!("Expected BackendError, got: {other:?}"),
            Ok(_) => panic!("Expected error, got Ok"),
        }
    }

    #[tokio::test]
    async fn chat_with_quota_backoff_retries() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        rt.fail_quota.store(true, Ordering::SeqCst);

        let agent = AgentHandle::new(Arc::clone(&rt), test_config(), None, None, None)
            .await
            .expect("create should succeed");

        {
            let mut response = agent
                .chat("Hello")
                .await
                .expect("should succeed after retry");
            if let Some(mut rx) = response.take_tool_call_stream() {
                let _call = rx.recv().await;
            }
        }

        agent.shutdown().await.expect("shutdown should succeed");
    }

    #[tokio::test]
    async fn conversation_id_tracking() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(rt, test_config(), None, None, None)
            .await
            .expect("create should succeed");

        assert!(agent.conversation_id().is_none());

        agent.set_conversation_id("conv_abc123".to_owned());
        assert_eq!(agent.conversation_id().as_deref(), Some("conv_abc123"));

        agent.shutdown().await.expect("shutdown should succeed");
    }

    #[tokio::test]
    async fn double_shutdown_is_idempotent() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(rt, test_config(), None, None, None)
            .await
            .expect("create should succeed");

        agent
            .shutdown()
            .await
            .expect("first shutdown should succeed");
        assert!(!agent.is_started());

        agent
            .shutdown()
            .await
            .expect("second shutdown should succeed");
    }

    #[tokio::test]
    async fn drop_without_shutdown_calls_try_shutdown() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(Arc::clone(&rt), test_config(), None, None, None)
            .await
            .expect("create should succeed");

        assert!(!rt.try_shutdown_called.load(Ordering::SeqCst));
        drop(agent);
        assert!(
            rt.try_shutdown_called.load(Ordering::SeqCst),
            "Drop should call try_shutdown_agent"
        );
    }

    #[tokio::test]
    async fn drop_after_shutdown_does_not_call_try_shutdown() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(Arc::clone(&rt), test_config(), None, None, None)
            .await
            .expect("create should succeed");

        agent.shutdown().await.expect("shutdown");
        assert!(!rt.try_shutdown_called.load(Ordering::SeqCst));

        drop(agent);
        assert!(
            !rt.try_shutdown_called.load(Ordering::SeqCst),
            "Drop after explicit shutdown should NOT call try_shutdown_agent"
        );
    }

    // ── chat_text() tests ──────────────────────────────────────────────

    #[tokio::test]
    async fn chat_text_returns_text() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(Arc::clone(&rt), test_config(), None, None, None)
            .await
            .expect("create agent");

        let text = agent.chat_text("Hello").await.expect("chat_text");
        assert!(!text.is_empty());

        agent.shutdown().await.expect("shutdown");
    }

    #[tokio::test]
    async fn cancel_completes_successfully() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(rt, test_config(), None, None, None)
            .await
            .expect("create agent");
        agent.cancel().await.expect("cancel should succeed");
    }

    #[tokio::test]
    async fn wait_for_idle_completes_successfully() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(rt, test_config(), None, None, None)
            .await
            .expect("create agent");
        agent
            .wait_for_idle()
            .await
            .expect("wait_for_idle should succeed");
    }

    #[tokio::test]
    async fn send_completes_successfully() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(rt, test_config(), None, None, None)
            .await
            .expect("create agent");
        agent
            .send("fire-and-forget message")
            .await
            .expect("send should succeed");
    }

    #[tokio::test]
    async fn signal_idle_completes_successfully() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(rt, test_config(), None, None, None)
            .await
            .expect("create agent");
        agent
            .signal_idle()
            .await
            .expect("signal_idle should succeed");
    }

    #[tokio::test]
    async fn wait_for_wakeup_returns_false_on_mock_timeout() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(rt, test_config(), None, None, None)
            .await
            .expect("create agent");
        let woken = agent
            .wait_for_wakeup(Duration::from_secs(1))
            .await
            .expect("wait_for_wakeup should succeed");
        assert!(!woken, "mock should return false (timeout)");
    }

    // ── Audit 4: Subagent lifecycle tests ──────────────────────────────

    #[tokio::test]
    async fn spawn_subagent_creates_child() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let parent = AgentHandle::new(Arc::clone(&rt), test_config(), None, None, None)
            .await
            .expect("create parent");
        let parent_id = parent.id();

        let child = parent
            .spawn_subagent(test_config(), None)
            .await
            .expect("spawn subagent");
        let child_id = child.id();

        // Child should have a distinct ID.
        assert_ne!(parent_id, child_id);
        assert!(child.is_started());
    }

    #[tokio::test]
    async fn spawn_subagent_with_registry_populates_tools() {
        #[derive(serde::Deserialize, schemars::JsonSchema)]
        struct Params {
            x: i32,
        }
        struct TestTool;
        impl crate::tools::RustTool for TestTool {
            type Params = Params;
            const NAME: &'static str = "test_tool";
            const DESCRIPTION: &'static str = "A test tool";
            async fn call(
                &self,
                params: Params,
                _ctx: &crate::tools::ToolContext,
            ) -> Result<crate::tools::ToolOutput, crate::tools::ToolError> {
                assert!(params.x > 0, "expected positive x, got {}", params.x);
                Ok("ok".into())
            }
        }

        let rt = Arc::new(ToolAwareMockRuntime::new());
        let parent = AgentHandle::new(Arc::clone(&rt), test_config(), None, None, None)
            .await
            .expect("create parent");

        let mut registry = crate::tools::ToolRegistry::new();
        registry.register(TestTool);

        let child = parent
            .spawn_subagent(test_config(), registry)
            .await
            .expect("spawn subagent with registry");

        // The child's config should have the tool definition from the registry.
        assert_eq!(child.config().tools.len(), 1);
        assert_eq!(child.config().tools[0].name, "test_tool");
    }

    #[tokio::test]
    async fn subagent_shutdown_lifecycle() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let parent = AgentHandle::new(Arc::clone(&rt), test_config(), None, None, None)
            .await
            .expect("create parent");

        let child = parent
            .spawn_subagent(test_config(), None)
            .await
            .expect("spawn subagent");

        // Shut down the child.
        child.shutdown().await.expect("shutdown child");
        assert!(!child.is_started());
    }

    // ── Conversation layer tests ──────────────────────────────────────

    #[tokio::test]
    async fn history_returns_messages() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(rt, test_config(), None, None, None)
            .await
            .expect("create agent");

        let history = agent.history().await.expect("history should succeed");
        assert_eq!(history.len(), 2);
        assert_eq!(history[0].role, MessageRole::User);
        assert_eq!(history[0].content, "Hello");
        assert_eq!(history[1].role, MessageRole::Model);
        assert_eq!(history[1].content, "Hi there!");
    }

    #[tokio::test]
    async fn turn_count_returns_zero_initially() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(rt, test_config(), None, None, None)
            .await
            .expect("create agent");

        let count = agent.turn_count().await.expect("turn_count should succeed");
        assert_eq!(count, 0);
    }

    #[tokio::test]
    async fn turn_count_increments_after_chat() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(Arc::clone(&rt), test_config(), None, None, None)
            .await
            .expect("create agent");

        let _response = agent.chat("Hello").await.expect("chat should succeed");
        let count = agent.turn_count().await.expect("turn_count");
        assert_eq!(count, 1);
    }

    #[tokio::test]
    async fn clear_history_resets_turn_count() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(Arc::clone(&rt), test_config(), None, None, None)
            .await
            .expect("create agent");

        let _response = agent.chat("Hello").await.expect("chat should succeed");
        assert_eq!(agent.turn_count().await.unwrap(), 1);

        agent.clear_history().await.expect("clear_history");
        assert_eq!(agent.turn_count().await.unwrap(), 0);
    }

    #[tokio::test]
    async fn total_usage_returns_metadata() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(rt, test_config(), None, None, None)
            .await
            .expect("create agent");

        let usage = agent.total_usage().await.expect("total_usage");
        assert_eq!(usage.prompt_token_count, Some(500));
        assert_eq!(usage.total_token_count, Some(800));
    }

    #[tokio::test]
    async fn last_turn_usage_returns_metadata() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(rt, test_config(), None, None, None)
            .await
            .expect("create agent");

        let usage = agent.last_turn_usage().await.expect("last_turn_usage");
        assert_eq!(usage.prompt_token_count, Some(100));
        assert_eq!(usage.total_token_count, Some(170));
    }

    #[tokio::test]
    async fn get_last_structured_output_none_initially() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(rt, test_config(), None, None, None)
            .await
            .expect("create agent");

        assert!(agent.get_last_structured_output().is_none());
    }

    #[tokio::test]
    async fn get_last_structured_output_after_chat() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(Arc::clone(&rt), test_config(), None, None, None)
            .await
            .expect("create agent");

        let mut response = agent.chat("Hello").await.expect("chat should succeed");
        // Set structured output via the shared state (the canonical path)
        {
            let mut state = response.shared_state.lock().unwrap();
            state.structured_output = Some(serde_json::json!({"answer": 42}));
        }
        // Finalize to pull shared state into the handle
        response.finalize();
        drop(response);

        let so = agent.get_last_structured_output();
        assert_eq!(so, Some(serde_json::json!({"answer": 42})));
    }

    #[tokio::test]
    async fn get_last_usage_none_initially() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(rt, test_config(), None, None, None)
            .await
            .expect("create agent");

        assert!(agent.get_last_usage().is_none());
    }

    #[tokio::test]
    async fn get_last_usage_after_chat() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(Arc::clone(&rt), test_config(), None, None, None)
            .await
            .expect("create agent");

        let response = agent.chat("Hello").await.expect("chat should succeed");

        // Simulate usage being populated in shared state
        if let Ok(mut state) = response.shared_state.lock() {
            state.usage = Some(UsageMetadata {
                prompt_token_count: Some(50),
                cached_content_token_count: None,
                candidates_token_count: Some(25),
                thoughts_token_count: None,
                total_token_count: Some(75),
            });
        }

        let usage = agent.get_last_usage().expect("should have usage");
        assert_eq!(usage.prompt_token_count, Some(50));
        assert_eq!(usage.total_token_count, Some(75));
    }

    #[tokio::test]
    async fn conversation_message_serde_roundtrip() {
        let msg = ConversationMessage {
            role: MessageRole::User,
            content: "Hello, world!".to_string(),
        };
        let json = serde_json::to_string(&msg).expect("serialize");
        let parsed: ConversationMessage = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(parsed, msg);
    }

    // ── New conversation methods ──────────────────────────────────────

    #[tokio::test]
    async fn last_response_returns_text() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(rt, test_config(), None, None, None)
            .await
            .expect("create agent");

        let resp = agent.last_response().await.expect("last_response");
        assert_eq!(resp.as_deref(), Some("Hi there!"));
    }

    #[tokio::test]
    async fn compaction_indices_returns_indices() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(rt, test_config(), None, None, None)
            .await
            .expect("create agent");

        let indices = agent
            .compaction_indices()
            .await
            .expect("compaction_indices");
        assert_eq!(indices, vec![3, 7]);
    }

    #[tokio::test]
    async fn delete_marks_agent_as_shutdown() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(rt, test_config(), None, None, None)
            .await
            .expect("create agent");

        assert!(agent.is_started());
        agent.delete().await.expect("delete");
        assert!(!agent.is_started());
    }

    #[tokio::test]
    async fn disconnect_marks_agent_as_shutdown() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(rt, test_config(), None, None, None)
            .await
            .expect("create agent");

        assert!(agent.is_started());
        agent.disconnect().await.expect("disconnect");
        assert!(!agent.is_started());
    }

    #[tokio::test]
    async fn is_idle_returns_true_on_mock() {
        let rt = Arc::new(ToolAwareMockRuntime::new());
        let agent = AgentHandle::new(rt, test_config(), None, None, None)
            .await
            .expect("create agent");

        let idle = agent.is_idle().await.expect("is_idle");
        assert!(idle, "mock should report idle");
    }
}