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
use std::{
    collections::BTreeSet,
    fmt, fs,
    path::{Component, Path, PathBuf},
};

use anyhow::{Context as AnyhowContext, Result};
use camino::{Utf8Path, Utf8PathBuf};

use super::Context;
use crate::{
    model::{Target, TargetScope, ViewTarget},
    view::{compare_view_entry, ReconcileOutcome},
};

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Severity {
    Info,
    Warn,
    Error,
}

impl fmt::Display for Severity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Info => f.write_str("info"),
            Self::Warn => f.write_str("warn"),
            Self::Error => f.write_str("error"),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Issue {
    pub severity: Severity,
    pub message: String,
    pub scope: String,
}

pub fn run(ctx: &Context) -> Result<()> {
    let issues = lint(ctx)?;
    if issues.is_empty() {
        println!("doctor: no issues");
        return Ok(());
    }

    for issue in &issues {
        eprintln!("{}: [{}] {}", issue.severity, issue.scope, issue.message);
    }
    if issues.iter().any(|issue| issue.severity != Severity::Info) {
        std::process::exit(1);
    }
    Ok(())
}

pub fn lint(ctx: &Context) -> Result<Vec<Issue>> {
    let mut issues = Vec::new();
    for target in ctx.all_targets()? {
        match target.scope {
            TargetScope::Global => check_global(&target, &mut issues)?,
            TargetScope::Project => check_project(&target, &mut issues)?,
        }
    }
    issues.sort_by(|left, right| {
        left.severity
            .cmp(&right.severity)
            .then(left.scope.cmp(&right.scope))
            .then(left.message.cmp(&right.message))
    });
    Ok(issues)
}

fn check_global(target: &Target, issues: &mut Vec<Issue>) -> Result<()> {
    if !target.canonical_path.is_dir() {
        issue(
            issues,
            target,
            Severity::Error,
            format!(
                "canonical store {} is missing or not a directory",
                target.canonical_path
            ),
        );
        return Ok(());
    }

    let canonical_real = canonicalize_utf8(&target.canonical_path)?;
    let canonical_skills = canonical_skill_names(&target.canonical_path)?;
    for view in &target.views {
        check_global_view(target, view, &canonical_real, &canonical_skills, issues)?;
    }
    Ok(())
}

fn check_global_view(
    target: &Target,
    view: &ViewTarget,
    canonical_real: &Utf8Path,
    canonical_skills: &BTreeSet<String>,
    issues: &mut Vec<Issue>,
) -> Result<()> {
    if !view.path.is_dir() {
        issue(
            issues,
            target,
            Severity::Error,
            format!(
                "view `{}` at {} is missing or not a directory",
                view.label, view.path
            ),
        );
        return Ok(());
    }

    let mut view_entries = BTreeSet::new();
    for entry in read_dir_utf8(&view.path)? {
        let name = entry_name(&entry)?;
        view_entries.insert(name.clone());
        let metadata = fs::symlink_metadata(&entry)
            .with_context(|| format!("failed to inspect view entry {entry}"))?;
        if !metadata.file_type().is_symlink() {
            issue_non_symlink(
                issues,
                target,
                &target.canonical_path.join(&name),
                &entry,
                format!(
                    "view `{}` entry `{name}` is not a symlink: {entry}",
                    view.label
                ),
            );
            continue;
        }

        match canonicalize_utf8(&entry) {
            Ok(resolved) => {
                if resolved.parent() != Some(canonical_real) {
                    issue(
                        issues,
                        target,
                        Severity::Error,
                        format!(
                            "view `{}` entry `{name}` resolves outside canonical store: {entry} -> {resolved}",
                            view.label
                        ),
                    );
                }
            }
            Err(err) => issue(
                issues,
                target,
                Severity::Error,
                format!(
                    "view `{}` entry `{name}` is a broken symlink: {entry}: {err}",
                    view.label
                ),
            ),
        }

        if !canonical_skills.contains(&name) {
            issue(
                issues,
                target,
                Severity::Error,
                format!(
                    "view `{}` entry `{name}` is not managed by skillnet; either register it or move it out of {}",
                    view.label, view.path
                ),
            );
        }
    }

    for skill in canonical_skills.difference(&view_entries) {
        issue(
            issues,
            target,
            Severity::Error,
            format!(
                "canonical skill `{skill}` is missing from view `{}` at {}",
                view.label, view.path
            ),
        );
    }
    Ok(())
}

