everruns 0.18.0

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
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
//! Stable service-provider interface for advanced, code-defined capabilities.
//!
//! Most application tools should use [`#[everruns::tool]`](macro@crate::tool).
//! This module is for reusable capability packages that need several typed
//! tools, capability-level instructions and metadata, execution context,
//! progress events, or call-scoped cancellation. It deliberately projects the
//! engine contracts needed by capability authors without exposing registries,
//! stores, tenancy, or host implementation types.
//!
//! The authoring types are the neutral `everruns-capability` contract
//! (EVE-873), re-exported here at their stable Framework paths. Capability
//! packages that want a dependency-light build can depend on
//! `everruns-capability` directly (its `definition` module is this same
//! surface); applications keep depending only on `everruns`. This module
//! additionally owns the private adapters that install a [`Definition`] onto
//! the in-process runtime.
//!
//! # Example
//!
//! ```
//! use everruns::{Agent, Model, capability};
//!
//! #[derive(capability::Deserialize, capability::JsonSchema)]
//! #[serde(crate = "everruns::capability::serde")]
//! #[schemars(crate = "everruns::capability::schemars")]
//! struct LookupInput {
//!     city: String,
//! }
//!
//! #[derive(capability::Serialize, capability::JsonSchema)]
//! #[serde(crate = "everruns::capability::serde")]
//! #[schemars(crate = "everruns::capability::schemars")]
//! struct LookupOutput {
//!     city: String,
//!     forecast: String,
//! }
//!
//! struct Lookup;
//!
//! #[capability::async_trait]
//! impl capability::Handler for Lookup {
//!     type Input = LookupInput;
//!     type Output = LookupOutput;
//!     type Error = capability::Error;
//!
//!     fn name(&self) -> &str { "lookup_weather" }
//!     fn description(&self) -> &str { "Look up the weather for a city." }
//!
//!     async fn execute(
//!         &self,
//!         input: Self::Input,
//!         context: capability::Context,
//!     ) -> Result<Self::Output, Self::Error> {
//!         context.progress("Looking up the forecast").await;
//!         Ok(LookupOutput {
//!             city: input.city,
//!             forecast: "sunny".into(),
//!         })
//!     }
//! }
//!
//! let weather = capability::Definition::new(
//!     "weather",
//!     "Weather",
//!     "Typed weather lookup tools.",
//! )
//! .instructions("Use weather data only when a user asks about a location.")
//! .tool(Lookup);
//!
//! let agent = Agent::builder()
//!     .instructions("You are a concise assistant.")
//!     .model(Model::simulated("done"))
//!     .capability(weather)
//!     .build()?;
//! # let _ = agent;
//! # Ok::<(), everruns::BuildError>(())
//! ```

#![deny(missing_docs)]

use std::sync::Arc;

use everruns_core::capabilities::Capability as CoreCapability;
use everruns_core::tool_context::ToolContext;
use everruns_core::tools::{Tool as CoreTool, ToolExecutionResult};
use everruns_provider::tool_types::ToolHints;
use serde_json::{Value, json};

pub use everruns_capability::definition::{
    CallCancellation, CancellationSignal, Context, Definition, Deserialize, Error, ErrorVisibility,
    Handler, Hints, JsonSchema, ProgressSink, Serialize, Tool, ToolSpec, async_trait, schemars,
    serde, serde_json,
};

// ============================================================================
// Runtime adapters (private): neutral contract -> in-process engine
// ============================================================================

/// Adapt a neutral [`Definition`] into the engine's `Capability` trait for
/// private registration on this agent's runtime.
pub(crate) fn runtime_adapter(definition: &Definition) -> RuntimeDefinition {
    RuntimeDefinition(definition.clone())
}

pub(crate) struct RuntimeDefinition(Definition);

#[async_trait]
impl CoreCapability for RuntimeDefinition {
    fn id(&self) -> &str {
        self.0.id()
    }

    fn name(&self) -> &str {
        self.0.name()
    }

    fn description(&self) -> &str {
        self.0.description()
    }

