vtcode-skills 0.185.2

Skill types, discovery, loading, and validation for VT Code
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
//! Evidence needed to identify an interrupted skill import.

use std::fs;
use std::path::Path;

use anyhow::{Context, Result, bail, ensure};
use serde::{Deserialize, Serialize};
use vtcode_commons::{VtCodePaths, fs::rename_synced, fs::sync_dir, validate_no_escaping_symlink_ancestors};

#[derive(Debug, Serialize, Deserialize)]
struct TransactionRecord {
    format_version: u32,
    skill_name: String,
    version: String,
    had_previous: bool,
    previous_index: Option<Vec<u8>>,
    intended_index: Vec<u8>,
}

/// Record exact index states and the validated target before any rename.
pub(super) fn record_transaction(
    staging: &Path,
    store: &Path,
    skill_name: &str,
    version: &str,
    had_previous: bool,
    intended_index: &[u8],
) -> Result<()> {
    let previous_index = read_index(store).context("read previous skill index for transaction")?;
    let record = TransactionRecord {
        format_version: 2,
        skill_name: skill_name.to_owned(),
        version: version.to_owned(),
        had_previous,
        previous_index,
        intended_index: intended_index.to_vec(),
    };
    let data = serde_json::to_vec(&record).context("serialize skill import transaction")?;
    VtCodePaths::write_private_file_atomic(staging.join("transaction.json"), &data)
        .context("record skill import transaction")?;
    sync_dir(staging).context("sync skill import transaction directory")
}

pub(super) fn read_index(store: &Path) -> Result<Option<Vec<u8>>> {
    let path = store.join("index.json");
    match fs::symlink_metadata(&path) {
        Ok(metadata) => {
            ensure!(metadata.is_file() && !metadata.file_type().is_symlink(), "skill index is not a regular file");
            Ok(Some(fs::read(path).context("read skill index")?))
        }
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(None),
        Err(error) => Err(error).context("inspect skill index"),
    }
}

pub(super) fn directory_exists(path: &Path) -> Result<bool> {
    match fs::symlink_metadata(path) {
        Ok(metadata) if metadata.is_dir() && !metadata.file_type().is_symlink() => Ok(true),
        Ok(_) => bail!("expected a regular directory: {}", path.display()),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(false),
        Err(error) => Err(error).with_context(|| format!("inspect {}", path.display())),
    }
}

/// Called with the store lock held. Unknown or inconsistent transactions are preserved.
pub(super) fn recover_abandoned_imports(store: &Path) -> Result<()> {
    for entry in fs::read_dir(store).context("inspect skill store for interrupted imports")? {
        let entry = entry.context("inspect skill store entry")?;
        if !entry.file_name().to_string_lossy().starts_with(".skill-import-") {
            continue;
        }
        recover_one(store, &entry.path())
            .with_context(|| format!("interrupted import at {}; preserve it for recovery", entry.path().display()))?;
    }
    Ok(())
}

