rig-agent 0.41.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
//! Typed context passed through tool execution.

use std::any::{Any, TypeId, type_name};
use std::collections::HashMap;
use std::hash::{BuildHasherDefault, Hasher};

use crate::tool::ToolExecutionError;
use rig_core::wasm_compat::{WasmCompatSend, WasmCompatSync};

type AnyMap = HashMap<TypeId, Box<dyn AnyClone>, BuildHasherDefault<IdHasher>>;

#[derive(Default)]
struct IdHasher(u64);

impl Hasher for IdHasher {
    fn write_u64(&mut self, id: u64) {
        self.0 = id;
    }

    fn write(&mut self, bytes: &[u8]) {
        for &byte in bytes {
            self.0 = self.0.rotate_left(8) ^ u64::from(byte);
        }
    }

    fn finish(&self) -> u64 {
        self.0
    }
}

trait AnyClone: Any + WasmCompatSend + WasmCompatSync {
    fn clone_box(&self) -> Box<dyn AnyClone>;
    fn as_any(&self) -> &dyn Any;
    fn as_any_mut(&mut self) -> &mut dyn Any;
    fn into_any(self: Box<Self>) -> Box<dyn Any>;
    fn type_name(&self) -> &'static str;
}

impl<T> AnyClone for T
where
    T: Clone + WasmCompatSend + WasmCompatSync + 'static,
{
    fn clone_box(&self) -> Box<dyn AnyClone> {
        Box::new(self.clone())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }

    fn into_any(self: Box<Self>) -> Box<dyn Any> {
        self
    }

    fn type_name(&self) -> &'static str {
        type_name::<T>()
    }
}

impl Clone for Box<dyn AnyClone> {
    fn clone(&self) -> Self {
        (**self).clone_box()
    }
}

/// Internal type map shared by tool contexts and hook scratchpads.
#[derive(Default, Clone)]
pub(crate) struct TypeMap {
    map: Option<Box<AnyMap>>,
}

impl TypeMap {
    pub(crate) const EMPTY: Self = Self { map: None };

    pub(crate) fn insert<T>(&mut self, value: T) -> Option<T>
    where
        T: Clone + WasmCompatSend + WasmCompatSync + 'static,
    {
        self.map
            .get_or_insert_with(Default::default)
            .insert(TypeId::of::<T>(), Box::new(value))
            .and_then(|previous| previous.into_any().downcast::<T>().ok())
            .map(|value| *value)
    }

    pub(crate) fn get<T>(&self) -> Option<&T>
    where
        T: WasmCompatSend + WasmCompatSync + 'static,
    {
        self.map
            .as_ref()
            .and_then(|map| map.get(&TypeId::of::<T>()))
            .and_then(|value| (**value).as_any().downcast_ref::<T>())
    }

    pub(crate) fn get_mut<T>(&mut self) -> Option<&mut T>
    where
        T: WasmCompatSend + WasmCompatSync + 'static,
    {
        self.map
            .as_mut()
            .and_then(|map| map.get_mut(&TypeId::of::<T>()))
            .and_then(|value| (**value).as_any_mut().downcast_mut::<T>())
    }

    pub(crate) fn remove<T>(&mut self) -> Option<T>
    where
        T: WasmCompatSend + WasmCompatSync + 'static,
    {
        self.map
            .as_mut()
            .and_then(|map| map.remove(&TypeId::of::<T>()))
            .and_then(|value| value.into_any().downcast::<T>().ok())
            .map(|value| *value)
    }

    pub(crate) fn contains<T>(&self) -> bool
    where
        T: WasmCompatSend + WasmCompatSync + 'static,
    {
        self.map
            .as_ref()
            .is_some_and(|map| map.contains_key(&TypeId::of::<T>()))
    }

    pub(crate) fn len(&self) -> usize {
        self.map.as_ref().map_or(0, |map| map.len())
    }

    fn type_names(&self) -> Vec<&'static str> {
        self.map
            .as_ref()
            .map(|map| map.values().map(|value| (**value).type_name()).collect())
            .unwrap_or_default()
    }
}

