everruns 0.20.1

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
//! Application-owned execution engine.

use std::collections::HashMap;
use std::fmt;
use std::sync::{Arc, Mutex, Weak};

#[cfg(feature = "local")]
use std::sync::OnceLock;

use async_trait::async_trait;
use everruns_host::{
    Environment, EnvironmentBindingStore, HostBackends, InMemoryEnvironmentBindingStore,
};
use tokio::sync::OnceCell;

use crate::agent::BackendInitError;
use crate::{Agent, ResumeError, Session, SessionEnvironmentError, SessionId};

/// The runtime resources owned by an [`Engine`].
pub(crate) struct EngineBackends {
    pub(crate) host: HostBackends,
    binding_store: Arc<dyn EnvironmentBindingStore>,
}

/// Private binding between a [`Session`] and its owning engine.
#[async_trait]
pub(crate) trait SessionExecution: Send + Sync + fmt::Debug {
    fn session_id(&self) -> SessionId;
    fn agent_snapshot(&self) -> Agent;
    async fn backends(&self) -> Result<Arc<EngineBackends>, BackendInitError>;
    async fn ensure_cataloged(&self) -> Result<(), crate::HistoryError>;
    async fn bind_environment(
        &self,
        environment: &Environment,
    ) -> Result<(), SessionEnvironmentError>;
    async fn bind_default_environment(&self) -> Result<Environment, SessionEnvironmentError>;
    async fn reopen_environment(&self) -> Result<Option<Environment>, ResumeError>;
}

/// Application-owned session execution engine.
///
/// Clones share a session catalog and backend bundle. Local profiles also share
/// one backend bundle across engines in the same process, preventing divergent
/// JSONL indexes and SQLite handles when an application constructs more than
/// one engine for the same profile.
#[derive(Clone, Default)]
pub struct Engine {
    inner: Arc<EngineInner>,
}

#[derive(Default)]
struct EngineInner {
    sessions: Mutex<HashMap<SessionId, EngineSessionEntry>>,
    memory_backends: Arc<OnceCell<Arc<EngineBackends>>>,
    #[cfg(feature = "local")]
    local_backends: Mutex<HashMap<std::path::PathBuf, Arc<OnceCell<Arc<EngineBackends>>>>>,
}

struct EngineSessionEntry {
    agent: Agent,
    state: Option<Weak<crate::session::SessionInner>>,
}

#[cfg(feature = "local")]
struct LocalBackendCell {
    workspace_root: std::path::PathBuf,
    cell: Weak<OnceCell<Arc<EngineBackends>>>,
}

#[cfg(feature = "local")]
fn local_backend_cells() -> &'static Mutex<HashMap<std::path::PathBuf, LocalBackendCell>> {
    static CELLS: OnceLock<Mutex<HashMap<std::path::PathBuf, LocalBackendCell>>> = OnceLock::new();
    CELLS.get_or_init(|| Mutex::new(HashMap::new()))
}

impl fmt::Debug for Engine {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        let sessions = self
            .inner
            .sessions
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .len();
        formatter
            .debug_struct("Engine")
            .field("sessions", &sessions)
            .finish()
    }
}

impl Engine {
    /// Construct an empty process-local engine.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a new engine-owned session from an immutable Agent snapshot.
    pub fn create(&self, agent: Agent) -> Session {
        let session_id = SessionId::new();
        self.attach_unchecked(session_id, agent);
        let session = Session::new(self.binding(session_id), None);
        self.remember_state(session_id, &session);
        session
    }

    /// Reopen a session already owned by this engine.
    pub async fn resume(&self, session_id: SessionId) -> Result<Session, ResumeError> {
        if let Some(state) = self.state(session_id) {
            return Ok(Session::from_inner(state));
        }
        if self.agent(session_id).is_none() {
            return Err(ResumeError::SessionNotFound { session_id });
        }
        let binding = self.binding(session_id);
        let environment = binding.reopen_environment().await?;
        let session = Session::new(binding, environment);
        self.remember_state(session_id, &session);
        Ok(session)
    }

