skillnet 0.6.0

Manage canonical AI skill stores, derived views, and calibration data for multi-phase-plan.
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
//! View materialisation primitives.
//!
//! A view is a flat directory of symlinks, one symlink per canonical skill
//! directory. Global views use absolute symlink targets because they live under
//! user homes. Project views use relative symlink targets so checked-in project
//! links remain portable across machines.
//!
//! Atomicity is per skill: each link is checked and, if needed, replaced by
//! creating a temporary symlink in the same directory and renaming it into
//! place. A partial failure may leave earlier skills synced and later skills
//! untouched. There is no transactional rollback; rerun the sync after fixing
//! the underlying error. Stale entries are only removed when `allow_delete` is
//! set. A failed write exits with the per-skill error, and status/doctor can
//! surface the remaining drift on the next inspection.

use std::{
    collections::{BTreeMap, BTreeSet},
    fs,
    os::unix::fs as unix_fs,
    path::{Component, Path, PathBuf},
    time::{SystemTime, UNIX_EPOCH},
};

use anyhow::{bail, Context, Result};
use camino::{Utf8Path, Utf8PathBuf};
use serde::Serialize;
use walkdir::WalkDir;

use crate::{
    fs_ops::{content_signature, copy_dir, newest_mtime_nanos},
    mirror::mirror_skill_dirs,
    model::{Target, ViewTarget},
};

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ViewSyncOptions {
    pub allow_delete: bool,
    pub force: bool,
    pub relative_links: bool,
}

