skillc 0.2.1

A development kit for Agent Skills - the open format for extending AI agent capabilities
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//! Diagnostic types per RFC-0005: Diagnostic Code Registry
//!
//! All error and warning messages are canonical and defined in [[RFC-0005:C-CODES]].
//! - Errors appear as `error[EXXX]: message` and exit with code 1
//! - Warnings appear as `warning[WXXX]: message` and do not affect exit code
//!
//! Design: SSOT — codes defined once in enums, messages once in `message()` methods.

use crossterm::style::Stylize;
use std::fmt;
use std::io::IsTerminal;

/// Error codes per [[RFC-0005:C-CODES]].
///
/// These codes appear in user-facing error messages and are used for
/// documentation and issue tracking.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ErrorCode {
    /// E001: Skill resolution failed (no matching directory in any store)
    E001,
    /// E002: Index is missing, corrupt, or stale
    E002,
    /// E003: Index filename exists but belongs to different skill
    E003,
    /// E004: Search query is empty or whitespace-only
    E004,
    /// E010: Directory exists but lacks SKILL.md
    E010,
    /// E011: SKILL.md lacks required `name` or `description`
    E011,
    /// E012: Symlink or path traversal would escape skill directory
    E012,
    /// E020: Gateway show command found no matching heading
    E020,
    /// E021: Gateway open command target does not exist
    E021,
    /// E022: Gateway sources --dir target does not exist
    E022,
    /// E030: Stats command received unknown query type
    E030,
    /// E031: Stats command received malformed filter value
    E031,
    /// E040: Sync command found no fallback logs to sync
    E040,
    /// E041: Sync command cannot write to primary runtime directory
    E041,
    /// E042: Sync command cannot read from fallback log database
    E042,
    /// E050: Init command target already has SKILL.md
    E050,
    /// E100: CLI parsing failed (unknown flag, missing value, etc.)
    E100,
    /// E999: Internal error (IO, database, parsing failures)
    E999,
}

impl fmt::Display for ErrorCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

/// Warning codes per [[RFC-0005:C-CODES]].
///
/// These codes appear in user-facing warning messages and are used for
/// documentation and issue tracking. Warnings do not affect exit code.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WarningCode {
    /// W001: Gateway show found multiple headings matching query
    W001,
    /// W002: Access logging failed, using fallback or disabled
    W002,
    /// W003: Local fallback logs exist and are older than threshold
    W003,
}

impl fmt::Display for WarningCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

/// Skillc warning type with canonical messages per [[RFC-0005:C-CODES]].
///
/// All warning messages include the code: `warning[WXXX]: message`.
/// Warnings are printed to stderr but do not cause command failure.
#[derive(Debug)]
pub enum SkillcWarning {
    /// W001: Multiple matches for section
    MultipleMatches(String),
    /// W002: Logging disabled
    LoggingDisabled,
    /// W003: Stale local logs
    StaleLogs(String),
}

impl SkillcWarning {
    /// Returns the warning code (single source of truth).
    pub fn code(&self) -> WarningCode {
        match self {
            SkillcWarning::MultipleMatches(_) => WarningCode::W001,
            SkillcWarning::LoggingDisabled => WarningCode::W002,
            SkillcWarning::StaleLogs(_) => WarningCode::W003,
        }
    }

    /// Returns the warning message (single source of truth).
    fn message(&self) -> String {
        match self {
            SkillcWarning::MultipleMatches(s) => {
                format!("multiple matches for '{}'; showing first", s)
            }
            SkillcWarning::LoggingDisabled => {
                "logging disabled; run 'skc sync' after session to merge logs".to_string()
            }
            SkillcWarning::StaleLogs(s) => {
                format!("stale local logs for '{}'; run 'skc sync' to upload", s)
            }
        }
    }

    /// Emit this warning to stderr.
    pub fn emit(&self) {
        eprintln!("warning[{}]: {}", self.code(), self.message());
    }
}

impl fmt::Display for SkillcWarning {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let prefix = format!("warning[{}]", self.code());
        if std::io::stderr().is_terminal() {
            write!(f, "{}: {}", prefix.yellow().bold(), self.message())
        } else {
            write!(f, "{}: {}", prefix, self.message())
        }
    }
}

