Skip to main content

everruns_core/
traits.rs

1// Core traits for pluggable backends
2//
3// These traits allow the agent loop to be used with different backends:
4// - In-memory implementations for examples and testing
5// - Database implementations for production
6// - Channel-based implementations for streaming
7
8use crate::agent::Agent;
9use crate::harness::Harness;
10use crate::provider::DriverId;
11use crate::session_file::{FileInfo, FileStat, GrepMatch, InitialFile, SessionFile};
12use crate::tool_types::{ToolCall, ToolDefinition, ToolResult};
13use crate::typed_id::{AgentId, HarnessId, ImageId, ModelId, SessionId, WorkspaceId};
14use async_trait::async_trait;
15use chrono::{DateTime, Utc};
16use std::any::{Any, TypeId};
17use std::collections::{HashMap, HashSet};
18use std::sync::Arc;
19use uuid::Uuid;
20
21/// Build a map of tool names to definitions for efficient lookup
22fn build_tool_map(tool_defs: &[ToolDefinition]) -> HashMap<&str, &ToolDefinition> {
23    tool_defs.iter().map(|def| (def.name(), def)).collect()
24}
25
26use crate::error::Result;
27
28// ============================================================================
29// ReasoningEffortHandle - live, turn-scoped reasoning-effort override
30// ============================================================================
31
32/// A live, shared handle to the reasoning effort for the current turn (EVE-595).
33///
34/// Each internal LLM step within a single `run_turn` re-reads this handle when
35/// building its provider request. A tool (or any in-turn actor) can call
36/// [`ReasoningEffortHandle::set`] mid-turn so that *subsequent* LLM steps in the
37/// same turn use the new effort, without waiting for the next turn.
38///
39/// When the handle holds `None`, the [`crate::atoms::ReasonAtom`] falls back to
40/// the effort resolved from the latest user message's `controls` — so callers
41/// that never set an override see no behavior change.
42#[derive(Clone, Default)]
43pub struct ReasoningEffortHandle {
44    inner: Arc<std::sync::RwLock<Option<String>>>,
45}
46
47impl ReasoningEffortHandle {
48    /// Create an empty handle (no override).
49    pub fn new() -> Self {
50        Self::default()
51    }
52
53    /// Create a handle pre-seeded with an effort override.
54    pub fn with_effort(effort: impl Into<String>) -> Self {
55        Self {
56            inner: Arc::new(std::sync::RwLock::new(Some(effort.into()))),
57        }
58    }
59
60    /// Set (or replace) the override effort. Subsequent LLM steps in the same
61    /// turn pick this up. Pass `None` to clear the override and fall back to the
62    /// message-derived effort.
63    pub fn set(&self, effort: Option<String>) {
64        // Recover from a poisoned lock so a panic elsewhere never silently
65        // disables mid-turn overrides; the stored value is a plain Option<String>
66        // with no broken invariant to worry about.
67        let mut guard = self.inner.write().unwrap_or_else(|e| e.into_inner());
68        *guard = effort;
69    }
70
71    /// Read the current override effort, if any.
72    pub fn get(&self) -> Option<String> {
73        // Recover from a poisoned lock rather than silently reverting to the
74        // message-derived effort mid-turn (see `set`).
75        let guard = self.inner.read().unwrap_or_else(|e| e.into_inner());
76        guard.clone()
77    }
78}
79
80impl std::fmt::Debug for ReasoningEffortHandle {
81    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
82        f.debug_struct("ReasoningEffortHandle")
83            .field("effort", &self.get())
84            .finish()
85    }
86}
87
88// ============================================================================
89// AgentStore - For retrieving agent configurations
90// ============================================================================
91
92/// Trait for retrieving agent configurations
93///
94/// Implementations can:
95/// - Load agents from a database
96/// - Keep agents in memory for testing
97/// - Load agents from a configuration file
98#[async_trait]
99pub trait AgentStore: Send + Sync {
100    /// Get an agent by ID
101    async fn get_agent(&self, agent_id: AgentId) -> Result<Option<Agent>>;
102}
103
104#[async_trait]
105impl<T: AgentStore + ?Sized> AgentStore for std::sync::Arc<T> {
106    async fn get_agent(&self, agent_id: AgentId) -> Result<Option<Agent>> {
107        (**self).get_agent(agent_id).await
108    }
109}
110
111// ============================================================================
112// HarnessStore - For retrieving harness configurations
113// ============================================================================
114
115/// Trait for retrieving harness configurations
116///
117/// Implementations can:
118/// - Load harnesses from a database
119/// - Keep harnesses in memory for testing
120///
121/// Returns the harness inheritance chain (root-to-leaf) so the caller
122/// can fold each harness as an `AgentConfigOverlay`. DB-backed stores
123/// return the raw chain; gRPC-backed stores may return a single
124/// pre-merged harness (functionally equivalent when folded).
125#[async_trait]
126pub trait HarnessStore: Send + Sync {
127    /// Get the harness inheritance chain, root-to-leaf.
128    ///
129    /// Returns `Ok(vec![])` if the harness does not exist.
130    /// A harness with no parent returns a single-element vec.
131    async fn get_harness_chain(&self, harness_id: HarnessId) -> Result<Vec<Harness>>;
132}
133
134#[async_trait]
135impl<T: HarnessStore + ?Sized> HarnessStore for std::sync::Arc<T> {
136    async fn get_harness_chain(&self, harness_id: HarnessId) -> Result<Vec<Harness>> {
137        (**self).get_harness_chain(harness_id).await
138    }
139}
140
141// ============================================================================
142// SessionStore - For retrieving session information
143// ============================================================================
144
145use crate::leased_resource::{LeasedResource, UpsertLeasedResource};
146use crate::session::Session;
147
148/// Trait for retrieving session configurations
149///
150/// Implementations can:
151/// - Load sessions from a database
152/// - Keep sessions in memory for testing
153#[async_trait]
154pub trait SessionStore: Send + Sync {
155    /// Get a session by ID
156    async fn get_session(&self, session_id: SessionId) -> Result<Option<Session>>;
157}
158
159#[async_trait]
160impl<T: SessionStore + ?Sized> SessionStore for std::sync::Arc<T> {
161    async fn get_session(&self, session_id: SessionId) -> Result<Option<Session>> {
162        (**self).get_session(session_id).await
163    }
164}
165
166/// Trait for updating mutable session metadata.
167#[async_trait]
168pub trait SessionMutator: Send + Sync {
169    /// Update a session's human-readable title.
170    async fn update_session_title(&self, session_id: SessionId, title: String) -> Result<Session>;
171}
172
173#[async_trait]
174impl<T: SessionMutator + ?Sized> SessionMutator for std::sync::Arc<T> {
175    async fn update_session_title(&self, session_id: SessionId, title: String) -> Result<Session> {
176        (**self).update_session_title(session_id, title).await
177    }
178}
179
180// ============================================================================
181// ProviderStore - For retrieving LLM provider configurations
182// ============================================================================
183
184/// Model information with provider details needed for LLM calls
185#[derive(Debug, Clone)]
186pub struct ResolvedModel {
187    /// The model ID string to pass to the LLM API (e.g., "gpt-4o", "claude-3-opus")
188    pub model: String,
189    /// Provider type for factory selection
190    pub provider_type: DriverId,
191    /// Decrypted API key (if configured)
192    pub api_key: Option<String>,
193    /// Optional base URL override
194    pub base_url: Option<String>,
195    /// Extra provider-specific metadata (OAuth tokens, account ids, etc.).
196    /// Used by embedder-defined providers that authenticate without an API key.
197    pub provider_metadata: Option<crate::driver_registry::ProviderMetadata>,
198}
199
200/// Trait for retrieving LLM provider and model configurations
201///
202/// This trait abstracts the database lookup and API key decryption needed
203/// to create LLM providers at runtime.
204///
205/// Implementations can:
206/// - Load from a database with encrypted API keys
207/// - Use in-memory configurations for testing
208/// - Load from environment variables for development
209#[async_trait]
210pub trait ProviderStore: Send + Sync {
211    /// Get model with provider info by model ID
212    ///
213    /// Returns the model string ID, provider type, decrypted API key, and base URL
214    /// needed to create an LLM provider via the factory.
215    async fn get_resolved_model(&self, model_id: ModelId) -> Result<Option<ResolvedModel>>;
216
217    /// Get the default model with provider info
218    ///
219    /// Returns the system default model when an agent has no default_model_id set.
220    async fn get_default_model(&self) -> Result<Option<ResolvedModel>>;
221}
222
223#[async_trait]
224impl<T: ProviderStore + ?Sized> ProviderStore for std::sync::Arc<T> {
225    async fn get_resolved_model(&self, model_id: ModelId) -> Result<Option<ResolvedModel>> {
226        (**self).get_resolved_model(model_id).await
227    }
228
229    async fn get_default_model(&self) -> Result<Option<ResolvedModel>> {
230        (**self).get_default_model().await
231    }
232}
233
234// ============================================================================
235// ImageArtifactStore - For durable image persistence from tools
236// ============================================================================
237
238/// Metadata for a stored image artifact.
239#[derive(Debug, Clone)]
240pub struct StoredImageInfo {
241    pub id: ImageId,
242    pub filename: String,
243    pub content_type: String,
244    pub size_bytes: i64,
245    pub metadata: serde_json::Value,
246    pub created_at: DateTime<Utc>,
247}
248
249/// Stored image artifact with binary data.
250#[derive(Debug, Clone)]
251pub struct StoredImage {
252    pub info: StoredImageInfo,
253    pub data: Vec<u8>,
254}
255
256/// Input for creating a stored image artifact.
257#[derive(Debug, Clone)]
258pub struct CreateStoredImage {
259    pub filename: String,
260    pub content_type: String,
261    pub data: Vec<u8>,
262    pub metadata: serde_json::Value,
263}
264
265#[async_trait]
266pub trait ImageArtifactStore: Send + Sync {
267    /// Persist an image artifact and return its durable metadata.
268    async fn create_image(&self, input: CreateStoredImage) -> Result<StoredImageInfo>;
269
270    /// Load a stored image artifact including bytes.
271    async fn get_image(&self, image_id: ImageId) -> Result<Option<StoredImage>>;
272
273    /// Load stored image metadata without binary data.
274    async fn get_image_info(&self, image_id: ImageId) -> Result<Option<StoredImageInfo>>;
275}
276
277// ============================================================================
278// ProviderCredentialStore - For tool-side provider credential resolution
279// ============================================================================
280
281/// Provider credentials resolved for tool-side API clients.
282#[derive(Debug, Clone)]
283pub struct ProviderCredentials {
284    pub api_key: String,
285    pub base_url: Option<String>,
286}
287
288#[async_trait]
289pub trait ProviderCredentialStore: Send + Sync {
290    /// Resolve default credentials for a provider type (for example `openai`).
291    ///
292    /// Implementations may apply environment fallbacks internally, but tools
293    /// should never read provider env vars directly.
294    async fn get_default_provider_credentials(
295        &self,
296        provider_type: &str,
297    ) -> Result<Option<ProviderCredentials>>;
298}
299
300// ============================================================================
301// ToolExecutor - For executing tool calls
302// ============================================================================
303
304/// Trait for executing tool calls
305///
306/// Implementations handle the actual tool execution:
307/// - Webhook calls
308/// - Built-in function execution
309/// - Mock execution for testing
310#[async_trait]
311pub trait ToolExecutor: Send + Sync {
312    /// Execute a single tool call (without context)
313    ///
314    /// This is the legacy method that doesn't provide context to tools.
315    /// Use `execute_with_context` when context is available.
316    async fn execute(&self, tool_call: &ToolCall, tool_def: &ToolDefinition) -> Result<ToolResult>;
317
318    /// Execute a single tool call with context
319    ///
320    /// This method provides runtime context to tools that need it (like filesystem tools).
321    /// The default implementation delegates to `execute()`.
322    async fn execute_with_context(
323        &self,
324        tool_call: &ToolCall,
325        tool_def: &ToolDefinition,
326        _context: &ToolContext,
327    ) -> Result<ToolResult> {
328        // Default: delegate to execute(), ignoring context
329        self.execute(tool_call, tool_def).await
330    }
331
332    /// Execute multiple tool calls (default: sequential)
333    async fn execute_batch(
334        &self,
335        tool_calls: &[ToolCall],
336        tool_defs: &[ToolDefinition],
337    ) -> Result<Vec<ToolResult>> {
338        let mut results = Vec::with_capacity(tool_calls.len());
339
340        let tool_map = build_tool_map(tool_defs);
341
342        for tool_call in tool_calls {
343            let tool_def = tool_map.get(tool_call.name.as_str()).ok_or_else(|| {
344                crate::error::AgentLoopError::tool(format!(
345                    "Tool definition not found: {}",
346                    tool_call.name
347                ))
348            })?;
349
350            results.push(self.execute(tool_call, tool_def).await?);
351        }
352
353        Ok(results)
354    }
355
356    /// Execute multiple tool calls in parallel
357    async fn execute_parallel(
358        &self,
359        tool_calls: &[ToolCall],
360        tool_defs: &[ToolDefinition],
361    ) -> Result<Vec<ToolResult>>
362    where
363        Self: Sized,
364    {
365        use futures::future::join_all;
366
367        let tool_map = build_tool_map(tool_defs);
368
369        let futures: Vec<_> = tool_calls
370            .iter()
371            .map(|tool_call| async {
372                let tool_def = tool_map.get(tool_call.name.as_str()).ok_or_else(|| {
373                    crate::error::AgentLoopError::tool(format!(
374                        "Tool definition not found: {}",
375                        tool_call.name
376                    ))
377                })?;
378                self.execute(tool_call, tool_def).await
379            })
380            .collect();
381
382        let results = join_all(futures).await;
383        results.into_iter().collect()
384    }
385}
386
387/// Delegating impl so callers can hold a `ToolExecutor` as a trait object
388/// (e.g. to choose between a plain registry and an MCP-routing composite at
389/// runtime without monomorphizing the consumer).
390#[async_trait]
391impl ToolExecutor for std::sync::Arc<dyn ToolExecutor> {
392    async fn execute(&self, tool_call: &ToolCall, tool_def: &ToolDefinition) -> Result<ToolResult> {
393        (**self).execute(tool_call, tool_def).await
394    }
395
396    async fn execute_with_context(
397        &self,
398        tool_call: &ToolCall,
399        tool_def: &ToolDefinition,
400        context: &ToolContext,
401    ) -> Result<ToolResult> {
402        (**self)
403            .execute_with_context(tool_call, tool_def, context)
404            .await
405    }
406
407    async fn execute_batch(
408        &self,
409        tool_calls: &[ToolCall],
410        tool_defs: &[ToolDefinition],
411    ) -> Result<Vec<ToolResult>> {
412        (**self).execute_batch(tool_calls, tool_defs).await
413    }
414}
415
416// ============================================================================
417// SessionFileSystem - For session filesystem operations
418// ============================================================================
419
420/// Trait for session filesystem operations
421///
422/// This trait abstracts the session filesystem contract for tools and hosts.
423/// Implementations can:
424/// - Store files in a database (production)
425/// - Use an in-memory filesystem for testing
426/// - Project files onto real disk or object storage
427#[async_trait]
428pub trait SessionFileSystem: Send + Sync {
429    /// Human-facing root path for this filesystem.
430    ///
431    /// `/workspace` is the stable agent namespace and the default. Embedded
432    /// runtimes backed by a host directory override this to expose the real
433    /// root, so shared capabilities avoid misleading users about where files
434    /// live.
435    fn display_root(&self) -> String {
436        crate::session_path::WORKSPACE_PREFIX.to_string()
437    }
438
439    /// Convert a canonical session path into a human-facing path.
440    ///
441    /// The default renders the `/workspace` alias; host-backed stores and
442    /// [`MountFs`](crate::mount_fs::MountFs) override it.
443    fn display_path(&self, path: &str) -> String {
444        crate::session_path::to_display_path(path)
445    }
446
447    /// Resolve an input path (any accepted spelling, relative or absolute) to an
448    /// absolute path within this filesystem's namespace. Relative inputs resolve
449    /// against the filesystem's current directory.
450    ///
451    /// This is how a shell seeds its working directory: a resolver like
452    /// [`MountFs`] returns the virtual path (`/workspace/sub`), so the shell and
453    /// the file tools address one namespace without the shell re-implementing
454    /// any `/workspace` handling. The default is the flat VFS session form.
455    ///
456    /// [`MountFs`]: crate::mount_fs::MountFs
457    fn resolve_path(&self, input: &str) -> String {
458        crate::session_path::to_session_path(input)
459    }
460
461    /// Read a file by path
462    async fn read_file(&self, session_id: SessionId, path: &str) -> Result<Option<SessionFile>>;
463
464    /// Write/create a file
465    async fn write_file(
466        &self,
467        session_id: SessionId,
468        path: &str,
469        content: &str,
470        encoding: &str,
471    ) -> Result<SessionFile>;
472
473    /// Write a file only if its current content snapshot still matches.
474    ///
475    /// Implementations backed by transactional storage should override this
476    /// with an atomic compare-and-set update.
477    async fn write_file_if_content_matches(
478        &self,
479        session_id: SessionId,
480        path: &str,
481        expected_content: &str,
482        expected_encoding: &str,
483        content: &str,
484        encoding: &str,
485    ) -> Result<Option<SessionFile>> {
486        let Some(existing) = self.read_file(session_id, path).await? else {
487            return Ok(None);
488        };
489
490        if existing.is_directory {
491            return Ok(None);
492        }
493
494        let current_content = existing.content.unwrap_or_default();
495        if current_content != expected_content || existing.encoding != expected_encoding {
496            return Ok(None);
497        }
498
499        self.write_file(session_id, path, content, encoding)
500            .await
501            .map(Some)
502    }
503
504    /// Delete a file or directory
505    async fn delete_file(&self, session_id: SessionId, path: &str, recursive: bool)
506    -> Result<bool>;
507
508    /// List files in a directory
509    async fn list_directory(&self, session_id: SessionId, path: &str) -> Result<Vec<FileInfo>>;
510
511    /// Get file metadata
512    async fn stat_file(&self, session_id: SessionId, path: &str) -> Result<Option<FileStat>>;
513
514    /// Search files by pattern (grep)
515    async fn grep_files(
516        &self,
517        session_id: SessionId,
518        pattern: &str,
519        path_pattern: Option<&str>,
520    ) -> Result<Vec<GrepMatch>>;
521
522    /// Create a directory
523    async fn create_directory(&self, session_id: SessionId, path: &str) -> Result<FileInfo>;
524
525    /// Seed a starter file into a session workspace.
526    async fn seed_initial_file(&self, session_id: SessionId, file: &InitialFile) -> Result<()> {
527        if file.is_readonly {
528            return Err(crate::error::AgentLoopError::store(
529                "read-only initial files require a SessionFileSystem-specific seed implementation",
530            ));
531        }
532        self.write_file(session_id, &file.path, &file.content, &file.encoding)
533            .await?;
534        Ok(())
535    }
536}
537
538/// A [`SessionFileSystem`] decorator that pins every operation to a fixed
539/// workspace key, ignoring the per-call `session_id`.
540///
541/// Used to re-key file I/O for a session attached to a shared workspace (where
542/// `workspace.id != session.id`): wrap the session's file store once with the
543/// session's `workspace_id`, and all downstream capability/tool access then
544/// addresses the attached workspace rather than the session's own keyspace. For
545/// the default 1:1 session the key equals the session id, so the wrapper is a
546/// transparent pass-through. See `specs/workspace.md`.
547pub struct WorkspaceScopedFileSystem {
548    inner: Arc<dyn SessionFileSystem>,
549    key: SessionId,
550}
551
552impl WorkspaceScopedFileSystem {
553    /// Wrap `inner`, pinning all operations to `workspace_id`'s key.
554    pub fn wrap(
555        inner: Arc<dyn SessionFileSystem>,
556        workspace_id: WorkspaceId,
557    ) -> Arc<dyn SessionFileSystem> {
558        Arc::new(Self {
559            inner,
560            key: SessionId::from_uuid(workspace_id.uuid()),
561        })
562    }
563}
564
565#[async_trait]
566impl SessionFileSystem for WorkspaceScopedFileSystem {
567    async fn read_file(&self, _session_id: SessionId, path: &str) -> Result<Option<SessionFile>> {
568        self.inner.read_file(self.key, path).await
569    }
570    async fn write_file(
571        &self,
572        _session_id: SessionId,
573        path: &str,
574        content: &str,
575        encoding: &str,
576    ) -> Result<SessionFile> {
577        self.inner
578            .write_file(self.key, path, content, encoding)
579            .await
580    }
581    async fn write_file_if_content_matches(
582        &self,
583        _session_id: SessionId,
584        path: &str,
585        expected_content: &str,
586        expected_encoding: &str,
587        content: &str,
588        encoding: &str,
589    ) -> Result<Option<SessionFile>> {
590        self.inner
591            .write_file_if_content_matches(
592                self.key,
593                path,
594                expected_content,
595                expected_encoding,
596                content,
597                encoding,
598            )
599            .await
600    }
601    async fn delete_file(
602        &self,
603        _session_id: SessionId,
604        path: &str,
605        recursive: bool,
606    ) -> Result<bool> {
607        self.inner.delete_file(self.key, path, recursive).await
608    }
609    async fn list_directory(&self, _session_id: SessionId, path: &str) -> Result<Vec<FileInfo>> {
610        self.inner.list_directory(self.key, path).await
611    }
612    async fn stat_file(&self, _session_id: SessionId, path: &str) -> Result<Option<FileStat>> {
613        self.inner.stat_file(self.key, path).await
614    }
615    async fn grep_files(
616        &self,
617        _session_id: SessionId,
618        pattern: &str,
619        path_pattern: Option<&str>,
620    ) -> Result<Vec<GrepMatch>> {
621        self.inner.grep_files(self.key, pattern, path_pattern).await
622    }
623    async fn create_directory(&self, _session_id: SessionId, path: &str) -> Result<FileInfo> {
624        self.inner.create_directory(self.key, path).await
625    }
626    async fn seed_initial_file(&self, _session_id: SessionId, file: &InitialFile) -> Result<()> {
627        self.inner.seed_initial_file(self.key, file).await
628    }
629
630    fn display_root(&self) -> String {
631        self.inner.display_root()
632    }
633
634    fn display_path(&self, path: &str) -> String {
635        self.inner.display_path(path)
636    }
637
638    fn resolve_path(&self, input: &str) -> String {
639        self.inner.resolve_path(input)
640    }
641}
642
643#[async_trait]
644impl<T: SessionFileSystem + ?Sized> SessionFileSystem for std::sync::Arc<T> {
645    fn display_root(&self) -> String {
646        (**self).display_root()
647    }
648
649    fn display_path(&self, path: &str) -> String {
650        (**self).display_path(path)
651    }
652
653    fn resolve_path(&self, input: &str) -> String {
654        (**self).resolve_path(input)
655    }
656
657    async fn read_file(&self, session_id: SessionId, path: &str) -> Result<Option<SessionFile>> {
658        (**self).read_file(session_id, path).await
659    }
660
661    async fn write_file(
662        &self,
663        session_id: SessionId,
664        path: &str,
665        content: &str,
666        encoding: &str,
667    ) -> Result<SessionFile> {
668        (**self)
669            .write_file(session_id, path, content, encoding)
670            .await
671    }
672
673    async fn write_file_if_content_matches(
674        &self,
675        session_id: SessionId,
676        path: &str,
677        expected_content: &str,
678        expected_encoding: &str,
679        content: &str,
680        encoding: &str,
681    ) -> Result<Option<SessionFile>> {
682        (**self)
683            .write_file_if_content_matches(
684                session_id,
685                path,
686                expected_content,
687                expected_encoding,
688                content,
689                encoding,
690            )
691            .await
692    }
693
694    async fn delete_file(
695        &self,
696        session_id: SessionId,
697        path: &str,
698        recursive: bool,
699    ) -> Result<bool> {
700        (**self).delete_file(session_id, path, recursive).await
701    }
702
703    async fn list_directory(&self, session_id: SessionId, path: &str) -> Result<Vec<FileInfo>> {
704        (**self).list_directory(session_id, path).await
705    }
706
707    async fn stat_file(&self, session_id: SessionId, path: &str) -> Result<Option<FileStat>> {
708        (**self).stat_file(session_id, path).await
709    }
710
711    async fn grep_files(
712        &self,
713        session_id: SessionId,
714        pattern: &str,
715        path_pattern: Option<&str>,
716    ) -> Result<Vec<GrepMatch>> {
717        (**self).grep_files(session_id, pattern, path_pattern).await
718    }
719
720    async fn create_directory(&self, session_id: SessionId, path: &str) -> Result<FileInfo> {
721        (**self).create_directory(session_id, path).await
722    }
723
724    async fn seed_initial_file(&self, session_id: SessionId, file: &InitialFile) -> Result<()> {
725        (**self).seed_initial_file(session_id, file).await
726    }
727}
728
729/// Backward-compatible alias for the old session filesystem trait name.
730pub use SessionFileSystem as SessionFileStore;
731
732/// Host-supplied values used by platform file-system factories.
733///
734/// The context is intentionally type-erased so `everruns-core` can own the
735/// platform contract without depending on server-only types such as
736/// `StorageBackend` or future object-storage clients.
737#[derive(Clone, Default)]
738pub struct SessionFileSystemFactoryContext {
739    values: Arc<HashMap<TypeId, Arc<dyn Any + Send + Sync>>>,
740}
741
742impl SessionFileSystemFactoryContext {
743    pub fn new() -> Self {
744        Self::default()
745    }
746
747    pub fn with<T: Any + Send + Sync>(mut self, value: Arc<T>) -> Self {
748        let values = Arc::make_mut(&mut self.values);
749        values.insert(TypeId::of::<T>(), value);
750        self
751    }
752
753    pub fn get<T: Any + Send + Sync>(&self) -> Option<Arc<T>> {
754        self.values
755            .get(&TypeId::of::<T>())
756            .and_then(|value| value.clone().downcast::<T>().ok())
757    }
758
759    pub fn with_workspace_roots(self, roots: Arc<crate::WorkspaceRootSet>) -> Self {
760        self.with(roots)
761    }
762
763    pub fn workspace_roots(&self) -> Option<Arc<crate::WorkspaceRootSet>> {
764        self.get::<crate::WorkspaceRootSet>()
765    }
766}
767
768/// Factory for deployment-selected session filesystem implementations.
769#[async_trait]
770pub trait SessionFileSystemFactory: Send + Sync {
771    /// Human-readable factory name for diagnostics.
772    fn name(&self) -> &'static str {
773        "SessionFileSystemFactory"
774    }
775
776    /// Whether this factory intentionally leaves filesystem selection to the
777    /// runtime default.
778    fn is_disabled(&self) -> bool {
779        false
780    }
781
782    /// Resolve a live filesystem from host-provided dependencies.
783    async fn create_session_file_system(
784        &self,
785        context: SessionFileSystemFactoryContext,
786    ) -> Result<Arc<dyn SessionFileSystem>>;
787}
788
789/// Default factory used when a platform does not configure session files.
790#[derive(Debug, Clone, Default)]
791pub struct DisabledSessionFileSystemFactory;
792
793#[async_trait]
794impl SessionFileSystemFactory for DisabledSessionFileSystemFactory {
795    fn name(&self) -> &'static str {
796        "DisabledSessionFileSystemFactory"
797    }
798
799    fn is_disabled(&self) -> bool {
800        true
801    }
802
803    async fn create_session_file_system(
804        &self,
805        _context: SessionFileSystemFactoryContext,
806    ) -> Result<Arc<dyn SessionFileSystem>> {
807        Err(crate::error::AgentLoopError::config(
808            "session filesystem is disabled",
809        ))
810    }
811}
812
813// ============================================================================
814// SessionStorageStore - For session key/value and secret storage
815// ============================================================================
816
817/// Info about a stored key (without its value)
818#[derive(Debug, Clone)]
819pub struct KeyInfo {
820    pub key: String,
821    pub created_at: chrono::DateTime<chrono::Utc>,
822    pub updated_at: chrono::DateTime<chrono::Utc>,
823}
824
825/// Info about a stored secret (without its value)
826#[derive(Debug, Clone)]
827pub struct SecretInfo {
828    pub name: String,
829    pub created_at: chrono::DateTime<chrono::Utc>,
830    pub updated_at: chrono::DateTime<chrono::Utc>,
831}
832
833/// Trait for session key/value and secret storage operations
834///
835/// This trait abstracts storage operations for tools that need to persist
836/// data within a session. Implementations can:
837/// - Store data in a database (production)
838/// - Use in-memory storage for testing
839///
840/// A single ranked hit from a Knowledge Base search. Public-id surface only;
841/// no internal UUIDs. See specs/knowledge-bases.md and specs/okf-adoption.md.
842#[derive(Debug, Clone, serde::Serialize)]
843pub struct KnowledgeSearchHit {
844    /// Entry public id (`kbe_…`).
845    pub id: String,
846    /// Owning Knowledge Base public id (`kb_…`).
847    pub kb_id: String,
848    pub title: String,
849    pub kind: String,
850    pub tags: Vec<String>,
851    /// Short body excerpt for the LLM.
852    pub snippet: String,
853    /// Optional OKF resource URI, when set on the entry.
854    pub resource: Option<String>,
855}
856
857/// Org-scoped search over curated Knowledge Bases, backing the agent-facing
858/// `search_knowledge` tool. Implementations MUST scope to `org_id` and silently
859/// ignore KB ids not owned by that org (no existence leak across tenants).
860#[async_trait]
861pub trait KnowledgeStore: Send + Sync {
862    async fn search_knowledge(
863        &self,
864        org_id: crate::typed_id::OrgId,
865        kb_public_ids: &[String],
866        query: &str,
867        kind: Option<&str>,
868        tags: &[String],
869        limit: usize,
870    ) -> Result<Vec<KnowledgeSearchHit>>;
871}
872
873/// Storage for session-scoped key/value pairs and secrets.
874///
875/// Key/value storage is for general data that doesn't need encryption.
876/// Secret storage is for sensitive data that is encrypted at rest.
877#[async_trait]
878pub trait SessionStorageStore: Send + Sync {
879    // Key/Value operations (plain text)
880
881    /// Set a key/value pair (creates or updates)
882    async fn set_value(&self, session_id: SessionId, key: &str, value: &str) -> Result<()>;
883
884    /// Get a value by key
885    async fn get_value(&self, session_id: SessionId, key: &str) -> Result<Option<String>>;
886
887    /// Delete a key/value pair
888    async fn delete_value(&self, session_id: SessionId, key: &str) -> Result<bool>;
889
890    /// List all keys in a session
891    async fn list_keys(&self, session_id: SessionId) -> Result<Vec<KeyInfo>>;
892
893    // Secret operations (encrypted)
894
895    /// Set a secret (creates or updates, value is encrypted before storage)
896    async fn set_secret(&self, session_id: SessionId, name: &str, value: &str) -> Result<()>;
897
898    /// Get a secret by name (value is decrypted before returning)
899    async fn get_secret(&self, session_id: SessionId, name: &str) -> Result<Option<String>>;
900
901    /// Delete a secret
902    async fn delete_secret(&self, session_id: SessionId, name: &str) -> Result<bool>;
903
904    /// List all secret names in a session (without values)
905    async fn list_secrets(&self, session_id: SessionId) -> Result<Vec<SecretInfo>>;
906}
907
908// ============================================================================
909// SessionScheduleStore - For session-scoped schedule operations
910// ============================================================================
911
912use crate::session_schedule::SessionSchedule;
913use crate::typed_id::ScheduleId;
914
915/// Trait for session schedule CRUD operations.
916///
917/// Used by scheduling tools to create, cancel, and list schedules.
918#[async_trait]
919pub trait SessionScheduleStore: Send + Sync {
920    /// Create a new schedule for a session.
921    async fn create_schedule(
922        &self,
923        session_id: SessionId,
924        description: String,
925        cron_expression: Option<String>,
926        scheduled_at: Option<chrono::DateTime<chrono::Utc>>,
927        timezone: String,
928    ) -> Result<SessionSchedule>;
929
930    /// Create a new schedule after enforcing create-time limits in the same
931    /// store operation. Backends with shared mutable state must override this
932    /// to make the check-and-create sequence atomic.
933    async fn create_schedule_enforcing_limits(
934        &self,
935        session_id: SessionId,
936        description: String,
937        cron_expression: Option<String>,
938        scheduled_at: Option<chrono::DateTime<chrono::Utc>>,
939        timezone: String,
940    ) -> std::result::Result<SessionSchedule, crate::session_schedule::ScheduleLimitError> {
941        let per_session = self
942            .count_active_schedules(session_id)
943            .await
944            .map_err(crate::session_schedule::ScheduleLimitError::Store)?;
945        if per_session >= crate::session_schedule::MAX_ACTIVE_SCHEDULES_PER_SESSION {
946            return Err(crate::session_schedule::ScheduleLimitError::Rejected(
947                format!(
948                    "Maximum {} active schedules per session. Cancel an existing schedule first.",
949                    crate::session_schedule::MAX_ACTIVE_SCHEDULES_PER_SESSION
950                ),
951            ));
952        }
953
954        let max_per_org = crate::session_schedule::max_active_schedules_per_org();
955        let per_org = self
956            .count_active_org_schedules()
957            .await
958            .map_err(crate::session_schedule::ScheduleLimitError::Store)?;
959        if i64::from(per_org) >= max_per_org {
960            return Err(crate::session_schedule::ScheduleLimitError::Rejected(
961                format!(
962                    "Maximum {max_per_org} active schedules per org reached. Cancel an existing schedule first."
963                ),
964            ));
965        }
966
967        if let Some(cron) = cron_expression.as_deref() {
968            crate::session_schedule::validate_cron_min_interval(cron)
969                .map_err(crate::session_schedule::ScheduleLimitError::Rejected)?;
970        }
971
972        self.create_schedule(
973            session_id,
974            description,
975            cron_expression,
976            scheduled_at,
977            timezone,
978        )
979        .await
980        .map_err(crate::session_schedule::ScheduleLimitError::Store)
981    }
982
983    /// Cancel (disable) a schedule.
984    async fn cancel_schedule(
985        &self,
986        session_id: SessionId,
987        schedule_id: ScheduleId,
988    ) -> Result<SessionSchedule>;
989
990    /// List schedules for a session.
991    async fn list_schedules(&self, session_id: SessionId) -> Result<Vec<SessionSchedule>>;
992
993    /// Count active (enabled) schedules for a session.
994    async fn count_active_schedules(&self, session_id: SessionId) -> Result<u32>;
995
996    /// Count active (enabled) schedules across the whole org this store is
997    /// scoped to. Used to enforce a per-org cap independent of session count:
998    /// `count_active_schedules` only bounds one session, so unlimited sessions
999    /// would otherwise imply unlimited active schedules per org.
1000    async fn count_active_org_schedules(&self) -> Result<u32>;
1001}
1002
1003// ============================================================================
1004// SessionResourceRegistry - Generic session-scoped resource registry
1005// ============================================================================
1006
1007/// Generic registry of resources active alongside a session.
1008///
1009/// Capabilities register resources here (sandboxes, subagents, browser sessions).
1010/// Agents query it ("what's running?"), infrastructure scans it for cleanup.
1011/// See `specs/session-resources.md`.
1012#[async_trait]
1013pub trait SessionResourceRegistry: Send + Sync {
1014    /// Register a resource (or update if resource_id already exists for this session).
1015    async fn register(
1016        &self,
1017        entry: crate::session_resource::RegisterSessionResource,
1018    ) -> Result<crate::session_resource::SessionResourceEntry>;
1019
1020    /// Update the status of a registered resource.
1021    async fn update_status(
1022        &self,
1023        session_id: SessionId,
1024        resource_id: &str,
1025        status: crate::session_resource::SessionResourceStatus,
1026    ) -> Result<Option<crate::session_resource::SessionResourceEntry>>;
1027
1028    /// Get a specific resource by ID.
1029    async fn get(
1030        &self,
1031        session_id: SessionId,
1032        resource_id: &str,
1033    ) -> Result<Option<crate::session_resource::SessionResourceEntry>>;
1034
1035    /// List resources for a session, optionally filtered.
1036    async fn list(
1037        &self,
1038        session_id: SessionId,
1039        filter: Option<&crate::session_resource::SessionResourceFilter>,
1040    ) -> Result<Vec<crate::session_resource::SessionResourceEntry>>;
1041
1042    /// Remove a resource from the registry.
1043    async fn deregister(&self, session_id: SessionId, resource_id: &str) -> Result<bool>;
1044}
1045
1046// ============================================================================
1047// LeasedResourceStore - For lifecycle-managed external resources
1048// ============================================================================
1049
1050/// Trait for session-scoped leased resource operations.
1051///
1052/// Tools use this store to register or refresh leases when they create or use
1053/// external provider resources. Cleanup workers operate through control-plane
1054/// storage APIs directly so they can claim work across organizations.
1055#[async_trait]
1056pub trait LeasedResourceStore: Send + Sync {
1057    /// Create or refresh a leased resource for a session.
1058    ///
1059    /// Implementations must treat this as an idempotent upsert keyed by the
1060    /// provider-specific resource identity so repeated tool usage extends the
1061    /// same lease instead of creating duplicate rows.
1062    async fn upsert_resource(&self, input: UpsertLeasedResource) -> Result<LeasedResource>;
1063
1064    /// Mark a leased resource as explicitly released.
1065    ///
1066    /// This is the fast path for explicit user intent such as "close browser"
1067    /// or "delete sandbox". It should transition the resource to `released`
1068    /// without waiting for the durable cleanup worker to observe lease expiry.
1069    async fn release_resource(
1070        &self,
1071        session_id: SessionId,
1072        provider: &str,
1073        resource_type: &str,
1074        external_id: &str,
1075    ) -> Result<Option<LeasedResource>>;
1076
1077    /// List leased resources currently associated with a session.
1078    ///
1079    /// Session surfaces use this for visibility. Released resources remain
1080    /// visible so operators can inspect cleanup outcomes and failure history.
1081    async fn list_resources(&self, session_id: SessionId) -> Result<Vec<LeasedResource>>;
1082}
1083
1084// ============================================================================
1085// ToolContext - Runtime context for tool execution
1086// ============================================================================
1087
1088/// Default maximum child depth for subagent delegation. Top-level sessions are
1089/// depth 0, their children are depth 1, and grandchildren are depth 2.
1090pub const DEFAULT_MAX_SUBAGENT_DEPTH: u32 = 2;
1091pub const DEFAULT_MAX_ACTIVE_DESCENDANT_SUBAGENT_TASKS: u32 = 16;
1092pub const DEFAULT_MAX_TOTAL_DESCENDANT_SUBAGENT_TASKS: u32 = 200;
1093
1094/// Resolved subagent spawn governance policy for a tool execution context.
1095#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1096pub struct SubagentNestingPolicy {
1097    pub platform_default: u32,
1098    pub org_override: Option<u32>,
1099    pub agent_override: Option<u32>,
1100    pub platform_default_max_active_descendant_tasks: u32,
1101    pub org_override_max_active_descendant_tasks: Option<u32>,
1102    pub agent_override_max_active_descendant_tasks: Option<u32>,
1103    pub platform_default_max_total_descendant_tasks: u32,
1104    pub org_override_max_total_descendant_tasks: Option<u32>,
1105    pub agent_override_max_total_descendant_tasks: Option<u32>,
1106}
1107
1108impl Default for SubagentNestingPolicy {
1109    fn default() -> Self {
1110        Self {
1111            platform_default: DEFAULT_MAX_SUBAGENT_DEPTH,
1112            org_override: None,
1113            agent_override: None,
1114            platform_default_max_active_descendant_tasks:
1115                DEFAULT_MAX_ACTIVE_DESCENDANT_SUBAGENT_TASKS,
1116            org_override_max_active_descendant_tasks: None,
1117            agent_override_max_active_descendant_tasks: None,
1118            platform_default_max_total_descendant_tasks:
1119                DEFAULT_MAX_TOTAL_DESCENDANT_SUBAGENT_TASKS,
1120            org_override_max_total_descendant_tasks: None,
1121            agent_override_max_total_descendant_tasks: None,
1122        }
1123    }
1124}
1125
1126impl SubagentNestingPolicy {
1127    pub fn max_subagent_depth(self) -> u32 {
1128        self.agent_override
1129            .or(self.org_override)
1130            .unwrap_or(self.platform_default)
1131    }
1132
1133    pub fn max_active_descendant_tasks(self) -> u32 {
1134        self.agent_override_max_active_descendant_tasks
1135            .or(self.org_override_max_active_descendant_tasks)
1136            .unwrap_or(self.platform_default_max_active_descendant_tasks)
1137    }
1138
1139    pub fn max_total_descendant_tasks(self) -> u32 {
1140        self.agent_override_max_total_descendant_tasks
1141            .or(self.org_override_max_total_descendant_tasks)
1142            .unwrap_or(self.platform_default_max_total_descendant_tasks)
1143    }
1144
1145    pub fn with_platform_default(mut self, depth: u32) -> Self {
1146        self.platform_default = depth;
1147        self
1148    }
1149
1150    pub fn with_org_override(mut self, depth: Option<u32>) -> Self {
1151        self.org_override = depth;
1152        self
1153    }
1154
1155    pub fn with_agent_override(mut self, depth: Option<u32>) -> Self {
1156        self.agent_override = depth;
1157        self
1158    }
1159
1160    pub fn with_agent_task_caps_override(
1161        mut self,
1162        max_active: Option<u32>,
1163        max_total: Option<u32>,
1164    ) -> Self {
1165        self.agent_override_max_active_descendant_tasks = max_active;
1166        self.agent_override_max_total_descendant_tasks = max_total;
1167        self
1168    }
1169}
1170
1171/// Type alias for the session SQL DB store trait object.
1172pub type SessionSqlDbStoreRef = Arc<dyn crate::session_sqldb::SessionSqlDbStore>;
1173
1174/// Resolves user connection tokens (e.g. GitHub) lazily at tool execution time.
1175///
1176/// Instead of eagerly injecting tokens at session creation, tools call this
1177/// resolver when they need a token. If the user hasn't connected, returns None.
1178#[async_trait]
1179pub trait UserConnectionResolver: Send + Sync {
1180    /// Get a decrypted connection token for the given provider.
1181    /// Returns None if the user has no connection for this provider.
1182    async fn get_connection_token(
1183        &self,
1184        session_id: SessionId,
1185        provider: &str,
1186    ) -> Result<Option<String>>;
1187
1188    /// Resolve the user ID of the connection used for a session/provider pair.
1189    ///
1190    /// This is used by leased resources to bind cleanup to the same provider
1191    /// identity that created the remote resource.
1192    async fn get_connection_user(
1193        &self,
1194        _session_id: SessionId,
1195        _provider: &str,
1196    ) -> Result<Option<Uuid>> {
1197        Ok(None)
1198    }
1199
1200    /// Resolve a provider token for a specific user.
1201    ///
1202    /// Cleanup workers use this to avoid "first org member wins" behavior when
1203    /// cleaning resources created by a specific provider connection owner.
1204    async fn get_connection_token_for_user(
1205        &self,
1206        _user_id: Uuid,
1207        _provider: &str,
1208    ) -> Result<Option<String>> {
1209        Ok(None)
1210    }
1211
1212    /// Get provider-specific metadata stored alongside the connection.
1213    /// Returns None if no metadata is stored or no connection exists.
1214    async fn get_connection_metadata(
1215        &self,
1216        _session_id: SessionId,
1217        _provider: &str,
1218    ) -> Result<Option<serde_json::Value>> {
1219        Ok(None)
1220    }
1221}
1222
1223// ============================================================================
1224// BudgetChecker - For querying budget status from tools
1225// ============================================================================
1226
1227/// Trait for checking budget status from within tool execution.
1228///
1229/// Implemented by gRPC adapters (worker → server) and direct adapters (in-process).
1230/// Used by the `check_budget` tool to return real budget data to agents.
1231/// The org_id is captured at construction time by the implementing adapter.
1232#[async_trait]
1233pub trait BudgetChecker: Send + Sync {
1234    /// Check all budgets for a session and return a tool-friendly response.
1235    async fn check_budgets(&self, session_id: &str) -> Result<crate::budget::BudgetToolResponse>;
1236}
1237
1238// ============================================================================
1239// PaymentAuthority - For capability-internal machine payments
1240// ============================================================================
1241
1242/// Internal authority for paid capability operations.
1243///
1244/// Capabilities call this with fixed, typed requests. The model never receives a
1245/// generic paid HTTP tool, wallet credentials, or payment payloads.
1246#[async_trait]
1247pub trait PaymentAuthority: Send + Sync {
1248    async fn execute_machine_payment(
1249        &self,
1250        session_id: SessionId,
1251        request: crate::payment::MachinePaymentRequest,
1252    ) -> Result<crate::payment::MachinePaymentResponse>;
1253}
1254
1255// OutboundToolRateLimiter - Per-org outbound tool-call rate limiting (TM-TOOL-009)
1256// ============================================================================
1257
1258/// Per-org gate on outbound tool execution.
1259///
1260/// Returns `true` if the call is within the per-org budget, `false` if the
1261/// org has exceeded its outbound tool rate limit for this window.
1262/// Implementations must be fail-open: Valkey/backend errors should return `true`
1263/// rather than blocking legitimate tool calls.
1264#[async_trait]
1265pub trait OutboundToolRateLimiter: Send + Sync {
1266    /// Key by the public org UUID (keyed string representation).
1267    async fn check_org(&self, org_id: &crate::typed_id::OrgId) -> bool;
1268}
1269
1270// ============================================================================
1271// DurableToolResultStore — per-tool-call idempotency (EVE-530)
1272// ============================================================================
1273
1274/// Result of a claim attempt on the per-tool-call idempotency store.
1275#[derive(Debug)]
1276pub enum ToolCallClaimResult {
1277    /// First claim for this (turn_id, tool_call_id); caller should execute the tool.
1278    /// `claim_token` must be passed to `settle_tool_call` to verify ownership.
1279    Claimed { claim_token: uuid::Uuid },
1280    /// A prior run already settled this call; replay the stored result.
1281    AlreadySettled {
1282        result_json: serde_json::Value,
1283        args_fingerprint: String,
1284    },
1285    /// A prior run started but never settled. For `AtMostOnce` tools the
1286    /// caller should NOT re-execute; for `Pure`/`Idempotent` tools the caller
1287    /// may re-execute and then try to settle (the settle CAS will be a no-op if
1288    /// a different claimer wins first).
1289    AlreadyRunning { args_fingerprint: String },
1290    /// A settled row exists but its `args_fingerprint` does not match the
1291    /// current call — this is a determinism violation (workflow replay with
1292    /// different inputs). The workflow should be failed loudly.
1293    DeterminismViolation {
1294        stored_fingerprint: String,
1295        current_fingerprint: String,
1296    },
1297}
1298
1299/// Read-only status of a tool call in durable storage (EVE-533).
1300#[derive(Debug, Clone)]
1301pub enum DurableToolCallStatus {
1302    /// Tool completed successfully or with an error; result is stored.
1303    Settled { result_json: serde_json::Value },
1304    /// Tool was settled with `interrupted` status; result may contain error details.
1305    Interrupted {
1306        result_json: Option<serde_json::Value>,
1307    },
1308    /// A claim exists but the tool never finished.
1309    Running,
1310}
1311
1312/// Durable per-tool-call idempotency store (EVE-530).
1313///
1314/// Implements the claim/settle CAS that prevents double-execution of
1315/// `AtMostOnce` tools on worker reclaim/replay.
1316#[async_trait]
1317pub trait DurableToolResultStore: Send + Sync + 'static {
1318    /// Atomically claim `(turn_id, tool_call_id)` before tool dispatch.
1319    ///
1320    /// - Inserts a `running` row if none exists → `Claimed`.
1321    /// - Finds an existing `settled` row → `AlreadySettled`.
1322    /// - Finds an existing `running` row → `AlreadyRunning`.
1323    /// - Finds a `settled` row with a mismatched `args_fingerprint`
1324    ///   (determinism violation) → `DeterminismViolation`.
1325    async fn try_claim_tool_call(
1326        &self,
1327        turn_id: &str,
1328        tool_call_id: &str,
1329        tool_name: &str,
1330        args_fingerprint: &str,
1331    ) -> Result<ToolCallClaimResult>;
1332
1333    /// Settle a previously claimed tool call with its result.
1334    ///
1335    /// `claim_token` must match the token returned by `try_claim_tool_call`.
1336    /// Returns `Ok(true)` if the row was updated, `Ok(false)` if the claim
1337    /// token no longer matches (ownership lost — treat as a warning).
1338    async fn settle_tool_call(
1339        &self,
1340        turn_id: &str,
1341        tool_call_id: &str,
1342        result_json: serde_json::Value,
1343        status: &str,
1344        claim_token: uuid::Uuid,
1345    ) -> Result<bool>;
1346
1347    /// Read-only lookup of a tool call's current status in durable storage (EVE-533).
1348    ///
1349    /// Used by transcript repair to decide whether to replay a stored result or
1350    /// synthesize an interrupted placeholder. Returns `None` if no row exists.
1351    async fn get_tool_call_status(
1352        &self,
1353        turn_id: &str,
1354        tool_call_id: &str,
1355    ) -> Result<Option<DurableToolCallStatus>>;
1356}
1357
1358/// No-op implementation — used when no durable store is configured (dev/test).
1359/// Every call is treated as a fresh first execution; no replay or ownership checks.
1360pub struct NoopDurableToolResultStore;
1361
1362#[async_trait]
1363impl DurableToolResultStore for NoopDurableToolResultStore {
1364    async fn try_claim_tool_call(
1365        &self,
1366        _turn_id: &str,
1367        _tool_call_id: &str,
1368        _tool_name: &str,
1369        _args_fingerprint: &str,
1370    ) -> Result<ToolCallClaimResult> {
1371        Ok(ToolCallClaimResult::Claimed {
1372            claim_token: uuid::Uuid::new_v4(),
1373        })
1374    }
1375
1376    async fn settle_tool_call(
1377        &self,
1378        _turn_id: &str,
1379        _tool_call_id: &str,
1380        _result_json: serde_json::Value,
1381        _status: &str,
1382        _claim_token: uuid::Uuid,
1383    ) -> Result<bool> {
1384        Ok(true)
1385    }
1386
1387    async fn get_tool_call_status(
1388        &self,
1389        _turn_id: &str,
1390        _tool_call_id: &str,
1391    ) -> Result<Option<DurableToolCallStatus>> {
1392        Ok(None)
1393    }
1394}
1395
1396// ============================================================================
1397// StreamHeartbeater — per-stream liveness signal for Reason activity (EVE-531)
1398// ============================================================================
1399
1400/// Progress snapshot carried in each stream heartbeat.
1401#[derive(Debug, Clone)]
1402pub struct StreamProgress {
1403    /// Accumulated text + thinking length (characters) at the time of heartbeat.
1404    pub accumulated_len: usize,
1405    /// Wall-clock time of the most recent received token (Unix seconds).
1406    pub last_delta_at: u64,
1407}
1408
1409/// Heartbeater the Reason streaming loop calls on delta batches and a keepalive
1410/// timer, signalling that the provider connection is alive.
1411///
1412/// Implementations bridge to the durable-execution layer (e.g. gRPC).
1413/// The no-op is used in dev/test where no durable store is present.
1414#[async_trait]
1415pub trait StreamHeartbeater: Send + Sync {
1416    /// Signal stream liveness with current progress.
1417    ///
1418    /// Must be best-effort: errors must not propagate to the caller.
1419    /// Cancel-safety is critical — if the worker dies the heartbeat stops
1420    /// and the existing task-level reclaim takes over.
1421    async fn heartbeat(&self, progress: StreamProgress);
1422}
1423
1424/// No-op heartbeater — treats every stream as perpetually alive (dev/test).
1425pub struct NoopStreamHeartbeater;
1426
1427#[async_trait]
1428impl StreamHeartbeater for NoopStreamHeartbeater {
1429    async fn heartbeat(&self, _progress: StreamProgress) {}
1430}
1431
1432// ============================================================================
1433// PartialStreamStore — partial-stream recovery for Reason activity (EVE-532)
1434// ============================================================================
1435
1436/// State of a partially-streamed assistant message detected in the event log.
1437#[derive(Debug, Clone)]
1438pub struct PartialStreamState {
1439    /// Accumulated text from the last `output.message.delta` for the turn.
1440    /// Empty when `output.message.started` was emitted but no delta arrived.
1441    pub accumulated: String,
1442}
1443
1444/// Consults the persisted event log to detect whether a `reason` activity
1445/// was interrupted after `output.message.started` but before
1446/// `output.message.completed` or `output.message.replaced`.
1447///
1448/// Used by `ReasonAtom` on re-entry to apply the ContinuePartial recovery
1449/// policy (EVE-532): finalize the partial text without a second provider call,
1450/// or restart clean if the partial is unusable.
1451#[async_trait]
1452pub trait PartialStreamStore: Send + Sync {
1453    /// Return the partial-stream state for `(session_id, turn_id)` if an
1454    /// in-flight assistant message exists (started but not completed).
1455    async fn get_partial_stream(
1456        &self,
1457        session_id: SessionId,
1458        turn_id: &str,
1459    ) -> Result<Option<PartialStreamState>>;
1460}
1461
1462/// No-op — always reports no partial stream (dev/test / in-memory mode).
1463pub struct NoopPartialStreamStore;
1464
1465#[async_trait]
1466impl PartialStreamStore for NoopPartialStreamStore {
1467    async fn get_partial_stream(
1468        &self,
1469        _session_id: SessionId,
1470        _turn_id: &str,
1471    ) -> Result<Option<PartialStreamState>> {
1472        Ok(None)
1473    }
1474}
1475
1476/// Runtime context provided to tools during execution.
1477///
1478/// This context contains:
1479/// - Session ID for scoping operations
1480/// - Optional stores for tools that need external access
1481///
1482/// Tools that need context-aware execution (like filesystem tools) can use
1483/// the `execute_with_context` method on the Tool trait.
1484#[derive(Clone)]
1485pub struct ToolContext {
1486    /// The session ID for the current execution
1487    pub session_id: SessionId,
1488    /// The workspace this session is attached to — the key for the virtual
1489    /// file store. For the default 1:1 session this equals
1490    /// `WorkspaceId::from_uuid(session_id.uuid())`; for a shared workspace it
1491    /// differs. File-system tools MUST key by this (via `workspace_fs_key`)
1492    /// rather than `session_id` so shared-workspace sessions read/write the
1493    /// attached workspace's files. See specs/workspace.md.
1494    pub workspace_id: WorkspaceId,
1495
1496    /// Optional file store for filesystem operations
1497    pub file_store: Option<Arc<dyn SessionFileSystem>>,
1498
1499    /// Optional storage store for key/value and secret storage
1500    pub storage_store: Option<Arc<dyn SessionStorageStore>>,
1501
1502    /// Optional durable image artifact store for tool-side media persistence.
1503    pub image_store: Option<Arc<dyn ImageArtifactStore>>,
1504
1505    /// Optional provider credential store for tool-side API clients.
1506    pub provider_credential_store: Option<Arc<dyn ProviderCredentialStore>>,
1507
1508    /// Optional system utility LLM service for capability internals.
1509    pub utility_llm_service: Option<Arc<dyn crate::UtilityLlmService>>,
1510
1511    /// Optional scoped-MCP tool invoker for capability internals that need to
1512    /// call an MCP server out-of-band (e.g. the guardrails `mcp` check
1513    /// delegating a decision to an external guardrail endpoint). The invoker
1514    /// resolves connections and credentials per the current session/org, so
1515    /// tenant scoping is enforced by the host that supplies it.
1516    pub mcp_invoker: Option<Arc<dyn crate::McpToolInvoker>>,
1517
1518    /// Optional outbound egress service for HTTP/API traffic.
1519    pub egress_service: Option<Arc<dyn crate::EgressService>>,
1520
1521    /// Optional session SQL database store
1522    pub sqldb_store: Option<SessionSqlDbStoreRef>,
1523
1524    /// Optional message retriever for tools that need conversation history access
1525    pub message_retriever: Option<Arc<dyn crate::message_retriever::MessageRetriever>>,
1526
1527    /// Optional session store for tools that need session metadata access.
1528    pub session_store: Option<Arc<dyn SessionStore>>,
1529
1530    /// Optional session mutator for tools that need to update session metadata.
1531    pub session_mutator: Option<Arc<dyn SessionMutator>>,
1532
1533    /// Optional agent store for tools that need agent metadata access.
1534    pub agent_store: Option<Arc<dyn AgentStore>>,
1535
1536    /// Optional resolver for user connection tokens (lazy GitHub token lookup, etc.)
1537    pub connection_resolver: Option<Arc<dyn UserConnectionResolver>>,
1538
1539    /// Optional session schedule store for scheduling tools.
1540    pub schedule_store: Option<Arc<dyn SessionScheduleStore>>,
1541
1542    /// Optional platform store for org-level management tools.
1543    pub platform_store: Option<Arc<dyn crate::platform_store::PlatformStore>>,
1544    /// Optional knowledge store backing the `search_knowledge` tool.
1545    pub knowledge_store: Option<Arc<dyn KnowledgeStore>>,
1546
1547    /// Optional hybrid retrieval over bound Knowledge Indexes for the
1548    /// `search_index` tool. Server-implemented; populated only on the server
1549    /// act path alongside `platform_store` / `connection_resolver`.
1550    pub knowledge_index_search: Option<Arc<dyn crate::vector_store::KnowledgeIndexSearch>>,
1551
1552    /// Optional leased resource store for lifecycle-managed provider resources.
1553    pub leased_resource_store: Option<Arc<dyn LeasedResourceStore>>,
1554
1555    /// Optional session resource registry — generic registry of active resources.
1556    pub session_resource_registry: Option<Arc<dyn SessionResourceRegistry>>,
1557
1558    /// Optional session task registry — background work owned by the session
1559    /// (specs/session-tasks.md).
1560    pub session_task_registry: Option<Arc<dyn crate::session_task::SessionTaskRegistry>>,
1561
1562    /// Optional event emitter for tools that need to stream progress updates.
1563    /// When set, tools can emit `tool.progress` events during execution.
1564    pub event_emitter: Option<Arc<dyn EventEmitter>>,
1565
1566    /// Event context for correlating progress events with the current tool call.
1567    /// Set by ActAtom when constructing the ToolContext.
1568    pub event_context: Option<crate::events::EventContext>,
1569
1570    /// The tool call ID for the current execution (set by ActAtom).
1571    /// Used by tools to emit correlated progress events.
1572    pub tool_call_id: Option<String>,
1573    /// Optional capability registry for blueprint lookups.
1574    pub capability_registry: Option<crate::capabilities::CapabilityRegistry>,
1575
1576    /// Optional registry of active built-in tools for meta-tools such as
1577    /// `spawn_background` that need to inspect or delegate to sibling tools.
1578    pub tool_registry: Option<Arc<crate::tools::ToolRegistry>>,
1579
1580    /// Optional allowlist of tools visible to the model for this turn.
1581    /// Registry-introspecting tools must filter through this before returning
1582    /// sibling tool metadata, because the execution registry can be a superset.
1583    pub visible_tool_names: Option<Arc<HashSet<String>>>,
1584
1585    /// Optional org ID for org-scoped operations.
1586    pub org_id: Option<crate::typed_id::OrgId>,
1587
1588    /// Merged network access list (harness ∩ agent ∩ session).
1589    /// When set, tools that make HTTP requests must check URLs against this list.
1590    pub network_access: Option<crate::network_access::NetworkAccessList>,
1591
1592    /// Resolved locale for localized tool behavior (BCP 47, e.g. `uk-UA`).
1593    /// When set, tools that support localization use this to produce
1594    /// locale-appropriate descriptions, error messages, and prompts.
1595    pub locale: Option<String>,
1596
1597    /// Optional budget checker for the check_budget tool.
1598    pub budget_checker: Option<Arc<dyn BudgetChecker>>,
1599
1600    /// Optional internal payment authority for paid capability tools.
1601    pub payment_authority: Option<Arc<dyn PaymentAuthority>>,
1602
1603    /// Optional durable spawn handle store for subagent reattach (EVE-535).
1604    /// When set, subagent delegation uses claim/settle to prevent duplicate spawning
1605    /// on parent worker reclaim.
1606    pub subagent_spawn_store: Option<Arc<dyn SubagentSpawnStore>>,
1607
1608    /// Resolved subagent nesting policy for this turn.
1609    pub subagent_nesting_policy: SubagentNestingPolicy,
1610
1611    /// Optional live reasoning-effort handle (EVE-595). When set, a tool can
1612    /// change the reasoning effort mid-turn; subsequent LLM steps in the same
1613    /// `run_turn` re-read it and use the new effort.
1614    pub reasoning_effort_handle: Option<ReasoningEffortHandle>,
1615}
1616
1617impl ToolContext {
1618    /// The virtual-file-store key for this execution, derived from the attached
1619    /// workspace. Carried through the `SessionFileSystem` trait's `SessionId`
1620    /// parameter (the store keys by `.uuid()`), so a shared-workspace session
1621    /// addresses the workspace's files rather than its own session-id keyspace.
1622    pub fn workspace_fs_key(&self) -> SessionId {
1623        SessionId::from_uuid(self.workspace_id.uuid())
1624    }
1625
1626    /// Override the attached workspace (default is the 1:1 session-derived id).
1627    pub fn with_workspace_id(mut self, workspace_id: WorkspaceId) -> Self {
1628        self.workspace_id = workspace_id;
1629        self
1630    }
1631
1632    /// Create a new tool context with just a session ID
1633    pub fn new(session_id: SessionId) -> Self {
1634        Self {
1635            session_id,
1636            workspace_id: WorkspaceId::from_uuid(session_id.uuid()),
1637            file_store: None,
1638            storage_store: None,
1639            image_store: None,
1640            provider_credential_store: None,
1641            utility_llm_service: None,
1642            mcp_invoker: None,
1643            egress_service: None,
1644            sqldb_store: None,
1645            message_retriever: None,
1646            session_store: None,
1647            session_mutator: None,
1648            agent_store: None,
1649            connection_resolver: None,
1650            schedule_store: None,
1651            platform_store: None,
1652            knowledge_store: None,
1653            knowledge_index_search: None,
1654            leased_resource_store: None,
1655            session_resource_registry: None,
1656            session_task_registry: None,
1657            event_emitter: None,
1658            event_context: None,
1659            tool_call_id: None,
1660            capability_registry: None,
1661            tool_registry: None,
1662            visible_tool_names: None,
1663            org_id: None,
1664            network_access: None,
1665            locale: None,
1666            budget_checker: None,
1667            payment_authority: None,
1668            subagent_spawn_store: None,
1669            subagent_nesting_policy: SubagentNestingPolicy::default(),
1670            reasoning_effort_handle: None,
1671        }
1672    }
1673
1674    /// Create a context with a file store
1675    pub fn with_file_store(session_id: SessionId, file_store: Arc<dyn SessionFileSystem>) -> Self {
1676        Self {
1677            session_id,
1678            workspace_id: WorkspaceId::from_uuid(session_id.uuid()),
1679            file_store: Some(file_store),
1680            storage_store: None,
1681            image_store: None,
1682            provider_credential_store: None,
1683            utility_llm_service: None,
1684            mcp_invoker: None,
1685            egress_service: None,
1686            sqldb_store: None,
1687            message_retriever: None,
1688            session_store: None,
1689            session_mutator: None,
1690            agent_store: None,
1691            connection_resolver: None,
1692            schedule_store: None,
1693            platform_store: None,
1694            knowledge_store: None,
1695            knowledge_index_search: None,
1696            leased_resource_store: None,
1697            session_resource_registry: None,
1698            session_task_registry: None,
1699            event_emitter: None,
1700            event_context: None,
1701            tool_call_id: None,
1702            capability_registry: None,
1703            tool_registry: None,
1704            visible_tool_names: None,
1705            org_id: None,
1706            network_access: None,
1707            locale: None,
1708            budget_checker: None,
1709            payment_authority: None,
1710            subagent_spawn_store: None,
1711            subagent_nesting_policy: SubagentNestingPolicy::default(),
1712            reasoning_effort_handle: None,
1713        }
1714    }
1715
1716    /// Create a context with a storage store
1717    pub fn with_storage_store(
1718        session_id: SessionId,
1719        storage_store: Arc<dyn SessionStorageStore>,
1720    ) -> Self {
1721        Self {
1722            session_id,
1723            workspace_id: WorkspaceId::from_uuid(session_id.uuid()),
1724            file_store: None,
1725            storage_store: Some(storage_store),
1726            image_store: None,
1727            provider_credential_store: None,
1728            utility_llm_service: None,
1729            mcp_invoker: None,
1730            egress_service: None,
1731            sqldb_store: None,
1732            message_retriever: None,
1733            session_store: None,
1734            session_mutator: None,
1735            agent_store: None,
1736            connection_resolver: None,
1737            schedule_store: None,
1738            platform_store: None,
1739            knowledge_store: None,
1740            knowledge_index_search: None,
1741            leased_resource_store: None,
1742            session_resource_registry: None,
1743            session_task_registry: None,
1744            event_emitter: None,
1745            event_context: None,
1746            tool_call_id: None,
1747            capability_registry: None,
1748            tool_registry: None,
1749            visible_tool_names: None,
1750            org_id: None,
1751            network_access: None,
1752            locale: None,
1753            budget_checker: None,
1754            payment_authority: None,
1755            subagent_spawn_store: None,
1756            subagent_nesting_policy: SubagentNestingPolicy::default(),
1757            reasoning_effort_handle: None,
1758        }
1759    }
1760
1761    /// Create a context with both file store and storage store
1762    pub fn with_stores(
1763        session_id: SessionId,
1764        file_store: Arc<dyn SessionFileSystem>,
1765        storage_store: Arc<dyn SessionStorageStore>,
1766    ) -> Self {
1767        Self {
1768            session_id,
1769            workspace_id: WorkspaceId::from_uuid(session_id.uuid()),
1770            file_store: Some(file_store),
1771            storage_store: Some(storage_store),
1772            sqldb_store: None,
1773            image_store: None,
1774            provider_credential_store: None,
1775            utility_llm_service: None,
1776            mcp_invoker: None,
1777            egress_service: None,
1778            message_retriever: None,
1779            session_store: None,
1780            session_mutator: None,
1781            agent_store: None,
1782            connection_resolver: None,
1783            schedule_store: None,
1784            platform_store: None,
1785            knowledge_store: None,
1786            knowledge_index_search: None,
1787            leased_resource_store: None,
1788            session_resource_registry: None,
1789            session_task_registry: None,
1790            event_emitter: None,
1791            event_context: None,
1792            tool_call_id: None,
1793            capability_registry: None,
1794            tool_registry: None,
1795            visible_tool_names: None,
1796            org_id: None,
1797            network_access: None,
1798            locale: None,
1799            budget_checker: None,
1800            payment_authority: None,
1801            subagent_spawn_store: None,
1802            subagent_nesting_policy: SubagentNestingPolicy::default(),
1803            reasoning_effort_handle: None,
1804        }
1805    }
1806
1807    /// Add a SQL database store to this context
1808    pub fn with_sqldb_store(mut self, sqldb_store: SessionSqlDbStoreRef) -> Self {
1809        self.sqldb_store = Some(sqldb_store);
1810        self
1811    }
1812
1813    /// Add a message retriever to this context
1814    pub fn with_message_retriever(
1815        mut self,
1816        retriever: Arc<dyn crate::message_retriever::MessageRetriever>,
1817    ) -> Self {
1818        self.message_retriever = Some(retriever);
1819        self
1820    }
1821
1822    /// Add a session store to this context.
1823    pub fn with_session_store(mut self, store: Arc<dyn SessionStore>) -> Self {
1824        self.session_store = Some(store);
1825        self
1826    }
1827
1828    /// Add a session mutator to this context.
1829    pub fn with_session_mutator(mut self, mutator: Arc<dyn SessionMutator>) -> Self {
1830        self.session_mutator = Some(mutator);
1831        self
1832    }
1833
1834    /// Add a live reasoning-effort handle (EVE-595). Tools can call
1835    /// [`ReasoningEffortHandle::set`] on it to change the effort used by
1836    /// subsequent LLM steps within the same turn.
1837    pub fn with_reasoning_effort_handle(mut self, handle: ReasoningEffortHandle) -> Self {
1838        self.reasoning_effort_handle = Some(handle);
1839        self
1840    }
1841
1842    /// Add an agent store to this context.
1843    pub fn with_agent_store(mut self, store: Arc<dyn AgentStore>) -> Self {
1844        self.agent_store = Some(store);
1845        self
1846    }
1847
1848    /// Add a connection resolver to this context
1849    pub fn with_connection_resolver(mut self, resolver: Arc<dyn UserConnectionResolver>) -> Self {
1850        self.connection_resolver = Some(resolver);
1851        self
1852    }
1853
1854    /// Create a context with an image artifact store.
1855    pub fn with_image_store(
1856        session_id: SessionId,
1857        image_store: Arc<dyn ImageArtifactStore>,
1858    ) -> Self {
1859        Self {
1860            session_id,
1861            workspace_id: WorkspaceId::from_uuid(session_id.uuid()),
1862            file_store: None,
1863            storage_store: None,
1864            image_store: Some(image_store),
1865            provider_credential_store: None,
1866            utility_llm_service: None,
1867            mcp_invoker: None,
1868            egress_service: None,
1869            sqldb_store: None,
1870            message_retriever: None,
1871            session_store: None,
1872            session_mutator: None,
1873            agent_store: None,
1874            connection_resolver: None,
1875            schedule_store: None,
1876            platform_store: None,
1877            knowledge_store: None,
1878            knowledge_index_search: None,
1879            leased_resource_store: None,
1880            session_resource_registry: None,
1881            session_task_registry: None,
1882            event_emitter: None,
1883            event_context: None,
1884            tool_call_id: None,
1885            capability_registry: None,
1886            tool_registry: None,
1887            visible_tool_names: None,
1888            org_id: None,
1889            network_access: None,
1890            locale: None,
1891            budget_checker: None,
1892            payment_authority: None,
1893            subagent_spawn_store: None,
1894            subagent_nesting_policy: SubagentNestingPolicy::default(),
1895            reasoning_effort_handle: None,
1896        }
1897    }
1898
1899    /// Set the provider credential store on this context.
1900    pub fn with_provider_credential_store(
1901        mut self,
1902        store: Arc<dyn ProviderCredentialStore>,
1903    ) -> Self {
1904        self.provider_credential_store = Some(store);
1905        self
1906    }
1907
1908    /// Set the utility LLM service on this context.
1909    pub fn with_utility_llm_service(mut self, service: Arc<dyn crate::UtilityLlmService>) -> Self {
1910        self.utility_llm_service = Some(service);
1911        self
1912    }
1913
1914    /// Set the scoped-MCP tool invoker on this context.
1915    pub fn with_mcp_invoker(mut self, invoker: Arc<dyn crate::McpToolInvoker>) -> Self {
1916        self.mcp_invoker = Some(invoker);
1917        self
1918    }
1919
1920    /// Set the outbound egress service on this context.
1921    pub fn with_egress_service(mut self, service: Arc<dyn crate::EgressService>) -> Self {
1922        self.egress_service = Some(service);
1923        self
1924    }
1925
1926    /// Set the outbound egress service on this context when available.
1927    /// Preserves any already-set service when `service` is `None`.
1928    pub fn with_egress_service_opt(
1929        mut self,
1930        service: Option<Arc<dyn crate::EgressService>>,
1931    ) -> Self {
1932        if let Some(service) = service {
1933            self.egress_service = Some(service);
1934        }
1935        self
1936    }
1937
1938    /// Set the session storage store on this context (builder method).
1939    pub fn with_storage_store_arc(mut self, store: Arc<dyn SessionStorageStore>) -> Self {
1940        self.storage_store = Some(store);
1941        self
1942    }
1943
1944    /// Add a session schedule store to this context.
1945    pub fn with_schedule_store(mut self, store: Arc<dyn SessionScheduleStore>) -> Self {
1946        self.schedule_store = Some(store);
1947        self
1948    }
1949
1950    /// Add a platform store to this context.
1951    pub fn with_platform_store(
1952        mut self,
1953        store: Arc<dyn crate::platform_store::PlatformStore>,
1954    ) -> Self {
1955        self.platform_store = Some(store);
1956        self
1957    }
1958
1959    /// Add a Knowledge Index search service to this context (for `search_index`).
1960    pub fn with_knowledge_index_search(
1961        mut self,
1962        search: Arc<dyn crate::vector_store::KnowledgeIndexSearch>,
1963    ) -> Self {
1964        self.knowledge_index_search = Some(search);
1965        self
1966    }
1967
1968    /// Add a leased resource store to this context.
1969    pub fn with_leased_resource_store(mut self, store: Arc<dyn LeasedResourceStore>) -> Self {
1970        self.leased_resource_store = Some(store);
1971        self
1972    }
1973
1974    /// Add a session resource registry to this context.
1975    pub fn with_session_resource_registry(
1976        mut self,
1977        registry: Arc<dyn SessionResourceRegistry>,
1978    ) -> Self {
1979        self.session_resource_registry = Some(registry);
1980        self
1981    }
1982
1983    /// Add a session task registry to this context.
1984    pub fn with_session_task_registry(
1985        mut self,
1986        registry: Arc<dyn crate::session_task::SessionTaskRegistry>,
1987    ) -> Self {
1988        self.session_task_registry = Some(registry);
1989        self
1990    }
1991
1992    /// Set org ID for org-scoped operations.
1993    pub fn with_org_id(mut self, org_id: crate::typed_id::OrgId) -> Self {
1994        self.org_id = Some(org_id);
1995        self
1996    }
1997
1998    /// Set the active built-in tool registry on this context.
1999    pub fn with_tool_registry(mut self, registry: Arc<crate::tools::ToolRegistry>) -> Self {
2000        self.tool_registry = Some(registry);
2001        self
2002    }
2003
2004    /// Set the tool names visible to the model in this turn.
2005    pub fn with_visible_tool_names(mut self, names: Arc<HashSet<String>>) -> Self {
2006        self.visible_tool_names = Some(names);
2007        self
2008    }
2009
2010    /// Set the merged network access list for URL filtering.
2011    pub fn with_network_access(
2012        mut self,
2013        network_access: Option<crate::network_access::NetworkAccessList>,
2014    ) -> Self {
2015        self.network_access = network_access;
2016        self
2017    }
2018
2019    /// Set the internal payment authority for paid capability operations.
2020    pub fn with_payment_authority(mut self, authority: Arc<dyn PaymentAuthority>) -> Self {
2021        self.payment_authority = Some(authority);
2022        self
2023    }
2024
2025    /// Set the durable subagent spawn handle store (EVE-535).
2026    pub fn with_subagent_spawn_store(mut self, store: Arc<dyn SubagentSpawnStore>) -> Self {
2027        self.subagent_spawn_store = Some(store);
2028        self
2029    }
2030
2031    /// Set the resolved subagent nesting policy.
2032    pub fn with_subagent_nesting_policy(mut self, policy: SubagentNestingPolicy) -> Self {
2033        self.subagent_nesting_policy = policy;
2034        self
2035    }
2036
2037    /// Emit a `tool.progress` event if an event emitter and context are available.
2038    ///
2039    /// This is a best-effort helper: failures are logged but not propagated,
2040    /// so tools never fail just because a progress event couldn't be sent.
2041    pub async fn emit_progress(&self, tool_name: &str, message: &str) {
2042        let (Some(emitter), Some(ctx), Some(call_id)) =
2043            (&self.event_emitter, &self.event_context, &self.tool_call_id)
2044        else {
2045            return;
2046        };
2047        if let Err(e) = emitter
2048            .emit(EventRequest::new(
2049                self.session_id,
2050                ctx.clone(),
2051                crate::events::ToolProgressData {
2052                    tool_call_id: call_id.clone(),
2053                    tool_name: tool_name.to_string(),
2054                    message: message.to_string(),
2055                    display_name: None,
2056                },
2057            ))
2058            .await
2059        {
2060            tracing::debug!(
2061                tool_call_id = call_id,
2062                tool_name,
2063                error = %e,
2064                "Failed to emit tool.progress event"
2065            );
2066        }
2067    }
2068
2069    /// Emit a `tool.output.delta` event if an event emitter and context are available.
2070    ///
2071    /// Streams incremental output chunks (e.g., stdout/stderr lines) for live
2072    /// rendering in UI and CLI. Best-effort: failures are logged, not propagated.
2073    pub async fn emit_tool_output(&self, tool_name: &str, delta: &str, stream: &str) {
2074        let (Some(emitter), Some(ctx), Some(call_id)) =
2075            (&self.event_emitter, &self.event_context, &self.tool_call_id)
2076        else {
2077            return;
2078        };
2079        if let Err(e) = emitter
2080            .emit(EventRequest::new(
2081                self.session_id,
2082                ctx.clone(),
2083                crate::events::ToolOutputDeltaData {
2084                    tool_call_id: call_id.clone(),
2085                    tool_name: tool_name.to_string(),
2086                    delta: delta.to_string(),
2087                    stream: stream.to_string(),
2088                },
2089            ))
2090            .await
2091        {
2092            tracing::debug!(
2093                tool_call_id = call_id,
2094                tool_name,
2095                error = %e,
2096                "Failed to emit tool.output.delta event"
2097            );
2098        }
2099    }
2100}
2101
2102impl std::fmt::Debug for ToolContext {
2103    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2104        f.debug_struct("ToolContext")
2105            .field("session_id", &self.session_id)
2106            .field("file_store", &self.file_store.is_some())
2107            .field("storage_store", &self.storage_store.is_some())
2108            .field("image_store", &self.image_store.is_some())
2109            .field(
2110                "provider_credential_store",
2111                &self.provider_credential_store.is_some(),
2112            )
2113            .field("utility_llm_service", &self.utility_llm_service.is_some())
2114            .field("egress_service", &self.egress_service.is_some())
2115            .field("sqldb_store", &self.sqldb_store.is_some())
2116            .field("message_retriever", &self.message_retriever.is_some())
2117            .field("session_store", &self.session_store.is_some())
2118            .field("session_mutator", &self.session_mutator.is_some())
2119            .field("agent_store", &self.agent_store.is_some())
2120            .field("connection_resolver", &self.connection_resolver.is_some())
2121            .field("schedule_store", &self.schedule_store.is_some())
2122            .field("platform_store", &self.platform_store.is_some())
2123            .field(
2124                "knowledge_index_search",
2125                &self.knowledge_index_search.is_some(),
2126            )
2127            .field(
2128                "leased_resource_store",
2129                &self.leased_resource_store.is_some(),
2130            )
2131            .field("event_emitter", &self.event_emitter.is_some())
2132            .field("tool_registry", &self.tool_registry.is_some())
2133            .field("payment_authority", &self.payment_authority.is_some())
2134            .field("subagent_spawn_store", &self.subagent_spawn_store.is_some())
2135            .field("subagent_nesting_policy", &self.subagent_nesting_policy)
2136            .field("org_id", &self.org_id)
2137            .finish()
2138    }
2139}
2140
2141// ============================================================================
2142// EventEmitter - For emitting events
2143// ============================================================================
2144
2145use crate::events::{Event, EventRequest};
2146
2147/// Trait for emitting events following the standard event protocol
2148///
2149/// Implementations can:
2150/// - Store events in a database
2151/// - Keep events in memory for testing
2152/// - Stream events via SSE/WebSocket
2153/// - Log events for debugging
2154///
2155/// Events follow a consistent schema: id, type, ts, context, data.
2156/// See specs/events.md for the full event protocol specification.
2157#[async_trait]
2158pub trait EventEmitter: Send + Sync {
2159    /// Emit an event request
2160    ///
2161    /// Takes an EventRequest (without id/sequence) and returns the stored Event
2162    /// with id and sequence assigned by the storage layer.
2163    async fn emit(&self, request: EventRequest) -> Result<Event>;
2164}
2165
2166/// Blanket impl: `Arc<E>` delegates to the inner emitter.
2167#[async_trait]
2168impl<E: EventEmitter + ?Sized> EventEmitter for Arc<E> {
2169    async fn emit(&self, request: EventRequest) -> Result<Event> {
2170        (**self).emit(request).await
2171    }
2172}
2173
2174/// No-op event emitter for when event emission is not needed
2175///
2176/// This is useful for testing or when event observability is disabled.
2177#[derive(Debug, Clone, Default)]
2178pub struct NoopEventEmitter;
2179
2180#[async_trait]
2181impl EventEmitter for NoopEventEmitter {
2182    async fn emit(&self, request: EventRequest) -> Result<Event> {
2183        // Return a dummy event with sequence 0
2184        Ok(request.into_event(crate::typed_id::EventId::new(), 0))
2185    }
2186}
2187
2188// Note: EventListener trait has been moved to event_listeners.rs module.
2189// Use `everruns_core::EventListener` or `everruns_core::event_listeners::EventListener`.
2190
2191// ============================================================================
2192// ImageResolver - For resolving image_file content to actual image data
2193// ============================================================================
2194
2195/// Resolved image data for LLM consumption
2196///
2197/// This struct contains the actual image data in a format suitable for
2198/// sending to LLM providers. Both OpenAI and Anthropic accept base64-encoded
2199/// images with media type information.
2200#[derive(Debug, Clone)]
2201pub struct ResolvedImage {
2202    /// Base64-encoded image data (without data URL prefix)
2203    pub base64: String,
2204    /// MIME type (e.g., "image/png", "image/jpeg")
2205    pub media_type: String,
2206}
2207
2208impl ResolvedImage {
2209    /// Create a new resolved image
2210    pub fn new(base64: impl Into<String>, media_type: impl Into<String>) -> Self {
2211        Self {
2212            base64: base64.into(),
2213            media_type: media_type.into(),
2214        }
2215    }
2216
2217    /// Convert to a data URL suitable for OpenAI Vision API
2218    ///
2219    /// Format: `data:{media_type};base64,{base64_data}`
2220    pub fn to_data_url(&self) -> String {
2221        format!("data:{};base64,{}", self.media_type, self.base64)
2222    }
2223}
2224
2225/// Trait for resolving image_file content parts to actual image data
2226///
2227/// When building LLM messages, `image_file` content parts contain only
2228/// a reference (UUID) to an uploaded image. This trait allows resolving
2229/// those references to actual image data.
2230///
2231/// # Provider-specific formatting
2232///
2233/// The resolved image data is then converted to provider-specific formats:
2234///
2235/// **OpenAI Vision:**
2236/// ```json
2237/// {
2238///   "type": "image_url",
2239///   "image_url": { "url": "data:image/png;base64,..." }
2240/// }
2241/// ```
2242///
2243/// **Anthropic Vision:**
2244/// ```json
2245/// {
2246///   "type": "image",
2247///   "source": { "type": "base64", "media_type": "image/png", "data": "..." }
2248/// }
2249/// ```
2250///
2251/// # Implementation notes
2252///
2253/// Implementations should:
2254/// - Fetch image data from storage (database, S3, etc.)
2255/// - Return base64-encoded data with media type
2256/// - Handle missing images gracefully (return None)
2257#[async_trait]
2258pub trait ImageResolver: Send + Sync {
2259    /// Resolve an image_file reference to actual image data
2260    ///
2261    /// Returns `None` if the image is not found.
2262    async fn resolve_image(&self, image_id: Uuid) -> Result<Option<ResolvedImage>>;
2263}
2264
2265// ============================================================================
2266// SubagentSpawnStore — durable spawn handles for subagent reattach (EVE-535)
2267// ============================================================================
2268
2269/// Result of attempting to claim a subagent spawn slot.
2270#[derive(Debug)]
2271pub enum SpawnClaimResult {
2272    /// First claim — child session does not yet exist.
2273    /// Proceed to create the child, then call `register_child_session`.
2274    Claimed {
2275        spawn_handle_id: uuid::Uuid,
2276        claim_token: uuid::Uuid,
2277    },
2278    /// Row exists but `child_session_id` was never registered (crash between
2279    /// claim and `register_child_session`). Re-create the child and call
2280    /// `register_child_session` — same flow as `Claimed`.
2281    ClaimedPendingChild {
2282        spawn_handle_id: uuid::Uuid,
2283        claim_token: uuid::Uuid,
2284    },
2285    /// Child session was created and is still running.
2286    /// Reattach: wait for the existing child and settle with the stored claim_token.
2287    AlreadyRunning {
2288        child_session_id: crate::typed_id::SessionId,
2289        /// Stored claim token — must be used for `settle_spawn` on this replay.
2290        claim_token: uuid::Uuid,
2291    },
2292    /// Child already finished on a previous execution.
2293    /// Fast-path: return the stored result immediately without waiting.
2294    AlreadySettled {
2295        child_session_id: crate::typed_id::SessionId,
2296        /// The `wait_for_idle` return value from the original execution.
2297        terminal_status: String,
2298        terminal_result: String,
2299    },
2300}
2301
2302/// Durable spawn handle store for subagent idempotency (EVE-535).
2303///
2304/// Maps `(parent_session_id, tool_call_id) → child_session_id` so that when
2305/// a parent's `act` is reclaimed mid-`wait_for_idle`, the tool can reattach
2306/// to the existing child instead of spawning a duplicate.
2307///
2308/// Lifecycle: claim → register_child_session → settle_spawn.
2309#[async_trait]
2310pub trait SubagentSpawnStore: Send + Sync + 'static {
2311    /// Attempt to claim a spawn slot for `(parent_session_id, tool_call_id)`.
2312    ///
2313    /// Does NOT accept `child_session_id` — the child session does not exist yet.
2314    /// Call `register_child_session` with the actual child ID after creating it.
2315    async fn try_claim_spawn(
2316        &self,
2317        parent_session_id: crate::typed_id::SessionId,
2318        tool_call_id: &str,
2319        claim_token: uuid::Uuid,
2320    ) -> Result<SpawnClaimResult>;
2321
2322    /// Register the actual child session ID after it has been created.
2323    ///
2324    /// Must be called after `try_claim_spawn` returns `Claimed` or
2325    /// `ClaimedPendingChild`, before waiting for the child to complete.
2326    async fn register_child_session(
2327        &self,
2328        spawn_handle_id: uuid::Uuid,
2329        claim_token: uuid::Uuid,
2330        child_session_id: crate::typed_id::SessionId,
2331    ) -> Result<()>;
2332
2333    /// Record the terminal result once the child has completed.
2334    ///
2335    /// `claim_token` must match the stored token. `terminal_status` is the
2336    /// `wait_for_idle` return value ("idle", "error", "timeout", etc.) and
2337    /// `terminal_result` is the last agent message.
2338    async fn settle_spawn(
2339        &self,
2340        parent_session_id: crate::typed_id::SessionId,
2341        tool_call_id: &str,
2342        claim_token: uuid::Uuid,
2343        terminal_status: &str,
2344        terminal_result: &str,
2345    ) -> Result<()>;
2346}
2347
2348/// Blanket impl: `Arc<S>` delegates to the inner store.
2349#[async_trait]
2350impl<S: SubagentSpawnStore + ?Sized> SubagentSpawnStore for Arc<S> {
2351    async fn try_claim_spawn(
2352        &self,
2353        parent_session_id: crate::typed_id::SessionId,
2354        tool_call_id: &str,
2355        claim_token: uuid::Uuid,
2356    ) -> Result<SpawnClaimResult> {
2357        (**self)
2358            .try_claim_spawn(parent_session_id, tool_call_id, claim_token)
2359            .await
2360    }
2361
2362    async fn register_child_session(
2363        &self,
2364        spawn_handle_id: uuid::Uuid,
2365        claim_token: uuid::Uuid,
2366        child_session_id: crate::typed_id::SessionId,
2367    ) -> Result<()> {
2368        (**self)
2369            .register_child_session(spawn_handle_id, claim_token, child_session_id)
2370            .await
2371    }
2372
2373    async fn settle_spawn(
2374        &self,
2375        parent_session_id: crate::typed_id::SessionId,
2376        tool_call_id: &str,
2377        claim_token: uuid::Uuid,
2378        terminal_status: &str,
2379        terminal_result: &str,
2380    ) -> Result<()> {
2381        (**self)
2382            .settle_spawn(
2383                parent_session_id,
2384                tool_call_id,
2385                claim_token,
2386                terminal_status,
2387                terminal_result,
2388            )
2389            .await
2390    }
2391}
2392
2393/// No-op spawn store — used when no durable store is configured (dev/test).
2394///
2395/// Always claims (no dedup); settle and register are no-ops.
2396pub struct NoopSubagentSpawnStore;
2397
2398#[async_trait]
2399impl SubagentSpawnStore for NoopSubagentSpawnStore {
2400    async fn try_claim_spawn(
2401        &self,
2402        _parent_session_id: crate::typed_id::SessionId,
2403        _tool_call_id: &str,
2404        claim_token: uuid::Uuid,
2405    ) -> Result<SpawnClaimResult> {
2406        Ok(SpawnClaimResult::Claimed {
2407            spawn_handle_id: uuid::Uuid::new_v4(),
2408            claim_token,
2409        })
2410    }
2411
2412    async fn register_child_session(
2413        &self,
2414        _spawn_handle_id: uuid::Uuid,
2415        _claim_token: uuid::Uuid,
2416        _child_session_id: crate::typed_id::SessionId,
2417    ) -> Result<()> {
2418        Ok(())
2419    }
2420
2421    async fn settle_spawn(
2422        &self,
2423        _parent_session_id: crate::typed_id::SessionId,
2424        _tool_call_id: &str,
2425        _claim_token: uuid::Uuid,
2426        _terminal_status: &str,
2427        _terminal_result: &str,
2428    ) -> Result<()> {
2429        Ok(())
2430    }
2431}
2432
2433// ============================================================================
2434// Tests
2435// ============================================================================
2436
2437#[cfg(test)]
2438mod tests {
2439    use super::*;
2440
2441    #[test]
2442    fn test_resolved_image_new() {
2443        let image = ResolvedImage::new("SGVsbG8=", "image/png");
2444        assert_eq!(image.base64, "SGVsbG8=");
2445        assert_eq!(image.media_type, "image/png");
2446    }
2447
2448    #[test]
2449    fn test_resolved_image_to_data_url() {
2450        let image = ResolvedImage::new("SGVsbG8=", "image/png");
2451        let data_url = image.to_data_url();
2452        assert_eq!(data_url, "data:image/png;base64,SGVsbG8=");
2453    }
2454
2455    #[test]
2456    fn test_resolved_image_jpeg() {
2457        let image = ResolvedImage::new("base64data", "image/jpeg");
2458        let data_url = image.to_data_url();
2459        assert!(data_url.starts_with("data:image/jpeg;base64,"));
2460    }
2461}