tinyagents 2.1.0

A recursive language-model (RLM) harness for Rust.
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
//! Durable observability for the graph runtime — journals, status stores, and
//! the journaling event sink.
//!
//! The live [`crate::graph::stream`] layer emits transient [`GraphEvent`]s into
//! an in-process [`GraphEventSink`]. This module makes that history **durable
//! and correlatable** so a UI, supervisor, or test can reconstruct a recursive
//! graph run tree after the fact:
//!
//! - [`GraphObservation`] — a durable envelope pairing an event with its run
//!   lineage (`run_id` / `parent_run_id` / `root_run_id`), `graph_id`,
//!   `checkpoint_id`, subgraph `namespace`, `step`, `offset`, and timestamp.
//! - [`GraphEventJournal`] — an append-only, offset-addressable journal of
//!   observations, with an [`InMemoryGraphEventJournal`] and a store-backed
//!   [`StoreGraphEventJournal`] (stream key = run id).
//! - [`GraphStatusStore`] — a compact "what is running now?" surface over
//!   [`crate::graph::GraphRunStatus`], with an [`InMemoryGraphStatusStore`].
//! - [`JournalGraphSink`] — a [`GraphEventSink`] that wraps each emitted event
//!   into a [`GraphObservation`] and appends it to a journal, optionally also
//!   forwarding to a live `inner` sink.
//! - [`GraphLatencyMetrics`] / [`GraphHealthSummary`] — rollups derived from a
//!   run's observations: per-step/per-node timings, and per-node
//!   success/failure counts (node-level **tool health** telemetry).
//! - [`GraphLangfuseExporter`] — exports a run's observations to Langfuse,
//!   turning supersteps and nodes into timed spans (failures promoted to
//!   `ERROR`) and attaching the health summary to the trace. It shares the
//!   harness [`LangfuseClient`](crate::harness::observability::LangfuseClient)
//!   transport and defaults its `traceId` to the run's `root_run_id`, so a
//!   graph run and the agent/tool runs its nodes spawn land under one trace.
//!
//! [`CompiledGraph`](crate::graph::CompiledGraph) can be wired to write to a
//! status store and a journal through its builder-style
//! [`with_status_store`](crate::graph::CompiledGraph::with_status_store) and
//! [`with_event_journal`](crate::graph::CompiledGraph::with_event_journal)
//! methods; both are opt-in and default off so existing runs are unchanged.
//!
//! The journaling sink bridges the synchronous [`GraphEventSink::emit`] hook to
//! the async journal API through a background drain (an
//! [`AppendWorker`](crate::harness::observability)): `emit` never blocks the
//! executor on I/O and persistence is best-effort (a full bounded queue drops
//! rather than stalls; backend errors are reported, not propagated). The
//! executor calls [`GraphEventSink::flush`] after the terminal run event so a
//! caller reading the journal right after the run returns sees a complete log.

mod langfuse;
mod types;

pub use langfuse::{GraphLangfuseExporter, SpanMetadataFn};
pub use types::*;

use std::collections::{HashMap, VecDeque};
use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::time::SystemTime;

use async_trait::async_trait;

use crate::error::Result;
use crate::graph::status::GraphRunStatus;
use crate::graph::stream::{GraphEvent, GraphEventSink};
use crate::harness::ids::{CheckpointId, EventId, GraphId, NodeId, RunId, ThreadId, now_ms};
use crate::harness::observability::{AppendWorker, DEFAULT_DRAIN_CAPACITY};
use crate::harness::store::AppendStore;

// ---------------------------------------------------------------------------
// GraphLatencyMetrics
// ---------------------------------------------------------------------------