/// Skillc error type with canonical messages per [[RFC-0005:C-CODES]].
///
/// All error messages include the error code: `error[EXXX]: message`.
/// Implementations MUST print these exact messages to stderr.
#[derive(Debug)]
pub enum SkillcError {
    // E001–E010: Unified errors (skill resolution, index state, query, path)
    SkillNotFound(String),
    IndexUnusable(String),
    IndexHashCollision(String),
    EmptyQuery,
    NotAValidSkill(String),

    // E011–E019: Compilation errors (RFC-0001)
    MissingFrontmatterField(String),
    InvalidFrontmatter(String),

    // E012: Path escape (unified)
    PathEscapesRoot(String),

    // E020–E029: Gateway errors (RFC-0002)
    SectionNotFound(String),
    /// Section not found with suggestions per [[RFC-0002:C-SHOW]]
    SectionNotFoundWithSuggestions(String, String),
    FileNotFound(String),
    DirectoryNotFound(String),

    // E030–E039: Analytics errors (RFC-0003)
    InvalidQueryType(String),
    InvalidFilter(String),

    // E040–E049: Sync errors (RFC-0007)
    NoLocalLogs,
    SyncDestNotWritable(String, String),
    SyncSourceNotReadable(String, String),

    // E050–E059: Scaffolding errors (RFC-0006)
    SkillAlreadyExists(String),

    // E100–E199: CLI parsing errors
    InvalidOption(String),

    // E999: Internal errors (with message)
    Internal(String),

    // E999: Internal errors
    Io(std::io::Error),
    Yaml(serde_yaml::Error),
    Json(serde_json::Error),
    Sql(rusqlite::Error),
    InvalidDatetime(String),
    InvalidPath(String),
}

impl SkillcError {
    /// Returns the error code (single source of truth).
    pub fn code(&self) -> ErrorCode {
        match self {
            SkillcError::SkillNotFound(_) => ErrorCode::E001,
            SkillcError::IndexUnusable(_) => ErrorCode::E002,
            SkillcError::IndexHashCollision(_) => ErrorCode::E003,
            SkillcError::EmptyQuery => ErrorCode::E004,
            SkillcError::NotAValidSkill(_) => ErrorCode::E010,
            SkillcError::MissingFrontmatterField(_) => ErrorCode::E011,
            SkillcError::InvalidFrontmatter(_) => ErrorCode::E011,
            SkillcError::PathEscapesRoot(_) => ErrorCode::E012,
            SkillcError::SectionNotFound(_) => ErrorCode::E020,
            SkillcError::SectionNotFoundWithSuggestions(_, _) => ErrorCode::E020,
            SkillcError::FileNotFound(_) => ErrorCode::E021,
            SkillcError::DirectoryNotFound(_) => ErrorCode::E022,
            SkillcError::InvalidQueryType(_) => ErrorCode::E030,
            SkillcError::InvalidFilter(_) => ErrorCode::E031,
            SkillcError::InvalidDatetime(_) => ErrorCode::E031,
            SkillcError::NoLocalLogs => ErrorCode::E040,
            SkillcError::SyncDestNotWritable(_, _) => ErrorCode::E041,
            SkillcError::SyncSourceNotReadable(_, _) => ErrorCode::E042,
            SkillcError::SkillAlreadyExists(_) => ErrorCode::E050,
            SkillcError::InvalidOption(_) => ErrorCode::E100,
            // E999: Internal errors
            SkillcError::Io(_) => ErrorCode::E999,
            SkillcError::Yaml(_) => ErrorCode::E999,
            SkillcError::Json(_) => ErrorCode::E999,
            SkillcError::Sql(_) => ErrorCode::E999,
            SkillcError::InvalidPath(_) => ErrorCode::E999,
            SkillcError::Internal(_) => ErrorCode::E999,
        }
    }

