graphrt-core 0.3.0

Executor-agnostic graph runtime for agentic AI workflows: graph model, variable pool, scheduler, events, snapshots and built-in control nodes.
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
//! [`Variables`]: the small ordered map of named values a node produces and a
//! pool namespace holds.
//!
//! A node's outputs become its namespace in the variable pool and travel in its
//! [`Event::NodeSucceeded`](crate::event::Event::NodeSucceeded) unchanged:
//! `Variables` is a reference-counted handle, so storing the outputs, emitting
//! them and taking pool snapshots share one allocation instead of copying the
//! map. Writes copy the entries only while they are shared (copy-on-write).
//!
//! Nodes produce a handful of variables, so entries are kept in insertion order
//! in a flat vector (the first few stored inline in the shared allocation) and
//! looked up by a linear scan, which beats hashing at these sizes. Names are
//! [`VarName`]s, which hold a `&'static str` without allocating.

use std::borrow::Borrow;
use std::collections::HashMap;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::{Deref, Index};
use std::sync::Arc;

use indexmap::IndexMap;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use smallvec::SmallVec;

use crate::value::Value;

/// Name of a variable in a [`Variables`] map.
///
/// Built from a string literal it costs no allocation (`"text".into()`); other
/// names are stored as a shared `Arc<str>`. Compares, hashes and orders as the
/// string it holds.
#[derive(Clone)]
pub struct VarName(NameRepr);

#[derive(Clone)]
enum NameRepr {
    Static(&'static str),
    Shared(Arc<str>),
}

impl VarName {
    /// A name from a string literal, usable in `const` contexts.
    pub const fn from_static(name: &'static str) -> Self {
        Self(NameRepr::Static(name))
    }

    pub fn as_str(&self) -> &str {
        match &self.0 {
            NameRepr::Static(name) => name,
            NameRepr::Shared(name) => name,
        }
    }
}

impl Deref for VarName {
    type Target = str;

    fn deref(&self) -> &str {
        self.as_str()
    }
}

impl AsRef<str> for VarName {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl Borrow<str> for VarName {
    fn borrow(&self) -> &str {
        self.as_str()
    }
}

impl PartialEq for VarName {
    fn eq(&self, other: &Self) -> bool {
        self.as_str() == other.as_str()
    }
}

impl Eq for VarName {}

impl PartialEq<str> for VarName {
    fn eq(&self, other: &str) -> bool {
        self.as_str() == other
    }
}

impl PartialEq<&str> for VarName {
    fn eq(&self, other: &&str) -> bool {
        self.as_str() == *other
    }
}

impl Hash for VarName {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.as_str().hash(state);
    }
}

impl PartialOrd for VarName {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for VarName {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.as_str().cmp(other.as_str())
    }
}

impl fmt::Display for VarName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl fmt::Debug for VarName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self.as_str(), f)
    }
}

impl From<&'static str> for VarName {
    fn from(name: &'static str) -> Self {
        Self::from_static(name)
    }
}

impl From<String> for VarName {
    fn from(name: String) -> Self {
        Self(NameRepr::Shared(Arc::from(name)))
    }
}

impl From<&String> for VarName {
    fn from(name: &String) -> Self {
        Self(NameRepr::Shared(Arc::from(name.as_str())))
    }
}

impl From<Box<str>> for VarName {
    fn from(name: Box<str>) -> Self {
        Self(NameRepr::Shared(Arc::from(name)))
    }
}

impl From<Arc<str>> for VarName {
    fn from(name: Arc<str>) -> Self {
        Self(NameRepr::Shared(name))
    }
}

impl From<&Arc<str>> for VarName {
    fn from(name: &Arc<str>) -> Self {
        Self(NameRepr::Shared(Arc::clone(name)))
    }
}

impl From<VarName> for String {
    fn from(name: VarName) -> Self {
        name.as_str().to_owned()
    }
}

impl From<VarName> for Arc<str> {
    fn from(name: VarName) -> Self {
        match name.0 {
            NameRepr::Static(name) => Arc::from(name),
            NameRepr::Shared(name) => name,
        }
    }
}

impl Serialize for VarName {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(self.as_str())
    }
}

impl<'de> Deserialize<'de> for VarName {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        String::deserialize(deserializer).map(Self::from)
    }
}

/// Entries stored inline in the shared allocation before spilling to the heap.
/// Most nodes produce at most this many outputs.
const INLINE_ENTRIES: usize = 4;