impl GraphLatencyMetrics {
    /// Builds latency rollups from durable observations for one graph run.
    ///
    /// Incomplete steps or node executions are ignored because there is no
    /// terminal timestamp to measure against. Duplicate node activations for the
    /// same `(node, step)` are paired in FIFO order.
    pub fn from_observations(observations: &[GraphObservation]) -> Self {
        let mut metrics = Self::default();
        let mut run_start: Option<u64> = None;
        let mut step_starts: HashMap<usize, u64> = HashMap::new();
        let mut node_starts: HashMap<(NodeId, usize), VecDeque<u64>> = HashMap::new();

        for obs in observations {
            match &obs.event {
                GraphEvent::RunStarted { .. } if run_start.is_none() => {
                    run_start = Some(obs.ts_ms);
                }
                GraphEvent::RunStarted { .. } => {}
                GraphEvent::RunCompleted { .. } | GraphEvent::RunFailed { .. } => {
                    if metrics.run_elapsed_ms.is_none()
                        && let Some(start) = run_start
                    {
                        metrics.run_elapsed_ms = Some(obs.ts_ms.saturating_sub(start));
                    }
                }
                GraphEvent::StepStarted { step, .. } => {
                    step_starts.insert(*step, obs.ts_ms);
                }
                GraphEvent::StepCompleted { step } => {
                    if let Some(start) = step_starts.remove(step) {
                        metrics.record_step(GraphStepLatency {
                            step: *step,
                            elapsed_ms: obs.ts_ms.saturating_sub(start),
                        });
                    }
                }
                GraphEvent::NodeStarted { node, step } => {
                    node_starts
                        .entry((node.clone(), *step))
                        .or_default()
                        .push_back(obs.ts_ms);
                }
                GraphEvent::NodeCompleted { node, step } => {
                    if let Some(start) = pop_node_start(&mut node_starts, node, *step) {
                        metrics.record_node(GraphNodeLatency {
                            node: node.clone(),
                            step: *step,
                            elapsed_ms: obs.ts_ms.saturating_sub(start),
                            failed: false,
                        });
                    }
                }
                GraphEvent::NodeFailed { node, step, .. } => {
                    if let Some(start) = pop_node_start(&mut node_starts, node, *step) {
                        metrics.record_node(GraphNodeLatency {
                            node: node.clone(),
                            step: *step,
                            elapsed_ms: obs.ts_ms.saturating_sub(start),
                            failed: true,
                        });
                    }
                }
                _ => {}
            }
        }

        metrics
    }

    /// Builds a run-level latency summary from a compact status snapshot.
    ///
    /// Status snapshots do not contain per-step or per-node timings, but they
    /// do carry started/updated/ended timestamps for end-to-end elapsed time.
    pub fn from_status(status: &GraphRunStatus) -> Self {
        let end = status.ended_at.unwrap_or(status.updated_at);
        Self {
            run_elapsed_ms: duration_ms(status.started_at, end),
            ..Self::default()
        }
    }

    /// Average completed-step latency.
    pub fn average_step_ms(&self) -> Option<u64> {
        average(self.total_step_ms, self.steps.len())
    }

    /// Average completed-node latency.
    pub fn average_node_ms(&self) -> Option<u64> {
        average(self.total_node_ms, self.nodes.len())
    }

    fn record_step(&mut self, latency: GraphStepLatency) {
        self.total_step_ms = self.total_step_ms.saturating_add(latency.elapsed_ms);
        self.max_step_ms = self.max_step_ms.max(latency.elapsed_ms);
        self.steps.push(latency);
    }

    fn record_node(&mut self, latency: GraphNodeLatency) {
        self.total_node_ms = self.total_node_ms.saturating_add(latency.elapsed_ms);
        self.max_node_ms = self.max_node_ms.max(latency.elapsed_ms);
        self.nodes.push(latency);
    }
}

// ---------------------------------------------------------------------------
// GraphHealthSummary
// ---------------------------------------------------------------------------

impl GraphHealthSummary {
    /// Builds a node/tool health rollup from durable observations for one run.
    ///
    /// Counts every `node.started`, `node.completed`, and `node.failed`
    /// observation per node, plus whether the run itself failed. Per-node
    /// entries are sorted by node id so the summary is deterministic.
    pub fn from_observations(observations: &[GraphObservation]) -> Self {
        let mut per_node: HashMap<NodeId, GraphNodeHealth> = HashMap::new();
        let mut summary = Self::default();

        for obs in observations {
            match &obs.event {
                GraphEvent::NodeStarted { node, .. } => {
                    entry_for(&mut per_node, node).started += 1;
                    summary.total_started += 1;
                }
                GraphEvent::NodeCompleted { node, .. } => {
                    entry_for(&mut per_node, node).completed += 1;
                    summary.total_completed += 1;
                }
                GraphEvent::NodeFailed { node, .. } => {
                    entry_for(&mut per_node, node).failed += 1;
                    summary.total_failed += 1;
                }
                GraphEvent::RunFailed { .. } => {
                    summary.run_failed = true;
                }
                _ => {}
            }
        }

        summary.nodes = per_node.into_values().collect();
        summary
            .nodes
            .sort_by(|a, b| a.node.as_str().cmp(b.node.as_str()));
        summary
    }
}