    /// Returns the error message (single source of truth).
    fn message(&self) -> String {
        match self {
            SkillcError::SkillNotFound(s) => format!("skill '{}' not found", s),
            SkillcError::IndexUnusable(s) => {
                format!("search index unusable; run 'skc build {}' to rebuild", s)
            }
            SkillcError::IndexHashCollision(s) => {
                format!(
                    "index hash collision; delete .skillc-meta/search-{}.db and rebuild",
                    s
                )
            }
            SkillcError::EmptyQuery => "empty query".to_string(),
            SkillcError::NotAValidSkill(s) => {
                format!("not a valid skill: '{}' (missing SKILL.md)", s)
            }
            SkillcError::MissingFrontmatterField(s) => {
                format!("missing frontmatter field '{}' in SKILL.md", s)
            }
            SkillcError::InvalidFrontmatter(s) => {
                format!("invalid frontmatter in SKILL.md: {}", s)
            }
            SkillcError::PathEscapesRoot(s) => format!("path escapes skill root: '{}'", s),
            SkillcError::SectionNotFound(s) => format!("section not found: '{}'", s),
            SkillcError::SectionNotFoundWithSuggestions(s, suggestions) => {
                format!("section not found: '{}'{}", s, suggestions)
            }
            SkillcError::FileNotFound(s) => format!("file not found: '{}'", s),
            SkillcError::DirectoryNotFound(s) => format!("directory not found: '{}'", s),
            SkillcError::InvalidQueryType(s) => format!("invalid query type: '{}'", s),
            SkillcError::InvalidFilter(s) => format!("invalid filter: '{}'", s),
            SkillcError::InvalidDatetime(s) => format!("invalid filter: '{}'", s),
            SkillcError::NoLocalLogs => "no local logs found".to_string(),
            SkillcError::SyncDestNotWritable(path, msg) => {
                format!("sync destination not writable: '{}' ({})", path, msg)
            }
            SkillcError::SyncSourceNotReadable(path, msg) => {
                format!("sync source not readable: '{}' ({})", path, msg)
            }
            SkillcError::SkillAlreadyExists(s) => format!("skill '{}' already exists", s),
            SkillcError::InvalidOption(s) => format!("invalid option: '{}'", s),
            // Internal errors: pass through the underlying message
            SkillcError::Io(e) => e.to_string(),
            SkillcError::Yaml(e) => e.to_string(),
            SkillcError::Json(e) => e.to_string(),
            SkillcError::Sql(e) => e.to_string(),
            SkillcError::InvalidPath(s) => s.clone(),
            SkillcError::Internal(s) => s.clone(),
        }
    }
}

impl fmt::Display for SkillcError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let prefix = format!("error[{}]", self.code());
        if std::io::stderr().is_terminal() {
            write!(f, "{}: {}", prefix.red().bold(), self.message())
        } else {
            write!(f, "{}: {}", prefix, self.message())
        }
    }
}

impl std::error::Error for SkillcError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            SkillcError::Io(e) => Some(e),
            SkillcError::Yaml(e) => Some(e),
            SkillcError::Json(e) => Some(e),
            SkillcError::Sql(e) => Some(e),
            _ => None,
        }
    }
}

// Manual From impls (replacing thiserror's #[from])
impl From<std::io::Error> for SkillcError {
    fn from(e: std::io::Error) -> Self {
        SkillcError::Io(e)
    }
}

impl From<serde_yaml::Error> for SkillcError {
    fn from(e: serde_yaml::Error) -> Self {
        SkillcError::Yaml(e)
    }
}

impl From<serde_json::Error> for SkillcError {
    fn from(e: serde_json::Error) -> Self {
        SkillcError::Json(e)
    }
}

impl From<rusqlite::Error> for SkillcError {
    fn from(e: rusqlite::Error) -> Self {
        SkillcError::Sql(e)
    }
}

