everruns 0.17.26

Build and run durable AI agents in Rust — the application-facing entrypoint to the Everruns agentic framework
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
//! Typed, in-process lifecycle hooks for Framework applications.
//!
//! Hooks are awaited extension points attached to an [`Agent`](crate::Agent),
//! not a second event subscription API. Applications register async closures
//! on [`AgentBuilder`](crate::AgentBuilder); each closure receives a small,
//! owned Framework context and may perform application work without importing
//! `everruns-core` or `everruns-runtime`.
//!
//! The contract is intentionally non-mutating: hooks cannot rewrite prompts,
//! tool arguments, tool results, or turn outcomes. A failing pre-effect hook
//! prevents the affected work; a failing post-effect hook cannot roll work
//! back, so the failure is isolated and surfaced through
//! [`Turn::hook_failures`](crate::Turn::hook_failures).

use std::fmt;
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, Mutex};

use async_trait::async_trait;
use everruns_core::atoms::{PreToolUseDecision, PreToolUseHook};
use everruns_core::capabilities::Capability;
use everruns_core::{PostToolExecHook, ToolCall, ToolContext, ToolDefinition, ToolResult};
use serde_json::Value;

pub(crate) const LIFECYCLE_HOOK_CAPABILITY_ID: &str = "__everruns_framework_lifecycle_hooks";

/// The boxed future returned by one type-erased lifecycle handler.
type HookFuture = Pin<Box<dyn Future<Output = Result<(), String>> + Send>>;

/// One async lifecycle handler receiving an owned, typed context.
type Handler<T> = Arc<dyn Fn(T) -> HookFuture + Send + Sync>;

/// Convert a lifecycle handler's output into the Framework hook result.
///
/// Async closures may return `()` when they are infallible, or
/// `Result<(), E>` for any displayable error. This keeps simple hooks concise
/// while allowing fallible hooks to use their application's own error type.
/// Applications normally rely on those implementations without naming this
/// trait; an application-owned output type may implement it for custom result
/// conversion.
pub trait IntoHookResult {
    /// Convert this handler output into a string-backed hook result.
    fn into_hook_result(self) -> Result<(), String>;
}

impl IntoHookResult for () {
    fn into_hook_result(self) -> Result<(), String> {
        Ok(())
    }
}

impl<E: fmt::Display> IntoHookResult for Result<(), E> {
    fn into_hook_result(self) -> Result<(), String> {
        self.map_err(|error| error.to_string())
    }
}

fn handler<T, F, Fut, O>(callback: F) -> Handler<T>
where
    T: Send + 'static,
    F: Fn(T) -> Fut + Send + Sync + 'static,
    Fut: Future<Output = O> + Send + 'static,
    O: IntoHookResult + 'static,
{
    Arc::new(move |context| {
        let future = callback(context);
        Box::pin(async move { future.await.into_hook_result() })
    })
}

/// A typed Framework lifecycle point.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum HookPoint {
    /// Before the first turn attempted by one session.
    AgentStart,
    /// Before one turn is handed to the runtime.
    TurnStart,
    /// Before one model-requested tool call executes.
    ToolStart,
    /// After one tool call reaches a terminal result, including a blocked call.
    ToolEnd,
    /// After one non-cancelled turn reaches a terminal outcome.
    Completion,
}

impl fmt::Display for HookPoint {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(match self {
            HookPoint::AgentStart => "agent_start",
            HookPoint::TurnStart => "turn_start",
            HookPoint::ToolStart => "tool_start",
            HookPoint::ToolEnd => "tool_end",
            HookPoint::Completion => "completion",
        })
    }
}

/// A lifecycle handler failure surfaced by the Framework.
///
/// `handler_index` is zero-based within one lifecycle point and follows
/// builder registration order. Tool failures also carry the relevant call
/// identity. No prompt, credential, or unrelated tool payload is copied into
/// the failure.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct HookFailure {
    /// Lifecycle point whose handler failed.
    pub point: HookPoint,
    /// Zero-based registration index within `point`.
    pub handler_index: usize,
    /// Display text returned by the handler's error.
    ///
    /// Tool-start failures use a generic model-visible error; this detailed
    /// message remains application-facing.
    pub message: String,
    /// Tool name for tool lifecycle points.
    pub tool_name: Option<String>,
    /// Tool-call id for tool lifecycle points.
    pub tool_call_id: Option<String>,
}

impl HookFailure {
    fn new(point: HookPoint, handler_index: usize, message: String) -> Self {
        Self {
            point,
            handler_index,
            message,
            tool_name: None,
            tool_call_id: None,
        }
    }