fn recover_one(store: &Path, staging: &Path) -> Result<()> {
    ensure!(directory_exists(staging)?, "transaction directory is missing");
    let journal = staging.join("transaction.json");
    let metadata = fs::symlink_metadata(&journal).context("inspect transaction record")?;
    ensure!(metadata.is_file() && !metadata.file_type().is_symlink(), "invalid transaction record");
    let record: TransactionRecord = serde_json::from_slice(&fs::read(&journal)?).context("parse transaction record")?;
    ensure!(record.format_version == 2, "unsupported transaction record version");
    let manifest = crate::types::SkillManifest {
        name: record.skill_name.clone(),
        description: "recovery".to_owned(),
        ..Default::default()
    };
    manifest.validate().context("invalid transaction skill name")?;
    ensure!(super::safe_version(&record.version), "invalid transaction version");
    let intended: super::SkillStoreIndex =
        serde_json::from_slice(&record.intended_index).context("invalid intended index")?;
    let previous: super::SkillStoreIndex = match &record.previous_index {
        Some(bytes) => serde_json::from_slice(bytes).context("invalid previous index")?,
        None => Default::default(),
    };
    let expected = super::updated_skill_index(previous, &record.skill_name, &record.version)?;
    let expected: super::SkillStoreIndex = serde_json::from_slice(&expected)?;
    ensure!(intended == expected, "transaction index changes more than the target version");
    let current = read_index(store).context("read current skill index")?;
    let committed = current.as_deref() == Some(record.intended_index.as_slice());
    ensure!(committed || current == record.previous_index, "skill index changed since interrupted import");
    let dest = store.join(&record.skill_name).join(&record.version);
    validate_no_escaping_symlink_ancestors(&dest, true).context("validate recovery destination")?;
    let parent = dest.parent().context("recovery destination has no parent")?;
    ensure!(directory_exists(parent)?, "recovery destination parent is missing");
    let staged = staging.join("new");
    let backup = staging.join("previous");
    let has_staged = directory_exists(&staged)?;
    let has_backup = directory_exists(&backup)?;
    let has_destination = directory_exists(&dest)?;
    ensure!(record.had_previous || !has_backup, "unexpected previous version");
    if has_staged {
        ensure!(
            !committed || record.previous_index.as_deref() == Some(record.intended_index.as_slice()),
            "index committed before version publication"
        );
        if record.had_previous && !has_backup && has_destination {
            rename_synced(&dest, &backup).context("back up previous version during recovery")?;
            #[cfg(test)]
            pause_recovery_for_test("backup")?;
        } else {
            ensure!(!has_destination && (has_backup == record.had_previous), "ambiguous transaction directories");
        }
        rename_synced(&staged, &dest).context("publish staged version during recovery")?;
        #[cfg(test)]
        pause_recovery_for_test("version")?;
    } else {
        ensure!(has_destination, "published version is missing");
        ensure!(!record.had_previous || has_backup || committed, "previous version is missing before index commit");
    }
    if !committed {
        VtCodePaths::write_private_file_atomic(store.join("index.json"), &record.intended_index)
            .context("publish recovered skill index")?;
        sync_dir(store).context("sync skill store after recovered index publish")?;
        #[cfg(test)]
        pause_recovery_for_test("index")?;
    }
    fs::remove_dir_all(staging).context("remove recovered transaction")?;
    Ok(())
}

