punch-skills 1.2.0

Skill/move system for the Punch Agent Combat System
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
//! Publishing pipeline for the skills marketplace.
//!
//! Handles validation, tarball creation, checksumming, and signing of skills
//! before they are submitted to the index repository.

use std::path::Path;

use sha2::{Digest, Sha256};
use tracing::debug;

use punch_types::PunchResult;
use punch_types::signing::SigningKeyPair;

use crate::loader::{SkillFrontmatter, parse_skill_md};
use crate::registry::{IndexEntry, ScanVerdict, validate_skill_name};
use crate::scanner::SkillScanner;

// ---------------------------------------------------------------------------
// Validation
// ---------------------------------------------------------------------------

/// Validate a skill directory is ready for publishing.
///
/// Returns a list of validation error messages (empty = valid).
pub fn validate_for_publish(dir: &Path) -> Vec<String> {
    let mut errors = Vec::new();

    let skill_path = dir.join("SKILL.md");
    if !skill_path.exists() {
        errors.push("SKILL.md not found in directory".to_string());
        return errors;
    }

    let content = match std::fs::read_to_string(&skill_path) {
        Ok(c) => c,
        Err(e) => {
            errors.push(format!("failed to read SKILL.md: {}", e));
            return errors;
        }
    };

    let frontmatter = match parse_skill_md(&content) {
        Ok((fm, _body)) => fm,
        Err(e) => {
            errors.push(format!("invalid SKILL.md format: {}", e));
            return errors;
        }
    };

    // Validate name slug
    if let Err(e) = validate_skill_name(&frontmatter.name) {
        errors.push(format!("invalid skill name: {}", e));
    }

    // Validate version is semver-ish
    if !is_valid_semver(&frontmatter.version) {
        errors.push(format!(
            "invalid version '{}' — must be semver (e.g., 1.0.0)",
            frontmatter.version
        ));
    }

    // Validate description is present
    if frontmatter.description.is_empty() {
        errors.push("description is required".to_string());
    }

    // Validate author is present
    if frontmatter.author.is_empty() {
        errors.push("author is required".to_string());
    }

    // Check total size of publishable files
    if let Ok(size) = publishable_size(dir)
        && size > 100 * 1024
    {
        errors.push(format!(
            "skill package is too large ({} bytes, maximum 100KB)",
            size
        ));
    }

    errors
}

/// Basic semver validation (major.minor.patch with optional pre-release).
fn is_valid_semver(version: &str) -> bool {
    let parts: Vec<&str> = version.split('-').collect();
    let core = parts[0];
    let nums: Vec<&str> = core.split('.').collect();
    if nums.len() != 3 {
        return false;
    }
    nums.iter().all(|n| n.parse::<u64>().is_ok())
}

/// Calculate total size of publishable files in the directory.
fn publishable_size(dir: &Path) -> PunchResult<u64> {
    let mut total = 0u64;
    for entry in std::fs::read_dir(dir)?.flatten() {
        let path = entry.path();
        if path.is_file()
            && let Some(ext) = path.extension().and_then(|e| e.to_str())
            && matches!(ext, "md" | "txt")
        {
            total += std::fs::metadata(&path)?.len();
        }
    }
    Ok(total)
}

// ---------------------------------------------------------------------------
// Tarball creation
// ---------------------------------------------------------------------------

/// Create a tar.gz archive of publishable files in the skill directory.
///
/// Only includes `.md` and `.txt` files (skills are text-only).
/// Maximum size enforced at 100KB.
pub fn create_tarball(dir: &Path) -> PunchResult<Vec<u8>> {
    let buf = Vec::new();
    let encoder = flate2::write::GzEncoder::new(buf, flate2::Compression::default());
    let mut archive = tar::Builder::new(encoder);

    for entry in std::fs::read_dir(dir)?.flatten() {
        let path = entry.path();
        if path.is_file()
            && let Some(ext) = path.extension().and_then(|e| e.to_str())
            && matches!(ext, "md" | "txt")
        {
            let file_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("file");
            debug!(file = file_name, "adding to tarball");
            archive
                .append_path_with_name(&path, file_name)
                .map_err(|e| {
                    punch_types::PunchError::Config(format!(
                        "failed to add {} to tarball: {}",
                        file_name, e
                    ))
                })?;
        }
    }

    let encoder = archive.into_inner().map_err(|e| {
        punch_types::PunchError::Config(format!("failed to finalize tarball: {}", e))
    })?;
    let compressed = encoder.finish().map_err(|e| {
        punch_types::PunchError::Config(format!("failed to compress tarball: {}", e))
    })?;

    if compressed.len() > 100 * 1024 {
        return Err(punch_types::PunchError::Config(format!(
            "compressed tarball is too large ({} bytes, maximum 100KB)",
            compressed.len()
        )));
    }

    Ok(compressed)
}

// ---------------------------------------------------------------------------
// Checksum & Signing
// ---------------------------------------------------------------------------