/// Returns the mutable health entry for `node`, inserting a zeroed one keyed by
/// the node id if absent.
fn entry_for<'a>(
    per_node: &'a mut HashMap<NodeId, GraphNodeHealth>,
    node: &NodeId,
) -> &'a mut GraphNodeHealth {
    per_node
        .entry(node.clone())
        .or_insert_with(|| GraphNodeHealth {
            node: node.clone(),
            started: 0,
            completed: 0,
            failed: 0,
        })
}

// ---------------------------------------------------------------------------
// InMemoryGraphEventJournal
// ---------------------------------------------------------------------------

impl InMemoryGraphEventJournal {
    /// Creates a new, empty in-memory journal.
    pub fn new() -> Self {
        Self::default()
    }

    /// Returns the number of observations stored for `run_id`.
    pub fn len(&self, run_id: &str) -> usize {
        self.runs
            .lock()
            .expect("InMemoryGraphEventJournal lock poisoned")
            .get(run_id)
            .map(|v| v.len())
            .unwrap_or(0)
    }

    /// Returns `true` when no observations are stored for `run_id`.
    pub fn is_empty(&self, run_id: &str) -> bool {
        self.len(run_id) == 0
    }
}

#[async_trait]
impl GraphEventJournal for InMemoryGraphEventJournal {
    async fn append(&self, obs: GraphObservation) -> Result<u64> {
        let mut runs = self
            .runs
            .lock()
            .map_err(|e| poisoned("InMemoryGraphEventJournal", e))?;
        let entries = runs.entry(obs.run_id.as_str().to_string()).or_default();
        let offset = entries.len() as u64;
        entries.push(obs);
        Ok(offset)
    }

    async fn read_from(&self, run_id: &str, offset: u64) -> Result<Vec<GraphObservation>> {
        let runs = self
            .runs
            .lock()
            .map_err(|e| poisoned("InMemoryGraphEventJournal", e))?;
        let Some(entries) = runs.get(run_id) else {
            return Ok(Vec::new());
        };
        Ok(entries.iter().skip(offset as usize).cloned().collect())
    }
}

// ---------------------------------------------------------------------------
// StoreGraphEventJournal
// ---------------------------------------------------------------------------

impl<A: AppendStore> StoreGraphEventJournal<A> {
    /// Wraps `store` as a graph event journal whose stream key is the run id.
    pub fn new(store: A) -> Self {
        Self { store }
    }

    /// Returns a reference to the backing store.
    pub fn store(&self) -> &A {
        &self.store
    }
}

#[async_trait]
impl<A: AppendStore + 'static> GraphEventJournal for StoreGraphEventJournal<A> {
    async fn append(&self, obs: GraphObservation) -> Result<u64> {
        let stream = obs.run_id.as_str().to_string();
        let value = serde_json::to_value(&obs)?;
        self.store.append(&stream, value).await
    }

    async fn read_from(&self, run_id: &str, offset: u64) -> Result<Vec<GraphObservation>> {
        let raw = self.store.read_from(run_id, offset).await?;
        let mut out = Vec::with_capacity(raw.len());
        for (_offset, value) in raw {
            out.push(serde_json::from_value(value)?);
        }
        Ok(out)
    }
}

// ---------------------------------------------------------------------------
// InMemoryGraphStatusStore
// ---------------------------------------------------------------------------

impl InMemoryGraphStatusStore {
    /// Creates a new, empty, **unbounded** in-memory status store.
    pub fn new() -> Self {
        Self::default()
    }

    /// Caps the store at `max` distinct runs.
    ///
    /// Once a `put_status` for a *new* run pushes the store past the cap, the
    /// oldest **terminal** run (completed / failed / cancelled) is evicted
    /// first; when every retained run is still live, the oldest run overall is
    /// evicted. Overwriting an already-recorded run never triggers eviction. A
    /// `max` of `0` retains nothing. The default (via [`Self::new`] /
    /// [`Default`]) is unbounded.
    pub fn with_max_runs(mut self, max: usize) -> Self {
        self.max_runs = Some(max);
        self
    }