/// Above this many entries, bulk inserts deduplicate names through a hash
/// index instead of a scan per entry.
const LINEAR_DEDUP_LIMIT: usize = 32;

type Entries = SmallVec<[(VarName, Value); INLINE_ENTRIES]>;

/// Ordered map of variable names to values, shared on clone.
///
/// Iteration follows insertion order; inserting an existing name replaces its
/// value in place. Cloning is a reference-count bump and an empty map holds no
/// allocation. Lookups scan the entries, so the map suits the handful of
/// variables a node produces, not large dictionaries (use a
/// [`Value::Object`] for those).
///
/// ```
/// use graphrt_core::variables::Variables;
/// use graphrt_core::Value;
///
/// let mut outputs = Variables::new();
/// outputs.insert("text", Value::from("hi"));
/// let shared = outputs.clone(); // no copy
/// assert_eq!(shared.get("text"), Some(&Value::from("hi")));
/// assert_eq!(outputs["missing"], Value::Null);
/// ```
#[derive(Clone, Default)]
pub struct Variables {
    entries: Option<Arc<Entries>>,
}

/// Returned by indexing a [`Variables`] with a missing name.
static NULL: Value = Value::Null;

impl Variables {
    pub const fn new() -> Self {
        Self { entries: None }
    }

    fn entries(&self) -> &[(VarName, Value)] {
        self.entries.as_deref().map_or(&[], |entries| entries.as_slice())
    }

    /// Entries for writing: allocated on first use, copied if shared.
    fn entries_mut(&mut self) -> &mut Entries {
        Arc::make_mut(self.entries.get_or_insert_with(Default::default))
    }

    fn position(&self, name: &str) -> Option<usize> {
        self.entries().iter().position(|(key, _)| key.as_str() == name)
    }

    pub fn len(&self) -> usize {
        self.entries().len()
    }

    pub fn is_empty(&self) -> bool {
        self.entries().is_empty()
    }

    pub fn get(&self, name: &str) -> Option<&Value> {
        self.entries().iter().find(|(key, _)| key.as_str() == name).map(|(_, value)| value)
    }

    pub fn get_key_value(&self, name: &str) -> Option<(&VarName, &Value)> {
        self.entries().iter().find(|(key, _)| key.as_str() == name).map(|(key, value)| (key, value))
    }

    /// Mutable access to a value (copies the entries first if they are shared).
    pub fn get_mut(&mut self, name: &str) -> Option<&mut Value> {
        let position = self.position(name)?;
        self.entries_mut().get_mut(position).map(|(_, value)| value)
    }

    pub fn contains_key(&self, name: &str) -> bool {
        self.position(name).is_some()
    }

    /// Insert or replace. A replaced value keeps its position and is returned.
    pub fn insert(&mut self, name: impl Into<VarName>, value: Value) -> Option<Value> {
        let name = name.into();
        if let Some(slot) = self.get_mut(&name) {
            return Some(std::mem::replace(slot, value));
        }
        self.entries_mut().push((name, value));
        None
    }

    /// [`Variables::insert`] for a borrowed name: the name is only copied when
    /// it is new, so overwriting an existing variable allocates nothing.
    pub fn insert_str(&mut self, name: &str, value: Value) -> Option<Value> {
        if let Some(slot) = self.get_mut(name) {
            return Some(std::mem::replace(slot, value));
        }
        self.entries_mut().push((VarName::from(Arc::<str>::from(name)), value));
        None
    }

    /// Remove a variable, keeping the order of the others.
    pub fn remove(&mut self, name: &str) -> Option<Value> {
        let position = self.position(name)?;
        let entries = self.entries_mut();
        (position < entries.len()).then(|| entries.remove(position).1)
    }

    /// Reserve room for `additional` more entries.
    pub fn reserve(&mut self, additional: usize) {
        if additional > 0 {
            self.entries_mut().reserve(additional);
        }
    }

