Skip to content

mission control for agent skills

feat: add doctor command

Alvin Unrealb003c768e74b
diff --git a/README.md b/README.md
index 238edb6..09e66b4 100644
--- a/README.md
+++ b/README.md
@@ -104,6 +104,23 @@ For agent-friendly registry search without opening the TUI:
lazyskills find --json "browser automation"
```
+### Doctor
+
+Run a read-only health report for the current project:
+
+```bash
+lazyskills doctor
+```
+
+It ranks scan errors before warnings and shows the affected skill or workspace, path, and safe next step. Use structured output in scripts or other agents:
+
+```bash
+lazyskills doctor --json
+lazyskills doctor --cwd /path/to/project
+```
+
+Doctor never changes skills or lock files. It reports findings and points to commands such as `lazyskills restore` when a locked skill is missing from disk.
+
### Restore from lock files
Restore skills that are recorded in the project or global lock file but missing from disk:
diff --git a/cmd/lazyskills/doctor.go b/cmd/lazyskills/doctor.go
new file mode 100644
index 0000000..bc2b98e
--- /dev/null
+++ b/cmd/lazyskills/doctor.go
@@ -0,0 +1,43 @@
+package main
+
+import (
+ "encoding/json"
+ "flag"
+ "fmt"
+ "os"
+
+ "github.com/alvinunreal/lazyskills/internal/doctor"
+ "github.com/alvinunreal/lazyskills/internal/scan"
+)
+
+func runDoctor(args []string) error {
+ fs := flag.NewFlagSet("doctor", flag.ContinueOnError)
+ fs.SetOutput(os.Stderr)
+ jsonOut := fs.Bool("json", false, "output JSON")
+ cwd := fs.String("cwd", "", "project working directory")
+ if err := fs.Parse(args); err != nil {
+ return err
+ }
+ if fs.NArg() != 0 {
+ return fmt.Errorf("usage: lazyskills doctor [--json] [--cwd <path>]")
+ }
+ if *cwd == "" {
+ var err error
+ *cwd, err = os.Getwd()
+ if err != nil {
+ return err
+ }
+ }
+ result, err := scan.Run(*cwd)
+ if err != nil {
+ return err
+ }
+ report := doctor.Build(result)
+ if *jsonOut {
+ enc := json.NewEncoder(os.Stdout)
+ enc.SetIndent("", " ")
+ return enc.Encode(report)
+ }
+ _, err = fmt.Fprint(os.Stdout, doctor.FormatText(report))
+ return err
+}
diff --git a/cmd/lazyskills/main.go b/cmd/lazyskills/main.go
index b9d4001..3284382 100644
--- a/cmd/lazyskills/main.go
+++ b/cmd/lazyskills/main.go
@@ -164,6 +164,9 @@ func run(args []string) error {
if len(args) > 0 && args[0] == "restore" {
return runRestore(args[1:])
}
+ if len(args) > 0 && args[0] == "doctor" {
+ return runDoctor(args[1:])
+ }
if len(args) == 0 {
cwd, err := os.Getwd()
if err != nil {
@@ -179,7 +182,7 @@ func run(args []string) error {
return err
}
if fs.NArg() > 0 {
- return fmt.Errorf("usage: lazyskills [--cwd <path>] | lazyskills scan --json [--cwd <path>] | lazyskills restore [--global|--project|--all] [--yes] [skills...] | lazyskills update [--check] [--print-command] | lazyskills find --json <query> | lazyskills version")
+ return fmt.Errorf("usage: lazyskills [--cwd <path>] | lazyskills scan --json [--cwd <path>] | lazyskills doctor [--json] [--cwd <path>] | lazyskills restore [--global|--project|--all] [--yes] [skills...] | lazyskills update [--check] [--print-command] | lazyskills find --json <query> | lazyskills version")
}
if *cwd == "" {
var err error
diff --git a/cmd/lazyskills/main_test.go b/cmd/lazyskills/main_test.go
index 8723be5..4329c97 100644
--- a/cmd/lazyskills/main_test.go
+++ b/cmd/lazyskills/main_test.go
@@ -12,6 +12,7 @@ import (
"testing"
"github.com/alvinunreal/lazyskills/internal/buildinfo"
+ "github.com/alvinunreal/lazyskills/internal/doctor"
"github.com/alvinunreal/lazyskills/internal/registry"
"github.com/alvinunreal/lazyskills/internal/selfupdate"
)
@@ -171,6 +172,37 @@ func TestCLIFind(t *testing.T) {
}
}
+func TestCLIDoctor(t *testing.T) {
+ home := t.TempDir()
+ project := t.TempDir()
+ t.Setenv("HOME", home)
+
+ out, err := captureRunStdout(t, []string{"doctor", "--cwd", project})
+ if err != nil {
+ t.Fatalf("doctor returned an error: %v", err)
+ }
+ if !strings.Contains(out, "LazySkills doctor") || !strings.Contains(out, "Healthy. No health issues found.") {
+ t.Fatalf("unexpected doctor text output: %q", out)
+ }
+
+ out, err = captureRunStdout(t, []string{"doctor", "--json", "--cwd", project})
+ if err != nil {
+ t.Fatalf("doctor JSON returned an error: %v", err)
+ }
+ var report doctor.Report
+ if err := json.Unmarshal([]byte(out), &report); err != nil {
+ t.Fatalf("doctor did not return JSON: %v\n%s", err, out)
+ }
+ if report.Cwd == "" || len(report.Findings) != 0 {
+ t.Fatalf("unexpected doctor report: %#v", report)
+ }
+
+ _, err = captureRunStdout(t, []string{"doctor", "extra"})
+ if err == nil || !strings.Contains(err.Error(), "usage: lazyskills doctor") {
+ t.Fatalf("expected doctor usage error, got %v", err)
+ }
+}
+
func captureRunStdout(t *testing.T, args []string) (string, error) {
t.Helper()
oldOut := os.Stdout
diff --git a/internal/doctor/doctor.go b/internal/doctor/doctor.go
new file mode 100644
index 0000000..5e89dbe
--- /dev/null
+++ b/internal/doctor/doctor.go
@@ -0,0 +1,229 @@
+// Package doctor turns a scan snapshot into a deterministic, read-only health report.
+package doctor
+
+import (
+ "fmt"
+ "sort"
+ "strings"
+
+ "github.com/alvinunreal/lazyskills/internal/compat"
+ "github.com/alvinunreal/lazyskills/internal/model"
+)
+
+type Summary struct {
+ Errors int `json:"errors"`
+ Warnings int `json:"warnings"`
+ Info int `json:"info"`
+}
+
+type Finding struct {
+ Subject string `json:"subject"`
+ Scope string `json:"scope,omitempty"`
+ Type string `json:"type"`
+ Severity string `json:"severity"`
+ Title string `json:"title"`
+ Message string `json:"message"`
+ Path string `json:"path,omitempty"`
+ Advice string `json:"advice"`
+}
+
+type Report struct {
+ Cwd string `json:"cwd"`
+ Summary Summary `json:"summary"`
+ Findings []Finding `json:"findings"`
+}
+
+// Build collects workspace and skill health issues into a report. It does not
+// inspect or modify the filesystem; callers pass the scan snapshot they own.
+func Build(result model.ScanResult) Report {
+ report := Report{
+ Cwd: compat.SanitizeMetadata(result.Cwd),
+ Findings: make([]Finding, 0, len(result.HealthIssues)),
+ }
+ for _, issue := range result.HealthIssues {
+ report.add("Workspace", "", "", issue)
+ }
+ for _, skill := range result.Skills {
+ if skill == nil {
+ continue
+ }
+ subject := compat.FirstNonEmpty(compat.SanitizeMetadata(skill.Name), "Unnamed skill")
+ for _, issue := range skill.HealthIssues {
+ report.add(subject, string(skill.Scope), skill.Name, issue)
+ }
+ }
+ sort.SliceStable(report.Findings, func(i, j int) bool {
+ left, right := report.Findings[i], report.Findings[j]
+ if severityRank(left.Severity) != severityRank(right.Severity) {
+ return severityRank(left.Severity) < severityRank(right.Severity)
+ }
+ if left.Subject != right.Subject {
+ return left.Subject < right.Subject
+ }
+ if left.Type != right.Type {
+ return left.Type < right.Type
+ }
+ return left.Path < right.Path
+ })
+ for _, finding := range report.Findings {
+ switch finding.Severity {
+ case "error":
+ report.Summary.Errors++
+ case "warning":
+ report.Summary.Warnings++
+ default:
+ report.Summary.Info++
+ }
+ }
+ return report
+}
+
+func (r *Report) add(subject, scope, skillName string, issue model.HealthIssue) {
+ severity := normalizedSeverity(issue.Severity)
+ r.Findings = append(r.Findings, Finding{
+ Subject: compat.SanitizeMetadata(subject),
+ Scope: compat.SanitizeMetadata(scope),
+ Type: compat.SanitizeMetadata(issue.Type),
+ Severity: severity,
+ Title: title(issue.Type),
+ Message: message(issue.Type, issue.Message),
+ Path: compat.SanitizeMetadata(issue.Path),
+ Advice: advice(issue.Type, scope, skillName),
+ })
+}
+
+func normalizedSeverity(value string) string {
+ switch strings.ToLower(compat.SanitizeMetadata(value)) {
+ case "error", "critical":
+ return "error"
+ case "warning", "warn":
+ return "warning"
+ default:
+ return "info"
+ }
+}
+
+func severityRank(value string) int {
+ switch value {
+ case "error":
+ return 0
+ case "warning":
+ return 1
+ default:
+ return 2
+ }
+}
+
+func title(issueType string) string {
+ switch issueType {
+ case "missing_skill_md":
+ return "Missing SKILL.md"
+ case "invalid_frontmatter":
+ return "Invalid frontmatter"
+ case "broken_symlink":
+ return "Broken symlink"
+ case "missing_project_lock":
+ return "Missing project lock entry"
+ case "missing_global_lock":
+ return "Missing global lock entry"
+ case "lock_without_files":
+ return "Locked skill is missing files"
+ case "ghost_agent_skill":
+ return "Agent-specific skill"
+ case "duplicate_name":
+ return "Duplicate skill name"
+ case "project_global_shadowing":
+ return "Project skill shadows global skill"
+ case "corrupt_project_lock":
+ return "Corrupt project lock file"
+ case "corrupt_global_lock":
+ return "Corrupt global lock file"
+ case "shared_scope_root":
+ return "Shared skills root"
+ default:
+ return strings.ReplaceAll(compat.SanitizeMetadata(issueType), "_", " ")
+ }
+}
+
+func message(issueType, value string) string {
+ if issueType == "ghost_agent_skill" {
+ return "This skill is installed only for specific agents and is not tracked by the canonical skills directory."
+ }
+ return compat.SanitizeMetadata(value)
+}
+
+func advice(issueType, scope, skillName string) string {
+ if issueType == "lock_without_files" && skillName != "" {
+ flag := "--project"
+ if scope == string(model.ScopeGlobal) {
+ flag = "--global"
+ }
+ return fmt.Sprintf("Restore it with: lazyskills restore %s %s", flag, shellQuote(compat.SanitizeMetadata(skillName)))
+ }
+ switch issueType {
+ case "broken_symlink":
+ return "Review the broken link in LazySkills before removing it."
+ case "missing_skill_md", "invalid_frontmatter":
+ return "Repair the skill's SKILL.md file, then run lazyskills doctor again."
+ case "missing_project_lock", "missing_global_lock":
+ return "Reinstall or update the skill from its source to record it in the lock file."
+ case "corrupt_project_lock", "corrupt_global_lock":
+ return "Back up and repair the lock file before running installs or updates."
+ case "duplicate_name", "project_global_shadowing":
+ return "Choose one copy or rename a conflicting skill, then run lazyskills doctor again."
+ case "shared_scope_root":
+ return "This location is shared through a symlinked skills root. Repair it at the canonical source."
+ case "ghost_agent_skill":
+ return "Move or reinstall it through the canonical skills directory if it should be shared."
+ default:
+ return "Review this configuration and run lazyskills doctor again."
+ }
+}
+
+func shellQuote(value string) string {
+ return "'" + strings.ReplaceAll(value, "'", "'\"'\"'") + "'"
+}
+
+func FormatText(report Report) string {
+ var out strings.Builder
+ out.WriteString("LazySkills doctor\n")
+ if report.Cwd != "" {
+ fmt.Fprintf(&out, "Workspace: %s\n", report.Cwd)
+ }
+ if len(report.Findings) == 0 {
+ out.WriteString("\nHealthy. No health issues found.\n")
+ return out.String()
+ }
+ fmt.Fprintf(&out, "\n%d error(s), %d warning(s), %d info item(s)\n", report.Summary.Errors, report.Summary.Warnings, report.Summary.Info)
+ currentSeverity := ""
+ for _, finding := range report.Findings {
+ if finding.Severity != currentSeverity {
+ currentSeverity = finding.Severity
+ fmt.Fprintf(&out, "\n%s\n", sectionTitle(currentSeverity))
+ }
+ subject := finding.Subject
+ if finding.Scope != "" {
+ subject += " [" + finding.Scope + "]"
+ }
+ fmt.Fprintf(&out, "- %s: %s\n", subject, finding.Title)
+ if finding.Message != "" {
+ fmt.Fprintf(&out, " %s\n", finding.Message)
+ }
+ if finding.Path != "" {
+ fmt.Fprintf(&out, " Path: %s\n", finding.Path)
+ }
+ fmt.Fprintf(&out, " Next: %s\n", finding.Advice)
+ }
+ return out.String()
+}
+
+func sectionTitle(severity string) string {
+ switch severity {
+ case "error":
+ return "Errors"
+ case "warning":
+ return "Warnings"
+ default:
+ return "Info"
+ }
+}
diff --git a/internal/doctor/doctor_test.go b/internal/doctor/doctor_test.go
new file mode 100644
index 0000000..55b908a
--- /dev/null
+++ b/internal/doctor/doctor_test.go
@@ -0,0 +1,60 @@
+package doctor
+
+import (
+ "strings"
+ "testing"
+
+ "github.com/alvinunreal/lazyskills/internal/model"
+)
+
+func TestBuildPrioritizesAndSanitizesFindings(t *testing.T) {
+ report := Build(model.ScanResult{
+ Cwd: "/tmp/work\x1b[31m",
+ HealthIssues: []model.HealthIssue{
+ {Type: "corrupt_project_lock", Severity: "warning", Message: "bad\x1b[31m lock", Path: "/tmp/lock"},
+ },
+ Skills: []*model.Skill{
+ {Name: "Zulu", Scope: model.ScopeGlobal, HealthIssues: []model.HealthIssue{{Type: "lock_without_files", Severity: "warning"}}},
+ {Name: "Alpha", Scope: model.ScopeProject, HealthIssues: []model.HealthIssue{{Type: "broken_symlink", Severity: "error", Path: "/tmp/a"}}},
+ },
+ })
+
+ if report.Summary.Errors != 1 || report.Summary.Warnings != 2 || report.Summary.Info != 0 {
+ t.Fatalf("unexpected summary: %#v", report.Summary)
+ }
+ if len(report.Findings) != 3 {
+ t.Fatalf("expected three findings, got %#v", report.Findings)
+ }
+ if report.Findings[0].Subject != "Alpha" || report.Findings[0].Severity != "error" {
+ t.Fatalf("expected error first, got %#v", report.Findings)
+ }
+ if report.Findings[1].Subject != "Workspace" || report.Findings[2].Subject != "Zulu" {
+ t.Fatalf("expected warnings sorted by subject, got %#v", report.Findings)
+ }
+ if !strings.Contains(report.Findings[2].Advice, "lazyskills restore --global 'Zulu'") {
+ t.Fatalf("expected global restore advice, got %q", report.Findings[2].Advice)
+ }
+ if strings.Contains(report.Cwd, "\x1b") || strings.Contains(report.Findings[1].Message, "\x1b") {
+ t.Fatalf("expected sanitized report: %#v", report)
+ }
+}
+
+func TestRestoreAdviceQuotesShellMetacharacters(t *testing.T) {
+ report := Build(model.ScanResult{Skills: []*model.Skill{{
+ Name: "$(bad)'name",
+ Scope: model.ScopeProject,
+ HealthIssues: []model.HealthIssue{{
+ Type: "lock_without_files", Severity: "warning",
+ }},
+ }}})
+ if got, want := report.Findings[0].Advice, "lazyskills restore --project '$(bad)'\"'\"'name'"; got != "Restore it with: "+want {
+ t.Fatalf("unexpected shell quoting: %q", got)
+ }
+}
+
+func TestFormatTextForHealthyReport(t *testing.T) {
+ out := FormatText(Build(model.ScanResult{Cwd: "/tmp/work"}))
+ if !strings.Contains(out, "Healthy. No health issues found.") {
+ t.Fatalf("unexpected healthy output: %q", out)
+ }
+}