#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct ViewSyncSummary {
    pub created: usize,
    pub updated: usize,
    pub unchanged: usize,
    pub removed: usize,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct DriftEntry {
    pub skill: String,
    pub kind: DriftKind,
    pub expected: Option<Utf8PathBuf>,
    pub actual: Option<Utf8PathBuf>,
    pub view_mtime_nanos: Option<u128>,
    pub canonical_mtime_nanos: Option<u128>,
    pub view_sha: Option<String>,
    pub canonical_sha: Option<String>,
    #[serde(skip)]
    pub reconcile_outcome: Option<ReconcileOutcome>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum DriftKind {
    Missing,
    WrongTarget,
    NonSymlink,
    Stale,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct FileDelta {
    pub skill: String,
    pub kind: FileDeltaKind,
    pub expected: Option<Utf8PathBuf>,
    pub actual: Option<Utf8PathBuf>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub enum ReconcileOutcome {
    Identical,
    ViewNewer {
        view_mtime: u128,
        canonical_mtime: u128,
    },
    CanonicalNewer {
        view_mtime: u128,
        canonical_mtime: u128,
    },
    EqualMtimeDifferentContent {
        view_sha: String,
        canonical_sha: String,
        mtime: u128,
    },
    BothAdvanced {
        view_only: Vec<Utf8PathBuf>,
        canonical_only: Vec<Utf8PathBuf>,
    },
    AdoptCandidate,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum Preference {
    View,
    Canonical,
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PromotionOptions {
    pub apply_promote: bool,
    pub force_demote: bool,
    pub prefer: Option<Preference>,
    pub adopt_new: bool,
    pub allow_delete: bool,
    pub relative_links: bool,
    pub project_root: Option<Utf8PathBuf>,
}

#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct PromotionSummary {
    pub view: ViewSyncSummary,
    pub promoted: Vec<String>,
    pub demoted_destructive: Vec<String>,
    pub adopted: Vec<String>,
    pub would_promote: Vec<WouldEntry>,
    pub would_demote_destructive: Vec<WouldEntry>,
    pub needs_tie_break: Vec<WouldEntry>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct WouldEntry {
    pub skill: String,
    pub outcome: ReconcileOutcome,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum FileDeltaKind {
    Missing,
    Extra,
    Modified,
}

pub fn materialize_view(canonical: &Utf8Path, view: &ViewTarget) -> Result<ViewSyncSummary> {
    materialize_view_with_options(canonical, view, ViewSyncOptions::default())
}

pub fn materialize_view_with_options(
    canonical: &Utf8Path,
    view: &ViewTarget,
    options: ViewSyncOptions,
) -> Result<ViewSyncSummary> {
    let expected = expected_skill_links(canonical)?;
    fs::create_dir_all(&view.path)
        .with_context(|| format!("failed to create view directory {}", view.path))?;

    let mut summary = ViewSyncSummary::default();
    for (skill, target) in &expected {
        let link = view.path.join(skill);
        let desired = desired_link_target(&view.path, target, options.relative_links)?;
        match ensure_symlink(&link, &desired, options.force)
            .with_context(|| format!("failed to sync skill `{skill}` into {}", view.path))?
        {
            LinkAction::Created => summary.created += 1,
            LinkAction::Updated => summary.updated += 1,
            LinkAction::Unchanged => summary.unchanged += 1,
        }
    }

    if options.allow_delete {
        for stale in stale_view_entries(&view.path, expected.keys())? {
            remove_view_entry(&stale)
                .with_context(|| format!("failed to remove stale view entry {stale}"))?;
            summary.removed += 1;
        }
    }

    Ok(summary)
}

pub fn materialize_view_with_promotion(
    canonical_root: &Utf8Path,
    view: &ViewTarget,
    options: PromotionOptions,
    ensure_clean: impl Fn(&Utf8Path) -> Result<()>,
) -> Result<PromotionSummary> {
    ensure_clean(canonical_root)?;

    let expected = expected_skill_links(canonical_root)?;
    fs::create_dir_all(&view.path)
        .with_context(|| format!("failed to create view directory {}", view.path))?;

    let mut summary = PromotionSummary::default();
    for (skill, canonical_skill) in &expected {
        let link = view.path.join(skill);
        let desired = desired_link_target(&view.path, canonical_skill, options.relative_links)?;
        match fs::symlink_metadata(&link) {
            Ok(metadata) if metadata.file_type().is_symlink() => {
                let current = read_link_utf8(&link)?;
                if current == desired {
                    summary.view.unchanged += 1;
                } else {
                    atomic_symlink(&desired, &link)?;
                    summary.view.updated += 1;
                }
            }
            Ok(_) => {
                let outcome = compare_view_entry(canonical_skill, &link)
                    .with_context(|| format!("failed to compare skill `{skill}`"))?;
                apply_reconcile_outcome(
                    ReconcileAction {
                        skill,
                        link: &link,
                        canonical_skill,
                        desired: &desired,
                    },
                    outcome,
                    &options,
                    &ensure_clean,
                    &mut summary,
                )?;
            }
            Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
                atomic_symlink(&desired, &link)?;
                summary.view.created += 1;
            }
            Err(err) => return Err(err).with_context(|| format!("failed to inspect {link}")),
        }
    }

    for stale in stale_view_entries(&view.path, expected.keys())? {
        let skill = stale
            .file_name()
            .context("stale view entry has no final component")?
            .to_string();
        let metadata = fs::symlink_metadata(&stale)
            .with_context(|| format!("failed to inspect stale view entry {stale}"))?;
        if metadata.file_type().is_symlink() {
            if options.allow_delete {
                remove_view_entry(&stale)
                    .with_context(|| format!("failed to remove stale view entry {stale}"))?;
                summary.view.removed += 1;
            }
            continue;
        }

        let canonical_skill = canonical_root.join(&skill);
        let outcome = compare_view_entry(&canonical_skill, &stale)
            .with_context(|| format!("failed to compare stale skill `{skill}`"))?;
        if matches!(outcome, ReconcileOutcome::AdoptCandidate)
            && options.adopt_new
            && options.apply_promote
        {
            ensure_project_clean_for_write(&options, &ensure_clean)?;
            promote_view_to_canonical(&stale, &canonical_skill)
                .with_context(|| format!("failed to adopt skill `{skill}`"))?;
            summary.adopted.push(skill);
        }
    }

    Ok(summary)
}

struct ReconcileAction<'a> {
    skill: &'a str,
    link: &'a Utf8Path,
    canonical_skill: &'a Utf8Path,
    desired: &'a Utf8Path,
}

fn apply_reconcile_outcome(
    action: ReconcileAction<'_>,
    outcome: ReconcileOutcome,
    options: &PromotionOptions,
    ensure_clean: &impl Fn(&Utf8Path) -> Result<()>,
    summary: &mut PromotionSummary,
) -> Result<()> {
    match outcome {
        ReconcileOutcome::Identical => {
            demote_to_symlink(action.link, action.desired)?;
            summary.view.updated += 1;
        }
        ReconcileOutcome::ViewNewer { .. } if options.apply_promote => {
            ensure_project_clean_for_write(options, ensure_clean)?;
            promote_view_to_canonical(action.link, action.canonical_skill)
                .with_context(|| format!("failed to promote skill `{}`", action.skill))?;
            demote_to_symlink(action.link, action.desired)?;
            summary.promoted.push(action.skill.to_string());
            summary.view.updated += 1;
        }
        ReconcileOutcome::ViewNewer { .. } => {
            summary.would_promote.push(WouldEntry {
                skill: action.skill.to_string(),
                outcome,
            });
        }
        ReconcileOutcome::CanonicalNewer { .. } if options.force_demote => {
            demote_to_symlink(action.link, action.desired)?;
            summary.demoted_destructive.push(action.skill.to_string());
            summary.view.updated += 1;
        }
        ReconcileOutcome::CanonicalNewer { .. } => {
            summary.would_demote_destructive.push(WouldEntry {
                skill: action.skill.to_string(),
                outcome,
            });
        }
        ReconcileOutcome::EqualMtimeDifferentContent { .. }
        | ReconcileOutcome::BothAdvanced { .. }
            if options.prefer == Some(Preference::View) && options.apply_promote =>
        {
            ensure_project_clean_for_write(options, ensure_clean)?;
            promote_view_to_canonical(action.link, action.canonical_skill)
                .with_context(|| format!("failed to promote skill `{}`", action.skill))?;
            demote_to_symlink(action.link, action.desired)?;
            summary.promoted.push(action.skill.to_string());
            summary.view.updated += 1;
        }
        ReconcileOutcome::EqualMtimeDifferentContent { .. }
        | ReconcileOutcome::BothAdvanced { .. }
            if options.prefer == Some(Preference::Canonical) && options.force_demote =>
        {
            demote_to_symlink(action.link, action.desired)?;
            summary.demoted_destructive.push(action.skill.to_string());
            summary.view.updated += 1;
        }
        ReconcileOutcome::EqualMtimeDifferentContent { .. }
        | ReconcileOutcome::BothAdvanced { .. } => {
            summary.needs_tie_break.push(WouldEntry {
                skill: action.skill.to_string(),
                outcome,
            });
        }
        ReconcileOutcome::AdoptCandidate if options.adopt_new && options.apply_promote => {
            ensure_project_clean_for_write(options, ensure_clean)?;
            promote_view_to_canonical(action.link, action.canonical_skill)
                .with_context(|| format!("failed to adopt skill `{}`", action.skill))?;
            summary.adopted.push(action.skill.to_string());
        }
        ReconcileOutcome::AdoptCandidate => {}
    }
    Ok(())
}

fn ensure_project_clean_for_write(
    options: &PromotionOptions,
    ensure_clean: &impl Fn(&Utf8Path) -> Result<()>,
) -> Result<()> {
    if let Some(project_root) = &options.project_root {
        ensure_clean(project_root)?;
    }
    Ok(())
}

fn demote_to_symlink(link: &Utf8Path, desired: &Utf8Path) -> Result<()> {
    remove_view_entry(link)?;
    atomic_symlink(desired, link)
}

pub fn compare_view_entry(
    canonical_skill: &Utf8Path,
    view_entry: &Utf8Path,
) -> Result<ReconcileOutcome> {
    let metadata = fs::symlink_metadata(view_entry)
        .with_context(|| format!("failed to inspect view entry {view_entry}"))?;
    if metadata.file_type().is_symlink() {
        bail!("{view_entry} is a symlink; compare_view_entry expects a non-symlink view entry");
    }
    if !canonical_skill.exists() {
        return Ok(ReconcileOutcome::AdoptCandidate);
    }

    let view_mtime = newest_mtime_nanos(view_entry)
        .with_context(|| format!("failed to compute newest mtime for {view_entry}"))?;
    let canonical_mtime = newest_mtime_nanos(canonical_skill)
        .with_context(|| format!("failed to compute newest mtime for {canonical_skill}"))?;
    let view_sha = content_signature(view_entry)
        .with_context(|| format!("failed to compute content signature for {view_entry}"))?;
    let canonical_sha = content_signature(canonical_skill)
        .with_context(|| format!("failed to compute content signature for {canonical_skill}"))?;

    if view_mtime > now_nanos()? {
        return Ok(ReconcileOutcome::EqualMtimeDifferentContent {
            view_sha,
            canonical_sha,
            mtime: view_mtime,
        });
    }

    if view_sha == canonical_sha {
        return Ok(ReconcileOutcome::Identical);
    }

    match view_mtime.cmp(&canonical_mtime) {
        std::cmp::Ordering::Greater => {
            let (view_only, canonical_only) = file_set_delta(view_entry, canonical_skill)?;
            if canonical_only.is_empty() {
                Ok(ReconcileOutcome::ViewNewer {
                    view_mtime,
                    canonical_mtime,
                })
            } else {
                Ok(ReconcileOutcome::BothAdvanced {
                    view_only,
                    canonical_only,
                })
            }
        }
        std::cmp::Ordering::Less => {
            let (view_only, canonical_only) = file_set_delta(view_entry, canonical_skill)?;
            if view_only.is_empty() {
                Ok(ReconcileOutcome::CanonicalNewer {
                    view_mtime,
                    canonical_mtime,
                })
            } else {
                Ok(ReconcileOutcome::BothAdvanced {
                    view_only,
                    canonical_only,
                })
            }
        }
        std::cmp::Ordering::Equal => Ok(ReconcileOutcome::EqualMtimeDifferentContent {
            view_sha,
            canonical_sha,
            mtime: view_mtime,
        }),
    }
}

pub fn promote_view_to_canonical(view_entry: &Utf8Path, canonical_skill: &Utf8Path) -> Result<()> {
    let canonical_parent = canonical_skill
        .parent()
        .context("canonical skill path has no parent")?;
    let skill = canonical_skill
        .file_name()
        .context("canonical skill path has no file name")?;
    let staging_parent = canonical_parent.join(".skillnet-tmp");
    let staging = staging_parent.join(format!("{skill}-{}", std::process::id()));

    if staging.exists() {
        fs::remove_dir_all(&staging)
            .with_context(|| format!("failed to remove stale staging directory {staging}"))?;
    }
    fs::create_dir_all(&staging_parent)
        .with_context(|| format!("failed to create staging parent {staging_parent}"))?;
    copy_dir(view_entry, &staging)
        .with_context(|| format!("failed to stage promoted skill from {view_entry}"))?;

    if canonical_skill.exists() {
        fs::remove_dir_all(canonical_skill).with_context(|| {
            format!("failed to remove existing canonical skill {canonical_skill}")
        })?;
    }
    fs::rename(&staging, canonical_skill)
        .with_context(|| format!("failed to replace canonical skill {canonical_skill}"))?;
    Ok(())
}

pub fn view_status(canonical: &Utf8Path, view: &ViewTarget) -> Result<Vec<DriftEntry>> {
    view_status_with_options(canonical, view, ViewSyncOptions::default())
}

pub fn view_status_with_options(
    canonical: &Utf8Path,
    view: &ViewTarget,
    options: ViewSyncOptions,
) -> Result<Vec<DriftEntry>> {
    let expected = expected_skill_links(canonical)?;
    let mut drift = Vec::new();

    for (skill, target) in &expected {
        let link = view.path.join(skill);
        let desired = desired_link_target(&view.path, target, options.relative_links)?;
        let metadata = match fs::symlink_metadata(&link) {
            Ok(metadata) => metadata,
            Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
                drift.push(DriftEntry {
                    skill: skill.clone(),
                    kind: DriftKind::Missing,
                    expected: Some(desired),
                    actual: None,
                    view_mtime_nanos: None,
                    canonical_mtime_nanos: None,
                    view_sha: None,
                    canonical_sha: None,
                    reconcile_outcome: None,
                });
                continue;
            }
            Err(err) => return Err(err).with_context(|| format!("failed to inspect {link}")),
        };
        if !metadata.file_type().is_symlink() {
            drift.push(non_symlink_drift_entry(skill, &desired, target, &link));
            continue;
        }
        let actual = read_link_utf8(&link)?;
        if actual != desired {
            drift.push(DriftEntry {
                skill: skill.clone(),
                kind: DriftKind::WrongTarget,
                expected: Some(desired),
                actual: Some(actual),
                view_mtime_nanos: None,
                canonical_mtime_nanos: None,
                view_sha: None,
                canonical_sha: None,
                reconcile_outcome: None,
            });
        }
    }

    for stale in stale_view_entries(&view.path, expected.keys())? {
        drift.push(DriftEntry {
            skill: stale
                .file_name()
                .context("stale view entry has no final component")?
                .to_string(),
            kind: DriftKind::Stale,
            expected: None,
            actual: Some(stale),
            view_mtime_nanos: None,
            canonical_mtime_nanos: None,
            view_sha: None,
            canonical_sha: None,
            reconcile_outcome: None,
        });
    }

    Ok(drift)
}

fn non_symlink_drift_entry(
    skill: &str,
    desired: &Utf8Path,
    canonical_skill: &Utf8Path,
    link: &Utf8Path,
) -> DriftEntry {
    let mut entry = DriftEntry {
        skill: skill.to_string(),
        kind: DriftKind::NonSymlink,
        expected: Some(desired.to_path_buf()),
        actual: Some(link.to_path_buf()),
        view_mtime_nanos: None,
        canonical_mtime_nanos: None,
        view_sha: None,
        canonical_sha: None,
        reconcile_outcome: None,
    };

    match compare_view_entry(canonical_skill, link) {
        Ok(outcome) => {
            match &outcome {
                ReconcileOutcome::ViewNewer {
                    view_mtime,
                    canonical_mtime,
                }
                | ReconcileOutcome::CanonicalNewer {
                    view_mtime,
                    canonical_mtime,
                } => {
                    entry.view_mtime_nanos = Some(*view_mtime);
                    entry.canonical_mtime_nanos = Some(*canonical_mtime);
                }
                ReconcileOutcome::EqualMtimeDifferentContent {
                    view_sha,
                    canonical_sha,
                    mtime,
                } => {
                    entry.view_mtime_nanos = Some(*mtime);
                    entry.canonical_mtime_nanos = Some(*mtime);
                    entry.view_sha = Some(view_sha.clone());
                    entry.canonical_sha = Some(canonical_sha.clone());
                }
                ReconcileOutcome::BothAdvanced { .. }
                | ReconcileOutcome::Identical
                | ReconcileOutcome::AdoptCandidate => {}
            }
            entry.reconcile_outcome = Some(outcome);
        }
        Err(err) => eprintln!("warn: failed to classify non-symlink view entry {link}: {err:#}"),
    }

    entry
}

pub fn view_diff(canonical: &Utf8Path, view: &ViewTarget) -> Result<Vec<FileDelta>> {
    view_diff_with_options(canonical, view, ViewSyncOptions::default())
}

pub fn view_diff_with_options(
    canonical: &Utf8Path,
    view: &ViewTarget,
    options: ViewSyncOptions,
) -> Result<Vec<FileDelta>> {
    view_status_with_options(canonical, view, options).map(|entries| {
        entries
            .into_iter()
            .map(|entry| FileDelta {
                skill: entry.skill,
                kind: match entry.kind {
                    DriftKind::Missing => FileDeltaKind::Missing,
                    DriftKind::Stale => FileDeltaKind::Extra,
                    DriftKind::WrongTarget | DriftKind::NonSymlink => FileDeltaKind::Modified,
                },
                expected: entry.expected,
                actual: entry.actual,
            })
            .collect()
    })
}

pub fn materialize_project(target: &Target) -> Result<ProjectSyncSummary> {
    materialize_project_with_options(
        target,
        ProjectSyncOptions {
            allow_delete: false,
            force: false,
        },
    )
}

pub fn materialize_project_with_options(
    target: &Target,
    options: ProjectSyncOptions,
) -> Result<ProjectSyncSummary> {
    let mut views = Vec::with_capacity(target.views.len());
    for view in &target.views {
        let summary = materialize_view_with_options(
            &target.canonical_path,
            view,
            ViewSyncOptions {
                allow_delete: options.allow_delete,
                force: options.force,
                relative_links: true,
            },
        )?;
        views.push(ProjectViewSummary {
            label: view.label.clone(),
            path: view.path.clone(),
            summary,
        });
    }

    let aggregator = match &target.aggregator_path {
        Some(path) => Some(ensure_aggregator_symlink(path, &target.canonical_path)?),
        None => None,
    };

    Ok(ProjectSyncSummary { views, aggregator })
}

pub fn materialize_project_with_promotion(
    target: &Target,
    options: PromotionOptions,
    ensure_clean: impl Fn(&Utf8Path) -> Result<()>,
) -> Result<ProjectPromotionSummary> {
    let mut views = Vec::with_capacity(target.views.len());
    for view in &target.views {
        let summary = materialize_view_with_promotion(
            &target.canonical_path,
            view,
            PromotionOptions {
                relative_links: true,
                project_root: target.project_root.clone(),
                ..options.clone()
            },
            &ensure_clean,
        )?;
        views.push(ProjectPromotionViewSummary {
            label: view.label.clone(),
            path: view.path.clone(),
            summary,
        });
    }

    let aggregator = match &target.aggregator_path {
        Some(path) => Some(ensure_aggregator_symlink(path, &target.canonical_path)?),
        None => None,
    };

    Ok(ProjectPromotionSummary { views, aggregator })
}

pub fn project_status(target: &Target) -> Result<Vec<DriftEntry>> {
    let mut drift = Vec::new();
    for view in &target.views {
        drift.extend(view_status_with_options(
            &target.canonical_path,
            view,
            ViewSyncOptions {
                relative_links: true,
                ..ViewSyncOptions::default()
            },
        )?);
    }
    if let Some(path) = &target.aggregator_path {
        if aggregator_status(path, &target.canonical_path)? != AggregatorStatus::Unchanged {
            drift.push(DriftEntry {
                skill: "aggregator".to_string(),
                kind: DriftKind::WrongTarget,
                expected: Some(target.canonical_path.clone()),
                actual: fs::read_link(path)
                    .ok()
                    .and_then(|path| Utf8PathBuf::from_path_buf(path).ok()),
                view_mtime_nanos: None,
                canonical_mtime_nanos: None,
                view_sha: None,
                canonical_sha: None,
                reconcile_outcome: None,
            });
        }
    }
    Ok(drift)
}

pub fn project_diff(target: &Target) -> Result<Vec<FileDelta>> {
    let mut deltas = Vec::new();
    for view in &target.views {
        deltas.extend(view_diff_with_options(
            &target.canonical_path,
            view,
            ViewSyncOptions {
                relative_links: true,
                ..ViewSyncOptions::default()
            },
        )?);
    }
    if let Some(path) = &target.aggregator_path {
        if aggregator_status(path, &target.canonical_path)? != AggregatorStatus::Unchanged {
            deltas.push(FileDelta {
                skill: "aggregator".to_string(),
                kind: FileDeltaKind::Modified,
                expected: Some(target.canonical_path.clone()),
                actual: fs::read_link(path)
                    .ok()
                    .and_then(|path| Utf8PathBuf::from_path_buf(path).ok()),
            });
        }
    }
    Ok(deltas)
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ProjectSyncOptions {
    pub allow_delete: bool,
    pub force: bool,
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ProjectSyncSummary {
    pub views: Vec<ProjectViewSummary>,
    pub aggregator: Option<AggregatorStatus>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProjectViewSummary {
    pub label: String,
    pub path: Utf8PathBuf,
    pub summary: ViewSyncSummary,
}

#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct ProjectPromotionSummary {
    pub views: Vec<ProjectPromotionViewSummary>,
    pub aggregator: Option<AggregatorStatus>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ProjectPromotionViewSummary {
    pub label: String,
    pub path: Utf8PathBuf,
    pub summary: PromotionSummary,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum AggregatorStatus {
    Created,
    Updated,
    Unchanged,
}

fn now_nanos() -> Result<u128> {
    Ok(SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .context("system clock is before the Unix epoch")?
        .as_nanos())
}

fn file_set_delta(
    view_entry: &Utf8Path,
    canonical_skill: &Utf8Path,
) -> Result<(Vec<Utf8PathBuf>, Vec<Utf8PathBuf>)> {
    let view_files = comparable_file_set(view_entry)?;
    let canonical_files = comparable_file_set(canonical_skill)?;
    let view_only = view_files.difference(&canonical_files).cloned().collect();
    let canonical_only = canonical_files.difference(&view_files).cloned().collect();
    Ok((view_only, canonical_only))
}

fn comparable_file_set(root: &Utf8Path) -> Result<BTreeSet<Utf8PathBuf>> {
    let mut files = BTreeSet::new();
    for entry in WalkDir::new(root).follow_links(false) {
        let entry = entry?;
        let path = Utf8PathBuf::from_path_buf(entry.path().to_path_buf())
            .map_err(|p| anyhow::anyhow!("non-UTF-8 path in skill tree: {}", p.display()))?;
        if path
            .components()
            .any(|component| component.as_str() == ".skillnet-tmp")
        {
            continue;
        }
        let metadata = fs::symlink_metadata(&path)?;
        if metadata.file_type().is_file() || metadata.file_type().is_symlink() {
            files.insert(path.strip_prefix(root)?.to_path_buf());
        }
    }
    Ok(files)
}

fn expected_skill_links(canonical: &Utf8Path) -> Result<BTreeMap<String, Utf8PathBuf>> {
    mirror_skill_dirs(canonical)?
        .into_iter()
        .map(|path| {
            let skill = path
                .file_name()
                .context("canonical skill directory has no final component")?
                .to_string();
            Ok((skill, path))
        })
        .collect()
}

fn ensure_symlink(link: &Utf8Path, desired: &Utf8Path, force: bool) -> Result<LinkAction> {
    match fs::symlink_metadata(link) {
        Ok(metadata) if metadata.file_type().is_symlink() => {
            let current = read_link_utf8(link)?;
            if current == desired {
                return Ok(LinkAction::Unchanged);
            }
            atomic_symlink(desired, link)?;
            Ok(LinkAction::Updated)
        }
        Ok(_) if force => {
            remove_view_entry(link)?;
            atomic_symlink(desired, link)?;
            Ok(LinkAction::Updated)
        }
        Ok(_) => bail!("{link} exists and is not a symlink; pass --force to replace it"),
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
            atomic_symlink(desired, link)?;
            Ok(LinkAction::Created)
        }
        Err(err) => Err(err).with_context(|| format!("failed to inspect {link}")),
    }
}

fn ensure_aggregator_symlink(path: &Utf8Path, canonical: &Utf8Path) -> Result<AggregatorStatus> {
    match fs::symlink_metadata(path) {
        Ok(metadata) if metadata.file_type().is_symlink() => {
            let current = read_link_utf8(path)?;
            if current == canonical {
                return Ok(AggregatorStatus::Unchanged);
            }
            atomic_symlink(canonical, path)?;
            Ok(AggregatorStatus::Updated)
        }
        Ok(metadata) if metadata.is_dir() => bail!(
            "aggregator path `{path}` is a directory; run `skillnet project clean <name>` first or remove it manually"
        ),
        Ok(_) => bail!("aggregator path `{path}` exists and is not a symlink"),
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
            atomic_symlink(canonical, path)?;
            Ok(AggregatorStatus::Created)
        }
        Err(err) => Err(err).with_context(|| format!("failed to inspect {path}")),
    }
}

fn aggregator_status(path: &Utf8Path, canonical: &Utf8Path) -> Result<AggregatorStatus> {
    match fs::symlink_metadata(path) {
        Ok(metadata) if metadata.file_type().is_symlink() => {
            let current = read_link_utf8(path)?;
            if current == canonical {
                Ok(AggregatorStatus::Unchanged)
            } else {
                Ok(AggregatorStatus::Updated)
            }
        }
        Ok(_) => Ok(AggregatorStatus::Updated),
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(AggregatorStatus::Created),
        Err(err) => Err(err).with_context(|| format!("failed to inspect {path}")),
    }
}

fn stale_view_entries<'a>(
    view_path: &Utf8Path,
    expected: impl Iterator<Item = &'a String>,
) -> Result<Vec<Utf8PathBuf>> {
    if !view_path.exists() {
        return Ok(Vec::new());
    }
    let expected = expected.cloned().collect::<BTreeSet<_>>();
    let mut stale = Vec::new();
    for entry in fs::read_dir(view_path)? {
        let entry = entry?;
        let path = Utf8PathBuf::from_path_buf(entry.path())
            .map_err(|p| anyhow::anyhow!("non-UTF-8 path in view: {}", p.display()))?;
        let name = path
            .file_name()
            .context("view entry has no final component")?
            .to_string();
        if !expected.contains(&name) {
            stale.push(path);
        }
    }
    stale.sort();
    Ok(stale)
}

fn remove_view_entry(path: &Utf8Path) -> Result<()> {
    let metadata = fs::symlink_metadata(path)?;
    if metadata.is_dir() && !metadata.file_type().is_symlink() {
        fs::remove_dir_all(path)
    } else {
        fs::remove_file(path)
    }
    .with_context(|| format!("failed to remove {path}"))
}

fn atomic_symlink(target: &Utf8Path, link: &Utf8Path) -> Result<()> {
    let parent = link.parent().context("symlink path has no parent")?;
    fs::create_dir_all(parent)?;
    let file_name = link.file_name().context("symlink path has no file name")?;
    let temp = parent.join(format!(".{file_name}.skillnet-tmp-{}", std::process::id()));
    if temp.exists() {
        remove_view_entry(&temp)?;
    }
    unix_fs::symlink(target, &temp)
        .with_context(|| format!("failed to create temporary symlink {temp} -> {target}"))?;
    fs::rename(&temp, link).with_context(|| format!("failed to replace symlink {link}"))
}

fn desired_link_target(
    link_parent: &Utf8Path,
    canonical_target: &Utf8Path,
    relative: bool,
) -> Result<Utf8PathBuf> {
    if !relative {
        return Ok(canonical_target.to_path_buf());
    }
    relative_path(link_parent, canonical_target)
}

fn relative_path(from_dir: &Utf8Path, to: &Utf8Path) -> Result<Utf8PathBuf> {
    let from = absolutize_for_relative(from_dir)?;
    let to = absolutize_for_relative(to)?;
    let from_components = normal_components(&from);
    let to_components = normal_components(&to);
    let common = from_components
        .iter()
        .zip(&to_components)
        .take_while(|(a, b)| a == b)
        .count();

    let mut out = PathBuf::new();
    for _ in common..from_components.len() {
        out.push("..");
    }
    for component in &to_components[common..] {
        out.push(component);
    }
    Utf8PathBuf::from_path_buf(out)
        .map_err(|p| anyhow::anyhow!("non-UTF-8 relative symlink target: {}", p.display()))
}

fn absolutize_for_relative(path: &Utf8Path) -> Result<PathBuf> {
    if path.is_absolute() {
        Ok(path.as_std_path().to_path_buf())
    } else {
        std::env::current_dir()
            .map(|cwd| cwd.join(path.as_std_path()))
            .context("failed to resolve current directory")
    }
}

fn normal_components(path: &Path) -> Vec<PathBuf> {
    path.components()
        .filter_map(|component| match component {
            Component::Prefix(prefix) => Some(PathBuf::from(prefix.as_os_str())),
            Component::RootDir => Some(PathBuf::from("/")),
            Component::Normal(part) => Some(PathBuf::from(part)),
            Component::ParentDir => Some(PathBuf::from("..")),
            Component::CurDir => None,
        })
        .collect()
}

fn read_link_utf8(path: &Utf8Path) -> Result<Utf8PathBuf> {
    Utf8PathBuf::from_path_buf(fs::read_link(path)?)
        .map_err(|p| anyhow::anyhow!("non-UTF-8 symlink target at {path}: {}", p.display()))
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum LinkAction {
    Created,
    Updated,
    Unchanged,
}