    /// `(name, value)` pairs in insertion order.
    pub fn iter(&self) -> Iter<'_> {
        Iter { entries: self.entries().iter() }
    }

    pub fn keys(&self) -> impl DoubleEndedIterator<Item = &str> + ExactSizeIterator + '_ {
        self.entries().iter().map(|(key, _)| key.as_str())
    }

    /// Entries with their [`VarName`]s (cloning a name never copies its text).
    pub(crate) fn named_entries(&self) -> impl Iterator<Item = (&VarName, &Value)> + '_ {
        self.entries().iter().map(|(name, value)| (name, value))
    }

    pub fn values(&self) -> impl DoubleEndedIterator<Item = &Value> + ExactSizeIterator + '_ {
        self.entries().iter().map(|(_, value)| value)
    }

    /// Whether both maps share the same storage (a clone of one another, unchanged since).
    pub fn ptr_eq(&self, other: &Self) -> bool {
        match (&self.entries, &other.entries) {
            (Some(a), Some(b)) => Arc::ptr_eq(a, b),
            (None, None) => true,
            _ => false,
        }
    }

    /// Copy into an `IndexMap` with owned names.
    pub fn to_index_map(&self) -> IndexMap<String, Value> {
        self.iter().map(|(name, value)| (name.to_owned(), value.clone())).collect()
    }
}

impl fmt::Debug for Variables {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_map().entries(self.iter()).finish()
    }
}

/// Map equality: the same names with equal values, in any order.
impl PartialEq for Variables {
    fn eq(&self, other: &Self) -> bool {
        self.len() == other.len() && self.iter().all(|(name, value)| other.get(name) == Some(value))
    }
}

/// Indexing a missing name yields [`Value::Null`] instead of panicking.
impl Index<&str> for Variables {
    type Output = Value;

    fn index(&self, name: &str) -> &Value {
        self.get(name).unwrap_or(&NULL)
    }
}

impl<K: Into<VarName>> Extend<(K, Value)> for Variables {
    fn extend<I: IntoIterator<Item = (K, Value)>>(&mut self, iter: I) {
        let iter = iter.into_iter();
        if self.len().saturating_add(iter.size_hint().0) <= LINEAR_DEDUP_LIMIT {
            for (name, value) in iter {
                self.insert(name, value);
            }
            return;
        }
        let entries = self.entries_mut();
        let mut index: HashMap<VarName, usize> =
            entries.iter().enumerate().map(|(position, (name, _))| (name.clone(), position)).collect();
        for (name, value) in iter {
            let name = name.into();
            match index.get(name.as_str()).and_then(|&position| entries.get_mut(position)) {
                Some(slot) => slot.1 = value,
                None => {
                    index.insert(name.clone(), entries.len());
                    entries.push((name, value));
                }
            }
        }
    }
}

impl<K: Into<VarName>> FromIterator<(K, Value)> for Variables {
    fn from_iter<I: IntoIterator<Item = (K, Value)>>(iter: I) -> Self {
        let mut variables = Self::new();
        variables.extend(iter);
        variables
    }
}

impl<K: Into<VarName>, const N: usize> From<[(K, Value); N]> for Variables {
    fn from(entries: [(K, Value); N]) -> Self {
        entries.into_iter().collect()
    }
}

impl From<IndexMap<String, Value>> for Variables {
    fn from(map: IndexMap<String, Value>) -> Self {
        // Keys of an `IndexMap` are unique: no deduplication needed.
        let entries: Entries = map.into_iter().map(|(name, value)| (VarName::from(name), value)).collect();
        if entries.is_empty() { Self::new() } else { Self { entries: Some(Arc::new(entries)) } }
    }
}

impl From<Variables> for IndexMap<String, Value> {
    fn from(variables: Variables) -> Self {
        variables.into_iter().map(|(name, value)| (String::from(name), value)).collect()
    }
}

/// Borrowing iterator over a [`Variables`] map.
#[derive(Clone, Debug)]
pub struct Iter<'a> {
    entries: std::slice::Iter<'a, (VarName, Value)>,
}

impl<'a> Iterator for Iter<'a> {
    type Item = (&'a str, &'a Value);

    fn next(&mut self) -> Option<Self::Item> {
        self.entries.next().map(|(name, value)| (name.as_str(), value))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.entries.size_hint()
    }
}

impl DoubleEndedIterator for Iter<'_> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.entries.next_back().map(|(name, value)| (name.as_str(), value))
    }
}

impl ExactSizeIterator for Iter<'_> {}

impl<'a> IntoIterator for &'a Variables {
    type Item = (&'a str, &'a Value);
    type IntoIter = Iter<'a>;

    fn into_iter(self) -> Iter<'a> {
        self.iter()
    }
}

/// Owning iterator over a [`Variables`] map (copies the entries if shared).
pub struct IntoIter {
    entries: smallvec::IntoIter<[(VarName, Value); INLINE_ENTRIES]>,
}

