everruns 0.21.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
//! SQLite-backed session identity catalog for local execution hosts.

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

use async_trait::async_trait;
use everruns_capability::CapabilityRef as AgentCapabilityConfig;
use everruns_core::execution_loading::SessionStore;
use everruns_core::session::ExecutionSession;
use everruns_host::{
    EnvironmentBindingError, EnvironmentBindingStore, RuntimeSessionStore, SessionBuilder,
    WorkspaceBinding,
};
use everruns_platform::SessionMutator;
use everruns_provider::error::{AgentLoopError, Result};
use everruns_provider::typed_id::{HarnessId, SessionId};
use rusqlite::{OptionalExtension, params};

use super::SqliteDb;

/// Durable session identity catalog for local Framework hosts.
///
/// SQLite stores session ids plus the provider-owned, credential-free opaque
/// workspace binding required to reopen the exact head. Full runtime session
/// configuration remains in memory and is rebuilt from the resuming Agent, so
/// MCP credentials and other host configuration are not serialized. Canonical
/// events remain the sole conversation write path.
// THREAT[TM-FS-017]: never serialize Agent/Session configuration or credentials
// here. Environment bindings are size-bounded and providers must keep secrets
// out of their opaque payload.
#[derive(Clone)]
pub struct LocalSessionStore {
    db: SqliteDb,
    sessions: Arc<Mutex<HashMap<SessionId, ExecutionSession>>>,
}

impl LocalSessionStore {
    /// Create or open the catalog schema in `db`.
    pub fn new(db: SqliteDb) -> Result<Self> {
        db.with_conn(|conn| {
            conn.execute_batch(
                "CREATE TABLE IF NOT EXISTS framework_sessions (
                    session_id TEXT PRIMARY KEY NOT NULL
                );
                 CREATE TABLE IF NOT EXISTS framework_session_environments (
                    session_id TEXT PRIMARY KEY NOT NULL,
                    binding_json TEXT NOT NULL,
                    FOREIGN KEY(session_id) REFERENCES framework_sessions(session_id)
                );
                 CREATE TABLE IF NOT EXISTS framework_workspace_head_claims (
                    provider_id TEXT NOT NULL,
                    workspace_id TEXT NOT NULL,
                    head_id TEXT NOT NULL,
                    access TEXT NOT NULL CHECK (access IN ('isolated', 'shared')),
                    owner_session_id TEXT,
                    PRIMARY KEY(provider_id, workspace_id, head_id),
                    FOREIGN KEY(owner_session_id) REFERENCES framework_sessions(session_id)
                );",
            )
        })
        .map_err(store_error)?;
        Ok(Self {
            db,
            sessions: Arc::new(Mutex::new(HashMap::new())),
        })
    }

    fn load(&self, session_id: SessionId) -> Result<Option<ExecutionSession>> {
        if let Some(session) = self
            .sessions
            .lock()
            .map_err(|_| AgentLoopError::store("local session catalog lock poisoned"))?
            .get(&session_id)
            .cloned()
        {
            return Ok(Some(session));
        }
        let exists = self
            .db
            .with_conn(|conn| {
                conn.query_row(
                    "SELECT 1 FROM framework_sessions WHERE session_id = ?1",
                    params![session_id.to_string()],
                    |_| Ok(()),
                )
                .optional()
            })
            .map_err(store_error)?
            .is_some();
        Ok(exists.then(|| SessionBuilder::new(HarnessId::new()).id(session_id).build()))
    }

    fn mutate(
        &self,
        session_id: SessionId,
        update: impl FnOnce(&mut ExecutionSession),
    ) -> Result<ExecutionSession> {
        let mut session = self
            .load(session_id)?
            .ok_or_else(|| AgentLoopError::store(format!("session not found: {session_id}")))?;
        update(&mut session);
        self.sessions
            .lock()
            .map_err(|_| AgentLoopError::store("local session catalog lock poisoned"))?
            .insert(session_id, session.clone());
        Ok(session)
    }
}