/// Context passed to every tool execution.
///
/// Callers insert typed inbound values with [`insert`](Self::insert). Tools read
/// those values with [`get`](Self::get) or [`require`](Self::require), and attach
/// host-only result metadata with [`insert_result`](Self::insert_result). Result
/// hooks inspect that metadata through [`result`](Self::result). Neither inbound
/// values nor result metadata are sent to the model.
///
/// Registry, server, and agent dispatch clone inbound values once per call.
/// Map-level mutations (inserting, replacing, or removing typed slots) affect
/// only that execution. Value-level isolation follows each value's [`Clone`]
/// semantics, so intentionally shared values such as `Arc<Mutex<_>>` continue
/// to share their referent across dispatches. The dispatch surface returns
/// result metadata without replacing the caller's inbound slots.
#[derive(Default, Clone)]
pub struct ToolContext {
    inbound: TypeMap,
    result: TypeMap,
}

impl ToolContext {
    /// Create an empty context.
    pub const fn new() -> Self {
        Self {
            inbound: TypeMap::EMPTY,
            result: TypeMap::EMPTY,
        }
    }

    /// Insert an inbound typed value, returning the displaced value if present.
    pub fn insert<T>(&mut self, value: T) -> Option<T>
    where
        T: Clone + WasmCompatSend + WasmCompatSync + 'static,
    {
        self.inbound.insert(value)
    }

    /// Read an inbound typed value.
    pub fn get<T>(&self) -> Option<&T>
    where
        T: WasmCompatSend + WasmCompatSync + 'static,
    {
        self.inbound.get::<T>()
    }

    /// Require an inbound typed value.
    pub fn require<T>(&self) -> Result<&T, MissingToolContext>
    where
        T: WasmCompatSend + WasmCompatSync + 'static,
    {
        self.get::<T>().ok_or(MissingToolContext(type_name::<T>()))
    }

    /// Mutably access an inbound typed value.
    pub fn get_mut<T>(&mut self) -> Option<&mut T>
    where
        T: WasmCompatSend + WasmCompatSync + 'static,
    {
        self.inbound.get_mut::<T>()
    }

    /// Remove an inbound typed value.
    pub fn remove<T>(&mut self) -> Option<T>
    where
        T: WasmCompatSend + WasmCompatSync + 'static,
    {
        self.inbound.remove::<T>()
    }

    /// Attach host-only metadata to this execution's result.
    pub fn insert_result<T>(&mut self, value: T) -> Option<T>
    where
        T: Clone + WasmCompatSend + WasmCompatSync + 'static,
    {
        self.result.insert(value)
    }

    /// Read host-only result metadata.
    pub fn result<T>(&self) -> Option<&T>
    where
        T: WasmCompatSend + WasmCompatSync + 'static,
    {
        self.result.get::<T>()
    }

    /// Require host-only result metadata.
    pub fn require_result<T>(&self) -> Result<&T, MissingToolContext>
    where
        T: WasmCompatSend + WasmCompatSync + 'static,
    {
        self.result::<T>()
            .ok_or(MissingToolContext(type_name::<T>()))
    }

    /// Whether this context contains the inbound type `T`.
    pub fn contains<T>(&self) -> bool
    where
        T: WasmCompatSend + WasmCompatSync + 'static,
    {
        self.inbound.contains::<T>()
    }

    /// Build a fresh execution context with the same inbound values and no
    /// result metadata.
    ///
    /// Dispatch always runs against this snapshot. Mutating its typed slots does
    /// not change the run-wide or caller-owned map. Values with shared/interior
    /// state remain shared according to their [`Clone`] implementation.
    pub(crate) fn for_dispatch(&self) -> Self {
        Self {
            inbound: self.inbound.clone(),
            result: TypeMap::EMPTY,
        }
    }

    /// Publish metadata produced by one dispatch while preserving the caller's
    /// inbound values.
    pub(crate) fn accept_dispatch_result(&mut self, dispatched: Self) {
        self.result = dispatched.result;
    }