impl fmt::Debug for IntoIter {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("IntoIter").field("remaining", &self.entries.len()).finish()
    }
}

impl Iterator for IntoIter {
    type Item = (VarName, Value);

    fn next(&mut self) -> Option<Self::Item> {
        self.entries.next()
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.entries.size_hint()
    }
}

impl DoubleEndedIterator for IntoIter {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.entries.next_back()
    }
}

impl ExactSizeIterator for IntoIter {}

impl IntoIterator for Variables {
    type Item = (VarName, Value);
    type IntoIter = IntoIter;

    fn into_iter(self) -> IntoIter {
        let entries = self.entries.map(Arc::unwrap_or_clone).unwrap_or_default();
        IntoIter { entries: entries.into_iter() }
    }
}

impl Serialize for Variables {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.collect_map(self.iter())
    }
}

impl<'de> Deserialize<'de> for Variables {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        // Later duplicates replace earlier values, as with `IndexMap`.
        IndexMap::<String, Value>::deserialize(deserializer).map(Self::from)
    }
}

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

    #[test]
    fn insert_replaces_in_place_and_keeps_order() {
        let mut vars = Variables::new();
        assert!(vars.is_empty());
        assert_eq!(vars.insert("a", Value::Int(1)), None);
        assert_eq!(vars.insert(String::from("b"), Value::Int(2)), None);
        assert_eq!(vars.insert("a", Value::Int(3)), Some(Value::Int(1)));
        assert_eq!(vars.keys().collect::<Vec<_>>(), ["a", "b"]);
        assert_eq!(vars["a"], Value::Int(3));
        assert_eq!(vars["missing"], Value::Null);
        assert_eq!(vars.insert_str("b", Value::Int(4)), Some(Value::Int(2)));
        assert_eq!(vars.insert_str("c", Value::Int(5)), None);
        assert_eq!(vars.remove("a"), Some(Value::Int(3)));
        assert_eq!(vars.remove("a"), None);
        assert_eq!(vars.keys().collect::<Vec<_>>(), ["b", "c"]);
    }

    #[test]
    fn clones_share_until_written() {
        let mut a = Variables::from([("x", Value::Int(1))]);
        let b = a.clone();
        assert!(a.ptr_eq(&b));
        a.insert("x", Value::Int(2));
        assert!(!a.ptr_eq(&b));
        assert_eq!(b["x"], Value::Int(1));
        assert_eq!(a["x"], Value::Int(2));
    }

    #[test]
    fn bulk_inserts_deduplicate_names() {
        let small: Variables = [("k", Value::Int(1)), ("k", Value::Int(2))].into_iter().collect();
        assert_eq!(small.len(), 1);
        assert_eq!(small["k"], Value::Int(2));
        let large: Variables = (0..100).chain(0..100).map(|i| (format!("k{i}"), Value::Int(i))).collect();
        assert_eq!(large.len(), 100);
        assert_eq!(large.keys().nth(42), Some("k42"));
    }

    #[test]
    fn equality_ignores_order() {
        let a = Variables::from([("x", Value::Int(1)), ("y", Value::Int(2))]);
        let b = Variables::from([("y", Value::Int(2)), ("x", Value::Int(1))]);
        assert_eq!(a, b);
        assert_ne!(a, Variables::from([("x", Value::Int(1))]));
    }

    #[test]
    fn serializes_as_an_ordered_map() {
        let vars = Variables::from([("z", Value::Int(1)), ("a", Value::from("s"))]);
        let json = serde_json::to_string(&vars).unwrap_or_default();
        assert_eq!(json, r#"{"z":1,"a":"s"}"#);
        let back: Variables = serde_json::from_str(&json).unwrap_or_default();
        assert_eq!(back, vars);
        assert_eq!(IndexMap::from(back.clone()).keys().collect::<Vec<_>>(), ["z", "a"]);
        assert_eq!(Variables::from(vars.to_index_map()), vars);
    }

    #[test]
    fn names_compare_as_strings() {
        let literal = VarName::from("name");
        let shared = VarName::from(String::from("name"));
        assert_eq!(literal, shared);
        assert_eq!(literal, "name");
        assert_eq!(String::from(shared.clone()), "name");
        assert_eq!(Arc::<str>::from(literal).as_ref(), "name");
        assert_eq!(format!("{shared} {shared:?}"), "name \"name\"");
    }
}