// serde_json represents Vec<u8> as decimal values, so the encoded ceiling is
// larger than the provider payload ceiling while remaining strictly bounded.
const MAX_BINDING_BYTES: usize = WorkspaceBinding::MAX_PAYLOAD_BYTES * 4 + 4096;

#[async_trait]
impl EnvironmentBindingStore for LocalSessionStore {
    async fn load(
        &self,
        session_id: SessionId,
    ) -> std::result::Result<Option<WorkspaceBinding>, EnvironmentBindingError> {
        let encoded = self
            .db
            .with_conn(|conn| {
                conn.query_row(
                    "SELECT binding_json FROM framework_session_environments WHERE session_id = ?1",
                    params![session_id.to_string()],
                    |row| row.get::<_, String>(0),
                )
                .optional()
            })
            .map_err(|_| EnvironmentBindingError::Unavailable)?;
        let Some(encoded) = encoded else {
            return Ok(None);
        };
        if encoded.len() > MAX_BINDING_BYTES {
            return Err(EnvironmentBindingError::Corrupt);
        }
        let binding: WorkspaceBinding =
            serde_json::from_str(&encoded).map_err(|_| EnvironmentBindingError::Corrupt)?;
        if binding.payload.len() > WorkspaceBinding::MAX_PAYLOAD_BYTES {
            return Err(EnvironmentBindingError::Corrupt);
        }
        Ok(Some(binding))
    }

    async fn bind(
        &self,
        session_id: SessionId,
        binding: &WorkspaceBinding,
    ) -> std::result::Result<(), EnvironmentBindingError> {
        if binding.payload.len() > WorkspaceBinding::MAX_PAYLOAD_BYTES {
            return Err(EnvironmentBindingError::Corrupt);
        }
        let encoded =
            serde_json::to_string(binding).map_err(|_| EnvironmentBindingError::Corrupt)?;
        if encoded.len() > MAX_BINDING_BYTES {
            return Err(EnvironmentBindingError::Corrupt);
        }
        self.db
            .with_conn_mut(|conn| {
                let transaction = conn.transaction()?;
                transaction.execute(
                    "INSERT INTO framework_sessions (session_id) VALUES (?1)
                     ON CONFLICT(session_id) DO NOTHING",
                    params![session_id.to_string()],
                )?;
                let recorded = transaction
                    .query_row(
                        "SELECT binding_json FROM framework_session_environments WHERE session_id = ?1",
                        params![session_id.to_string()],
                        |row| row.get::<_, String>(0),
                    )
                    .optional()?;
                if let Some(recorded) = recorded {
                    if recorded != encoded {
                        return Err(rusqlite::Error::InvalidParameterName(
                            "environment binding conflict".into(),
                        ));
                    }
                } else {
                    let provider_id = binding.provider_id.to_string();
                    let workspace_id = binding.workspace_id.to_string();
                    let head_id = binding.head_id.to_string();
                    let access = match binding.access {
                        everruns_host::WorkspaceHeadAccess::Isolated => "isolated",
                        everruns_host::WorkspaceHeadAccess::Shared => "shared",
                    };
                    let owner = (binding.access == everruns_host::WorkspaceHeadAccess::Isolated)
                        .then(|| session_id.to_string());
                    let claim = transaction
                        .query_row(
                            "SELECT access, owner_session_id
                             FROM framework_workspace_head_claims
                             WHERE provider_id = ?1 AND workspace_id = ?2 AND head_id = ?3",
                            params![provider_id, workspace_id, head_id],
                            |row| Ok((row.get::<_, String>(0)?, row.get::<_, Option<String>>(1)?)),
                        )
                        .optional()?;
                    if let Some((recorded_access, recorded_owner)) = claim {
                        if recorded_access != access
                            || (access == "isolated"
                                && recorded_owner.as_deref() != owner.as_deref())
                        {
                            return Err(rusqlite::Error::InvalidParameterName(
                                "workspace head claim conflict".into(),
                            ));
                        }
                    } else {
                        transaction.execute(
                            "INSERT INTO framework_workspace_head_claims
                             (provider_id, workspace_id, head_id, access, owner_session_id)
                             VALUES (?1, ?2, ?3, ?4, ?5)",
                            params![provider_id, workspace_id, head_id, access, owner],
                        )?;
                    }
                    transaction.execute(
                        "INSERT INTO framework_session_environments (session_id, binding_json)
                         VALUES (?1, ?2)",
                        params![session_id.to_string(), encoded],
                    )?;
                }
                transaction.commit()
            })
            .map_err(|error| {
                if error.to_string().contains("environment binding conflict")
                    || error.to_string().contains("workspace head claim conflict")
                    || error.to_string().contains("UNIQUE constraint failed")
                {
                    EnvironmentBindingError::Conflict
                } else {
                    EnvironmentBindingError::Unavailable
                }
            })
    }
}