    fn for_tool(
        point: HookPoint,
        handler_index: usize,
        message: String,
        tool_name: &str,
        tool_call_id: &str,
    ) -> Self {
        Self {
            point,
            handler_index,
            message,
            tool_name: Some(tool_name.to_string()),
            tool_call_id: Some(tool_call_id.to_string()),
        }
    }
}

impl fmt::Display for HookFailure {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "{} hook #{} failed: {}",
            self.point, self.handler_index, self.message
        )
    }
}

impl std::error::Error for HookFailure {}

/// Context passed to an `on_agent_start` handler.
///
/// An agent starts independently in every [`Session`](crate::Session), just
/// before that session's first turn. If the chain fails, the next run retries
/// the whole chain; handlers with external effects should therefore be
/// idempotent.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct AgentStartContext {
    /// Human-readable agent name.
    pub agent_name: String,
    /// Typed identity of the session beginning execution.
    pub session_id: crate::SessionId,
}

/// Context passed to an `on_turn_start` handler.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct TurnStartContext {
    /// Human-readable agent name.
    pub agent_name: String,
    /// Typed identity of the session running the turn.
    pub session_id: crate::SessionId,
    /// Exact input that will be handed to the runtime after the hook chain.
    pub input: crate::InputMessage,
}

/// Context passed to an `on_tool_start` handler.
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct ToolStartContext {
    /// Typed identity of the session running the tool.
    pub session_id: crate::SessionId,
    /// Opaque parent turn id when available.
    pub turn_id: Option<String>,
    /// Opaque id of this tool call.
    pub tool_call_id: String,
    /// Model-facing tool name.
    pub tool_name: String,
    /// Tool arguments that will be passed to the tool implementation.
    pub arguments: Value,
}

/// Context passed to an `on_tool_end` handler.
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct ToolEndContext {
    /// Typed identity of the session running the tool.
    pub session_id: crate::SessionId,
    /// Opaque parent turn id when available.
    pub turn_id: Option<String>,
    /// Opaque id of this tool call.
    pub tool_call_id: String,
    /// Model-facing tool name.
    pub tool_name: String,
    /// Tool arguments selected for execution. A blocked call carries the
    /// arguments it would have executed.
    pub arguments: Value,
    /// Structured success value, when the tool produced one.
    pub result: Option<Value>,
    /// Tool error text, when execution failed.
    pub error: Option<String>,
}

impl ToolEndContext {
    /// Whether the tool returned without an error.
    pub fn success(&self) -> bool {
        self.error.is_none()
    }
}

/// Context passed to an `on_completion` handler.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct CompletionContext {
    /// Human-readable agent name.
    pub agent_name: String,
    /// Typed identity of the session whose turn completed.
    pub session_id: crate::SessionId,
    /// Stable Framework projection of the terminal turn.
    pub turn: crate::Turn,
}

/// Ordered callback lists attached to a built agent.
#[derive(Clone, Default)]
pub(crate) struct LifecycleHooks {
    agent_start: Vec<Handler<AgentStartContext>>,
    turn_start: Vec<Handler<TurnStartContext>>,
    tool_start: Vec<Handler<ToolStartContext>>,
    tool_end: Vec<Handler<ToolEndContext>>,
    completion: Vec<Handler<CompletionContext>>,
}

impl fmt::Debug for LifecycleHooks {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("LifecycleHooks")
            .field("agent_start", &self.agent_start.len())
            .field("turn_start", &self.turn_start.len())
            .field("tool_start", &self.tool_start.len())
            .field("tool_end", &self.tool_end.len())
            .field("completion", &self.completion.len())
            .finish()
    }
}

