skill-runtime 0.3.0

Core execution engine for Skill - WASM sandbox, Docker runtime, and native skill execution
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! Job types and data structures
//!
//! Defines the core job types used for background processing of skill operations.

use std::collections::HashMap;
use std::time::Duration;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// Unique job identifier
pub type JobId = Uuid;

/// Job status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum JobStatus {
    /// Job is queued and waiting to be processed
    Pending,
    /// Job is currently being processed
    Running,
    /// Job completed successfully
    Completed,
    /// Job failed (may be retried)
    Failed,
    /// Job was cancelled
    Cancelled,
    /// Job exceeded max retries and is dead
    Dead,
}

impl std::fmt::Display for JobStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Pending => write!(f, "pending"),
            Self::Running => write!(f, "running"),
            Self::Completed => write!(f, "completed"),
            Self::Failed => write!(f, "failed"),
            Self::Cancelled => write!(f, "cancelled"),
            Self::Dead => write!(f, "dead"),
        }
    }
}

/// Job priority levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum JobPriority {
    Low = 0,
    Normal = 1,
    High = 2,
    Critical = 3,
}

impl Default for JobPriority {
    fn default() -> Self {
        Self::Normal
    }
}

/// Types of background jobs supported
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum JobType {
    /// Execute a skill with given parameters
    SkillExecution {
        skill_id: String,
        tool_name: String,
        parameters: serde_json::Value,
    },

    /// Generate examples for a skill using AI
    ExampleGeneration {
        skill_id: String,
        tool_names: Vec<String>,
        provider: String,
    },

    /// Index a skill into the search pipeline
    SkillIndexing {
        skill_id: String,
        skill_path: String,
    },

    /// Reindex all skills
    FullReindex,

    /// Train/update embeddings based on usage patterns
    EmbeddingUpdate {
        skill_ids: Vec<String>,
    },

    /// Cleanup old job records
    Maintenance {
        task: MaintenanceTask,
    },

    /// Custom job type for extensibility
    Custom {
        name: String,
        payload: serde_json::Value,
    },
}

/// Maintenance tasks
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MaintenanceTask {
    /// Remove completed jobs older than threshold
    CleanupCompletedJobs { older_than_days: u32 },
    /// Remove dead jobs
    CleanupDeadJobs,
    /// Vacuum database (SQLite only)
    VacuumDatabase,
    /// Re-queue orphaned jobs
    RequeueOrphaned,
}

/// A background job
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Job {
    /// Unique job identifier
    pub id: JobId,

    /// Job type and payload
    pub job_type: JobType,

    /// Current status
    pub status: JobStatus,

    /// Job priority
    pub priority: JobPriority,

    /// Number of attempts made
    pub attempts: u32,

    /// Maximum retry attempts
    pub max_attempts: u32,

    /// When the job was created
    pub created_at: DateTime<Utc>,

    /// When the job was last updated
    pub updated_at: DateTime<Utc>,

    /// When the job should run (for delayed jobs)
    pub scheduled_at: Option<DateTime<Utc>>,

    /// When the job started running
    pub started_at: Option<DateTime<Utc>>,

    /// When the job completed
    pub completed_at: Option<DateTime<Utc>>,

    /// Worker ID processing this job
    pub worker_id: Option<String>,

    /// Error message if failed
    pub error: Option<String>,

    /// Job result (if completed)
    pub result: Option<serde_json::Value>,

    /// Arbitrary metadata
    pub metadata: HashMap<String, String>,
}

impl Job {
    /// Create a new job
    pub fn new(job_type: JobType) -> Self {
        let now = Utc::now();
        Self {
            id: Uuid::new_v4(),
            job_type,
            status: JobStatus::Pending,
            priority: JobPriority::Normal,
            attempts: 0,
            max_attempts: 3,
            created_at: now,
            updated_at: now,
            scheduled_at: None,
            started_at: None,
            completed_at: None,
            worker_id: None,
            error: None,
            result: None,
            metadata: HashMap::new(),
        }
    }

    /// Create a skill execution job
    pub fn skill_execution(
        skill_id: impl Into<String>,
        tool_name: impl Into<String>,
        parameters: serde_json::Value,
    ) -> Self {
        Self::new(JobType::SkillExecution {
            skill_id: skill_id.into(),
            tool_name: tool_name.into(),
            parameters,
        })
    }

    /// Create an example generation job
    pub fn example_generation(
        skill_id: impl Into<String>,
        tool_names: Vec<String>,
        provider: impl Into<String>,
    ) -> Self {
        Self::new(JobType::ExampleGeneration {
            skill_id: skill_id.into(),
            tool_names,
            provider: provider.into(),
        })
    }

    /// Create a skill indexing job
    pub fn skill_indexing(
        skill_id: impl Into<String>,
        skill_path: impl Into<String>,
    ) -> Self {
        Self::new(JobType::SkillIndexing {
            skill_id: skill_id.into(),
            skill_path: skill_path.into(),
        })
    }

    /// Set job priority
    pub fn with_priority(mut self, priority: JobPriority) -> Self {
        self.priority = priority;
        self
    }