    /// Returns the number of distinct runs with a recorded status.
    pub fn len(&self) -> usize {
        self.state
            .lock()
            .expect("InMemoryGraphStatusStore lock poisoned")
            .statuses
            .len()
    }

    /// Returns `true` when no statuses have been recorded.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl StatusStoreState {
    /// Removes `run_id` from the thread index, dropping the thread's bucket
    /// when it becomes empty.
    fn unindex_thread(&mut self, thread_id: &str, run_id: &str) {
        if let Some(runs) = self.by_thread.get_mut(thread_id) {
            runs.retain(|id| id != run_id);
            if runs.is_empty() {
                self.by_thread.remove(thread_id);
            }
        }
    }

    /// Evicts one run: the oldest terminal run if any, otherwise the oldest
    /// run overall. No-op when the store is empty.
    fn evict_one(&mut self) {
        let idx = self
            .order
            .iter()
            .position(|id| self.statuses.get(id).is_none_or(|s| s.is_terminal()))
            .unwrap_or(0);
        let Some(run_id) = self.order.remove(idx) else {
            return;
        };
        if let Some(evicted) = self.statuses.remove(&run_id)
            && let Some(thread_id) = evicted.thread_id.as_ref()
        {
            self.unindex_thread(thread_id.as_str(), &run_id);
        }
    }
}

#[async_trait]
impl GraphStatusStore for InMemoryGraphStatusStore {
    async fn put_status(&self, status: GraphRunStatus) -> Result<()> {
        let mut state = self
            .state
            .lock()
            .map_err(|e| poisoned("InMemoryGraphStatusStore", e))?;
        let run_id = status.run_id.as_str().to_string();
        let thread_id = status.thread_id.as_ref().map(|t| t.as_str().to_string());

        match state.statuses.insert(run_id.clone(), status) {
            Some(previous) => {
                // Overwrite: keep the thread index coherent if the thread
                // changed (rare, but cheap to handle).
                let previous_thread = previous.thread_id.as_ref().map(|t| t.as_str().to_string());
                if previous_thread != thread_id {
                    if let Some(old) = previous_thread {
                        state.unindex_thread(&old, &run_id);
                    }
                    if let Some(new) = thread_id {
                        state.by_thread.entry(new).or_default().push(run_id);
                    }
                }
            }
            None => {
                // First status for this run: index it and enforce the cap.
                if let Some(thread) = thread_id {
                    state
                        .by_thread
                        .entry(thread)
                        .or_default()
                        .push(run_id.clone());
                }
                state.order.push_back(run_id);
                if let Some(max) = self.max_runs {
                    while state.statuses.len() > max {
                        state.evict_one();
                    }
                }
            }
        }
        Ok(())
    }

    async fn get_status(&self, run_id: &str) -> Result<Option<GraphRunStatus>> {
        let state = self
            .state
            .lock()
            .map_err(|e| poisoned("InMemoryGraphStatusStore", e))?;
        Ok(state.statuses.get(run_id).cloned())
    }

    async fn list_by_thread(&self, thread_id: &str) -> Result<Vec<GraphRunStatus>> {
        let state = self
            .state
            .lock()
            .map_err(|e| poisoned("InMemoryGraphStatusStore", e))?;
        Ok(state
            .by_thread
            .get(thread_id)
            .map(|runs| {
                runs.iter()
                    .filter_map(|id| state.statuses.get(id).cloned())
                    .collect()
            })
            .unwrap_or_default())
    }
}

// ---------------------------------------------------------------------------
// JournalGraphSink
// ---------------------------------------------------------------------------

impl JournalGraphSink {
    /// Builds a journal sink for `run_id` of `graph_id`. `root_run_id` defaults
    /// to `run_id` (a top-level run) and the namespace is empty; use the
    /// builder methods to set a parent, root, thread, namespace, or downstream
    /// sink.
    pub fn new(journal: Arc<dyn GraphEventJournal>, run_id: RunId, graph_id: GraphId) -> Self {
        let worker = Arc::new(AppendWorker::spawn(
            "graph-journal-sink",
            DEFAULT_DRAIN_CAPACITY,
            move |obs: GraphObservation| {
                let journal = Arc::clone(&journal);
                async move { journal.append(obs).await.map(|_| ()) }
            },
        ));
        Self {
            worker,
            inner: None,
            root_run_id: run_id.clone(),
            run_id,
            parent_run_id: None,
            thread_id: None,
            graph_id,
            namespace: Vec::new(),
            offset: Arc::new(std::sync::atomic::AtomicU64::new(0)),
            step: Arc::new(std::sync::atomic::AtomicU64::new(0)),
        }
    }