pub type Result<T> = std::result::Result<T, SkillcError>;

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

    #[test]
    fn test_error_code_display() {
        assert_eq!(ErrorCode::E001.to_string(), "E001");
        assert_eq!(ErrorCode::E010.to_string(), "E010");
        assert_eq!(ErrorCode::E999.to_string(), "E999");
    }

    #[test]
    fn test_warning_code_display() {
        assert_eq!(WarningCode::W001.to_string(), "W001");
        assert_eq!(WarningCode::W002.to_string(), "W002");
        assert_eq!(WarningCode::W003.to_string(), "W003");
    }

    #[test]
    fn test_skillc_error_display() {
        // Tests check for content presence since output includes ANSI color codes
        let err = SkillcError::SkillNotFound("my-skill".to_string());
        let s = err.to_string();
        assert!(s.contains("error[E001]"));
        assert!(s.contains("skill 'my-skill' not found"));

        let err = SkillcError::NotAValidSkill("/path/to/skill".to_string());
        let s = err.to_string();
        assert!(s.contains("error[E010]"));
        assert!(s.contains("not a valid skill: '/path/to/skill' (missing SKILL.md)"));

        let err = SkillcError::EmptyQuery;
        let s = err.to_string();
        assert!(s.contains("error[E004]"));
        assert!(s.contains("empty query"));

        let err = SkillcError::SectionNotFound("Quick Start".to_string());
        let s = err.to_string();
        assert!(s.contains("error[E020]"));
        assert!(s.contains("section not found: 'Quick Start'"));

        let err = SkillcError::FileNotFound("README.md".to_string());
        let s = err.to_string();
        assert!(s.contains("error[E021]"));
        assert!(s.contains("file not found: 'README.md'"));

        let err = SkillcError::NoLocalLogs;
        let s = err.to_string();
        assert!(s.contains("error[E040]"));
        assert!(s.contains("no local logs found"));
    }

    #[test]
    fn test_skillc_error_codes() {
        assert_eq!(
            SkillcError::SkillNotFound("x".into()).code(),
            ErrorCode::E001
        );
        assert_eq!(
            SkillcError::IndexUnusable("x".into()).code(),
            ErrorCode::E002
        );
        assert_eq!(
            SkillcError::IndexHashCollision("x".into()).code(),
            ErrorCode::E003
        );
        assert_eq!(SkillcError::EmptyQuery.code(), ErrorCode::E004);
        assert_eq!(
            SkillcError::NotAValidSkill("x".into()).code(),
            ErrorCode::E010
        );
        assert_eq!(
            SkillcError::MissingFrontmatterField("x".into()).code(),
            ErrorCode::E011
        );
        assert_eq!(
            SkillcError::PathEscapesRoot("x".into()).code(),
            ErrorCode::E012
        );
        assert_eq!(
            SkillcError::SectionNotFound("x".into()).code(),
            ErrorCode::E020
        );
        assert_eq!(
            SkillcError::FileNotFound("x".into()).code(),
            ErrorCode::E021
        );
        assert_eq!(
            SkillcError::DirectoryNotFound("x".into()).code(),
            ErrorCode::E022
        );
        assert_eq!(
            SkillcError::InvalidQueryType("x".into()).code(),
            ErrorCode::E030
        );
        assert_eq!(
            SkillcError::InvalidFilter("x".into()).code(),
            ErrorCode::E031
        );
        assert_eq!(SkillcError::NoLocalLogs.code(), ErrorCode::E040);
        assert_eq!(
            SkillcError::SkillAlreadyExists("x".into()).code(),
            ErrorCode::E050
        );
        assert_eq!(
            SkillcError::InvalidOption("x".into()).code(),
            ErrorCode::E100
        );
        assert_eq!(SkillcError::Internal("x".into()).code(), ErrorCode::E999);
    }

    #[test]
    fn test_skillc_warning_display() {
        // Tests check for content presence since output includes ANSI color codes
        let warn = SkillcWarning::MultipleMatches("section".to_string());
        let s = warn.to_string();
        assert!(s.contains("warning[W001]"));
        assert!(s.contains("multiple matches for 'section'; showing first"));

        let warn = SkillcWarning::LoggingDisabled;
        let s = warn.to_string();
        assert!(s.contains("warning[W002]"));
        assert!(s.contains("logging disabled; run 'skc sync' after session to merge logs"));

        let warn = SkillcWarning::StaleLogs("rust".to_string());
        let s = warn.to_string();
        assert!(s.contains("warning[W003]"));
        assert!(s.contains("stale local logs for 'rust'; run 'skc sync' to upload"));
    }

    #[test]
    fn test_error_from_io() {
        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
        let err: SkillcError = io_err.into();
        assert_eq!(err.code(), ErrorCode::E999);
        assert!(err.to_string().contains("file not found"));
    }

    #[test]
    fn test_error_source() {
        use std::error::Error;

        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "test");
        let err = SkillcError::Io(io_err);
        assert!(err.source().is_some());

        let err = SkillcError::SkillNotFound("x".to_string());
        assert!(err.source().is_none());
    }
}