#[async_trait]
impl SessionStore for LocalSessionStore {
    async fn get_session(&self, session_id: SessionId) -> Result<Option<ExecutionSession>> {
        self.load(session_id)
    }
}

#[async_trait]
impl SessionMutator for LocalSessionStore {
    async fn update_session_title(
        &self,
        session_id: SessionId,
        title: String,
    ) -> Result<ExecutionSession> {
        self.mutate(session_id, |session| session.title = Some(title))
    }

    async fn upsert_session_capability(
        &self,
        session_id: SessionId,
        capability: AgentCapabilityConfig,
    ) -> Result<ExecutionSession> {
        self.mutate(session_id, |session| {
            if let Some(existing) = session
                .capabilities
                .iter_mut()
                .find(|existing| existing.capability_id() == capability.capability_id())
            {
                *existing = capability;
            } else {
                session.capabilities.push(capability);
            }
        })
    }

    async fn remove_session_capability(
        &self,
        session_id: SessionId,
        capability_id: &str,
    ) -> Result<ExecutionSession> {
        self.mutate(session_id, |session| {
            session
                .capabilities
                .retain(|capability| capability.capability_id() != capability_id);
        })
    }
}

#[async_trait]
impl RuntimeSessionStore for LocalSessionStore {
    async fn add_session(&self, session: ExecutionSession) -> Result<()> {
        let session_id = session.id;
        self.db
            .with_conn(|conn| {
                conn.execute(
                    "INSERT INTO framework_sessions (session_id) VALUES (?1)
                     ON CONFLICT(session_id) DO NOTHING",
                    params![session_id.to_string()],
                )?;
                Ok(())
            })
            .map_err(store_error)?;
        self.sessions
            .lock()
            .map_err(|_| AgentLoopError::store("local session catalog lock poisoned"))?
            .insert(session_id, session);
        Ok(())
    }
}

fn store_error(error: impl std::fmt::Display) -> AgentLoopError {
    AgentLoopError::store(error.to_string())
}

#[cfg(test)]
mod tests {
    use everruns_core::execution_loading::SessionStore;
    use everruns_host::{
        EnvironmentBindingStore, RuntimeSessionStore, WorkspaceHeadAccess, WorkspaceHeadId,
        WorkspaceProviderId,
    };
    use everruns_provider::typed_id::WorkspaceId;

    use super::*;