    fn system_prompt_addition(&self) -> Option<&str> {
        self.0.instructions_text()
    }

    fn metadata(&self) -> Option<Value> {
        self.0.metadata_value().cloned()
    }

    fn tools(&self) -> Vec<Box<dyn CoreTool>> {
        self.0
            .tools()
            .iter()
            .cloned()
            .map(|tool| Box::new(CoreToolAdapter(tool)) as Box<dyn CoreTool>)
            .collect()
    }
}

/// Adapt one neutral capability [`Tool`] onto the engine tool contract,
/// bridging execution context, progress, cancellation, and structured errors.
pub(crate) struct CoreToolAdapter(pub(crate) Tool);

#[async_trait]
impl CoreTool for CoreToolAdapter {
    fn name(&self) -> &str {
        self.0.spec().name()
    }

    fn display_name(&self) -> Option<&str> {
        self.0.spec().display_name()
    }

    fn description(&self) -> &str {
        self.0.spec().description()
    }

    fn parameters_schema(&self) -> Value {
        self.0.spec().input_schema().clone()
    }

    fn hints(&self) -> ToolHints {
        hints_to_core(self.0.spec().hints())
    }

    async fn execute(&self, _arguments: Value) -> ToolExecutionResult {
        ToolExecutionResult::internal_error_msg(
            "advanced capability tool execution requires runtime context",
        )
    }

    async fn execute_with_context(
        &self,
        arguments: Value,
        context: &ToolContext,
    ) -> ToolExecutionResult {
        let context = context_from_core(self.0.spec().name(), context);
        match self.0.invoke(arguments, context).await {
            Ok(output) => ToolExecutionResult::success(output),
            Err(error) => error_into_execution_result(error),
        }
    }

    fn requires_context(&self) -> bool {
        true
    }
}

fn hints_to_core(hints: &Hints) -> ToolHints {
    ToolHints {
        readonly: hints.readonly,
        destructive: hints.destructive,
        idempotent: hints.idempotent,
        open_world: hints.open_world,
        long_running: hints.long_running,
        concurrency_class: hints.concurrency_class.clone(),
        metadata: hints.metadata.clone(),
        ..ToolHints::default()
    }
}

/// Build the neutral execution [`Context`] from the engine's `ToolContext`.
fn context_from_core(tool_name: &str, inner: &ToolContext) -> Context {
    Context::new(
        tool_name,
        inner.session_id.to_string(),
        inner.workspace_id.to_string(),
    )
    .with_locale(inner.locale.clone())
    .with_progress_sink(Arc::new(CoreProgressSink {
        tool_name: tool_name.to_string(),
        inner: inner.clone(),
    }))
    .with_cancellation_signal(Arc::new(TokenCancellationSignal(
        inner.cancellation.clone().unwrap_or_default(),
    )))
}

struct CoreProgressSink {
    tool_name: String,
    inner: ToolContext,
}

#[async_trait]
impl ProgressSink for CoreProgressSink {
    async fn emit(&self, tool_name: &str, message: &str) {
        // Prefer the tool name the context was armed with; the neutral layer
        // passes the same value through.
        let name = if tool_name.is_empty() {
            &self.tool_name
        } else {
            tool_name
        };
        self.inner.emit_progress(name, message).await;
    }
}

struct TokenCancellationSignal(tokio_util::sync::CancellationToken);

impl CancellationSignal for TokenCancellationSignal {
    fn is_cancelled(&self) -> bool {
        self.0.is_cancelled()
    }

    fn cancelled<'a>(&'a self) -> everruns_capability::definition::BoxFuture<'a, ()> {
        Box::pin(self.0.cancelled())
    }
}