impl LifecycleHooks {
    pub(crate) fn on_agent_start<F, Fut, O>(&mut self, callback: F)
    where
        F: Fn(AgentStartContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = O> + Send + 'static,
        O: IntoHookResult + 'static,
    {
        self.agent_start.push(handler(callback));
    }

    pub(crate) fn on_turn_start<F, Fut, O>(&mut self, callback: F)
    where
        F: Fn(TurnStartContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = O> + Send + 'static,
        O: IntoHookResult + 'static,
    {
        self.turn_start.push(handler(callback));
    }

    pub(crate) fn on_tool_start<F, Fut, O>(&mut self, callback: F)
    where
        F: Fn(ToolStartContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = O> + Send + 'static,
        O: IntoHookResult + 'static,
    {
        self.tool_start.push(handler(callback));
    }

    pub(crate) fn on_tool_end<F, Fut, O>(&mut self, callback: F)
    where
        F: Fn(ToolEndContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = O> + Send + 'static,
        O: IntoHookResult + 'static,
    {
        self.tool_end.push(handler(callback));
    }

    pub(crate) fn on_completion<F, Fut, O>(&mut self, callback: F)
    where
        F: Fn(CompletionContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = O> + Send + 'static,
        O: IntoHookResult + 'static,
    {
        self.completion.push(handler(callback));
    }

    pub(crate) fn has_tool_hooks(&self) -> bool {
        !self.tool_start.is_empty() || !self.tool_end.is_empty()
    }

    pub(crate) async fn run_agent_start(
        &self,
        context: AgentStartContext,
    ) -> Result<(), HookFailure> {
        run_pre_effect(&self.agent_start, context, HookPoint::AgentStart).await
    }

    pub(crate) async fn run_turn_start(
        &self,
        context: TurnStartContext,
    ) -> Result<(), HookFailure> {
        run_pre_effect(&self.turn_start, context, HookPoint::TurnStart).await
    }

    pub(crate) async fn run_completion(&self, context: CompletionContext) -> Vec<HookFailure> {
        let mut failures = Vec::new();
        for (handler_index, handler) in self.completion.iter().enumerate() {
            if let Err(message) = handler(context.clone()).await {
                failures.push(HookFailure::new(
                    HookPoint::Completion,
                    handler_index,
                    message,
                ));
            }
        }
        failures
    }
}

async fn run_pre_effect<T: Clone>(
    handlers: &[Handler<T>],
    context: T,
    point: HookPoint,
) -> Result<(), HookFailure> {
    for (handler_index, handler) in handlers.iter().enumerate() {
        if let Err(message) = handler(context.clone()).await {
            return Err(HookFailure::new(point, handler_index, message));
        }
    }
    Ok(())
}

/// Per-session state shared with the runtime's tool-hook adapters.
pub(crate) struct HookRunState {
    hooks: LifecycleHooks,
    failures: Mutex<Vec<HookFailure>>,
}

impl HookRunState {
    pub(crate) fn new(hooks: LifecycleHooks) -> Arc<Self> {
        Arc::new(Self {
            hooks,
            failures: Mutex::new(Vec::new()),
        })
    }

    pub(crate) fn hooks(&self) -> &LifecycleHooks {
        &self.hooks
    }

    pub(crate) fn begin_turn(&self) {
        self.failures
            .lock()
            .expect("hook failures mutex poisoned")
            .clear();
    }

    pub(crate) fn take_failures(&self) -> Vec<HookFailure> {
        std::mem::take(&mut *self.failures.lock().expect("hook failures mutex poisoned"))
    }

    fn record(&self, failure: HookFailure) {
        self.failures
            .lock()
            .expect("hook failures mutex poisoned")
            .push(failure);
    }

    pub(crate) fn capability(self: &Arc<Self>) -> Option<LifecycleHookCapability> {
        self.hooks
            .has_tool_hooks()
            .then(|| LifecycleHookCapability {
                state: self.clone(),
            })
    }
}

/// Private bridge from Framework closures to the runtime's in-process tool
/// hook seams. The application never constructs a capability or persisted hook
/// record.
pub(crate) struct LifecycleHookCapability {
    state: Arc<HookRunState>,
}

impl Capability for LifecycleHookCapability {
    fn id(&self) -> &str {
        LIFECYCLE_HOOK_CAPABILITY_ID
    }

    fn name(&self) -> &str {
        "Framework lifecycle hooks"
    }

    fn description(&self) -> &str {
        "Runs typed in-process lifecycle handlers registered by the application."
    }

    fn pre_tool_use_hooks(&self) -> Vec<Arc<dyn PreToolUseHook>> {
        if self.state.hooks.tool_start.is_empty() {
            Vec::new()
        } else {
            vec![Arc::new(ToolStartHook {
                state: self.state.clone(),
            })]
        }
    }

    fn post_tool_exec_hooks(&self) -> Vec<Arc<dyn PostToolExecHook>> {
        if self.state.hooks.tool_end.is_empty() {
            Vec::new()
        } else {
            vec![Arc::new(ToolEndHook {
                state: self.state.clone(),
            })]
        }
    }
}

struct ToolStartHook {
    state: Arc<HookRunState>,
}

#[async_trait]
impl PreToolUseHook for ToolStartHook {
    async fn before_exec(
        &self,
        tool_call: ToolCall,
        _tool_def: &ToolDefinition,
        context: &ToolContext,
    ) -> PreToolUseDecision {
        let hook_context = ToolStartContext {
            session_id: context.session_id,
            turn_id: context
                .event_context
                .as_ref()
                .and_then(|event| event.turn_id)
                .map(|turn_id| turn_id.to_string()),
            tool_call_id: tool_call.id.clone(),
            tool_name: tool_call.name.clone(),
            arguments: tool_call.execution_arguments(),
        };
        for (handler_index, handler) in self.state.hooks.tool_start.iter().enumerate() {
            if let Err(message) = handler(hook_context.clone()).await {
                let failure = HookFailure::for_tool(
                    HookPoint::ToolStart,
                    handler_index,
                    message,
                    &tool_call.name,
                    &tool_call.id,
                );
                // The runtime turns this reason into model-visible tool
                // output. Keep application error details on `HookFailure` so
                // credentials or backend diagnostics are not sent to the LLM.
                let reason = format!(
                    "tool call blocked by {} hook #{}",
                    failure.point, failure.handler_index
                );
                self.state.record(failure);
                return PreToolUseDecision::Block {
                    tool_call,
                    reason,
                    user_message: None,
                };
            }
        }
        PreToolUseDecision::Continue(tool_call)
    }
}

struct ToolEndHook {
    state: Arc<HookRunState>,
}

#[async_trait]
impl PostToolExecHook for ToolEndHook {
    async fn after_exec(
        &self,
        tool_call: &ToolCall,
        _tool_def: &ToolDefinition,
        result: &mut ToolResult,
        context: &ToolContext,
    ) {
        let hook_context = ToolEndContext {
            session_id: context.session_id,
            turn_id: context
                .event_context
                .as_ref()
                .and_then(|event| event.turn_id)
                .map(|turn_id| turn_id.to_string()),
            tool_call_id: tool_call.id.clone(),
            tool_name: tool_call.name.clone(),
            arguments: tool_call.execution_arguments(),
            result: result.result.clone(),
            error: result.error.clone(),
        };
        for (handler_index, handler) in self.state.hooks.tool_end.iter().enumerate() {
            if let Err(message) = handler(hook_context.clone()).await {
                self.state.record(HookFailure::for_tool(
                    HookPoint::ToolEnd,
                    handler_index,
                    message,
                    &tool_call.name,
                    &tool_call.id,
                ));
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use std::sync::atomic::{AtomicUsize, Ordering};

    use super::*;

    #[tokio::test]
    async fn pre_effect_hooks_run_in_order_and_stop_on_error() {
        let seen = Arc::new(Mutex::new(Vec::new()));
        let mut hooks = LifecycleHooks::default();
        for (label, fails) in [("first", false), ("second", true), ("third", false)] {
            let seen = seen.clone();
            hooks.on_agent_start(move |_context| {
                let seen = seen.clone();
                async move {
                    seen.lock().unwrap().push(label);
                    if fails { Err("broken") } else { Ok(()) }
                }
            });
        }

        let failure = hooks
            .run_agent_start(AgentStartContext {
                agent_name: "agent".into(),
                session_id: crate::SessionId::new(),
            })
            .await
            .expect_err("second hook fails");

        assert_eq!(*seen.lock().unwrap(), ["first", "second"]);
        assert_eq!(failure.point, HookPoint::AgentStart);
        assert_eq!(failure.handler_index, 1);
        assert_eq!(failure.message, "broken");
    }

    #[tokio::test]
    async fn post_effect_hooks_isolate_errors_and_continue() {
        let calls = Arc::new(AtomicUsize::new(0));
        let mut hooks = LifecycleHooks::default();
        hooks.on_completion(|_context| async move { Err::<(), _>("broken") });
        let later = calls.clone();
        hooks.on_completion(move |_context| {
            let later = later.clone();
            async move {
                later.fetch_add(1, Ordering::SeqCst);
            }
        });

        let failures = hooks
            .run_completion(CompletionContext {
                agent_name: "agent".into(),
                session_id: crate::SessionId::new(),
                turn: crate::Turn::cancelled(everruns_core::typed_id::TurnId::new()),
            })
            .await;

        assert_eq!(calls.load(Ordering::SeqCst), 1);
        assert_eq!(failures.len(), 1);
        assert_eq!(failures[0].point, HookPoint::Completion);
        assert_eq!(failures[0].handler_index, 0);
    }

    #[test]
    fn empty_hooks_do_not_register_a_runtime_capability() {
        let state = HookRunState::new(LifecycleHooks::default());
        assert!(state.capability().is_none());
    }
}