fn check_project(target: &Target, issues: &mut Vec<Issue>) -> Result<()> {
    let Some(project_root) = &target.project_root else {
        issue(
            issues,
            target,
            Severity::Error,
            "project target is missing its project root metadata".to_string(),
        );
        return Ok(());
    };
    let canonical_rel = target
        .canonical_rel
        .as_deref()
        .unwrap_or(".skills")
        .trim_matches('/');

    if !project_root.exists() {
        issue(
            issues,
            target,
            Severity::Warn,
            format!(
                "project repository path {project_root} does not exist; skipping project checks"
            ),
        );
        return Ok(());
    }
    if !project_root.is_dir() {
        issue(
            issues,
            target,
            Severity::Error,
            format!("project repository path {project_root} exists but is not a directory"),
        );
        return Ok(());
    }
    if !target.canonical_path.is_dir() {
        issue(
            issues,
            target,
            Severity::Error,
            format!(
                "project canonical store {} is missing or not a directory",
                target.canonical_path
            ),
        );
        return Ok(());
    }

    let canonical_skills = canonical_skill_names(&target.canonical_path)?;
    for view in &target.views {
        check_project_view(
            target,
            view,
            project_root,
            canonical_rel,
            &canonical_skills,
            issues,
        )?;
    }
    check_project_aggregator(target, issues)?;
    Ok(())
}

fn check_project_view(
    target: &Target,
    view: &ViewTarget,
    project_root: &Utf8Path,
    canonical_rel: &str,
    canonical_skills: &BTreeSet<String>,
    issues: &mut Vec<Issue>,
) -> Result<()> {
    if !view.path.is_dir() {
        issue(
            issues,
            target,
            Severity::Error,
            format!(
                "project view `{}` at {} is missing or not a directory",
                view.label, view.path
            ),
        );
        return Ok(());
    }

    let mut view_entries = BTreeSet::new();
    for entry in read_dir_utf8(&view.path)? {
        let name = entry_name(&entry)?;
        view_entries.insert(name.clone());
        let metadata = fs::symlink_metadata(&entry)
            .with_context(|| format!("failed to inspect project view entry {entry}"))?;
        if !metadata.file_type().is_symlink() {
            issue_non_symlink(
                issues,
                target,
                &target.canonical_path.join(&name),
                &entry,
                format!(
                    "project view `{}` entry `{name}` is not a symlink: {entry}",
                    view.label
                ),
            );
            continue;
        }

        let actual = read_link_utf8(&entry)?;
        if actual.is_absolute() {
            issue(
                issues,
                target,
                Severity::Error,
                format!(
                    "project view `{}` entry `{name}` uses an absolute target: {entry} -> {actual}",
                    view.label
                ),
            );
        }
        let expected = relative_path(&view.path, &project_root.join(canonical_rel).join(&name))?;
        if actual != expected {
            issue(
                issues,
                target,
                Severity::Error,
                format!(
                    "project view `{}` entry `{name}` has target {actual}; expected {expected}",
                    view.label
                ),
            );
        }
        if !canonical_skills.contains(&name) {
            issue(
                issues,
                target,
                Severity::Error,
                format!(
                    "project view `{}` entry `{name}` has no canonical skill under {}",
                    view.label, target.canonical_path
                ),
            );
            continue;
        }
        let resolved = resolve_link_target(&view.path, &actual);
        if resolved != target.canonical_path.join(&name) {
            issue(
                issues,
                target,
                Severity::Error,
                format!(
                    "project view `{}` entry `{name}` resolves to {resolved}; expected {}",
                    view.label,
                    target.canonical_path.join(&name)
                ),
            );
        }
    }

    for skill in canonical_skills.difference(&view_entries) {
        issue(
            issues,
            target,
            Severity::Error,
            format!(
                "canonical skill `{skill}` is missing from project view `{}` at {}",
                view.label, view.path
            ),
        );
    }
    Ok(())
}

fn check_project_aggregator(target: &Target, issues: &mut Vec<Issue>) -> Result<()> {
    let Some(path) = &target.aggregator_path else {
        return Ok(());
    };
    match fs::symlink_metadata(path) {
        Ok(metadata) if metadata.file_type().is_symlink() => {
            let actual = read_link_utf8(path)?;
            if actual != target.canonical_path {
                issue(
                    issues,
                    target,
                    Severity::Error,
                    format!(
                        "aggregator symlink {path} points to {actual}; expected {}",
                        target.canonical_path
                    ),
                );
                return Ok(());
            }
            match canonicalize_utf8(path) {
                Ok(resolved) if resolved == canonicalize_utf8(&target.canonical_path)? => {}
                Ok(resolved) => issue(
                    issues,
                    target,
                    Severity::Error,
                    format!(
                        "aggregator symlink {path} resolves to {resolved}; expected {}",
                        target.canonical_path
                    ),
                ),
                Err(err) => issue(
                    issues,
                    target,
                    Severity::Error,
                    format!("aggregator symlink {path} is broken: {err}"),
                ),
            }
        }
        Ok(_) => issue(
            issues,
            target,
            Severity::Error,
            format!("aggregator path {path} exists but is not a symlink"),
        ),
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => issue(
            issues,
            target,
            Severity::Error,
            format!("aggregator symlink {path} is missing"),
        ),
        Err(err) => return Err(err).with_context(|| format!("failed to inspect {path}")),
    }
    Ok(())
}