    #[tokio::test]
    async fn catalog_survives_reopen_without_persisting_runtime_configuration() {
        let root = tempfile::tempdir().unwrap();
        let path = root.path().join("local.db");
        let session_id = SessionId::new();

        let store = LocalSessionStore::new(SqliteDb::open(&path).unwrap()).unwrap();
        let session = SessionBuilder::new(HarnessId::new()).id(session_id).build();
        store.add_session(session).await.unwrap();
        store
            .update_session_title(session_id, "resumable".into())
            .await
            .unwrap();
        drop(store);

        let reopened = LocalSessionStore::new(SqliteDb::open(&path).unwrap()).unwrap();
        let restored = reopened.get_session(session_id).await.unwrap().unwrap();
        assert_eq!(restored.id, session_id);
        assert_eq!(restored.title, None);

        let columns = reopened
            .db
            .with_conn(|conn| {
                let mut statement = conn.prepare("PRAGMA table_info(framework_sessions)")?;
                statement
                    .query_map([], |row| row.get::<_, String>(1))?
                    .collect::<rusqlite::Result<Vec<_>>>()
            })
            .unwrap();
        assert_eq!(columns, vec!["session_id"]);
    }

    #[tokio::test]
    async fn missing_catalog_identity_is_not_a_placeholder_session() {
        let db = SqliteDb::open_in_memory().unwrap();
        let store = LocalSessionStore::new(db).unwrap();
        let session_id = SessionId::new();

        assert!(store.get_session(session_id).await.unwrap().is_none());
    }

    #[tokio::test]
    async fn opaque_binding_survives_reopen_and_enforces_isolated_ownership() {
        let root = tempfile::tempdir().unwrap();
        let path = root.path().join("local.db");
        let first = SessionId::new();
        let second = SessionId::new();
        let binding = WorkspaceBinding {
            provider_id: WorkspaceProviderId::new("test.workspace").unwrap(),
            workspace_id: WorkspaceId::from_seed(41),
            head_id: WorkspaceHeadId::new(),
            access: WorkspaceHeadAccess::Isolated,
            payload: b"opaque-v1".to_vec(),
        };

        let store = LocalSessionStore::new(SqliteDb::open(&path).unwrap()).unwrap();
        store.bind(first, &binding).await.unwrap();
        assert_eq!(
            store.bind(second, &binding).await,
            Err(EnvironmentBindingError::Conflict)
        );
        drop(store);

        let reopened = LocalSessionStore::new(SqliteDb::open(&path).unwrap()).unwrap();
        assert_eq!(
            EnvironmentBindingStore::load(&reopened, first)
                .await
                .unwrap(),
            Some(binding)
        );
    }

    #[tokio::test]
    async fn explicitly_shared_binding_accepts_multiple_sessions() {
        let store = LocalSessionStore::new(SqliteDb::open_in_memory().unwrap()).unwrap();
        let binding = WorkspaceBinding {
            provider_id: WorkspaceProviderId::new("test.workspace").unwrap(),
            workspace_id: WorkspaceId::from_seed(42),
            head_id: WorkspaceHeadId::new(),
            access: WorkspaceHeadAccess::Shared,
            payload: b"opaque-v1".to_vec(),
        };
        store.bind(SessionId::new(), &binding).await.unwrap();
        store.bind(SessionId::new(), &binding).await.unwrap();
    }

    #[tokio::test]
    async fn a_head_cannot_switch_between_isolated_and_shared_claims() {
        for initial_access in [WorkspaceHeadAccess::Isolated, WorkspaceHeadAccess::Shared] {
            let store = LocalSessionStore::new(SqliteDb::open_in_memory().unwrap()).unwrap();
            let mut binding = WorkspaceBinding {
                provider_id: WorkspaceProviderId::new("test.workspace").unwrap(),
                workspace_id: WorkspaceId::from_seed(43),
                head_id: WorkspaceHeadId::new(),
                access: initial_access,
                payload: b"opaque-v1".to_vec(),
            };
            store.bind(SessionId::new(), &binding).await.unwrap();
            binding.access = match initial_access {
                WorkspaceHeadAccess::Isolated => WorkspaceHeadAccess::Shared,
                WorkspaceHeadAccess::Shared => WorkspaceHeadAccess::Isolated,
            };
            assert_eq!(
                store.bind(SessionId::new(), &binding).await,
                Err(EnvironmentBindingError::Conflict)
            );
        }
    }
}