/// Compute SHA-256 checksum of data, returned as hex string.
pub fn compute_checksum(data: &[u8]) -> String {
    let mut hasher = Sha256::new();
    hasher.update(data);
    let result = hasher.finalize();
    result.iter().map(|b| format!("{:02x}", b)).collect()
}

/// Sign a checksum string with an Ed25519 keypair.
///
/// Returns hex-encoded 64-byte signature.
pub fn sign_checksum(checksum: &str, key: &SigningKeyPair) -> String {
    let sig = key.sign(checksum.as_bytes());
    sig.to_bytes()
        .iter()
        .map(|b| format!("{:02x}", b))
        .collect()
}

// ---------------------------------------------------------------------------
// Index entry building
// ---------------------------------------------------------------------------

/// Build an index entry from publish artifacts.
pub fn build_index_entry(
    frontmatter: &SkillFrontmatter,
    checksum: &str,
    signature: &str,
    public_key: &str,
    source_url: &str,
    scan_result: ScanVerdict,
) -> IndexEntry {
    IndexEntry {
        name: frontmatter.name.clone(),
        version: frontmatter.version.clone(),
        checksum: checksum.to_string(),
        signature: signature.to_string(),
        public_key: public_key.to_string(),
        source_url: source_url.to_string(),
        scan_result,
    }
}

// ---------------------------------------------------------------------------
// Dry run
// ---------------------------------------------------------------------------