    /// Set max attempts
    pub fn with_max_attempts(mut self, max: u32) -> Self {
        self.max_attempts = max;
        self
    }

    /// Schedule job for later
    pub fn scheduled_at(mut self, when: DateTime<Utc>) -> Self {
        self.scheduled_at = Some(when);
        self
    }

    /// Schedule job after a delay
    pub fn delayed(mut self, delay: Duration) -> Self {
        self.scheduled_at = Some(Utc::now() + chrono::Duration::from_std(delay).unwrap_or_default());
        self
    }

    /// Add metadata
    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }

    /// Check if job can be retried
    pub fn can_retry(&self) -> bool {
        self.attempts < self.max_attempts && self.status == JobStatus::Failed
    }

    /// Check if job is terminal (won't change state)
    pub fn is_terminal(&self) -> bool {
        matches!(self.status, JobStatus::Completed | JobStatus::Cancelled | JobStatus::Dead)
    }

    /// Get time since creation
    pub fn age(&self) -> chrono::Duration {
        Utc::now() - self.created_at
    }

    /// Get execution duration (if completed)
    pub fn duration(&self) -> Option<chrono::Duration> {
        match (self.started_at, self.completed_at) {
            (Some(start), Some(end)) => Some(end - start),
            _ => None,
        }
    }
}

/// Job progress update
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JobProgress {
    /// Job ID
    pub job_id: JobId,

    /// Progress percentage (0-100)
    pub percentage: u8,

    /// Current step description
    pub step: String,

    /// Additional details
    pub details: Option<String>,

    /// Timestamp
    pub timestamp: DateTime<Utc>,
}

impl JobProgress {
    pub fn new(job_id: JobId, percentage: u8, step: impl Into<String>) -> Self {
        Self {
            job_id,
            percentage: percentage.min(100),
            step: step.into(),
            details: None,
            timestamp: Utc::now(),
        }
    }

    pub fn with_details(mut self, details: impl Into<String>) -> Self {
        self.details = Some(details.into());
        self
    }
}

/// Job statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct JobStats {
    /// Total jobs in queue
    pub total: usize,

    /// Jobs by status
    pub by_status: HashMap<String, usize>,

    /// Average execution time (ms)
    pub avg_execution_ms: u64,

    /// Success rate (0.0-1.0)
    pub success_rate: f32,

    /// Jobs processed in last hour
    pub throughput_per_hour: usize,

    /// Current active workers
    pub active_workers: usize,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_job_creation() {
        let job = Job::skill_execution("kubernetes", "apply", serde_json::json!({"file": "deploy.yaml"}));

        assert_eq!(job.status, JobStatus::Pending);
        assert_eq!(job.priority, JobPriority::Normal);
        assert_eq!(job.attempts, 0);
        assert_eq!(job.max_attempts, 3);
    }

    #[test]
    fn test_job_builder() {
        let job = Job::skill_indexing("kubernetes", "/skills/kubernetes")
            .with_priority(JobPriority::High)
            .with_max_attempts(5)
            .with_metadata("source", "cli");

        assert_eq!(job.priority, JobPriority::High);
        assert_eq!(job.max_attempts, 5);
        assert_eq!(job.metadata.get("source"), Some(&"cli".to_string()));
    }

    #[test]
    fn test_job_can_retry() {
        let mut job = Job::skill_execution("test", "run", serde_json::json!({}));
        job.status = JobStatus::Failed;
        job.attempts = 1;
        job.max_attempts = 3;

        assert!(job.can_retry());

        job.attempts = 3;
        assert!(!job.can_retry());
    }

    #[test]
    fn test_job_is_terminal() {
        let mut job = Job::skill_execution("test", "run", serde_json::json!({}));

        assert!(!job.is_terminal());

        job.status = JobStatus::Completed;
        assert!(job.is_terminal());

        job.status = JobStatus::Cancelled;
        assert!(job.is_terminal());

        job.status = JobStatus::Dead;
        assert!(job.is_terminal());
    }

    #[test]
    fn test_job_progress() {
        let job_id = Uuid::new_v4();
        let progress = JobProgress::new(job_id, 50, "Processing tools")
            .with_details("Tool 5 of 10");

        assert_eq!(progress.percentage, 50);
        assert_eq!(progress.step, "Processing tools");
        assert_eq!(progress.details, Some("Tool 5 of 10".to_string()));
    }

    #[test]
    fn test_job_type_serialization() {
        let job_type = JobType::SkillExecution {
            skill_id: "kubernetes".to_string(),
            tool_name: "apply".to_string(),
            parameters: serde_json::json!({"file": "test.yaml"}),
        };

        let json = serde_json::to_string(&job_type).unwrap();
        assert!(json.contains("skill_execution"));

        let parsed: JobType = serde_json::from_str(&json).unwrap();
        match parsed {
            JobType::SkillExecution { skill_id, .. } => assert_eq!(skill_id, "kubernetes"),
            _ => panic!("Wrong job type"),
        }
    }
}