rig-agent 0.43.0

Rig's classic agent runtime.
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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
use std::{
    future::{Future, pending, poll_fn},
    sync::{
        Arc,
        atomic::{AtomicBool, Ordering},
    },
    task::Poll,
    time::Duration,
};

use crate::{
    test_utils::{
        BarrierMockToolIndex, MockAddTool, MockBarrierTool, MockControlledTool, MockSubtractTool,
        MockToolIndex,
    },
    tool::{
        Tool, ToolContext, ToolEmbedding, ToolExecutionError, ToolSet,
        server::{ToolServer, ToolServerHandle},
    },
};

async fn execute_tool(
    handle: &ToolServerHandle,
    name: &str,
    args: &str,
) -> Result<String, ToolExecutionError> {
    execute_tool_with_context(handle, name, args, &mut ToolContext::new()).await
}

/// A dynamic tool whose liveness follows `live`, standing in for a remote
/// tool whose transport can disconnect.
fn liveness_gated_tool(name: &str, live: Arc<AtomicBool>) -> crate::tool::DynamicTool {
    crate::tool::DynamicTool::new(name, "gated", serde_json::json!({"type": "object"}), |_| {
        Box::pin(async { Ok(crate::tool::ToolOutput::text("ok")) })
    })
    .with_liveness(move || live.load(Ordering::SeqCst))
}

/// The sync snapshot and the async, prompt-less `tool_defs` read the
/// same always-exposed registry in the same order.
#[tokio::test]
async fn sync_snapshot_matches_async_prompt_less_read() {
    let handle = ToolServer::new()
        .tool(MockAddTool)
        .tool(MockSubtractTool)
        .run();

    let sync_defs = handle.static_tool_defs();
    let async_defs = handle.tool_defs(None).await.unwrap();
    assert_eq!(sync_defs.len(), 2);
    assert_eq!(sync_defs, async_defs);

    let snapshot = handle.snapshot();
    assert_eq!(snapshot.definitions(), sync_defs.as_slice());
    assert_eq!(
        snapshot.names().collect::<Vec<_>>(),
        vec!["add", "subtract"]
    );
    assert_eq!(snapshot.len(), 2);
    assert!(!snapshot.is_empty());
}

/// A tool whose remote backing disconnected is retired by every read path —
/// sync snapshot, sync definitions, forked toolset, and the async read.
#[tokio::test]
async fn retired_tools_are_absent_from_every_read_path() {
    let live = Arc::new(AtomicBool::new(true));
    let handle = ToolServer::new()
        .tool(MockAddTool)
        .dynamic_tool(liveness_gated_tool("remote", live.clone()))
        .run();
    assert_eq!(
        handle.snapshot().names().collect::<Vec<_>>(),
        vec!["add", "remote"]
    );

    live.store(false, Ordering::SeqCst);

    assert_eq!(handle.snapshot().names().collect::<Vec<_>>(), vec!["add"]);
    assert_eq!(handle.static_tool_defs().len(), 1);
    assert!(!handle.toolset().contains("remote"));
    assert_eq!(handle.tool_defs(None).await.unwrap().len(), 1);
}