    /// Attach a persisted local session to this engine.
    ///
    /// Rebuild the Agent from trusted application configuration after a process
    /// restart, attach it to the persisted id, then call [`resume`](Self::resume).
    pub async fn attach(&self, session_id: SessionId, agent: Agent) -> Result<(), ResumeError> {
        if self.agent(session_id).is_some() {
            return Ok(());
        }
        let backends = self
            .backends_for(&agent)
            .await
            .map_err(|error| error.resume_error())?;
        let exists = backends
            .host
            .session_store
            .get_session(session_id)
            .await
            .map_err(|_| ResumeError::Unavailable)?
            .is_some();
        if !exists {
            return Err(ResumeError::SessionNotFound { session_id });
        }
        self.inner
            .sessions
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .entry(session_id)
            .or_insert(EngineSessionEntry { agent, state: None });
        Ok(())
    }

    pub(crate) fn agent(&self, session_id: SessionId) -> Option<Agent> {
        self.inner
            .sessions
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .get(&session_id)
            .map(|entry| entry.agent.clone())
    }

    async fn backends_for(&self, agent: &Agent) -> Result<Arc<EngineBackends>, BackendInitError> {
        let cell = self.backend_cell(agent)?;
        cell.get_or_try_init(|| initialize_backends(agent))
            .await
            .cloned()
    }

    fn backend_cell(
        &self,
        agent: &Agent,
    ) -> Result<Arc<OnceCell<Arc<EngineBackends>>>, BackendInitError> {
        #[cfg(feature = "local")]
        if let Some(config) = agent.local_config() {
            let key = absolute_path(config.data_dir());
            let workspace_root = absolute_path(config.workspace_root());
            let mut cells = local_backend_cells()
                .lock()
                .unwrap_or_else(|poisoned| poisoned.into_inner());
            if let Some(existing) = cells.get(&key)
                && let Some(cell) = existing.cell.upgrade()
            {
                if existing.workspace_root != workspace_root {
                    return Err(BackendInitError::Host(
                        everruns_provider::error::AgentLoopError::config(format!(
                            "local profile {} is already open with workspace {}; requested {}",
                            key.display(),
                            existing.workspace_root.display(),
                            workspace_root.display()
                        )),
                    ));
                }
                self.inner
                    .local_backends
                    .lock()
                    .unwrap_or_else(|poisoned| poisoned.into_inner())
                    .insert(key, cell.clone());
                return Ok(cell);
            }
            let cell = Arc::new(OnceCell::new());
            cells.insert(
                key.clone(),
                LocalBackendCell {
                    workspace_root,
                    cell: Arc::downgrade(&cell),
                },
            );
            self.inner
                .local_backends
                .lock()
                .unwrap_or_else(|poisoned| poisoned.into_inner())
                .insert(key, cell.clone());
            return Ok(cell);
        }
        #[cfg(not(feature = "local"))]
        let _ = agent;
        Ok(self.inner.memory_backends.clone())
    }

    fn attach_unchecked(&self, session_id: SessionId, agent: Agent) {
        self.inner
            .sessions
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .insert(session_id, EngineSessionEntry { agent, state: None });
    }

    fn state(&self, session_id: SessionId) -> Option<Arc<crate::session::SessionInner>> {
        self.inner
            .sessions
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .get(&session_id)
            .and_then(|entry| entry.state.as_ref()?.upgrade())
    }

    fn remember_state(&self, session_id: SessionId, session: &Session) {
        if let Some(entry) = self
            .inner
            .sessions
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .get_mut(&session_id)
        {
            entry.state = Some(Arc::downgrade(&session.inner()));
        }
    }

    fn binding(&self, session_id: SessionId) -> Arc<dyn SessionExecution> {
        Arc::new(EngineSessionExecution {
            engine: self.clone(),
            session_id,
        })
    }
}

/// Compatibility name for the application-owned process-local engine.
pub type InMemoryEngine = Engine;