    /// Sets the parent and root run ids stamped onto every observation.
    pub fn with_lineage(mut self, parent_run_id: Option<RunId>, root_run_id: RunId) -> Self {
        self.parent_run_id = parent_run_id;
        self.root_run_id = root_run_id;
        self
    }

    /// Sets the thread id stamped onto every observation.
    pub fn with_thread(mut self, thread_id: Option<ThreadId>) -> Self {
        self.thread_id = thread_id;
        self
    }

    /// Sets the checkpoint namespace stamped onto every observation.
    ///
    /// A subgraph sink is given the child namespace here so its observations
    /// carry the nested path.
    pub fn with_namespace(mut self, namespace: Vec<String>) -> Self {
        self.namespace = namespace;
        self
    }

    /// Forwards every event to `inner` in addition to journaling it, so one
    /// configured sink can both persist and broadcast.
    pub fn with_inner(mut self, inner: Arc<dyn GraphEventSink>) -> Self {
        self.inner = Some(inner);
        self
    }

    /// Builds the durable observation for `event`, advancing the offset and the
    /// latest-step trackers.
    fn observe(&self, event: &GraphEvent) -> GraphObservation {
        let offset = self.offset.fetch_add(1, Ordering::Relaxed);
        // Track the latest superstep so events without a step (route, run
        // lifecycle) still carry the step they happened during.
        let step = match event.step() {
            Some(step) => {
                self.step.store(step as u64, Ordering::Relaxed);
                step
            }
            None => self.step.load(Ordering::Relaxed) as usize,
        };
        GraphObservation {
            event_id: EventId::new(format!("{}-{offset}", self.run_id.as_str())),
            run_id: self.run_id.clone(),
            root_run_id: self.root_run_id.clone(),
            parent_run_id: self.parent_run_id.clone(),
            thread_id: self.thread_id.clone(),
            graph_id: self.graph_id.clone(),
            checkpoint_id: checkpoint_of(event),
            namespace: self.namespace.clone(),
            step,
            offset,
            ts_ms: now_ms(),
            event: event.clone(),
        }
    }
}

impl GraphEventSink for JournalGraphSink {
    fn emit(&self, event: GraphEvent) {
        let obs = self.observe(&event);
        // Hand off to the background drain; never block the executor on I/O.
        self.worker.submit(obs);
        if let Some(inner) = &self.inner {
            inner.emit(event);
        }
    }

    fn flush(&self) {
        self.worker.flush();
        if let Some(inner) = &self.inner {
            inner.flush();
        }
    }
}

/// Extracts the checkpoint id a [`GraphEvent::CheckpointSaved`] carries, so the
/// observation envelope can record it directly.
fn checkpoint_of(event: &GraphEvent) -> Option<CheckpointId> {
    match event {
        GraphEvent::CheckpointSaved { checkpoint_id } => Some(checkpoint_id.clone()),
        _ => None,
    }
}

// ---------------------------------------------------------------------------
// Shared helpers
// ---------------------------------------------------------------------------

fn pop_node_start(
    starts: &mut HashMap<(NodeId, usize), VecDeque<u64>>,
    node: &NodeId,
    step: usize,
) -> Option<u64> {
    let key = (node.clone(), step);
    let queue = starts.get_mut(&key)?;
    let start = queue.pop_front();
    if queue.is_empty() {
        starts.remove(&key);
    }
    start
}

fn average(total: u64, count: usize) -> Option<u64> {
    (count > 0).then_some(total / count as u64)
}

fn duration_ms(start: SystemTime, end: SystemTime) -> Option<u64> {
    end.duration_since(start)
        .ok()
        .map(|duration| duration.as_millis() as u64)
}

/// Builds a uniform poisoned-lock validation error for the in-memory backends.
fn poisoned<E: std::fmt::Display>(what: &str, err: E) -> crate::error::TinyAgentsError {
    crate::error::TinyAgentsError::Validation(format!("{what} lock poisoned: {err}"))
}

#[cfg(test)]
mod test;