/// A snapshot pins implementations: it keeps executing the tool it was
/// taken with after the registry replaces or removes that name.
#[tokio::test]
async fn snapshot_executes_pinned_implementation() {
    let handle = ToolServer::new().tool(MockAddTool).run();
    let snapshot = handle.snapshot();
    handle.remove_tool("add");
    assert!(handle.snapshot().is_empty());

    let mut context = ToolContext::new();
    let result = snapshot
        .execute("add", r#"{"x": 2, "y": 3}"#, &mut context)
        .await;
    assert_eq!(result.output().render(), "5");
}

/// `toolset()` forks the registry: the fork shares implementations but
/// later changes on either side stay local.
#[tokio::test]
async fn toolset_forks_the_registry() {
    let handle = ToolServer::new().tool(MockAddTool).run();
    let mut fork = handle.toolset();
    assert!(fork.contains("add"));

    fork.add_tool(MockSubtractTool);
    assert!(!handle.snapshot().names().any(|name| name == "subtract"));

    handle.remove_tool("add");
    assert!(fork.contains("add"));

    // The fork builds a second, independent server with the same tools.
    let second = ToolServer::new().run();
    second.add_tools(fork);
    assert_eq!(
        execute_tool(&second, "add", r#"{"x": 1, "y": 1}"#)
            .await
            .unwrap(),
        "2"
    );
}

/// The sync read needs no executor: a plain test reads definitions that
/// another thread registered, with no runtime in sight.
#[test]
fn static_tool_defs_reads_without_a_runtime() {
    let handle = ToolServer::new().run();
    assert!(handle.static_tool_defs().is_empty());

    let writer = handle.clone();
    std::thread::spawn(move || {
        writer.add_tool(MockAddTool);
        writer.add_tool(MockSubtractTool);
    })
    .join()
    .expect("registering thread");

    let names = handle
        .static_tool_defs()
        .into_iter()
        .map(|definition| definition.name)
        .collect::<Vec<_>>();
    assert_eq!(names, vec!["add", "subtract"]);
}

async fn execute_tool_with_context(
    handle: &ToolServerHandle,
    name: &str,
    args: &str,
    context: &mut ToolContext,
) -> Result<String, ToolExecutionError> {
    let result = handle.execute(name, args, context).await;
    match result.error() {
        Some(error) => Err(error.clone()),
        None => Ok(result.output().render()),
    }
}

struct NamedTool;

impl NamedTool {
    fn new() -> Self {
        Self
    }
}

impl Tool for NamedTool {
    const NAME: &'static str = "registered_named";
    type Error = rig::tool::ToolExecutionError;
    type Args = serde_json::Value;
    type Output = String;

    fn description(&self) -> String {
        "uses its canonical name".to_string()
    }

    fn parameters(&self) -> serde_json::Value {
        serde_json::json!({"type": "object", "properties": {}})
    }

    async fn call(
        &self,
        _context: &mut crate::tool::ToolContext,
        _args: Self::Args,
    ) -> Result<Self::Output, crate::tool::ToolExecutionError> {
        Ok("ok".to_string())
    }
}

struct ReplacementTool {
    description: &'static str,
    output: &'static str,
}

impl Tool for ReplacementTool {
    const NAME: &'static str = "replacement";
    type Error = rig::tool::ToolExecutionError;
    type Args = serde_json::Value;
    type Output = String;

    fn description(&self) -> String {
        self.description.to_string()
    }

    fn parameters(&self) -> serde_json::Value {
        serde_json::json!({"type": "object", "properties": {}})
    }

    async fn call(
        &self,
        _context: &mut ToolContext,
        _args: Self::Args,
    ) -> Result<Self::Output, ToolExecutionError> {
        Ok(self.output.to_string())
    }
}

#[derive(Debug, thiserror::Error)]
#[error("init error")]
struct InitError;

impl ToolEmbedding for NamedTool {
    type InitError = InitError;
    type Context = ();
    type State = ();

    fn embedding_docs(&self) -> Vec<String> {
        vec!["named retrieved tool".to_string()]
    }

    fn context(&self) -> Self::Context {}

    fn init(_state: Self::State, _context: Self::Context) -> Result<Self, Self::InitError> {
        Ok(Self::new())
    }
}

#[tokio::test]
pub async fn test_toolserver() {
    let server = ToolServer::new();

    let handle = server.run();

    handle.add_tool(MockAddTool);
    let res = handle.tool_defs(None).await.unwrap();

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

    let json_args_as_string = serde_json::to_string(&serde_json::json!({"x": 2, "y": 5})).unwrap();
    let res = execute_tool(&handle, "add", &json_args_as_string)
        .await
        .unwrap();
    assert_eq!(res, "7");

    handle.remove_tool("add");
    let res = handle.tool_defs(None).await.unwrap();

    assert_eq!(res.len(), 0);
}

#[tokio::test]
async fn definition_snapshot_pins_the_exact_tool_registration() {
    let handle = ToolServer::new()
        .tool(ReplacementTool {
            description: "first schema",
            output: "first implementation",
        })
        .run();
    let snapshot = handle.snapshot_tool_defs(None).await.unwrap();

    handle.add_tool(ReplacementTool {
        description: "second schema",
        output: "second implementation",
    });

    assert_eq!(snapshot.definitions()[0].description, "first schema");
    let dispatch = snapshot
        .execute_scoped(ReplacementTool::NAME, "{}", &ToolContext::new())
        .await;
    assert_eq!(dispatch.result.output().render(), "first implementation");

    let live = handle
        .execute_scoped(ReplacementTool::NAME, "{}", &ToolContext::new())
        .await;
    assert_eq!(live.result.output().render(), "second implementation");

    let next_snapshot = handle.snapshot_tool_defs(None).await.unwrap();
    assert_eq!(next_snapshot.definitions()[0].description, "second schema");
    let dispatch = next_snapshot
        .execute_scoped(ReplacementTool::NAME, "{}", &ToolContext::new())
        .await;
    assert_eq!(dispatch.result.output().render(), "second implementation");
}

#[tokio::test]
pub async fn test_toolserver_append_toolset_matches_add_tool() {
    let mut via_add_tool = {
        let handle = ToolServer::new().run();
        handle.add_tool(MockAddTool);
        handle.add_tool(MockSubtractTool);
        handle.tool_defs(None).await.unwrap()
    };
    via_add_tool.sort_by(|a, b| a.name.cmp(&b.name));

    let mut via_append_toolset = {
        let handle = ToolServer::new().run();
        let mut toolset = ToolSet::default();
        toolset.add_tool(MockAddTool);
        toolset.add_tool(MockSubtractTool);
        handle.add_tools(toolset);
        handle.tool_defs(None).await.unwrap()
    };
    via_append_toolset.sort_by(|a, b| a.name.cmp(&b.name));

    assert_eq!(via_add_tool.len(), via_append_toolset.len());
    assert!(
        via_add_tool
            .iter()
            .zip(via_append_toolset.iter())
            .all(|(a, b)| a.name == b.name),
        "append_toolset must surface the same LLM-visible tools as add_tool",
    );
}

#[tokio::test]
pub async fn builder_tool_uses_canonical_static_name() {
    let handle = ToolServer::new().tool(NamedTool::new()).run();

    let defs = handle.tool_defs(None).await.unwrap();
    assert_eq!(defs.len(), 1);
    assert_eq!(defs[0].name, NamedTool::NAME);
}

#[tokio::test]
pub async fn handle_add_tool_uses_canonical_static_name() {
    let handle = ToolServer::new().run();
    handle.add_tool(NamedTool::new());

    let defs = handle.tool_defs(None).await.unwrap();
    assert_eq!(defs.len(), 1);
    assert_eq!(defs[0].name, NamedTool::NAME);
}

#[tokio::test]
pub async fn retrieval_resolves_canonical_key() {
    let mut toolset = ToolSet::default();
    toolset
        .add_retrieved_tool(NamedTool::new())
        .expect("the tool context serializes");
    let handle = ToolServer::new()
        .retrieved_tools(1, MockToolIndex::new([NamedTool::NAME]), toolset)
        .run();

    let defs = handle
        .tool_defs(Some("use the changing tool".to_string()))
        .await
        .unwrap();
    assert_eq!(defs.len(), 1);
    assert_eq!(defs[0].name, NamedTool::NAME);
}

#[tokio::test]
pub async fn get_tool_defs_preserves_static_registration_order() {
    let handle = ToolServer::new().run();
    handle.add_tool(MockSubtractTool);
    handle.add_tool(MockAddTool);

    let defs = handle.tool_defs(None).await.unwrap();
    assert_eq!(
        defs.iter().map(|def| def.name.as_str()).collect::<Vec<_>>(),
        vec!["subtract", "add"]
    );
}

#[tokio::test]
pub async fn get_tool_defs_dedupes_dynamic_and_static_overlap() {
    // One shared toolset backs both lists, so a dynamically retrieved
    // name that is also static must yield a single definition.
    let handle = ToolServer::new()
        .tool(MockAddTool)
        .retrieved_tools(1, MockToolIndex::new(["add"]), ToolSet::default())
        .run();

    let defs = handle
        .tool_defs(Some("add two numbers".to_string()))
        .await
        .unwrap();
    assert_eq!(
        defs.len(),
        1,
        "dynamic/static name overlap must not produce duplicate declarations: {:?}",
        defs.iter().map(|def| def.name.as_str()).collect::<Vec<_>>()
    );
    assert_eq!(defs[0].name, "add");
}

#[tokio::test]
async fn retrieval_registration_preserves_existing_always_exposure() {
    let handle = ToolServer::new()
        .tool(MockAddTool)
        .retrieved_tools(
            1,
            MockToolIndex::new(["add"]),
            ToolSet::from_tools(vec![MockAddTool]),
        )
        .run();

    let defs = handle.tool_defs(None).await.unwrap();
    assert_eq!(
        defs.iter()
            .map(|definition| definition.name.as_str())
            .collect::<Vec<_>>(),
        vec!["add"],
        "merging a retrieval implementation must not demote an always-exposed registration"
    );
}

#[tokio::test]
pub async fn duplicate_registration_advertises_one_definition() {
    let handle = ToolServer::new().tool(MockAddTool).run();
    handle.add_tool(MockAddTool);

    let mut toolset = ToolSet::default();
    toolset.add_tool(MockAddTool);
    handle.add_tools(toolset);

    let defs = handle.tool_defs(None).await.unwrap();
    assert_eq!(
        defs.len(),
        1,
        "re-registering a name must not advertise duplicate declarations"
    );
    assert_eq!(defs[0].name, "add");
}

#[tokio::test]
pub async fn test_toolserver_retrieved_tools() {
    // Create a toolset with both tools
    let mut toolset = ToolSet::default();
    toolset.add_tool(MockAddTool);
    toolset.add_tool(MockSubtractTool);

    // Create a mock index that will return "subtract" as the dynamic tool
    let mock_index = MockToolIndex::new(["subtract"]);

    // Build server with static tool "add" and dynamic tools from the mock index
    let server = ToolServer::new().tool(MockAddTool).retrieved_tools(
        1,
        mock_index,
        ToolSet::from_tools(vec![MockSubtractTool]),
    );

    let handle = server.run();

    // Test with None prompt - should only return static tools
    let res = handle.tool_defs(None).await.unwrap();
    assert_eq!(res.len(), 1);
    assert_eq!(res[0].name, "add");

    // Test with Some prompt - should return both static and dynamic tools
    let res = handle
        .tool_defs(Some("calculate difference".to_string()))
        .await
        .unwrap();
    assert_eq!(res.len(), 2);

    // Check that both tools are present (order may vary)
    let tool_names: Vec<&str> = res.iter().map(|t| t.name.as_str()).collect();
    assert!(tool_names.contains(&"add"));
    assert!(tool_names.contains(&"subtract"));
}

#[tokio::test]
pub async fn test_toolserver_retrieved_tools_missing_implementation() {
    // Create a mock index that returns a tool ID that doesn't exist in the toolset
    let mock_index = MockToolIndex::new(["nonexistent_tool"]);

    // Build server with only static tool, but dynamic index references missing tool
    let server =
        ToolServer::new()
            .tool(MockAddTool)
            .retrieved_tools(1, mock_index, ToolSet::default());

    let handle = server.run();

    // Test with Some prompt - should only return static tool since dynamic tool is missing
    let res = handle
        .tool_defs(Some("some query".to_string()))
        .await
        .unwrap();
    assert_eq!(res.len(), 1);
    assert_eq!(res[0].name, "add");
}

#[tokio::test]
pub async fn test_toolserver_concurrent_tool_execution() {
    let num_calls = 3;
    let barrier = Arc::new(tokio::sync::Barrier::new(num_calls));

    let server = ToolServer::new().tool(MockBarrierTool::new(barrier.clone()));
    let handle = server.run();

    // Make concurrent calls
    let futures: Vec<_> = (0..num_calls)
        .map(|_| execute_tool(&handle, "barrier_tool", "{}"))
        .collect();

    // If execution is sequential, the first call will block at the barrier forever.
    // We use a 1-second timeout to fail fast instead of hanging the test runner.
    let result =
        tokio::time::timeout(Duration::from_secs(1), futures::future::join_all(futures)).await;

    assert!(
        result.is_ok(),
        "Tool execution deadlocked! Tools are executing sequentially instead of concurrently."
    );

    // All calls should succeed
    for res in result.unwrap() {
        assert!(res.is_ok(), "Tool call failed: {res:?}");
        assert_eq!(res.unwrap(), "done");
    }
}

#[tokio::test]
pub async fn test_toolserver_write_while_tool_running() {
    let started = Arc::new(tokio::sync::Notify::new());
    let allow_finish = Arc::new(tokio::sync::Notify::new());

    // Build server with the controlled tool that waits at a barrier during execution
    let tool = MockControlledTool::new(started.clone(), allow_finish.clone());

    let server = ToolServer::new().tool(tool);
    let handle = server.run();

    // Start tool call in background
    let handle_clone = handle.clone();
    let call_task =
        tokio::spawn(async move { execute_tool(&handle_clone, "controlled", "{}").await });

    // Wait until we are strictly inside `call()`
    started.notified().await;

    // Write to the state (add a tool) while the tool call is mid-execution.
    // If the read lock were incorrectly held across tool execution, this
    // sync call would block forever and the test harness would time out.
    handle.add_tool(MockAddTool);

    // Allow the background tool to finish and clean up
    allow_finish.notify_one();
    let call_result = call_task.await.unwrap();
    assert_eq!(call_result.unwrap(), "42");
}

#[tokio::test]
pub async fn test_toolserver_parallel_retrieval() {
    // We expect exactly 2 parallel searches to hit the barrier at the same time
    let barrier = Arc::new(tokio::sync::Barrier::new(2));

    let index1 = BarrierMockToolIndex::new(barrier.clone(), "add");
    let index2 = BarrierMockToolIndex::new(barrier.clone(), "subtract");

    // Put both tools in the toolset so they resolve correctly
    let mut toolset = ToolSet::default();
    toolset.add_tool(MockAddTool);
    toolset.add_tool(MockSubtractTool);

    let server = ToolServer::new()
        .retrieved_tools(1, index1, ToolSet::default())
        .retrieved_tools(1, index2, toolset);

    let handle = server.run();

    // This will trigger a search across both indices.
    // If fetched sequentially, the first index will wait at the barrier forever.
    let get_defs = tokio::time::timeout(
        std::time::Duration::from_secs(1),
        handle.tool_defs(Some("do math".to_string())),
    )
    .await;

    assert!(
        get_defs.is_ok(),
        "Dynamic tools were fetched sequentially! The first query deadlocked waiting for the second query to start."
    );

    let defs = get_defs.unwrap().unwrap();
    assert_eq!(defs.len(), 2);

    let tool_names: Vec<&str> = defs.iter().map(|t| t.name.as_str()).collect();
    assert!(tool_names.contains(&"add"));
    assert!(tool_names.contains(&"subtract"));
}

#[derive(serde::Serialize, serde::Deserialize)]
struct SessionId(String);

impl rig_core::tool::ContextValue for SessionId {
    const KEY: &'static str = "test.server_session_id";
}

#[derive(serde::Serialize, serde::Deserialize)]
struct Counter(usize);

impl rig_core::tool::ContextValue for Counter {
    const KEY: &'static str = "test.counter";
}

#[derive(serde::Serialize, serde::Deserialize)]
struct CounterResult(usize);

impl rig_core::tool::ContextValue for CounterResult {
    const KEY: &'static str = "test.counter_result";
}

#[derive(serde::Serialize, serde::Deserialize)]
struct Note(String);

impl rig_core::tool::ContextValue for Note {
    const KEY: &'static str = "test.note";
}

#[derive(serde::Deserialize, serde::Serialize)]
struct ContextReader;

impl crate::tool::Tool for ContextReader {
    const NAME: &'static str = "context_reader";
    type Error = rig::tool::ToolExecutionError;
    type Args = serde_json::Value;
    type Output = String;

    fn description(&self) -> String {
        "Reads SessionId from context".to_string()
    }

    fn parameters(&self) -> serde_json::Value {
        serde_json::json!({"type": "object", "properties": {}})
    }

    async fn call(
        &self,
        context: &mut ToolContext,
        _args: Self::Args,
    ) -> Result<Self::Output, ToolExecutionError> {
        if let Some(Counter(value)) = context.get::<Counter>()? {
            context.insert(Counter(value + 1))?;
            context.insert_result(CounterResult(value + 1))?;
        }
        Ok(context.get::<SessionId>()?.map_or_else(
            || "no session".to_string(),
            |session| format!("session:{}", session.0),
        ))
    }
}

#[tokio::test]
async fn context_reaches_the_single_execute_path() {
    let handle = ToolServer::new().tool(ContextReader).run();
    let mut context = ToolContext::new();
    context.insert(SessionId("abc-123".to_string())).unwrap();
    let result = execute_tool_with_context(&handle, "context_reader", "{}", &mut context)
        .await
        .unwrap();
    assert_eq!(result, "session:abc-123");
}

#[tokio::test]
async fn server_dispatch_snapshot_isolates_and_only_publishes_result_metadata() {
    let handle = ToolServer::new().tool(ContextReader).run();
    let mut context = ToolContext::new();
    context.insert(Counter(0)).unwrap();

    let result = execute_tool_with_context(&handle, "context_reader", "{}", &mut context)
        .await
        .unwrap();

    assert_eq!(result, "no session");
    assert_eq!(
        context.get::<Counter>().unwrap().map(|value| value.0),
        Some(0),
        "tool-local inbound mutations must not change the caller's context"
    );
    assert_eq!(
        context.result::<CounterResult>().unwrap().map(|v| v.0),
        Some(1)
    );
}

struct PendingTool(Arc<AtomicBool>);

impl Tool for PendingTool {
    const NAME: &'static str = "pending";
    type Error = rig::tool::ToolExecutionError;
    type Args = ();
    type Output = ();

    fn description(&self) -> String {
        "never completes".into()
    }

    fn parameters(&self) -> serde_json::Value {
        serde_json::json!({"type": "object"})
    }

    async fn call(
        &self,
        context: &mut ToolContext,
        _args: Self::Args,
    ) -> Result<Self::Output, ToolExecutionError> {
        context.insert_result(Note("unpublished".to_string()))?;
        self.0.store(true, Ordering::SeqCst);
        pending().await
    }
}

#[tokio::test]
async fn cancelled_server_dispatch_does_not_retain_stale_result_metadata() {
    let started = Arc::new(AtomicBool::new(false));
    let handle = ToolServer::new().tool(PendingTool(started.clone())).run();
    let mut context = ToolContext::new();
    context.insert_result(Note("stale".to_string())).unwrap();

    let mut execution = Box::pin(handle.execute(PendingTool::NAME, "null", &mut context));
    tokio::time::timeout(
        Duration::from_secs(1),
        poll_fn(|cx| {
            assert!(execution.as_mut().poll(cx).is_pending());
            started.load(Ordering::SeqCst).then_some(()).map_or_else(
                || {
                    cx.waker().wake_by_ref();
                    Poll::Pending
                },
                Poll::Ready,
            )
        }),
    )
    .await
    .expect("pending tool did not start");
    drop(execution);

    assert!(context.result::<Note>().unwrap().is_none());
}

#[tokio::test]
async fn empty_tool_context_uses_default() {
    let handle = ToolServer::new().tool(ContextReader).run();
    let result = execute_tool(&handle, "context_reader", "{}").await.unwrap();

    assert_eq!(result, "no session");
}

#[tokio::test]
async fn tool_ignoring_context_still_works() {
    let handle = ToolServer::new().tool(MockAddTool).run();
    let mut context = ToolContext::new();
    context.insert(SessionId("ignored".to_string())).unwrap();
    let args = serde_json::to_string(&serde_json::json!({"x": 3, "y": 7})).unwrap();
    let result = execute_tool_with_context(&handle, "add", &args, &mut context)
        .await
        .unwrap();

    assert_eq!(result, "10");
}

#[tokio::test]
async fn execute_classifies_a_missing_tool_as_not_found() {
    let handle = ToolServer::new().tool(MockAddTool).run();
    let error = execute_tool(&handle, "does_not_exist", "{}")
        .await
        .unwrap_err();
    assert_eq!(error.kind(), crate::tool::ToolErrorKind::NotFound);
    assert!(
        error
            .model_feedback()
            .is_some_and(|feedback| feedback.contains("does_not_exist"))
    );
}