#[cfg(feature = "local")]
fn absolute_path(path: &std::path::Path) -> std::path::PathBuf {
    let absolute = if path.is_absolute() {
        path.to_path_buf()
    } else {
        std::env::current_dir()
            .unwrap_or_else(|_| std::path::PathBuf::from("."))
            .join(path)
    };
    let mut normalized = std::path::PathBuf::new();
    for component in absolute.components() {
        match component {
            std::path::Component::CurDir => {}
            std::path::Component::ParentDir => {
                normalized.pop();
            }
            other => normalized.push(other.as_os_str()),
        }
    }
    // Canonicalize the longest existing prefix so a profile path keeps the
    // same identity before and after its workspace directory is created. This
    // also normalizes platform aliases such as macOS `/var` and `/private/var`.
    let mut existing = normalized.as_path();
    let mut missing = Vec::new();
    loop {
        if let Ok(mut canonical) = std::fs::canonicalize(existing) {
            for component in missing.iter().rev() {
                canonical.push(component);
            }
            return canonical;
        }
        let Some(name) = existing.file_name() else {
            return normalized;
        };
        missing.push(name.to_os_string());
        let Some(parent) = existing.parent() else {
            return normalized;
        };
        existing = parent;
    }
}

async fn initialize_backends(agent: &Agent) -> Result<Arc<EngineBackends>, BackendInitError> {
    let backends = HostBackends::in_memory();
    #[cfg(feature = "local")]
    if let Some(config) = agent.local_config() {
        let profile = config.profile();
        profile.ensure_dirs().map_err(|error| {
            BackendInitError::Host(everruns_provider::error::AgentLoopError::config(
                error.to_string(),
            ))
        })?;
        let event_log = Arc::new(
            everruns_host::JsonlEventLog::open(config.data_dir().join("events.jsonl"))
                .await
                .map_err(BackendInitError::Event)?,
        );
        let local = crate::local::LocalBackends::new(profile, backends.with_event_log(event_log))
            .map_err(BackendInitError::Host)?;
        let session_store = Arc::new(
            crate::local::LocalSessionStore::new(local.db.clone())
                .map_err(BackendInitError::Host)?,
        );
        return Ok(Arc::new(EngineBackends {
            host: local
                .runtime_backends
                .with_session_store(session_store.clone()),
            binding_store: session_store,
        }));
    }
    #[cfg(not(feature = "local"))]
    let _ = agent;
    Ok(Arc::new(EngineBackends {
        host: backends,
        binding_store: Arc::new(InMemoryEnvironmentBindingStore::default()),
    }))
}

struct EngineSessionExecution {
    engine: Engine,
    session_id: SessionId,
}

impl fmt::Debug for EngineSessionExecution {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("SessionExecution")
            .field("session_id", &self.session_id)
            .finish_non_exhaustive()
    }
}

#[async_trait]
impl SessionExecution for EngineSessionExecution {
    fn session_id(&self) -> SessionId {
        self.session_id
    }

    fn agent_snapshot(&self) -> Agent {
        self.engine
            .agent(self.session_id)
            .expect("engine binding references a cataloged session")
    }

    async fn backends(&self) -> Result<Arc<EngineBackends>, BackendInitError> {
        self.engine.backends_for(&self.agent_snapshot()).await
    }

    async fn ensure_cataloged(&self) -> Result<(), crate::HistoryError> {
        let agent =
            self.engine
                .agent(self.session_id)
                .ok_or(crate::HistoryError::SessionNotFound {
                    session_id: self.session_id,
                })?;
        let backends = self
            .engine
            .backends_for(&agent)
            .await
            .map_err(|error| error.history_error())?;
        agent.catalog_session(&backends.host, self.session_id).await
    }

    async fn bind_environment(
        &self,
        environment: &Environment,
    ) -> Result<(), SessionEnvironmentError> {
        let agent = self.agent_snapshot();
        let backends = self
            .backends()
            .await
            .map_err(|_| SessionEnvironmentError::Unavailable)?;
        agent
            .bind_session_environment(
                backends.binding_store.as_ref(),
                self.session_id,
                environment,
            )
            .await
    }

    async fn bind_default_environment(&self) -> Result<Environment, SessionEnvironmentError> {
        let agent = self.agent_snapshot();
        let backends = self
            .backends()
            .await
            .map_err(|_| SessionEnvironmentError::Unavailable)?;
        agent
            .bind_default_session_environment(backends.binding_store.as_ref(), self.session_id)
            .await
    }

    async fn reopen_environment(&self) -> Result<Option<Environment>, ResumeError> {
        let agent = self.agent_snapshot();
        let backends = self
            .backends()
            .await
            .map_err(|error| error.resume_error())?;
        agent
            .reopen_session_environment(backends.binding_store.as_ref(), self.session_id)
            .await
    }
}