skill_context/
context.rs

1//! Core execution context types.
2//!
3//! This module defines the main `ExecutionContext` struct which represents
4//! a complete execution environment for skill tools.
5
6use chrono::{DateTime, Utc};
7use serde::{Deserialize, Serialize};
8
9use crate::environment::EnvironmentConfig;
10use crate::mounts::Mount;
11use crate::resources::ResourceConfig;
12use crate::runtime::RuntimeOverrides;
13use crate::secrets::SecretsConfig;
14
15/// A complete execution environment definition.
16///
17/// Execution contexts define everything needed to run a skill tool:
18/// - File and directory mounts
19/// - Environment variables
20/// - Secrets and credentials
21/// - Resource limits
22/// - Runtime-specific overrides
23#[derive(Debug, Clone, Serialize, Deserialize)]
24#[serde(rename_all = "snake_case")]
25pub struct ExecutionContext {
26    /// Unique identifier for this context.
27    pub id: String,
28
29    /// Human-readable name.
30    pub name: String,
31
32    /// Optional description.
33    #[serde(default, skip_serializing_if = "Option::is_none")]
34    pub description: Option<String>,
35
36    /// Optional parent context to inherit from.
37    #[serde(default, skip_serializing_if = "Option::is_none")]
38    pub inherits_from: Option<String>,
39
40    /// File and directory mounts.
41    #[serde(default)]
42    pub mounts: Vec<Mount>,
43
44    /// Environment variable definitions.
45    #[serde(default)]
46    pub environment: EnvironmentConfig,
47
48    /// Secret references.
49    #[serde(default)]
50    pub secrets: SecretsConfig,
51
52    /// Resource limits and capabilities.
53    #[serde(default)]
54    pub resources: ResourceConfig,
55
56    /// Runtime-specific overrides.
57    #[serde(default, skip_serializing_if = "Option::is_none")]
58    pub runtime_overrides: Option<RuntimeOverrides>,
59
60    /// Metadata.
61    #[serde(default)]
62    pub metadata: ContextMetadata,
63}
64
65impl ExecutionContext {
66    /// Create a new execution context with the given ID and name.
67    pub fn new(id: impl Into<String>, name: impl Into<String>) -> Self {
68        Self {
69            id: id.into(),
70            name: name.into(),
71            description: None,
72            inherits_from: None,
73            mounts: Vec::new(),
74            environment: EnvironmentConfig::default(),
75            secrets: SecretsConfig::default(),
76            resources: ResourceConfig::default(),
77            runtime_overrides: None,
78            metadata: ContextMetadata::new(),
79        }
80    }
81
82    /// Create a context that inherits from another context.
83    pub fn inheriting(
84        id: impl Into<String>,
85        name: impl Into<String>,
86        parent_id: impl Into<String>,
87    ) -> Self {
88        let mut ctx = Self::new(id, name);
89        ctx.inherits_from = Some(parent_id.into());
90        ctx
91    }
92
93    /// Set the description.
94    pub fn with_description(mut self, description: impl Into<String>) -> Self {
95        self.description = Some(description.into());
96        self
97    }
98
99    /// Add a mount.
100    pub fn with_mount(mut self, mount: Mount) -> Self {
101        self.mounts.push(mount);
102        self
103    }
104
105    /// Set environment configuration.
106    pub fn with_environment(mut self, environment: EnvironmentConfig) -> Self {
107        self.environment = environment;
108        self
109    }
110
111    /// Set secrets configuration.
112    pub fn with_secrets(mut self, secrets: SecretsConfig) -> Self {
113        self.secrets = secrets;
114        self
115    }
116
117    /// Set resource configuration.
118    pub fn with_resources(mut self, resources: ResourceConfig) -> Self {
119        self.resources = resources;
120        self
121    }
122
123    /// Set runtime overrides.
124    pub fn with_runtime_overrides(mut self, overrides: RuntimeOverrides) -> Self {
125        self.runtime_overrides = Some(overrides);
126        self
127    }
128
129    /// Add a tag to the context.
130    pub fn with_tag(mut self, tag: impl Into<String>) -> Self {
131        self.metadata.tags.push(tag.into());
132        self
133    }
134
135    /// Check if this context inherits from another.
136    pub fn has_parent(&self) -> bool {
137        self.inherits_from.is_some()
138    }
139
140    /// Get all required secret keys.
141    pub fn required_secrets(&self) -> Vec<&str> {
142        self.secrets
143            .secrets
144            .iter()
145            .filter(|(_, def)| def.required)
146            .map(|(key, _)| key.as_str())
147            .collect()
148    }
149
150    /// Get all required mounts.
151    pub fn required_mounts(&self) -> Vec<&Mount> {
152        self.mounts.iter().filter(|m| m.required).collect()
153    }
154
155    /// Update the metadata timestamps.
156    pub fn touch(&mut self) {
157        self.metadata.updated_at = Utc::now();
158        self.metadata.version += 1;
159    }
160}
161
162/// Metadata about an execution context.
163#[derive(Debug, Clone, Serialize, Deserialize)]
164#[serde(rename_all = "snake_case")]
165pub struct ContextMetadata {
166    /// When the context was created.
167    pub created_at: DateTime<Utc>,
168
169    /// When the context was last modified.
170    pub updated_at: DateTime<Utc>,
171
172    /// Who created this context (optional).
173    #[serde(default, skip_serializing_if = "Option::is_none")]
174    pub created_by: Option<String>,
175
176    /// Tags for categorization.
177    #[serde(default, skip_serializing_if = "Vec::is_empty")]
178    pub tags: Vec<String>,
179
180    /// Version number (incremented on each update).
181    #[serde(default = "default_version")]
182    pub version: u32,
183}
184
185fn default_version() -> u32 {
186    1
187}
188
189impl ContextMetadata {
190    /// Create new metadata with current timestamps.
191    pub fn new() -> Self {
192        let now = Utc::now();
193        Self {
194            created_at: now,
195            updated_at: now,
196            created_by: None,
197            tags: Vec::new(),
198            version: 1,
199        }
200    }
201
202    /// Create metadata with a creator.
203    pub fn with_creator(mut self, creator: impl Into<String>) -> Self {
204        self.created_by = Some(creator.into());
205        self
206    }
207}
208
209impl Default for ContextMetadata {
210    fn default() -> Self {
211        Self::new()
212    }
213}
214
215#[cfg(test)]
216mod tests {
217    use super::*;
218
219    #[test]
220    fn test_context_creation() {
221        let ctx = ExecutionContext::new("test-ctx", "Test Context")
222            .with_description("A test context")
223            .with_tag("test")
224            .with_tag("development");
225
226        assert_eq!(ctx.id, "test-ctx");
227        assert_eq!(ctx.name, "Test Context");
228        assert_eq!(ctx.description, Some("A test context".to_string()));
229        assert_eq!(ctx.metadata.tags, vec!["test", "development"]);
230        assert!(!ctx.has_parent());
231    }
232
233    #[test]
234    fn test_context_inheritance() {
235        let ctx = ExecutionContext::inheriting("child-ctx", "Child Context", "parent-ctx");
236
237        assert!(ctx.has_parent());
238        assert_eq!(ctx.inherits_from, Some("parent-ctx".to_string()));
239    }
240
241    #[test]
242    fn test_context_serialization() {
243        let ctx = ExecutionContext::new("test", "Test");
244        let json = serde_json::to_string(&ctx).unwrap();
245        let deserialized: ExecutionContext = serde_json::from_str(&json).unwrap();
246
247        assert_eq!(ctx.id, deserialized.id);
248        assert_eq!(ctx.name, deserialized.name);
249    }
250
251    #[test]
252    fn test_context_toml_serialization() {
253        let ctx = ExecutionContext::new("test", "Test")
254            .with_description("Test context")
255            .with_tag("test");
256
257        let toml_str = toml::to_string_pretty(&ctx).unwrap();
258        let deserialized: ExecutionContext = toml::from_str(&toml_str).unwrap();
259
260        assert_eq!(ctx.id, deserialized.id);
261        assert_eq!(ctx.description, deserialized.description);
262    }
263
264    #[test]
265    fn test_metadata_versioning() {
266        let mut ctx = ExecutionContext::new("test", "Test");
267        assert_eq!(ctx.metadata.version, 1);
268
269        ctx.touch();
270        assert_eq!(ctx.metadata.version, 2);
271
272        ctx.touch();
273        assert_eq!(ctx.metadata.version, 3);
274    }
275}