/// Perform a full publish dry run: validate, scan, create tarball, checksum.
///
/// Returns a summary report as a string. Does not actually publish.
pub fn dry_run(dir: &Path) -> PunchResult<String> {
    let mut report = String::new();

    // Validate
    let errors = validate_for_publish(dir);
    if !errors.is_empty() {
        report.push_str("Validation FAILED:\n");
        for e in &errors {
            report.push_str(&format!("  - {}\n", e));
        }
        return Ok(report);
    }
    report.push_str("Validation: PASSED\n");

    // Parse frontmatter
    let skill_path = dir.join("SKILL.md");
    let content = std::fs::read_to_string(&skill_path)?;
    let (frontmatter, _body) = parse_skill_md(&content)?;
    report.push_str(&format!("  Name: {}\n", frontmatter.name));
    report.push_str(&format!("  Version: {}\n", frontmatter.version));
    report.push_str(&format!("  Author: {}\n", frontmatter.author));

    // Security scan
    let scanner = SkillScanner::new();
    let verdict = scanner.scan(&content);
    match &verdict {
        ScanVerdict::Clean => report.push_str("Security scan: CLEAN\n"),
        ScanVerdict::Warning(findings) => {
            report.push_str(&format!("Security scan: {} WARNING(s)\n", findings.len()));
            for f in findings {
                report.push_str(&format!(
                    "  - [{}] L{}: {}\n",
                    f.severity, f.line, f.description
                ));
            }
        }
        ScanVerdict::Rejected(findings) => {
            report.push_str(&format!(
                "Security scan: REJECTED ({} finding(s))\n",
                findings.len()
            ));
            for f in findings {
                report.push_str(&format!(
                    "  - [{}] L{}: {}\n",
                    f.severity, f.line, f.description
                ));
            }
        }
    }

    // Create tarball
    let tarball = create_tarball(dir)?;
    let checksum = compute_checksum(&tarball);
    report.push_str(&format!("Tarball size: {} bytes\n", tarball.len()));
    report.push_str(&format!("Checksum: {}\n", checksum));

    report.push_str("\nDry run complete. Ready to publish.\n");
    Ok(report)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn make_skill_dir(name: &str, version: &str, author: &str) -> tempfile::TempDir {
        let dir = tempfile::tempdir().unwrap();
        let content = format!(
            "---\nname: {}\nversion: {}\ndescription: A test skill\nauthor: {}\ncategory: test\ntags: [test]\n---\n\n# Test Skill\n\nThis is a test.\n",
            name, version, author
        );
        fs::write(dir.path().join("SKILL.md"), content).unwrap();
        dir
    }

    #[test]
    fn test_validate_valid_skill() {
        let dir = make_skill_dir("test-skill", "1.0.0", "Test Author");
        let errors = validate_for_publish(dir.path());
        assert!(errors.is_empty(), "expected no errors, got: {:?}", errors);
    }

    #[test]
    fn test_validate_missing_skill_md() {
        let dir = tempfile::tempdir().unwrap();
        let errors = validate_for_publish(dir.path());
        assert!(!errors.is_empty());
        assert!(errors[0].contains("SKILL.md not found"));
    }

    #[test]
    fn test_validate_invalid_name() {
        let dir = make_skill_dir("Bad_Name", "1.0.0", "Author");
        let errors = validate_for_publish(dir.path());
        assert!(!errors.is_empty());
    }

    #[test]
    fn test_validate_invalid_version() {
        let dir = make_skill_dir("valid-name", "not-a-version", "Author");
        let errors = validate_for_publish(dir.path());
        assert!(errors.iter().any(|e| e.contains("version")));
    }

    #[test]
    fn test_validate_missing_description() {
        let dir = tempfile::tempdir().unwrap();
        let content = "---\nname: test-skill\nversion: 1.0.0\nauthor: test\n---\n\nBody here.";
        fs::write(dir.path().join("SKILL.md"), content).unwrap();
        let errors = validate_for_publish(dir.path());
        assert!(errors.iter().any(|e| e.contains("description")));
    }

    #[test]
    fn test_validate_missing_author() {
        let dir = tempfile::tempdir().unwrap();
        let content =
            "---\nname: test-skill\nversion: 1.0.0\ndescription: A test\n---\n\nBody here.";
        fs::write(dir.path().join("SKILL.md"), content).unwrap();
        let errors = validate_for_publish(dir.path());
        assert!(errors.iter().any(|e| e.contains("author")));
    }

    #[test]
    fn test_is_valid_semver() {
        assert!(is_valid_semver("1.0.0"));
        assert!(is_valid_semver("0.1.0"));
        assert!(is_valid_semver("10.20.30"));
        assert!(is_valid_semver("1.0.0-beta"));
        assert!(!is_valid_semver("1.0"));
        assert!(!is_valid_semver("latest"));
        assert!(!is_valid_semver("1.0.0.0"));
    }

    #[test]
    fn test_create_tarball() {
        let dir = make_skill_dir("tarball-test", "1.0.0", "Author");
        let tarball = create_tarball(dir.path()).unwrap();
        assert!(!tarball.is_empty());
    }

    #[test]
    fn test_compute_checksum() {
        let data = b"hello world";
        let checksum = compute_checksum(data);
        assert_eq!(checksum.len(), 64); // SHA-256 = 32 bytes = 64 hex chars
        assert!(checksum.chars().all(|c| c.is_ascii_hexdigit()));

        // Same input = same checksum
        assert_eq!(compute_checksum(data), checksum);

        // Different input = different checksum
        assert_ne!(compute_checksum(b"different"), checksum);
    }

    #[test]
    fn test_sign_checksum() {
        let (keypair, _vk) = punch_types::signing::generate_keypair();
        let checksum = compute_checksum(b"test data");
        let signature = sign_checksum(&checksum, &keypair);
        assert_eq!(signature.len(), 128); // 64 bytes = 128 hex chars
    }

    #[test]
    fn test_build_index_entry() {
        let fm = SkillFrontmatter {
            name: "test-skill".to_string(),
            version: "1.0.0".to_string(),
            description: "A test".to_string(),
            author: "Author".to_string(),
            category: "test".to_string(),
            tags: vec![],
            tools: vec![],
            requires: vec![],
        };
        let entry = build_index_entry(
            &fm,
            "abc123",
            "sig456",
            "pub789",
            "https://example.com/skill.tar.gz",
            ScanVerdict::Clean,
        );
        assert_eq!(entry.name, "test-skill");
        assert_eq!(entry.version, "1.0.0");
        assert_eq!(entry.checksum, "abc123");
        assert_eq!(entry.scan_result, ScanVerdict::Clean);
    }

    #[test]
    fn test_dry_run_valid_skill() {
        let dir = make_skill_dir("dry-run-test", "1.0.0", "Author");
        let report = dry_run(dir.path()).unwrap();
        assert!(report.contains("Validation: PASSED"));
        assert!(report.contains("dry-run-test"));
        assert!(report.contains("Checksum:"));
    }

    #[test]
    fn test_dry_run_invalid_skill() {
        let dir = tempfile::tempdir().unwrap();
        let report = dry_run(dir.path()).unwrap();
        assert!(report.contains("Validation FAILED"));
    }

    #[test]
    fn test_tarball_only_includes_text() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(
            dir.path().join("SKILL.md"),
            "---\nname: test\nversion: 1.0.0\n---\n\nBody.",
        )
        .unwrap();
        fs::write(dir.path().join("notes.txt"), "extra notes").unwrap();
        fs::write(dir.path().join("binary.bin"), vec![0u8; 100]).unwrap();
        fs::write(dir.path().join("image.png"), vec![0u8; 100]).unwrap();

        let tarball = create_tarball(dir.path()).unwrap();
        // Verify by decompressing and checking entries
        let decoder = flate2::read::GzDecoder::new(&tarball[..]);
        let mut archive = tar::Archive::new(decoder);
        let names: Vec<String> = archive
            .entries()
            .unwrap()
            .filter_map(|e| e.ok())
            .filter_map(|e| e.path().ok().map(|p| p.to_string_lossy().to_string()))
            .collect();
        assert!(names.contains(&"SKILL.md".to_string()));
        assert!(names.contains(&"notes.txt".to_string()));
        assert!(!names.iter().any(|n| n.ends_with(".bin")));
        assert!(!names.iter().any(|n| n.ends_with(".png")));
    }
}