fn canonical_skill_names(canonical_path: &Utf8Path) -> Result<BTreeSet<String>> {
    crate::mirror::mirror_skill_dirs(canonical_path)?
        .into_iter()
        .map(|path| entry_name(&path))
        .collect()
}

fn issue(issues: &mut Vec<Issue>, target: &Target, severity: Severity, message: String) {
    issues.push(Issue {
        severity,
        message,
        scope: target.name.clone(),
    });
}

fn issue_non_symlink(
    issues: &mut Vec<Issue>,
    target: &Target,
    canonical_skill: &Utf8Path,
    view_entry: &Utf8Path,
    prefix: String,
) {
    let (severity, hint) = match compare_view_entry(canonical_skill, view_entry) {
        Ok(outcome) => {
            let (severity, hint) = classify_non_symlink(&outcome);
            (severity, hint.to_string())
        }
        Err(err) => {
            let hint =
                format!("could not classify entry: {err}; rerun doctor or check permissions");
            (Severity::Error, hint)
        }
    };
    issue(issues, target, severity, format!("{prefix}; {hint}"));
}

fn classify_non_symlink(outcome: &ReconcileOutcome) -> (Severity, &'static str) {
    match outcome {
        ReconcileOutcome::Identical => {
            (Severity::Info, "`skillnet sync` will silently demote to symlink")
        }
        ReconcileOutcome::ViewNewer { .. } => {
            (
                Severity::Warn,
                "`skillnet sync --apply-promote` will pull view → canonical and re-link",
            )
        }
        ReconcileOutcome::CanonicalNewer { .. } => {
            (
                Severity::Error,
                "`skillnet sync --force` will destroy view-side edits; review before running",
            )
        }
        ReconcileOutcome::EqualMtimeDifferentContent { .. } => {
            (
                Severity::Error,
                "`skillnet sync --apply-promote --prefer view|canonical` required",
            )
        }
        ReconcileOutcome::BothAdvanced { .. } => {
            (
                Severity::Error,
                "`skillnet sync --apply-promote --prefer view|canonical` required; per-file merge is not supported",
            )
        }
        ReconcileOutcome::AdoptCandidate => {
            (
                Severity::Info,
                "view-only skill; `skillnet sync --apply-promote --adopt-new` to promote",
            )
        }
    }
}

fn read_dir_utf8(path: &Utf8Path) -> Result<Vec<Utf8PathBuf>> {
    let mut entries = Vec::new();
    for entry in fs::read_dir(path).with_context(|| format!("failed to read directory {path}"))? {
        let entry = entry?;
        entries.push(
            Utf8PathBuf::from_path_buf(entry.path())
                .map_err(|path| anyhow::anyhow!("non-UTF-8 path: {}", path.display()))?,
        );
    }
    entries.sort();
    Ok(entries)
}

fn entry_name(path: &Utf8Path) -> Result<String> {
    path.file_name()
        .map(ToString::to_string)
        .with_context(|| format!("path has no final component: {path}"))
}

fn canonicalize_utf8(path: &Utf8Path) -> Result<Utf8PathBuf> {
    Utf8PathBuf::from_path_buf(fs::canonicalize(path)?)
        .map_err(|path| anyhow::anyhow!("non-UTF-8 path: {}", path.display()))
}

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

fn resolve_link_target(link_parent: &Utf8Path, target: &Utf8Path) -> Utf8PathBuf {
    if target.is_absolute() {
        return normalize_utf8(target);
    }
    normalize_utf8(&link_parent.join(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(|path| anyhow::anyhow!("non-UTF-8 relative symlink target: {}", path.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 normalize_utf8(path: &Utf8Path) -> Utf8PathBuf {
    let mut out = PathBuf::new();
    for component in path.as_std_path().components() {
        match component {
            Component::CurDir => {}
            Component::ParentDir => {
                out.pop();
            }
            Component::Prefix(prefix) => out.push(prefix.as_os_str()),
            Component::RootDir => out.push("/"),
            Component::Normal(part) => out.push(part),
        }
    }
    Utf8PathBuf::from_path_buf(out)
        .unwrap_or_else(|path| Utf8PathBuf::from(path.to_string_lossy().to_string()))
}