/// Serialize a structured capability [`Error`] onto the engine's tool result
/// contract, preserving the model-visibility boundary: user errors keep code,
/// message, and details; internal errors are logged and replaced with a
/// generic model-visible message.
fn error_into_execution_result(error: Error) -> ToolExecutionResult {
    let (visibility, code, message, details) = error.into_parts();
    match visibility {
        ErrorVisibility::User => {
            let payload = json!({
                "code": code,
                "message": message,
                "details": details,
            });
            ToolExecutionResult::tool_error(payload.to_string())
        }
        // `ErrorVisibility` is non-exhaustive; treat anything unknown as
        // internal so new variants never leak details to the model.
        _ => ToolExecutionResult::internal_error_msg(format!(
            "capability error [{}]: {}{}",
            code,
            message,
            details
                .map(|details| format!("; details={details}"))
                .unwrap_or_default()
        )),
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::time::Duration;

    use everruns_core::tool_context::ToolContext;
    use everruns_core::tools::Tool as _;
    use everruns_llmsim::LlmSimConfig;
    use everruns_provider::tool_types::ToolCall;
    use everruns_provider::typed_id::SessionId;
    use serde_json::{Value, json};
    use tokio::sync::Notify;
    use tokio::time::timeout;

    use super::*;
    use crate::{
        Agent, CancellationToken, InMemoryEngine, Model, RunOptions, SessionEventKind,
        TurnStopReason,
    };

    #[derive(Deserialize, JsonSchema)]
    struct LookupInput {
        city: String,
    }

    #[derive(Debug, Serialize, JsonSchema)]
    struct LookupOutput {
        city: String,
        temperatures: Vec<i32>,
    }

    struct Lookup;

    #[async_trait]
    impl Handler for Lookup {
        type Input = LookupInput;
        type Output = LookupOutput;
        type Error = Error;

        fn name(&self) -> &str {
            "lookup_weather"
        }

        fn description(&self) -> &str {
            "Look up a typed weather forecast."
        }

        fn hints(&self) -> Hints {
            Hints::default().readonly(true).idempotent(true)
        }

        async fn execute(
            &self,
            input: Self::Input,
            context: Context,
        ) -> Result<Self::Output, Self::Error> {
            context.progress("forecast ready").await;
            Ok(LookupOutput {
                city: input.city,
                temperatures: vec![18, 21],
            })
        }
    }

    fn lookup_capability() -> Definition {
        Definition::new("weather", "Weather", "Typed weather tools.")
            .metadata(json!({ "owner": "example" }))
            .tool(Lookup)
    }

    fn scripted_model(calls: Vec<Vec<ToolCall>>) -> Model {
        let sim = LlmSimConfig::fixed("done").with_tool_call_sequence(calls);
        Model::simulated_with_config(sim)
    }

    #[test]
    fn exposes_input_output_schemas_and_hints() {
        let capability = lookup_capability();
        let spec = capability.tools()[0].spec();

        assert_eq!(spec.name(), "lookup_weather");
        assert_eq!(spec.input_schema()["type"], "object");
        assert_eq!(spec.output_schema()["type"], "object");
        assert_eq!(
            spec.output_schema()["properties"]["temperatures"]["type"],
            "array"
        );
        assert_eq!(spec.hints().readonly, Some(true));
        assert_eq!(spec.hints().idempotent, Some(true));
        assert_eq!(
            capability.metadata_value(),
            Some(&json!({ "owner": "example" }))
        );
    }

    #[tokio::test]
    async fn typed_non_string_result_serializes_as_json() {
        let tool = CoreToolAdapter(lookup_capability().tools()[0].clone());
        let context = ToolContext::new(SessionId::new())
            .with_cancellation(tokio_util::sync::CancellationToken::new());
        let result = tool
            .execute_with_context(json!({ "city": "Kyiv" }), &context)
            .await;

        let ToolExecutionResult::Success(value) = result else {
            panic!("expected success, got {result:?}");
        };
        assert_eq!(value, json!({ "city": "Kyiv", "temperatures": [18, 21] }));
    }

    struct Failing;

    #[async_trait]
    impl Handler for Failing {
        type Input = LookupInput;
        type Output = LookupOutput;
        type Error = Error;

        fn name(&self) -> &str {
            "failing_lookup"
        }

        fn description(&self) -> &str {
            "Return a structured not-found error."
        }

        async fn execute(
            &self,
            input: Self::Input,
            _context: Context,
        ) -> Result<Self::Output, Self::Error> {
            Err(Error::user("city_not_found", "No forecast exists")
                .details(json!({ "city": input.city })))
        }
    }

    #[tokio::test]
    async fn structured_user_error_keeps_code_message_and_details() {
        let capability = Definition::new("failures", "Failures", "Error test.").tool(Failing);
        let tool = CoreToolAdapter(capability.tools()[0].clone());
        let context = ToolContext::new(SessionId::new());
        let result = tool
            .execute_with_context(json!({ "city": "Atlantis" }), &context)
            .await;

        let ToolExecutionResult::ToolError(payload) = result else {
            panic!("expected tool error, got {result:?}");
        };
        let payload: Value = serde_json::from_str(&payload).expect("structured JSON error");
        assert_eq!(payload["code"], "city_not_found");
        assert_eq!(payload["message"], "No forecast exists");
        assert_eq!(payload["details"]["city"], "Atlantis");
    }

    #[test]
    fn internal_error_details_do_not_cross_the_model_boundary() {
        let result = error_into_execution_result(
            Error::internal("upstream_failed", "private endpoint returned token=secret")
                .details(json!({ "trace": "private-trace" })),
        )
        .into_tool_result("call_private", "private_tool");

        let visible = result.error.expect("generic model-visible error");
        assert_eq!(
            visible,
            "An internal error occurred while executing the tool"
        );
        assert!(!visible.contains("secret"));
        assert!(!visible.contains("private-trace"));
    }

    #[tokio::test]
    async fn capability_runs_end_to_end_and_emits_progress() {
        let model = scripted_model(vec![
            vec![ToolCall {
                id: "call_weather".into(),
                name: "lookup_weather".into(),
                arguments: json!({ "city": "Kyiv" }),
            }],
            vec![],
        ]);
        let agent = Agent::builder()
            .instructions("Use the weather capability.")
            .model(model)
            .capability(lookup_capability())
            .build()
            .expect("valid agent");
        let session = InMemoryEngine::new().create(agent.clone());
        let mut events = session.events();

        let turn = session.run("Weather?").await.expect("turn runs");
        assert!(turn.success, "turn should recover: {:?}", turn.error);
        assert_eq!(turn.tool_calls, 1);

        let mut saw_progress = false;
        while let Ok(Ok(Some(event))) = timeout(Duration::from_millis(50), events.recv()).await {
            if matches!(
                event.kind,
                SessionEventKind::ToolProgress {
                    ref tool_name,
                    ref message,
                    ..
                } if tool_name == "lookup_weather" && message == "forecast ready"
            ) {
                saw_progress = true;
                break;
            }
        }
        assert!(saw_progress, "advanced context emitted correlated progress");
    }

    #[tokio::test]
    async fn user_error_is_a_recoverable_runtime_tool_error() {
        let model = scripted_model(vec![
            vec![ToolCall {
                id: "call_failure".into(),
                name: "failing_lookup".into(),
                arguments: json!({ "city": "Atlantis" }),
            }],
            vec![],
        ]);
        let capability = Definition::new("failures", "Failures", "Error test.").tool(Failing);
        let agent = Agent::builder()
            .instructions("Try the lookup and handle errors.")
            .model(model)
            .capability(capability)
            .build()
            .expect("valid agent");

        let turn = InMemoryEngine::new()
            .create(agent)
            .run("Find Atlantis")
            .await
            .expect("turn runs");
        assert!(turn.success, "model should recover from a tool error");
        assert_eq!(turn.tool_calls, 1);
    }

    struct Completing {
        child_cancelled: Arc<Notify>,
    }

    #[async_trait]
    impl Handler for Completing {
        type Input = LookupInput;
        type Output = LookupOutput;
        type Error = Error;

        fn name(&self) -> &str {
            "completing_lookup"
        }

        fn description(&self) -> &str {
            "Return successfully after starting call-scoped child work."
        }

        async fn execute(
            &self,
            input: Self::Input,
            context: Context,
        ) -> Result<Self::Output, Self::Error> {
            let cancellation = context.cancellation().clone();
            let child_cancelled = self.child_cancelled.clone();
            tokio::spawn(async move {
                cancellation.cancelled().await;
                child_cancelled.notify_one();
            });
            Ok(LookupOutput {
                city: input.city,
                temperatures: vec![],
            })
        }
    }

    #[tokio::test]
    async fn successful_call_completion_notifies_child_work() {
        let child_cancelled = Arc::new(Notify::new());
        let capability =
            Definition::new("completing", "Completing", "Completion test.").tool(Completing {
                child_cancelled: child_cancelled.clone(),
            });
        let model = scripted_model(vec![
            vec![ToolCall {
                id: "call_completing".into(),
                name: "completing_lookup".into(),
                arguments: json!({ "city": "Kyiv" }),
            }],
            vec![],
        ]);
        let agent = Agent::builder()
            .instructions("Run the completing lookup.")
            .model(model)
            .capability(capability)
            .build()
            .expect("valid agent");

        let turn = InMemoryEngine::new()
            .create(agent.clone())
            .run("start")
            .await
            .expect("turn runs");
        assert!(turn.success);
        timeout(Duration::from_secs(2), child_cancelled.notified())
            .await
            .expect("child observed successful call completion");
    }

    struct Hanging {
        started: Arc<Notify>,
        child_cancelled: Arc<Notify>,
        cancellation_seen: Arc<AtomicBool>,
    }

    #[async_trait]
    impl Handler for Hanging {
        type Input = LookupInput;
        type Output = LookupOutput;
        type Error = Error;

        fn name(&self) -> &str {
            "hanging_lookup"
        }

        fn description(&self) -> &str {
            "Wait until the enclosing turn is cancelled."
        }

        async fn execute(
            &self,
            _input: Self::Input,
            context: Context,
        ) -> Result<Self::Output, Self::Error> {
            let cancellation = context.cancellation().clone();
            let seen = self.cancellation_seen.clone();
            let child_cancelled = self.child_cancelled.clone();
            tokio::spawn(async move {
                cancellation.cancelled().await;
                seen.store(true, Ordering::SeqCst);
                child_cancelled.notify_one();
            });
            self.started.notify_one();
            std::future::pending().await
        }
    }

    #[tokio::test]
    async fn turn_cancellation_stops_tool_and_notifies_child_work() {
        let started = Arc::new(Notify::new());
        let child_cancelled = Arc::new(Notify::new());
        let cancellation_seen = Arc::new(AtomicBool::new(false));
        let capability =
            Definition::new("hanging", "Hanging", "Cancellation test.").tool(Hanging {
                started: started.clone(),
                child_cancelled: child_cancelled.clone(),
                cancellation_seen: cancellation_seen.clone(),
            });
        let model = scripted_model(vec![vec![ToolCall {
            id: "call_hanging".into(),
            name: "hanging_lookup".into(),
            arguments: json!({ "city": "Kyiv" }),
        }]]);
        let agent = Agent::builder()
            .instructions("Run the hanging lookup.")
            .model(model)
            .capability(capability)
            .build()
            .expect("valid agent");
        let cancellation = CancellationToken::new();
        let cancel_from_test = cancellation.clone();

        let session = InMemoryEngine::new().create(agent);
        let run = tokio::spawn(async move {
            session
                .run_with("start", RunOptions::new().cancel_token(cancellation))
                .await
        });
        timeout(Duration::from_secs(2), started.notified())
            .await
            .expect("tool started");
        cancel_from_test.cancel();

        let turn = timeout(Duration::from_secs(2), run)
            .await
            .expect("run stopped")
            .expect("task joined")
            .expect("run result");
        assert_eq!(turn.stop_reason, TurnStopReason::Cancelled);
        timeout(Duration::from_secs(2), child_cancelled.notified())
            .await
            .expect("child observed call cancellation");
        assert!(cancellation_seen.load(Ordering::SeqCst));
    }
}