#[cfg(test)]
fn pause_recovery_for_test(phase: &str) -> Result<()> {
    if std::env::var("VTCODE_TEST_RECOVERY_KILL_PHASE").ok().as_deref() == Some(phase) {
        let ready = std::env::var_os("VTCODE_TEST_RECOVERY_KILL_READY").context("missing test signal path")?;
        fs::write(ready, phase)?;
        loop {
            std::thread::park();
        }
    }
    Ok(())
}

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

    #[test]
    fn killed_recovery_process_can_be_retried() {
        const STORE_ENV: &str = "VTCODE_TEST_RECOVERY_KILL_STORE";
        if let Some(store) = std::env::var_os(STORE_ENV) {
            let _ = super::super::load_skill_index(Path::new(&store)).unwrap();
            return;
        }
        let previous = b"{\"skills\":{\"sample\":{\"latest_version\":\"0.9\",\"versions\":[\"0.9\",\"1.2\"]}}}";
        let intended = b"{\"skills\":{\"sample\":{\"latest_version\":\"1.2\",\"versions\":[\"0.9\",\"1.2\"]}}}";
        for phase in ["backup", "version", "index"] {
            let root = tempfile::tempdir().unwrap();
            let store = root.path().join("store");
            let dest = store.join("sample/1.2");
            fs::create_dir_all(&dest).unwrap();
            fs::write(dest.join("SKILL.md"), "old").unwrap();
            fs::write(store.join("index.json"), previous).unwrap();
            let staging = tempfile::Builder::new().prefix(".skill-import-").tempdir_in(&store).unwrap();
            fs::create_dir(staging.path().join("new")).unwrap();
            fs::write(staging.path().join("new/SKILL.md"), "new").unwrap();
            record_transaction(staging.path(), &store, "sample", "1.2", true, intended).unwrap();
            let ready = root.path().join("ready");
            let mut child = std::process::Command::new(std::env::current_exe().unwrap())
                .args([
                    "--exact",
                    "bundle::recovery::tests::killed_recovery_process_can_be_retried",
                    "--nocapture",
                ])
                .env(STORE_ENV, &store)
                .env("VTCODE_TEST_RECOVERY_KILL_PHASE", phase)
                .env("VTCODE_TEST_RECOVERY_KILL_READY", &ready)
                .stdout(std::process::Stdio::null())
                .stderr(std::process::Stdio::null())
                .spawn()
                .unwrap();
            let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
            while !ready.exists() && std::time::Instant::now() < deadline {
                assert!(child.try_wait().unwrap().is_none(), "child exited before {phase} boundary");
                std::thread::sleep(std::time::Duration::from_millis(10));
            }
            let observed = ready.exists();
            child.kill().unwrap();
            let status = child.wait().unwrap();
            assert!(observed && !status.success(), "child did not stop at {phase} boundary");
            let index = super::super::load_skill_index(&store).unwrap();
            assert_eq!(index.skills["sample"].latest_version, "1.2");
            assert_eq!(fs::read(dest.join("SKILL.md")).unwrap(), b"new");
            assert_eq!(fs::read(store.join("index.json")).unwrap(), intended);
            assert!(!staging.path().exists());
        }
    }

    #[test]
    fn killed_import_process_recovers_on_index_load() {
        const CHILD_STORE: &str = "VTCODE_TEST_SKILL_KILL_STORE";
        const CHILD_PHASE: &str = "VTCODE_TEST_SKILL_KILL_PHASE";
        const INTENDED: &[u8] =
            b"{\"skills\":{\"sample\":{\"latest_version\":\"1.2\",\"versions\":[\"0.9\",\"1.2\"]}}}";
        if let Some(store) = std::env::var_os(CHILD_STORE) {
            let store = std::path::PathBuf::from(store);
            let phase = std::env::var(CHILD_PHASE).unwrap();
            let destination = store.join("sample/1.2");
            let ready = store.parent().unwrap().join("ready");
            VtCodePaths::with_private_file_lock(store.join("index.json"), || {
                let staging = tempfile::Builder::new().prefix(".skill-import-").tempdir_in(&store)?;
                fs::create_dir(staging.path().join("new"))?;
                fs::write(staging.path().join("new/SKILL.md"), "new")?;
                record_transaction(staging.path(), &store, "sample", "1.2", true, INTENDED)?;
                super::super::publish_staged_skill(staging, &destination, || {
                    if phase == "committed" {
                        VtCodePaths::write_private_file_atomic(store.join("index.json"), INTENDED)?;
                    }
                    fs::write(&ready, "ready")?;
                    loop {
                        std::thread::park();
                    }
                })
            })
            .unwrap();
            return;
        }

        for phase in ["published", "committed"] {
            let root = tempfile::tempdir().unwrap();
            let store = root.path().join("store");
            let destination = store.join("sample/1.2");
            fs::create_dir_all(&destination).unwrap();
            fs::write(destination.join("SKILL.md"), "old").unwrap();
            fs::write(
                store.join("index.json"),
                b"{\"skills\":{\"sample\":{\"latest_version\":\"0.9\",\"versions\":[\"0.9\",\"1.2\"]}}}",
            )
            .unwrap();
            let mut child = std::process::Command::new(std::env::current_exe().unwrap())
                .args([
                    "--exact",
                    "bundle::recovery::tests::killed_import_process_recovers_on_index_load",
                    "--nocapture",
                ])
                .env(CHILD_STORE, &store)
                .env(CHILD_PHASE, phase)
                .stdout(std::process::Stdio::null())
                .stderr(std::process::Stdio::null())
                .spawn()
                .unwrap();
            let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
            while !root.path().join("ready").exists() && std::time::Instant::now() < deadline {
                assert!(child.try_wait().unwrap().is_none(), "child exited before {phase} boundary");
                std::thread::sleep(std::time::Duration::from_millis(10));
            }
            let ready = root.path().join("ready").exists();
            child.kill().unwrap();
            let status = child.wait().unwrap();
            assert!(ready && !status.success(), "child did not stop at {phase} boundary");
            let index = super::super::load_skill_index(&store).unwrap();
            assert_eq!(index.skills["sample"].latest_version, "1.2");
            assert_eq!(fs::read(destination.join("SKILL.md")).unwrap(), b"new");
            assert_eq!(fs::read(store.join("index.json")).unwrap(), INTENDED);
            assert!(
                fs::read_dir(&store).unwrap().all(|entry| !entry
                    .unwrap()
                    .file_name()
                    .to_string_lossy()
                    .starts_with(".skill-import-"))
            );
        }
    }

    #[test]
    fn records_distinct_prior_and_intended_index_bytes() {
        let store = tempfile::tempdir().unwrap();
        let staging = tempfile::tempdir_in(store.path()).unwrap();
        fs::write(store.path().join("index.json"), b"old index\n").unwrap();
        record_transaction(staging.path(), store.path(), "sample", "2.0", true, b"new index").unwrap();
        let bytes = fs::read(staging.path().join("transaction.json")).unwrap();
        let record: TransactionRecord = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(record.format_version, 2);
        assert_eq!(record.skill_name, "sample");
        assert_eq!(record.version, "2.0");
        assert_eq!(record.previous_index.as_deref(), Some(b"old index\n".as_slice()));
        assert_eq!(record.intended_index, b"new index");
    }

    #[test]
    fn records_absent_previous_index() {
        let store = tempfile::tempdir().unwrap();
        let staging = tempfile::tempdir_in(store.path()).unwrap();
        record_transaction(staging.path(), store.path(), "other", "0.0.0", false, b"{}").unwrap();
        let record: TransactionRecord =
            serde_json::from_slice(&fs::read(staging.path().join("transaction.json")).unwrap()).unwrap();
        assert!(record.previous_index.is_none());
        assert_eq!(record.intended_index, b"{}");
    }

    #[test]
    fn interrupted_publication_recovers_each_state() {
        let old_index = b"{\"skills\":{}}";
        let new_index = b"{\"skills\":{\"sample\":{\"latest_version\":\"1.2\",\"versions\":[\"1.2\"]}}}";
        for had_previous in [false, true] {
            for phase in 0..=3 {
                if !had_previous && phase == 1 {
                    continue;
                }
                let store = tempfile::tempdir().unwrap();
                fs::write(store.path().join("index.json"), old_index).unwrap();
                let parent = store.path().join("sample");
                fs::create_dir(&parent).unwrap();
                let dest = parent.join("1.2");
                if had_previous {
                    fs::create_dir(&dest).unwrap();
                    fs::write(dest.join("SKILL.md"), "old").unwrap();
                }
                let staging = tempfile::Builder::new()
                    .prefix(".skill-import-")
                    .tempdir_in(store.path())
                    .unwrap();
                fs::create_dir(staging.path().join("new")).unwrap();
                fs::write(staging.path().join("new/SKILL.md"), "new").unwrap();
                record_transaction(staging.path(), store.path(), "sample", "1.2", had_previous, new_index).unwrap();
                if had_previous && phase >= 1 {
                    fs::rename(&dest, staging.path().join("previous")).unwrap();
                }
                if phase >= 2 {
                    fs::rename(staging.path().join("new"), &dest).unwrap();
                }
                if phase == 3 {
                    fs::write(store.path().join("index.json"), new_index).unwrap();
                }
                recover_abandoned_imports(store.path()).unwrap();
                assert_eq!(
                    fs::read_to_string(dest.join("SKILL.md")).unwrap(),
                    "new",
                    "phase={phase} previous={had_previous}"
                );
                assert_eq!(fs::read(store.path().join("index.json")).unwrap(), new_index);
                recover_abandoned_imports(store.path()).unwrap();
            }
        }
    }

    #[test]
    fn recovery_rejects_changed_index_without_touching_transaction() {
        let store = tempfile::tempdir().unwrap();
        fs::write(store.path().join("index.json"), b"{\"skills\":{}}").unwrap();
        fs::create_dir(store.path().join("sample")).unwrap();
        let staging = tempfile::Builder::new()
            .prefix(".skill-import-")
            .tempdir_in(store.path())
            .unwrap();
        fs::create_dir(staging.path().join("new")).unwrap();
        let intended = b"{\"skills\":{\"sample\":{\"latest_version\":\"1.2\",\"versions\":[\"1.2\"]}}}";
        record_transaction(staging.path(), store.path(), "sample", "1.2", false, intended).unwrap();
        fs::write(store.path().join("index.json"), "changed").unwrap();
        assert!(format!("{:#}", recover_abandoned_imports(store.path()).unwrap_err()).contains("index changed"));
        assert!(staging.path().join("new").is_dir());
        assert_eq!(fs::read_to_string(store.path().join("index.json")).unwrap(), "changed");
        assert!(!store.path().join("sample/1.2").exists());
    }

    #[test]
    fn recovery_rejects_untrusted_record_paths() {
        let store = tempfile::tempdir().unwrap();
        let staging = tempfile::Builder::new()
            .prefix(".skill-import-")
            .tempdir_in(store.path())
            .unwrap();
        let record = TransactionRecord {
            format_version: 2,
            skill_name: "../outside".to_owned(),
            version: "1.2".to_owned(),
            had_previous: false,
            previous_index: None,
            intended_index: b"{\"skills\":{}}".to_vec(),
        };
        fs::write(staging.path().join("transaction.json"), serde_json::to_vec(&record).unwrap()).unwrap();
        assert!(
            format!("{:#}", recover_abandoned_imports(store.path()).unwrap_err())
                .contains("invalid transaction skill name")
        );
        assert!(staging.path().join("transaction.json").exists());
    }

    #[test]
    fn recovery_rejects_index_that_removes_another_skill() {
        let store = tempfile::tempdir().unwrap();
        let previous = br#"{"skills":{"other":{"latest_version":"9","versions":["9"]}}}"#;
        let malicious = br#"{"skills":{"sample":{"latest_version":"1.2","versions":["1.2"]}}}"#;
        fs::write(store.path().join("index.json"), previous).unwrap();
        fs::create_dir(store.path().join("sample")).unwrap();
        let staging = tempfile::Builder::new()
            .prefix(".skill-import-")
            .tempdir_in(store.path())
            .unwrap();
        fs::create_dir(staging.path().join("new")).unwrap();
        record_transaction(staging.path(), store.path(), "sample", "1.2", false, malicious).unwrap();
        assert!(format!("{:#}", recover_abandoned_imports(store.path()).unwrap_err()).contains("more than the target"));
        assert_eq!(fs::read(store.path().join("index.json")).unwrap(), previous);
        assert!(staging.path().join("new").is_dir());
    }

    #[cfg(unix)]
    #[test]
    fn transaction_refuses_symlinked_index() {
        let store = tempfile::tempdir().unwrap();
        let staging = tempfile::tempdir_in(store.path()).unwrap();
        let target = store.path().join("target");
        fs::write(&target, "unchanged").unwrap();
        std::os::unix::fs::symlink(&target, store.path().join("index.json")).unwrap();
        assert!(record_transaction(staging.path(), store.path(), "sample", "1.2", false, b"{}").is_err());
        assert_eq!(fs::read_to_string(target).unwrap(), "unchanged");
        assert!(!staging.path().join("transaction.json").exists());
    }

    #[test]
    fn recovery_rejects_unexpected_backup_without_previous() {
        let store = tempfile::tempdir().unwrap();
        let old_index = b"{\"skills\":{}}";
        fs::write(store.path().join("index.json"), old_index).unwrap();
        fs::create_dir(store.path().join("sample")).unwrap();
        let staging = tempfile::Builder::new()
            .prefix(".skill-import-")
            .tempdir_in(store.path())
            .unwrap();
        fs::create_dir(staging.path().join("new")).unwrap();
        fs::create_dir(staging.path().join("previous")).unwrap();
        let intended = b"{\"skills\":{\"sample\":{\"latest_version\":\"1.2\",\"versions\":[\"1.2\"]}}}";
        record_transaction(staging.path(), store.path(), "sample", "1.2", false, intended).unwrap();
        assert!(
            format!("{:#}", recover_abandoned_imports(store.path()).unwrap_err())
                .contains("unexpected previous version")
        );
        assert!(staging.path().join("new").is_dir());
        assert!(staging.path().join("previous").is_dir());
        assert_eq!(fs::read(store.path().join("index.json")).unwrap(), old_index);
        assert!(!store.path().join("sample/1.2").exists());
    }

    #[test]
    fn recovery_rejects_existing_destination_for_first_import() {
        let store = tempfile::tempdir().unwrap();
        let old_index = b"{\"skills\":{}}";
        fs::write(store.path().join("index.json"), old_index).unwrap();
        let dest = store.path().join("sample/1.2");
        fs::create_dir_all(&dest).unwrap();
        fs::write(dest.join("SKILL.md"), "squat").unwrap();
        let staging = tempfile::Builder::new()
            .prefix(".skill-import-")
            .tempdir_in(store.path())
            .unwrap();
        fs::create_dir(staging.path().join("new")).unwrap();
        fs::write(staging.path().join("new/SKILL.md"), "new").unwrap();
        let intended = b"{\"skills\":{\"sample\":{\"latest_version\":\"1.2\",\"versions\":[\"1.2\"]}}}";
        record_transaction(staging.path(), store.path(), "sample", "1.2", false, intended).unwrap();
        assert!(
            format!("{:#}", recover_abandoned_imports(store.path()).unwrap_err()).contains("ambiguous transaction")
        );
        assert_eq!(fs::read_to_string(dest.join("SKILL.md")).unwrap(), "squat");
        assert!(staging.path().join("new").is_dir());
        assert_eq!(fs::read(store.path().join("index.json")).unwrap(), old_index);
    }

    #[test]
    fn recovery_rejects_missing_directories_after_publication() {
        let store = tempfile::tempdir().unwrap();
        let old_index = b"{\"skills\":{}}";
        fs::write(store.path().join("index.json"), old_index).unwrap();
        fs::create_dir(store.path().join("sample")).unwrap();
        let staging = tempfile::Builder::new()
            .prefix(".skill-import-")
            .tempdir_in(store.path())
            .unwrap();
        let intended = b"{\"skills\":{\"sample\":{\"latest_version\":\"1.2\",\"versions\":[\"1.2\"]}}}";
        record_transaction(staging.path(), store.path(), "sample", "1.2", false, intended).unwrap();
        assert!(
            format!("{:#}", recover_abandoned_imports(store.path()).unwrap_err())
                .contains("published version is missing")
        );
        assert_eq!(fs::read(store.path().join("index.json")).unwrap(), old_index);
        assert!(!store.path().join("sample/1.2").exists());
    }

    #[test]
    fn recovery_rejects_missing_backup_before_index_commit() {
        let store = tempfile::tempdir().unwrap();
        let previous = b"{\"skills\":{\"sample\":{\"latest_version\":\"0.9\",\"versions\":[\"0.9\"]}}}";
        let intended = b"{\"skills\":{\"sample\":{\"latest_version\":\"1.2\",\"versions\":[\"0.9\",\"1.2\"]}}}";
        fs::write(store.path().join("index.json"), previous).unwrap();
        let dest = store.path().join("sample/1.2");
        fs::create_dir_all(&dest).unwrap();
        fs::write(dest.join("SKILL.md"), "new").unwrap();
        let staging = tempfile::Builder::new()
            .prefix(".skill-import-")
            .tempdir_in(store.path())
            .unwrap();
        record_transaction(staging.path(), store.path(), "sample", "1.2", true, intended).unwrap();
        assert!(
            format!("{:#}", recover_abandoned_imports(store.path()).unwrap_err())
                .contains("previous version is missing")
        );
        assert_eq!(fs::read_to_string(dest.join("SKILL.md")).unwrap(), "new");
        assert_eq!(fs::read(store.path().join("index.json")).unwrap(), previous);
    }

    #[test]
    fn recovery_rejects_committed_index_with_unpublished_staging() {
        let store = tempfile::tempdir().unwrap();
        let old_index = b"{\"skills\":{}}";
        let intended = b"{\"skills\":{\"sample\":{\"latest_version\":\"1.2\",\"versions\":[\"1.2\"]}}}";
        fs::write(store.path().join("index.json"), old_index).unwrap();
        fs::create_dir(store.path().join("sample")).unwrap();
        let staging = tempfile::Builder::new()
            .prefix(".skill-import-")
            .tempdir_in(store.path())
            .unwrap();
        fs::create_dir(staging.path().join("new")).unwrap();
        fs::write(staging.path().join("new/SKILL.md"), "new").unwrap();
        record_transaction(staging.path(), store.path(), "sample", "1.2", false, intended).unwrap();
        fs::write(store.path().join("index.json"), intended).unwrap();
        assert!(
            format!("{:#}", recover_abandoned_imports(store.path()).unwrap_err())
                .contains("committed before version publication")
        );
        assert!(staging.path().join("new").is_dir());
        assert_eq!(fs::read(store.path().join("index.json")).unwrap(), intended);
        assert!(!store.path().join("sample/1.2").exists());
    }

    #[test]
    fn recovery_rejects_invalid_record_version() {
        let store = tempfile::tempdir().unwrap();
        let staging = tempfile::Builder::new()
            .prefix(".skill-import-")
            .tempdir_in(store.path())
            .unwrap();
        let record = TransactionRecord {
            format_version: 2,
            skill_name: "sample".to_owned(),
            version: "../evil".to_owned(),
            had_previous: false,
            previous_index: None,
            intended_index: b"{\"skills\":{}}".to_vec(),
        };
        fs::write(staging.path().join("transaction.json"), serde_json::to_vec(&record).unwrap()).unwrap();
        assert!(
            format!("{:#}", recover_abandoned_imports(store.path()).unwrap_err())
                .contains("invalid transaction version")
        );
        assert!(staging.path().join("transaction.json").exists());
    }

    #[test]
    fn recovery_rejects_missing_destination_parent() {
        let store = tempfile::tempdir().unwrap();
        let old_index = b"{\"skills\":{}}";
        let intended = b"{\"skills\":{\"sample\":{\"latest_version\":\"1.2\",\"versions\":[\"1.2\"]}}}";
        fs::write(store.path().join("index.json"), old_index).unwrap();
        let staging = tempfile::Builder::new()
            .prefix(".skill-import-")
            .tempdir_in(store.path())
            .unwrap();
        fs::create_dir(staging.path().join("new")).unwrap();
        record_transaction(staging.path(), store.path(), "sample", "1.2", false, intended).unwrap();
        assert!(
            format!("{:#}", recover_abandoned_imports(store.path()).unwrap_err())
                .contains("destination parent is missing")
        );
        assert!(staging.path().join("new").is_dir());
        assert_eq!(fs::read(store.path().join("index.json")).unwrap(), old_index);
    }
}