    /// Clear metadata from the previous dispatch before starting another one.
    pub(crate) fn clear_dispatch_result(&mut self) {
        self.result = TypeMap::EMPTY;
    }

    /// Clone only the inbound values, for nested tool execution.
    pub(crate) fn inbound_only(&self) -> Self {
        self.for_dispatch()
    }
}

impl std::fmt::Debug for ToolContext {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ToolContext")
            .field("inbound_entries", &self.inbound.len())
            .field("inbound_types", &self.inbound.type_names())
            .field("result_entries", &self.result.len())
            .field("result_types", &self.result.type_names())
            .finish()
    }
}

/// A required typed value was missing from a [`ToolContext`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[error("required tool context value of type `{0}` was not found")]
pub struct MissingToolContext(pub &'static str);

impl From<MissingToolContext> for ToolExecutionError {
    fn from(error: MissingToolContext) -> Self {
        ToolExecutionError::other(error.to_string()).with_source(error)
    }
}

#[cfg(not(target_family = "wasm"))]
const _: fn() = || {
    fn assert_send_sync<T: Send + Sync>() {}
    assert_send_sync::<ToolContext>();
};

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

    #[test]
    fn context_separates_inbound_and_result_values() {
        let mut context = ToolContext::new();
        context.insert(42_u32);
        context.insert_result("request-1".to_string());
        assert_eq!(context.get::<u32>(), Some(&42));
        assert_eq!(
            context.result::<String>().map(String::as_str),
            Some("request-1")
        );

        let next = context.for_dispatch();
        assert_eq!(next.get::<u32>(), Some(&42));
        assert!(next.result::<String>().is_none());
    }

    #[test]
    fn missing_context_converts_into_a_tool_execution_error() {
        fn require_value(context: &ToolContext) -> Result<u32, ToolExecutionError> {
            Ok(*context.require::<u32>()?)
        }

        let error = require_value(&ToolContext::new()).unwrap_err();
        assert!(error.is::<MissingToolContext>());
        assert_eq!(
            error.model_feedback(),
            Some("required tool context value of type `u32` was not found")
        );
    }
}

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

    #[test]
    fn insert_and_get_returns_value() {
        let mut c = ToolContext::new();
        assert_eq!(c.insert(42u32), None);
        assert_eq!(c.get::<u32>(), Some(&42));
    }
    #[test]
    fn get_missing_type_returns_none() {
        assert_eq!(ToolContext::new().get::<u32>(), None);
    }
    #[test]
    fn insert_overwrites_and_returns_previous() {
        let mut c = ToolContext::new();
        c.insert(1u32);
        assert_eq!(c.insert(2u32), Some(1));
        assert_eq!(c.get::<u32>(), Some(&2));
    }
    #[test]
    fn different_types_are_independent() {
        let mut c = ToolContext::new();
        c.insert(42u32);
        c.insert("hello".to_string());
        assert_eq!(c.get::<u32>(), Some(&42));
        assert_eq!(c.get::<String>().map(String::as_str), Some("hello"));
    }
    #[test]
    fn contains_tracks_types() {
        let mut c = ToolContext::new();
        c.insert(42u32);
        assert!(c.contains::<u32>());
        assert!(!c.contains::<String>());
    }
    #[test]
    fn clone_produces_independent_copy() {
        let mut c = ToolContext::new();
        c.insert(42u32);
        let mut clone = c.clone();
        clone.insert(99u32);
        assert_eq!(c.get::<u32>(), Some(&42));
        assert_eq!(clone.get::<u32>(), Some(&99));
    }
    #[test]
    fn clone_deep_copies_heap_values() {
        let mut c = ToolContext::new();
        c.insert(vec![1u8, 2, 3]);
        let mut clone = c.clone();
        clone.get_mut::<Vec<u8>>().unwrap().push(4);
        assert_eq!(c.get::<Vec<u8>>(), Some(&vec![1, 2, 3]));
        assert_eq!(clone.get::<Vec<u8>>(), Some(&vec![1, 2, 3, 4]));
    }
    #[test]
    fn clone_preserves_intentionally_shared_value_state() {
        let shared = std::sync::Arc::new(std::sync::Mutex::new(1_u32));
        let mut context = ToolContext::new();
        context.insert(shared.clone());

        let snapshot = context.for_dispatch();
        *snapshot
            .get::<std::sync::Arc<std::sync::Mutex<u32>>>()
            .expect("shared value")
            .lock()
            .expect("shared value lock") = 2;

        assert_eq!(*shared.lock().expect("shared value lock"), 2);
        assert!(context.contains::<std::sync::Arc<std::sync::Mutex<u32>>>());
    }
    #[test]
    fn empty_context_is_default_and_allocation_free() {
        let c = ToolContext::default();
        assert!(!c.contains::<u32>());
        assert!(c.inbound.map.is_none());
        assert!(c.result.map.is_none());
    }
    #[test]
    fn get_mut_modifies_in_place() {
        let mut c = ToolContext::new();
        c.insert(42u32);
        *c.get_mut::<u32>().unwrap() = 99;
        assert_eq!(c.get::<u32>(), Some(&99));
    }
    #[test]
    fn remove_returns_value_and_clears_entry() {
        let mut c = ToolContext::new();
        c.insert(42u32);
        assert_eq!(c.remove::<u32>(), Some(42));
        assert!(!c.contains::<u32>());
    }
    #[test]
    fn remove_missing_type_returns_none() {
        assert_eq!(ToolContext::new().remove::<u32>(), None);
    }
    #[test]
    fn require_present_returns_value() {
        let mut c = ToolContext::new();
        c.insert(42u32);
        assert_eq!(c.require::<u32>().copied(), Ok(42));
    }
    #[test]
    fn require_missing_names_type() {
        let e = ToolContext::new().require::<u32>().unwrap_err();
        assert!(e.to_string().contains("u32"));
    }
    #[test]
    fn result_metadata_round_trips_and_requires() {
        #[derive(Clone, Debug, PartialEq)]
        struct Id(u32);
        let mut c = ToolContext::new();
        c.insert_result(Id(7));
        assert_eq!(c.result::<Id>(), Some(&Id(7)));
        assert_eq!(c.require_result::<Id>(), Ok(&Id(7)));
        assert!(c.get::<Id>().is_none());
    }
    #[test]
    fn debug_reports_types_without_values() {
        #[derive(Clone)]
        struct Secret(&'static str);
        let mut c = ToolContext::new();
        c.insert(42u32);
        c.insert_result(Secret("do-not-print"));
        let d = format!("{c:?}");
        assert!(d.contains("u32"));
        assert!(d.contains("Secret"));
        assert!(!d.contains("do-not-print"));
        assert_eq!(c.result::<Secret>().map(|s| s.0), Some("do-not-print"));
    }
    #[test]
    fn dispatch_snapshot_isolates_inbound_and_publishes_only_result_metadata() {
        let mut c = ToolContext::new();
        c.insert(7u32);
        c.insert_result("old".to_string());
        let mut d = c.for_dispatch();
        assert_eq!(d.get::<u32>(), Some(&7));
        assert!(d.result::<String>().is_none());
        *d.get_mut::<u32>().expect("snapshot value") = 8;
        d.insert_result("new".to_string());

        c.accept_dispatch_result(d);
        assert_eq!(c.get::<u32>(), Some(&7));
        assert_eq!(c.result::<String>().map(String::as_str), Some("new"));
    }
    #[test]
    fn many_distinct_types_round_trip_through_type_id_hasher() {
        #[derive(Clone, PartialEq, Debug)]
        struct A(u8);
        #[derive(Clone, PartialEq, Debug)]
        struct B(u16);
        let mut c = ToolContext::new();
        c.insert(A(1));
        c.insert(B(2));
        c.insert(3u32);
        c.insert("four".to_string());
        assert_eq!(c.get::<A>(), Some(&A(1)));
        assert_eq!(c.get::<B>(), Some(&B(2)));
        assert_eq!(c.get::<u32>(), Some(&3));
        assert_eq!(c.get::<String>().map(String::as_str), Some("four"));
    }
}