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
// Public backend contract for the embedded host.
// Seeding stores remain writable host configuration. Conversation history is
// different: canonical events are the only write path and EventHistory is the
// rebuildable read projection.
use crate::agent_definition::AgentDefinition;
use crate::harness_definition::HarnessDefinition;
use crate::host::SessionMutator;
use crate::host::events::{EventLog, EventSink, InMemoryEventLog, NoopEventSink};
use crate::host::in_memory::{
InMemoryAgentStore, InMemoryCompactionCheckpointStore, InMemoryHarnessStore,
InMemoryProviderStore, InMemorySessionStorageStore, InMemorySessionStore,
};
use crate::session::ExecutionSession;
use crate::session_task::SessionTaskRegistry;
use crate::{
connection_services::UserConnectionResolver, execution_loading::AgentStore,
execution_loading::HarnessStore, execution_loading::SessionStore,
provider_resolution::ProviderStore, session_services::SessionScheduleStore,
session_services::SessionStorageStore,
};
use async_trait::async_trait;
use everruns_contracts::error::Result;
use everruns_contracts::model_spec::ModelSpec;
use everruns_contracts::typed_id::HarnessId;
use std::sync::Arc;
/// Factory producing a per-org [`SessionScheduleStore`]. Embedders that have a
/// single global store can ignore the `org_id` argument and return the same
/// `Arc` every time.
pub type ScheduleStoreFactory = Arc<dyn Fn(i64) -> Arc<dyn SessionScheduleStore> + Send + Sync>;
/// Agent store contract for runtime seeding and lookup.
///
/// Seeds portable [`AgentDefinition`] values (EVE-877): the embedded host
/// carries no stored Agent persistence records.
#[async_trait]
pub trait RuntimeAgentStore: AgentStore + Send + Sync {
/// Insert or replace an agent definition.
async fn add_agent(&self, agent: AgentDefinition) -> Result<()>;
}
/// Harness store contract for runtime seeding and lookup.
///
/// Seeds portable [`HarnessDefinition`] values under an embedder-chosen id
/// (EVE-881): the embedded host carries no stored Harness persistence records.
#[async_trait]
pub trait RuntimeHarnessStore: HarnessStore + Send + Sync {
/// Insert or replace a harness definition under the given id.
async fn add_harness(&self, harness_id: HarnessId, harness: HarnessDefinition) -> Result<()>;
}
/// Session store contract for runtime seeding, lookup, and mutation.
///
/// Seeds portable [`ExecutionSession`] values (EVE-882): the embedded host
/// carries no stored Session persistence records.
#[async_trait]
pub trait RuntimeSessionStore: SessionStore + SessionMutator + Send + Sync {
/// Insert or replace a session's portable execution view.
async fn add_session(&self, session: ExecutionSession) -> Result<()>;
}
/// Provider store contract for runtime lookup and default-model configuration.
#[async_trait]
pub trait RuntimeProviderStore: ProviderStore + Send + Sync {
/// Set the runtime default credential-free model selection.
async fn set_default_model_spec(&self, model: ModelSpec) -> Result<()>;
}
/// Non-filesystem backend bundle supplied to an execution host.
///
/// Use this when you want the public runtime orchestration but your own store
/// implementations instead of the built-in in-memory ones. Session filesystem
/// selection is always resolved from `HostComposition`.
#[derive(Clone)]
pub struct HostBackends {
/// Optional host-selected shell hook dispatcher; no integration is attached by core.
pub bash_hook_dispatcher_factory: Option<crate::host::BashHookDispatcherFactory>,
/// Optional shared fenced journal for native asynchronous tool execution.
pub native_async_store: Option<Arc<dyn crate::native_async_store::NativeAsyncStore>>,
/// Harness definitions available to the runtime.
pub harness_store: Arc<dyn RuntimeHarnessStore>,
/// Agent definitions available to the runtime.
pub agent_store: Arc<dyn RuntimeAgentStore>,
/// Session records and mutable session metadata.
pub session_store: Arc<dyn RuntimeSessionStore>,
/// Coherent canonical event log. This is the sole conversation write path.
pub event_log: Arc<dyn EventLog>,
/// Durable replacement context used to reconstruct compacted model input.
pub compaction_checkpoint_store: Arc<dyn crate::CompactionCheckpointStore>,
/// Model/provider resolution and default-model configuration.
pub provider_store: Arc<dyn RuntimeProviderStore>,
/// Optional, non-blocking live observation sink notified after commit.
pub event_sink: Arc<dyn EventSink>,
/// Session key/value + secret storage backend.
pub storage_store: Arc<dyn SessionStorageStore>,
/// Optional resolver for user connection tokens (e.g. GitHub, Daytona).
///
/// When set, the runtime exposes it through `ToolContext.connection_resolver`
/// so connection-aware capabilities can resolve tokens lazily at tool time.
/// `None` (the default) leaves the resolver unset, matching prior behavior.
/// There is no in-memory default because a connection resolver implies a
/// real credential source the embedder must supply.
pub connection_resolver: Option<Arc<dyn UserConnectionResolver>>,
/// Optional session-task registry injected into the act path so background
/// tools, subagents, and monitors persist their lifecycle. `None` (the
/// default) leaves `RuntimeHostAdapter::session_task_registry` returning
/// `None`, matching prior in-memory behavior.
pub session_task_registry: Option<Arc<dyn SessionTaskRegistry>>,
/// Optional per-org schedule store factory. `None` (the default) leaves
/// `RuntimeHostAdapter::schedule_store` returning `None`.
pub schedule_store_factory: Option<ScheduleStoreFactory>,
/// Optional higher-level typed services supplied to tool contexts.
pub tool_context_extensions_factory: Option<crate::host::ToolContextExtensionsFactory>,
/// Optional neutral subagent delegate supplied by a higher-level host.
pub subagent_delegate_factory: Option<crate::host::SubagentDelegateFactory>,
/// Optional higher-level tool augmentation policy.
pub tool_augmentor: Option<Arc<dyn crate::host::HostToolAugmentor>>,
}
impl std::fmt::Debug for HostBackends {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// Backends are trait objects; report the shape only.
f.debug_struct("HostBackends").finish_non_exhaustive()
}
}
impl HostBackends {
/// Backend bundle with in-memory implementations for every store.
///
/// Suitable for tests, examples, and the default runtime configuration.
/// Use the chainable `with_*` setters to override individual stores.
pub fn in_memory() -> Self {
Self {
bash_hook_dispatcher_factory: None,
harness_store: Arc::new(InMemoryHarnessStore::new()),
agent_store: Arc::new(InMemoryAgentStore::new()),
session_store: Arc::new(InMemorySessionStore::new()),
event_log: Arc::new(InMemoryEventLog::new()),
native_async_store: None,
compaction_checkpoint_store: Arc::new(InMemoryCompactionCheckpointStore::default()),
provider_store: Arc::new(InMemoryProviderStore::new()),
event_sink: Arc::new(NoopEventSink),
storage_store: Arc::new(InMemorySessionStorageStore::new()),
connection_resolver: None,
session_task_registry: None,
schedule_store_factory: None,
tool_context_extensions_factory: None,
subagent_delegate_factory: None,
tool_augmentor: None,
}
}
/// Inject the shell-hook implementation selected by an embedding host.
pub fn with_bash_hook_dispatcher_factory(
mut self,
factory: crate::host::BashHookDispatcherFactory,
) -> Self {
self.bash_hook_dispatcher_factory = Some(factory);
self
}
pub fn with_harness_store(mut self, store: Arc<dyn RuntimeHarnessStore>) -> Self {
self.harness_store = store;
self
}
pub fn with_agent_store(mut self, store: Arc<dyn RuntimeAgentStore>) -> Self {
self.agent_store = store;
self
}
pub fn with_session_store(mut self, store: Arc<dyn RuntimeSessionStore>) -> Self {
self.session_store = store;
self
}
/// Replace the coherent canonical event log.
pub fn with_event_log(mut self, log: Arc<dyn EventLog>) -> Self {
self.event_log = log;
self
}
pub fn with_native_async_store(
mut self,
store: Arc<dyn crate::native_async_store::NativeAsyncStore>,
) -> Self {
self.native_async_store = Some(store);
self
}
pub fn with_compaction_checkpoint_store(
mut self,
store: Arc<dyn crate::CompactionCheckpointStore>,
) -> Self {
self.compaction_checkpoint_store = store;
self
}
pub fn with_provider_store(mut self, store: Arc<dyn RuntimeProviderStore>) -> Self {
self.provider_store = store;
self
}
/// Install a non-blocking post-commit observation sink.
pub fn with_event_sink(mut self, sink: Arc<dyn EventSink>) -> Self {
self.event_sink = sink;
self
}
pub fn with_storage_store(mut self, store: Arc<dyn SessionStorageStore>) -> Self {
self.storage_store = store;
self
}
/// Supply a resolver for user connection tokens (e.g. GitHub, Daytona).
///
/// The runtime forwards it into `ToolContext` so connection-aware
/// capabilities resolve tokens lazily at tool execution time.
pub fn with_connection_resolver(mut self, resolver: Arc<dyn UserConnectionResolver>) -> Self {
self.connection_resolver = Some(resolver);
self
}
/// Inject a session-task registry so background tools / subagents / monitors
/// persist their lifecycle through the act path. Additive: leaving this unset
/// keeps the prior behavior (the adapter returns `None`).
pub fn with_session_task_registry(mut self, registry: Arc<dyn SessionTaskRegistry>) -> Self {
self.session_task_registry = Some(registry);
self
}
/// Inject a per-org schedule store factory. The closure is invoked with the
/// internal org id each time the act path needs a schedule store.
pub fn with_schedule_store_factory(mut self, factory: ScheduleStoreFactory) -> Self {
self.schedule_store_factory = Some(factory);
self
}
/// Inject type-erased tool services supplied by a higher-level host.
pub fn with_tool_context_extensions_factory(
mut self,
factory: crate::host::ToolContextExtensionsFactory,
) -> Self {
self.tool_context_extensions_factory = Some(factory);
self
}
/// Inject a neutral subagent delegate supplied by a higher-level host.
pub fn with_subagent_delegate_factory(
mut self,
factory: crate::host::SubagentDelegateFactory,
) -> Self {
self.subagent_delegate_factory = Some(factory);
self
}
/// Inject higher-level turn-dependent tools.
pub fn with_tool_augmentor(
mut self,
augmentor: Arc<dyn crate::host::HostToolAugmentor>,
) -> Self {
self.tool_augmentor = Some(augmentor);
self
}
}
#[async_trait]
impl RuntimeAgentStore for InMemoryAgentStore {
async fn add_agent(&self, agent: AgentDefinition) -> Result<()> {
InMemoryAgentStore::add_agent(self, agent).await;
Ok(())
}
}
#[async_trait]
impl RuntimeHarnessStore for InMemoryHarnessStore {
async fn add_harness(&self, harness_id: HarnessId, harness: HarnessDefinition) -> Result<()> {
InMemoryHarnessStore::add_harness(self, harness_id, harness).await;
Ok(())
}
}
#[async_trait]
impl RuntimeSessionStore for InMemorySessionStore {
async fn add_session(&self, session: ExecutionSession) -> Result<()> {
InMemorySessionStore::add_session(self, session).await;
Ok(())
}
}
#[async_trait]
impl RuntimeProviderStore for InMemoryProviderStore {
async fn set_default_model_spec(&self, model: ModelSpec) -> Result<()> {
InMemoryProviderStore::set_default_model_spec(self, model).await;
Ok(())
}
}