diff --git a/README.md b/README.md index c271756..bf602e4 100644 --- a/README.md +++ b/README.md @@ -62,14 +62,20 @@ agentsync doctor # what has drifted Run without `--apply` first to see what a command would do. +For Claude project skills, put a directory such as `skills/release-check/SKILL.md` +in the canonical checkout. Run `agentsync link --skill release-check` from the +project to preview the link, then add `--apply`. Use +`agentsync import --skill release-check` to preview capturing an existing +project skill and its support files. [Project skills](docs/skills.md) + ## Commands | Command | Does | |---|---| | `init` | find your agents repo and write the config file | -| `scan` | classify project agents against the canonical repo (read-only) | -| `import` | capture project-only agents into `project-agents/` | -| `link` | replace project agent files with symlinks into the canonical repo | +| `scan` | classify project agents and skills against the canonical repo (read-only) | +| `import` | capture project-only agents or selected skill directories | +| `link` | provision project agents and selected skills from the canonical repo | | `doctor` | report broken, foreign, or mismatched links and stale captures; never writes | | `reconcile` | act on that drift, gated on what agentsync itself recorded | | `inventory` | regenerate `inventory.json`, a record of every agent on the machine | @@ -85,6 +91,7 @@ Run without `--apply` first to see what a command would do. - [scan](docs/scan.md) - [import](docs/import.md) - [link](docs/link.md) +- [Project skills](docs/skills.md) - [doctor](docs/doctor.md) - [reconcile](docs/reconcile.md) - [inventory](docs/inventory.md) diff --git a/cmd/agentsync/doctor.go b/cmd/agentsync/doctor.go index 8e69805..fcb455f 100644 --- a/cmd/agentsync/doctor.go +++ b/cmd/agentsync/doctor.go @@ -11,6 +11,7 @@ import ( "github.com/agentic-tooling/agentsync/internal/doctor" "github.com/agentic-tooling/agentsync/internal/scan" + "github.com/agentic-tooling/agentsync/internal/skills" ) // runDoctor reports the ways this repo and the projects it serves have drifted @@ -64,13 +65,20 @@ func runDoctor(args []string, stdout, stderr io.Writer) int { return exitFailure } + skillReport, err := scanSkills(repo, res.SkillDirs) + if err != nil { + fmt.Fprintf(stderr, "agentsync: %v\n", err) + return exitFailure + } + dirs := append(res.Dirs, skillReport.Dirs...) run := doctorRun{ repoRoot: repo.Root, root: root.Path, home: repo.Home, res: res, - check: repo.CheckHome(root.Path, res.Dirs), + check: repo.CheckHome(root.Path, dirs), report: doctor.Diagnose(repo, res), + skills: skillReport, } findings, problems := overrideFindings(repo.Home, res.Dirs) run.report.Findings = append(run.report.Findings, findings...) @@ -85,7 +93,7 @@ func runDoctor(args []string, stdout, stderr io.Writer) int { // projects the scan walked, HomeWarning judges the capture keys nothing // walked, and the reproduction that made this necessary is precisely the // run where the first has nothing to say and the second has everything. - warnHome(stderr, repo, root.Path, res.Dirs, *allowOutsideHome) + warnHome(stderr, repo, root.Path, dirs, *allowOutsideHome) if warning := run.report.HomeWarning(repo.Home); warning != "" { fmt.Fprintf(stderr, "agentsync: warning: %s\n", warning) } @@ -114,6 +122,7 @@ type doctorRun struct { res scan.Result check scan.HomeCheck report doctor.Report + skills skills.Report } // exitCode maps the run onto the CLI contract. @@ -132,10 +141,10 @@ type doctorRun struct { // A foreign link never affects the code, in either direction. It is reported and // it is not a fault. func (r doctorRun) exitCode() int { - if len(r.report.Problems) > 0 { + if len(r.report.Problems)+len(r.skills.Problems) > 0 { return exitFailure } - if len(r.report.NeedsHuman()) > 0 || len(r.res.Problems) > 0 { + if len(r.report.NeedsHuman())+len(r.res.Problems)+len(r.skills.AtRisk().Entries) > 0 { return exitAtRisk } return exitOK @@ -144,7 +153,7 @@ func (r doctorRun) exitCode() int { // incomplete reports whether the scan failed to look at part of the tree, which // disqualifies the summary from claiming everything lines up. func (r doctorRun) incomplete() bool { - return len(r.res.Problems) > 0 || len(r.report.Problems) > 0 + return len(r.res.Problems)+len(r.report.Problems)+len(r.skills.Problems) > 0 } // doctorHome is the home-directory signal in machine-readable form. @@ -179,6 +188,7 @@ type doctorHome struct { // doctorReport is the --json form. type doctorReport struct { + skills.Report Repo string `json:"repo"` Root string `json:"root"` Home doctorHome `json:"home"` @@ -195,8 +205,9 @@ type doctorReport struct { func (r doctorRun) emitJSON(w io.Writer) error { rep := doctorReport{ - Repo: r.repoRoot, - Root: r.root, + Report: r.skills, + Repo: r.repoRoot, + Root: r.root, Home: doctorHome{ Status: r.check.Status.String(), Path: r.check.Home, @@ -264,6 +275,7 @@ func (r doctorRun) emitText(w io.Writer) { } emitDoctorProblems(w, r.report.Problems, r.res.Problems) + emitSkills(w, r.skills) fmt.Fprintln(w, strings.Repeat("-", 60)) r.emitSummary(w) @@ -281,7 +293,7 @@ func (r doctorRun) emitSummary(w io.Writer) { // project's own git tracks, and on a --project key that names nothing. // None of those is drift, none is doctor's to find, and a summary that // read like an all-clear for the whole toolchain would be relied on as one. - switch n := len(r.report.NeedsHuman()); { + switch n := len(r.report.NeedsHuman()) + len(r.skills.AtRisk().Entries); { case n == 0 && !r.incomplete(): fmt.Fprintln(w, "Nothing has drifted: every link resolves to its counterpart and every capture has a live file.") fmt.Fprintln(w, "That is a statement about drift only — whether `link` would run clean is its own question, and `agentsync link` answers it.") diff --git a/cmd/agentsync/import.go b/cmd/agentsync/import.go index f74122c..f7828af 100644 --- a/cmd/agentsync/import.go +++ b/cmd/agentsync/import.go @@ -13,6 +13,7 @@ import ( "github.com/agentic-tooling/agentsync/internal/importer" "github.com/agentic-tooling/agentsync/internal/scan" + "github.com/agentic-tooling/agentsync/internal/skills" ) // runImport captures project-only agent files into the repo. @@ -31,6 +32,8 @@ func runImport(args []string, stdout, stderr io.Writer) int { all := registerAll(fs) apply := fs.Bool("apply", false, "write the captures; without it this is a dry run") var update updateFlag + var skillNames stringList + fs.Var(&skillNames, skills.Flag, "capture a project skill directory (repeatable)") fs.Var(&update, "update", "replace a captured copy whose content differs; --update=/ for one, repeatable") includeTracked := fs.Bool("include-tracked", false, @@ -78,6 +81,25 @@ func runImport(args []string, stdout, stderr io.Writer) int { return exitFailure } + if len(skillNames) > 0 { + if err := skills.ValidateNames(skillNames); err != nil { + fmt.Fprintf(stderr, "agentsync: %v\n", err) + return exitFailure + } + if update.set.Enabled() { + fmt.Fprintln(stderr, "agentsync: --update applies to agent captures; skill captures are write-once") + return exitFailure + } + var keys []string + if root.Project != "" { + keys = []string{root.Project} + } + return (skillRun{ + mode: skills.Import, apply: *apply, allowOutsideHome: *allowOutsideHome, + asJSON: *asJSON, failOnPending: *failOnPending, keys: keys, names: skillNames, + }).run(repo, repoFrom, root, stdout, stderr) + } + res, err := repo.Scan(root.Path) if err != nil { fmt.Fprintf(stderr, "agentsync: %v\n", err) diff --git a/cmd/agentsync/link.go b/cmd/agentsync/link.go index 715d24c..822acaa 100644 --- a/cmd/agentsync/link.go +++ b/cmd/agentsync/link.go @@ -11,6 +11,7 @@ import ( "github.com/agentic-tooling/agentsync/internal/linker" "github.com/agentic-tooling/agentsync/internal/scan" + "github.com/agentic-tooling/agentsync/internal/skills" "github.com/agentic-tooling/agentsync/internal/state" ) @@ -143,8 +144,9 @@ func runLink(args []string, stdout, stderr io.Writer) int { allowOutsideHome := fs.Bool(scan.AllowOutsideHomeFlag, false, "projects legitimately live outside $HOME; permit the write anyway") - var projects, agents stringList + var projects, agents, skillNames stringList fs.Var(&projects, linker.ProjectFlag, "restrict to this project key (repeatable)") + fs.Var(&skillNames, skills.Flag, "link a project skill directory (repeatable)") fs.Var(&agents, linker.AgentFlag, "also link this agent into the selected projects even if absent (repeatable; requires --"+linker.ProjectFlag+")") @@ -197,6 +199,18 @@ func runLink(args []string, stdout, stderr io.Writer) int { if len(opts.Projects) == 0 && root.Project != "" { opts.Projects = []string{root.Project} } + if err := skills.ValidateNames(skillNames); err != nil { + fmt.Fprintf(stderr, "agentsync: %v\n", err) + return exitFailure + } + skill := skillRun{ + mode: skills.Link, apply: *apply, excludeLocal: *excludeLocal, + allowOutsideHome: *allowOutsideHome, asJSON: *asJSON, + keys: opts.Projects, names: skillNames, + } + if len(skillNames) > 0 && len(agents)+len(undecline.keys) == 0 { + return skill.run(repo, repoFrom, root, stdout, stderr) + } if err := opts.Validate(); err != nil { fmt.Fprintf(stderr, "agentsync: %v\n", err) // The root is *why* no project was selected, and saying so is what @@ -255,6 +269,15 @@ func runLink(args []string, stdout, stderr io.Writer) int { cleared: cleared, unmatched: unmatched, } + var catalog *skills.Catalog + if len(skillNames) > 0 { + catalog, run.skills, _, err = skill.prepare(repo, root) + if err != nil { + fmt.Fprintf(stderr, "agentsync: %v\n", err) + return exitFailure + } + defer func() { _ = catalog.Close() }() + } if run.apply { run.done, run.errs = plan.Apply(repo, st) // Save clears Dirty only on success, so a dirty document means the @@ -262,6 +285,9 @@ func runLink(args []string, stdout, stderr io.Writer) int { if st.Dirty() { run.cleared, run.uncleared = nil, run.cleared } + if catalog != nil { + catalog.Apply(run.skills, *excludeLocal) + } } emitRepoChoice(reportStream(*asJSON, stdout, stderr), repo, repoFrom) @@ -281,8 +307,10 @@ func runLink(args []string, stdout, stderr io.Writer) int { // really appeared. Excludes and state records change nothing a session // loads, so neither earns this line. emitRestartNotice(reportStream(*asJSON, stdout, stderr), len(run.done.Links) > 0) + _, written, blocked, failed := run.skills.Counts() + emitSkillRestart(reportStream(*asJSON, stdout, stderr), written) - return run.exitCode() + return max(run.exitCode(), skillExitCode(0, blocked, failed, false)) } // linkRun is one invocation: what was planned, what was written, and what went @@ -297,6 +325,7 @@ type linkRun struct { plan linker.Plan done linker.Applied errs []error + skills skills.Plan // statePath is where the run's records go, named in the report because // people go and look at this file by hand. @@ -352,6 +381,7 @@ func (r linkRun) linkCaveat() string { // linkReport is the --json form. It reports the whole plan, not just the // writes: what was refused and why is the part a user cannot reconstruct. type linkReport struct { + SkillActions skills.Plan `json:"skill_actions,omitempty"` Apply bool `json:"apply"` ExcludeLocal bool `json:"exclude_local"` Repo string `json:"repo"` @@ -388,6 +418,7 @@ type linkReport struct { func (r linkRun) emitJSON(w io.Writer) error { rep := linkReport{ + SkillActions: r.skills, Apply: r.apply, ExcludeLocal: r.opts.ExcludeLocal, Repo: r.repoRoot, @@ -530,6 +561,9 @@ func (r linkRun) emitText(w io.Writer) { fmt.Fprintln(w, strings.Repeat("-", 60)) r.emitSummary(w) + if len(r.skills) > 0 { + emitSkillActions(w, r.skills, r.apply) + } } func (r linkRun) emitSummary(w io.Writer) { diff --git a/cmd/agentsync/main.go b/cmd/agentsync/main.go index ed374af..324e930 100644 --- a/cmd/agentsync/main.go +++ b/cmd/agentsync/main.go @@ -58,15 +58,16 @@ import ( "github.com/agentic-tooling/agentsync/internal/config" "github.com/agentic-tooling/agentsync/internal/override" "github.com/agentic-tooling/agentsync/internal/scan" + "github.com/agentic-tooling/agentsync/internal/skills" ) -const usage = `agentsync — inspect project-local Claude agents against the canonical repo +const usage = `agentsync — inspect project-local Claude agents and skills against the canonical repo Usage: agentsync init [flags] find your agent repo and write the config file - agentsync scan [flags] classify project agents against the repo (read-only) - agentsync import [flags] capture project-only agents into project-agents/ - agentsync link [flags] replace project agent files with symlinks into the repo + agentsync scan [flags] classify project agents and skills (read-only) + agentsync import [flags] capture project-only agents or selected skill directories + agentsync link [flags] provision project agents and selected skills from the repo agentsync doctor [flags] report broken/foreign/mismatched links and stale captures agentsync reconcile [flags] act on that drift, gated on what this tool recorded agentsync inventory [flags] regenerate inventory.json: every agent on this machine @@ -103,7 +104,8 @@ you are standing in. --all is accepted there and means what the default already means. Config file: $XDG_CONFIG_HOME/agentsync/config.toml, else -~/.config/agentsync/config.toml. It may set repo and root; $AGENTSYNC_REPO and +~/.config/agentsync/config.toml. It may set repo, root and an optional +checkout-relative skills_root (default skills/). $AGENTSYNC_REPO and $AGENTSYNC_ROOT beat the file, and both must be absolute paths. A file that exists but is broken refuses. agentsync init writes it, and is the one command that takes --repo and --root. @@ -130,6 +132,8 @@ scan flags: import flags: --apply write the captures; without it import is a dry run + --skill NAME capture a selected project skill directory into project-skills/ + (repeatable); existing skill captures are preserved --update=KEY replace the captured copy of one file, named as the / pair the report prints (repeatable). Attach the value with '='; import takes no positional @@ -159,13 +163,17 @@ link flags: scan reports, not a directory name) --agent NAME also link this agent into the selected projects even if no file is present (repeatable; requires --project) - --exclude-local add the agents directory to the project's - .git/info/exclude before linking. Without it, a project + --skill NAME link a canonical skill directory into .claude/skills/ + (repeatable; requires a current or selected project) + --exclude-local add a rule to the project's .git/info/exclude before linking. + For agents the rule covers the agents directory. Without it, a project whose own git tracks that directory is refused: a symlink into this repo names an absolute path that resolves on this machine only, so committing one breaks every other clone. info/exclude is used rather than .gitignore because .gitignore is tracked and shared. + For skills the rule covers the specific symlink. A skill + path already in the index or HEAD always blocks linking. --undecline=KEY clear the declined marker for one agent, named as the / pair the report prints (repeatable), so it can be linked again. A declined marker records that @@ -471,23 +479,34 @@ func runScan(args []string, stdout, stderr io.Writer) int { return exitFailure } + skillReport, err := scanSkills(repo, res.SkillDirs) + if err != nil { + fmt.Fprintf(stderr, "agentsync: %v\n", err) + return exitFailure + } + // After the scan, because the projects it found are what decides whether // $HOME is the right one — and before the report, so a terminal user // reads the caveat before the numbers it applies to. - warnHome(stderr, repo, root.Path, res.Dirs, *allowOutsideHome) + warnHome(stderr, repo, root.Path, append(res.Dirs, skillReport.Dirs...), *allowOutsideHome) emitRepoChoice(reportStream(*asJSON, stdout, stderr), repo, repoFrom) emitRootChoice(reportStream(*asJSON, stdout, stderr), repo.Home, root) + skillRisk := skillReport.AtRisk() + if *atRiskOnly { + skillReport = skillRisk + } if *asJSON { - if err := emitJSON(stdout, res, *atRiskOnly); err != nil { + if err := emitJSON(stdout, res, *atRiskOnly, skillReport); err != nil { fmt.Fprintf(stderr, "agentsync: %v\n", err) return exitFailure } } else { emitText(stdout, res, repo.Root, root.Path, *atRiskOnly, entryOverrides(res, stderr)) + emitSkills(stdout, skillReport) } - if *failOnRisk && (len(res.AtRisk()) > 0 || len(res.Problems) > 0) { + if *failOnRisk && (len(res.AtRisk())+len(res.Problems)+len(skillRisk.Entries)+len(skillRisk.Problems) > 0) { return exitAtRisk } return exitOK @@ -536,14 +555,17 @@ func filterAtRisk(res scan.Result) scan.Result { } } -func emitJSON(w io.Writer, res scan.Result, atRiskOnly bool) error { +func emitJSON(w io.Writer, res scan.Result, atRiskOnly bool, skillReport skills.Report) error { out := res if atRiskOnly { out = filterAtRisk(res) } enc := json.NewEncoder(w) enc.SetIndent("", " ") - return enc.Encode(out) + return enc.Encode(struct { + scan.Result + skills.Report + }{out, skillReport}) } // stateOrder controls report grouping: most urgent first. diff --git a/cmd/agentsync/main_test.go b/cmd/agentsync/main_test.go index 1130abc..1fea761 100644 --- a/cmd/agentsync/main_test.go +++ b/cmd/agentsync/main_test.go @@ -11,6 +11,7 @@ import ( "testing" "github.com/agentic-tooling/agentsync/internal/scan" + "github.com/agentic-tooling/agentsync/internal/skills" "github.com/agentic-tooling/agentsync/internal/testfs" ) @@ -92,7 +93,7 @@ func renderText(t *testing.T, res scan.Result, atRiskOnly bool) string { func renderJSON(t *testing.T, res scan.Result, atRiskOnly bool) string { t.Helper() var buf bytes.Buffer - if err := emitJSON(&buf, res, atRiskOnly); err != nil { + if err := emitJSON(&buf, res, atRiskOnly, skills.Report{}); err != nil { t.Fatalf("emitJSON: %v", err) } return buf.String() diff --git a/cmd/agentsync/reconcile.go b/cmd/agentsync/reconcile.go index 45e72e6..8fccfa8 100644 --- a/cmd/agentsync/reconcile.go +++ b/cmd/agentsync/reconcile.go @@ -12,6 +12,7 @@ import ( "github.com/agentic-tooling/agentsync/internal/doctor" "github.com/agentic-tooling/agentsync/internal/reconcile" "github.com/agentic-tooling/agentsync/internal/scan" + "github.com/agentic-tooling/agentsync/internal/skills" "github.com/agentic-tooling/agentsync/internal/state" ) @@ -189,6 +190,11 @@ func runReconcile(args []string, stdout, stderr io.Writer) int { return exitFailure } + skillReport, err := scanSkills(repo, res.SkillDirs) + if err != nil { + fmt.Fprintf(stderr, "agentsync: %v\n", err) + return exitFailure + } diag := doctor.Diagnose(repo, res) plan, err := reconcile.NewPlan(repo, res, diag, st, opts) if err != nil { @@ -214,7 +220,7 @@ func runReconcile(args []string, stdout, stderr io.Writer) int { return exitFailure } } else { - warnHome(stderr, repo, root.Path, plan.Dirs, *allowOutsideHome) + warnHome(stderr, repo, root.Path, append(plan.Dirs, skillReport.Dirs...), *allowOutsideHome) } // doctor's second home check, for the reason doctor prints it: the re-key // candidates below are derived from orphaned capture directories, which is @@ -234,6 +240,7 @@ func runReconcile(args []string, stdout, stderr io.Writer) int { apply: *apply, plan: plan, statePath: state.Path(repo), + skills: skillReport, } if run.apply { run.done, run.errs = plan.Apply(repo, st) @@ -287,6 +294,7 @@ type reconcileRun struct { plan reconcile.Plan done reconcile.Applied errs []error + skills skills.Report // statePath is the document every gate here reads, named in the report // because people go and look at this file by hand. @@ -307,10 +315,10 @@ type reconcileRun struct { // it would make the code un-clearable. The summary says so and points at // doctor, whose exit code does assert it. func (r reconcileRun) exitCode() int { - if len(r.errs) > 0 || len(r.plan.Problems) > 0 { + if len(r.errs)+len(r.plan.Problems)+len(r.skills.Problems) > 0 { return exitFailure } - if len(r.plan.Refused) > 0 || len(r.plan.Unresolved()) > 0 || len(r.res.Problems) > 0 { + if len(r.plan.Refused)+len(r.plan.Unresolved())+len(r.res.Problems)+len(r.skills.AtRisk().Entries) > 0 { return exitAtRisk } return exitOK @@ -319,12 +327,13 @@ func (r reconcileRun) exitCode() int { // incomplete reports whether the scan failed to look at part of the tree, which // disqualifies the summary from claiming anything about what it did not read. func (r reconcileRun) incomplete() bool { - return len(r.res.Problems) > 0 || len(r.plan.Problems) > 0 + return len(r.res.Problems)+len(r.plan.Problems)+len(r.skills.Problems) > 0 } // reconcileReport is the --json form. It carries the whole plan, not just the // writes: what was refused and why is the part a user cannot reconstruct. type reconcileReport struct { + skills.Report Apply bool `json:"apply"` Repo string `json:"repo"` Root string `json:"root"` @@ -364,6 +373,7 @@ type reconcileReport struct { func (r reconcileRun) emitJSON(w io.Writer) error { rep := reconcileReport{ + Report: r.skills, Apply: r.apply, Repo: r.repoRoot, Root: r.root, @@ -515,6 +525,7 @@ func (r reconcileRun) emitText(w io.Writer) { } emitDoctorProblems(w, r.plan.Problems, r.res.Problems) + emitSkills(w, r.skills) fmt.Fprintln(w, strings.Repeat("-", 60)) r.emitSummary(w) @@ -584,6 +595,9 @@ func (r reconcileRun) emitSummary(w io.Writer) { fmt.Fprintf(w, "doctor's diagnosis of this same tree holds %d finding%s reconcile has no flag for — stale captures above all. They do NOT move this exit code: `agentsync doctor` is where that assertion lives.\n", n, plural(n, "", "s")) } + if len(r.skills.AtRisk().Entries) > 0 { + fmt.Fprintln(w, "Resolve the project skill findings manually. Reconcile reports skill directories without changing them.") + } if n := len(r.plan.Problems); n > 0 { fmt.Fprintf(w, "%d path%s could not be judged and %s left untouched.\n", n, plural(n, "", "s"), plural(n, "was", "were")) diff --git a/cmd/agentsync/skills.go b/cmd/agentsync/skills.go new file mode 100644 index 0000000..6263a92 --- /dev/null +++ b/cmd/agentsync/skills.go @@ -0,0 +1,150 @@ +package main + +import ( + "encoding/json" + "fmt" + "io" + + "github.com/agentic-tooling/agentsync/internal/config" + "github.com/agentic-tooling/agentsync/internal/scan" + "github.com/agentic-tooling/agentsync/internal/skills" +) + +func openCatalog(repo *scan.Repo, strict bool) (*skills.Catalog, error) { + shared, err := config.SkillsRoot(strict) + if err != nil { + return nil, err + } + return skills.Open(repo, shared) +} + +func scanSkills(repo *scan.Repo, dirs []string) (skills.Report, error) { + catalog, err := openCatalog(repo, len(dirs) > 0) + if err != nil { + return skills.Report{}, err + } + defer func() { _ = catalog.Close() }() + return catalog.ScanDirs(dirs), nil +} + +func emitSkills(w io.Writer, report skills.Report) { + if len(report.Entries) == 0 && len(report.Problems) == 0 { + return + } + fmt.Fprintln(w, "Project skills") + for _, e := range report.Entries { + fmt.Fprintf(w, " %s %s/%s", e.State, e.Project, e.Name) + if e.Reason != "" { + fmt.Fprintf(w, " (%s)", e.Reason) + } + if e.LinkTarget != "" { + fmt.Fprintf(w, " -> %s", e.LinkTarget) + } + fmt.Fprintln(w) + } + emitProblems(w, report.Problems) +} + +type skillRun struct { + mode skills.Mode + apply bool + excludeLocal bool + allowOutsideHome bool + asJSON bool + failOnPending bool + keys []string + names []string +} + +func (r skillRun) run(repo *scan.Repo, repoFrom string, root rootChoice, stdout, stderr io.Writer) int { + catalog, plan, scanned, err := r.prepare(repo, root) + if err != nil { + fmt.Fprintf(stderr, "agentsync: %v\n", err) + return exitFailure + } + defer func() { _ = catalog.Close() }() + if r.apply { + catalog.Apply(plan, r.excludeLocal) + } + w := reportStream(r.asJSON, stdout, stderr) + emitRepoChoice(w, repo, repoFrom) + emitRootChoice(w, repo.Home, root) + if r.asJSON { + report := struct { + Apply bool `json:"apply"` + Repo string `json:"repo"` + Root string `json:"root"` + Actions skills.Plan `json:"skill_actions"` + Problems []scan.ScanProblem `json:"skill_problems,omitempty"` + }{r.apply, repo.Root, root.Path, plan, scanned.Problems} + enc := json.NewEncoder(stdout) + enc.SetIndent("", " ") + if err := enc.Encode(report); err != nil { + fmt.Fprintf(stderr, "agentsync: %v\n", err) + return exitFailure + } + } else { + emitSkillActions(w, plan, r.apply) + emitProblems(w, scanned.Problems) + } + pending, written, blocked, failed := plan.Counts() + if r.mode == skills.Link { + emitSkillRestart(w, written) + } + return skillExitCode(pending, blocked+len(scanned.Problems), failed, r.failOnPending) +} + +func (r skillRun) prepare(repo *scan.Repo, root rootChoice) (*skills.Catalog, skills.Plan, skills.Report, error) { + var scanned skills.Report + if r.mode == skills.Link && len(r.keys) == 0 { + return nil, nil, scanned, fmt.Errorf("--skill requires a current project or --project with --all") + } + catalog, err := openCatalog(repo, true) + if err != nil { + return nil, nil, scanned, err + } + if r.mode == skills.Import && len(r.keys) == 0 { + scanned, err = catalog.Scan(root.Path) + if err != nil { + _ = catalog.Close() + return nil, nil, scanned, err + } + } + plan := catalog.Plan(r.mode, r.keys, r.names, scanned, r.excludeLocal) + if err := repo.RequireHome(root.Path, plan.Dirs(), r.allowOutsideHome); err != nil { + _ = catalog.Close() + return nil, nil, scanned, err + } + return catalog, plan, scanned, nil +} + +func emitSkillRestart(w io.Writer, written int) { + if written > 0 { + fmt.Fprintln(w, "Restart your Claude Code session to load the linked project skills.") + } +} + +func skillExitCode(pending, blocked, failed int, failOnPending bool) int { + if failed > 0 { + return exitFailure + } + if blocked > 0 || (failOnPending && pending > 0) { + return exitAtRisk + } + return exitOK +} + +func emitSkillActions(w io.Writer, plan skills.Plan, apply bool) { + if !apply { + fmt.Fprintln(w, "DRY RUN — pass --apply to carry out the pending skill changes.") + } + for _, item := range plan { + fmt.Fprintf(w, " %s %s/%s: %s\n", item.Status, item.Project, item.Name, item.Reason) + if item.Canonical != "" { + fmt.Fprintf(w, " %s <-> %s\n", item.Path, item.Canonical) + } + if item.ExcludeFile != "" { + fmt.Fprintf(w, " %s in %s\n", item.Pattern, item.ExcludeFile) + } + } +} diff --git a/cmd/agentsync/skills_test.go b/cmd/agentsync/skills_test.go new file mode 100644 index 0000000..a1e9ad3 --- /dev/null +++ b/cmd/agentsync/skills_test.go @@ -0,0 +1,388 @@ +package main + +import ( + "encoding/json" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/agentic-tooling/agentsync/internal/skills" + "github.com/agentic-tooling/agentsync/internal/testfs" +) + +func skillFixture(t *testing.T) (repo, project string) { + t.Helper() + requireGit(t) + home, err := filepath.EvalSymlinks(t.TempDir()) + if err != nil { + t.Fatal(err) + } + repo, project = filepath.Join(home, "canonical"), filepath.Join(home, "project") + testfs.GitRepo(t, repo, "README.md", "canonical") + testfs.GitRepo(t, project, "README.md", "project") + t.Setenv("HOME", home) + t.Setenv(envRepo, repo) + t.Setenv(envRoot, home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, "config")) + t.Setenv("GIT_CONFIG_GLOBAL", os.DevNull) + t.Setenv("GIT_CONFIG_NOSYSTEM", "1") + t.Chdir(project) + return repo, project +} + +func runSkillCLI(t *testing.T, want int, contain string, args ...string) string { + t.Helper() + code, stdout, stderr := runCLI(t, args...) + if code != want || !strings.Contains(stdout+stderr, contain) { + t.Fatalf("%v exit=%d, want %d with %q\n%s\n%s", args, code, want, contain, stdout, stderr) + } + return stdout +} + +func TestSkillsLinkApplyAndRepeat(t *testing.T) { + repo, project := skillFixture(t) + canonical := filepath.Join(repo, "catalog", "internal", "format") + override := filepath.Join(repo, "project-skills", "project", "format") + mustWriteT(t, filepath.Join(canonical, "SKILL.md"), "shared") + mustWriteT(t, filepath.Join(repo, "catalog", "internal", "report", "SKILL.md"), "other") + mustWriteT(t, filepath.Join(override, "SKILL.md"), "project-specific") + cfg := filepath.Join(os.Getenv("XDG_CONFIG_HOME"), "agentsync", "config.toml") + mustWriteT(t, cfg, "repo = '/different'\nskills_root = 'catalog/internal'\n") + target := filepath.Join(project, ".claude", "skills", "format") + + runSkillCLI(t, exitOK, "DRY RUN", "link", "--skill", "format", "--exclude-local") + if _, err := os.Lstat(filepath.Dir(target)); !os.IsNotExist(err) { + t.Fatalf("dry run touched the project: %v", err) + } + runSkillCLI(t, exitOK, "Restart your Claude Code session", "link", "--all", "--project", "project/", "--skill", "report", "--skill", "format", "--apply", "--json", "--exclude-local") + for name, want := range map[string]string{"format": override, "report": filepath.Join(repo, "catalog", "internal", "report")} { + path := filepath.Join(filepath.Dir(target), name) + if got, err := os.Readlink(path); err != nil || got != want { + t.Fatalf("link %s = %q, %v; want %s", name, got, err, want) + } + testfs.Git(t, project, "check-ignore", "-q", "--", path) + } + runSkillCLI(t, exitOK, "already linked", "link", "--skill", "format", "--apply") +} + +func TestSkillsLinkKeepsOccupiedPaths(t *testing.T) { + repo, project := skillFixture(t) + for _, name := range []string{"same", "damaged"} { + mustWriteT(t, filepath.Join(repo, "skills", name, "SKILL.md"), "same") + } + dir := filepath.Join(project, ".claude", "skills") + mustWriteT(t, filepath.Join(dir, "same", "SKILL.md"), "same") + if err := os.Symlink(filepath.Join(repo, "missing"), filepath.Join(dir, "damaged")); err != nil { + t.Fatal(err) + } + runSkillCLI(t, exitAtRisk, "already occupies", "link", "--skill", "same", "--skill", "damaged", "--apply") + if info, err := os.Lstat(filepath.Join(dir, "same")); err != nil || !info.IsDir() { + t.Fatalf("matching real directory replaced: %v, %v", info, err) + } + if got, err := os.Readlink(filepath.Join(dir, "damaged")); err != nil || got != filepath.Join(repo, "missing") { + t.Fatalf("broken link replaced: %q, %v", got, err) + } +} + +func TestSkillsLinkChecksIndexAndHEAD(t *testing.T) { + repo, project := skillFixture(t) + for _, name := range []string{"committed", "staged", "sibling"} { + mustWriteT(t, filepath.Join(repo, "skills", name, "SKILL.md"), name) + } + work := filepath.Join(project, ".claude", "skills") + mustWriteT(t, filepath.Join(work, "committed", "SKILL.md"), "history") + testfs.Git(t, project, "add", ".claude") + testfs.Git(t, project, "commit", "-qm", "skill") + testfs.Git(t, project, "rm", "-rf", "--", ".claude/skills/committed") + mustWriteT(t, filepath.Join(work, "staged", "nested", "file"), "index") + testfs.Git(t, project, "add", ".claude") + if err := os.RemoveAll(filepath.Join(work, "staged")); err != nil { + t.Fatal(err) + } + runSkillCLI(t, exitAtRisk, "index or HEAD", "link", "--skill", "committed", "--skill", "staged", "--exclude-local", "--apply") + for _, name := range []string{"committed", "staged"} { + if _, err := os.Lstat(filepath.Join(work, name)); !os.IsNotExist(err) { + t.Fatalf("tracked path was shadowed: %s, %v", name, err) + } + } + runSkillCLI(t, exitAtRisk, "git tracks the skills directory", "link", "--skill", "sibling", "--apply") + runSkillCLI(t, exitOK, "applied", "link", "--skill", "sibling", "--exclude-local", "--apply") + testfs.Git(t, project, "check-ignore", "-q", "--", ".claude/skills/sibling") +} + +func TestSkillsImportCapturesFullTree(t *testing.T) { + repo, project := skillFixture(t) + source := filepath.Join(project, ".claude", "skills", "build") + dest := filepath.Join(repo, "project-skills", "project", "build") + mustWriteT(t, filepath.Join(source, "SKILL.md"), "local") + mustWriteT(t, filepath.Join(source, "scripts", "go.sh"), "#!/bin/sh\n") + mustMkdirT(t, filepath.Join(source, "empty")) + if err := os.Chmod(filepath.Join(source, "scripts", "go.sh"), 0o755); err != nil { + t.Fatal(err) + } + runSkillCLI(t, exitAtRisk, "pending", "import", "--skill", "build", "--fail-on-pending") + if _, err := os.Lstat(dest); !os.IsNotExist(err) { + t.Fatalf("preview captured a skill: %v", err) + } + runSkillCLI(t, exitOK, "applied", "import", "--all", "--skill", "build", "--apply", "--json") + for _, rel := range []string{"SKILL.md", "scripts/go.sh", "empty"} { + original, err := os.Stat(filepath.Join(source, rel)) + if err != nil { + t.Fatal(err) + } + saved, err := os.Stat(filepath.Join(dest, rel)) + if err != nil || original.Mode() != saved.Mode() { + t.Fatalf("capture missed %s or its permissions: %v, %v", rel, saved, err) + } + if original.Mode().IsRegular() { + first, errFirst := os.ReadFile(filepath.Join(source, rel)) + second, errSecond := os.ReadFile(filepath.Join(dest, rel)) + if errFirst != nil || errSecond != nil || string(first) != string(second) { + t.Fatalf("capture changed %s: %q / %q", rel, first, second) + } + } + } + runSkillCLI(t, exitOK, "matching content", "import", "--skill", "build", "--apply") + mustWriteT(t, filepath.Join(source, "scripts", "go.sh"), "changed") + runSkillCLI(t, exitAtRisk, "different content", "import", "--skill", "build", "--apply") + if data, err := os.ReadFile(filepath.Join(dest, "scripts", "go.sh")); err != nil || string(data) != "#!/bin/sh\n" { + t.Fatalf("capture overwritten: %q, %v", data, err) + } +} + +func TestSkillsReportsDrift(t *testing.T) { + repo, project := skillFixture(t) + dir := filepath.Join(project, ".claude", "skills") + for _, name := range []string{"same", "nested", "renamed", "linked"} { + mustWriteT(t, filepath.Join(repo, "skills", name, "SKILL.md"), "test") + } + for _, name := range []string{"local", "same", "nested", "renamed"} { + mustWriteT(t, filepath.Join(dir, name, "SKILL.md"), "test") + } + mustWriteT(t, filepath.Join(dir, "nested", "a"), "p") + mustWriteT(t, filepath.Join(repo, "skills", "nested", "a"), "q") + mustWriteT(t, filepath.Join(dir, "renamed", "a"), "p") + mustWriteT(t, filepath.Join(repo, "skills", "renamed", "b"), "p") + external, err := filepath.EvalSymlinks(t.TempDir()) + if err != nil { + t.Fatal(err) + } + for name, target := range map[string]string{ + "linked": filepath.Join(repo, "skills", "linked"), "broken": filepath.Join(repo, "missing"), + "foreign": external, "misdirected": filepath.Join(repo, "skills", "same"), + } { + if err := os.Symlink(target, filepath.Join(dir, name)); err != nil { + t.Fatal(err) + } + } + mustMkdirT(t, filepath.Join(dir, "no-manifest")) + want := map[string]skills.State{ + "same": skills.Match, "nested": skills.Drift, "renamed": skills.Drift, "linked": skills.Linked, + "local": skills.LocalOnly, "broken": skills.Broken, "foreign": skills.Foreign, "misdirected": skills.Drift, + } + for _, cmd := range []string{"scan", "doctor", "reconcile"} { + args := []string{cmd, "--json"} + code := exitAtRisk + if cmd == "scan" { + args = append(args, "--fail-on-risk") + } + if cmd == "reconcile" { + args = append(args, "--apply", "--adopt", "--prune") + } + output := runSkillCLI(t, code, "skills", args...) + var report skills.Report + if err := json.Unmarshal([]byte(output), &report); err != nil { + t.Fatal(err) + } + if len(report.Entries) != len(want) { + t.Fatalf("%s: skills = %+v, want %v", cmd, report.Entries, want) + } + for _, row := range report.Entries { + if row.State != want[row.Name] { + t.Errorf("%s: %s = %s; want %s", cmd, row.Name, row.State, want[row.Name]) + } + } + } + result := runSkillCLI(t, exitAtRisk, "manually", "reconcile") + if !strings.Contains(result, "link points at a different canonical path") { + t.Fatalf("text omitted mismatch reason: %s", result) + } + atRisk := runSkillCLI(t, exitOK, "local-only", "scan", "--at-risk") + if strings.Contains(atRisk, "canonical-match") { + t.Fatalf("at-risk report included canonical matches: %s", atRisk) + } + if _, err := os.Readlink(filepath.Join(dir, "broken")); err != nil { + t.Fatalf("reconcile replaced or removed the broken link: %v", err) + } +} + +func TestSkillsRefuseGuards(t *testing.T) { + t.Run("git error", func(t *testing.T) { + repo, project := skillFixture(t) + mustWriteT(t, filepath.Join(repo, "skills", "run", "SKILL.md"), "canonical") + mustWriteT(t, filepath.Join(project, ".claude", "skills", "local", "SKILL.md"), "local") + testfs.BreakGitfile(t, project) + runSkillCLI(t, exitFailure, "git could not inspect", "link", "--skill", "run", "--apply", "--exclude-local") + runSkillCLI(t, exitFailure, "git could not inspect", "import", "--skill", "local", "--apply") + }) + t.Run("nested symlink", func(t *testing.T) { + repo, project := skillFixture(t) + source := filepath.Join(project, ".claude", "skills", "local") + mustWriteT(t, filepath.Join(source, "SKILL.md"), "local") + if err := os.Symlink(repo, filepath.Join(source, "external")); err != nil { + t.Fatal(err) + } + runSkillCLI(t, exitFailure, "not a regular file or directory", "import", "--skill", "local", "--apply") + runSkillCLI(t, exitFailure, "skill_problems", "doctor", "--json") + runSkillCLI(t, exitAtRisk, "not a regular file or directory", "scan", "--fail-on-risk") + }) + t.Run("outside canonical", func(t *testing.T) { + repo, project := skillFixture(t) + outside := filepath.Join(project, "catalog") + mustWriteT(t, filepath.Join(outside, "shared", "run", "SKILL.md"), "outside") + if err := os.Symlink(outside, filepath.Join(repo, "escape")); err != nil { + t.Fatal(err) + } + mustWriteT(t, filepath.Join(os.Getenv("XDG_CONFIG_HOME"), "agentsync", "config.toml"), "skills_root = 'escape/shared'\n") + for _, cmd := range []string{"scan", "doctor", "reconcile"} { + runSkillCLI(t, exitFailure, "must stay inside", cmd) + } + runSkillCLI(t, exitFailure, "must stay inside", "link", "--skill", "run", "--apply") + }) + t.Run("project home gate", func(t *testing.T) { + repo, _ := skillFixture(t) + t.Setenv("HOME", t.TempDir()) + mustWriteT(t, filepath.Join(repo, "skills", "run", "SKILL.md"), "canonical") + runSkillCLI(t, exitFailure, "refusing to write", "link", "--skill", "run", "--apply") + }) + t.Run("cli selectors", func(t *testing.T) { + skillFixture(t) + runSkillCLI(t, exitFailure, "invalid --skill", "link", "--skill", "../outside") + runSkillCLI(t, exitFailure, "invalid --skill", "import", "--skill", "../outside") + runSkillCLI(t, exitFailure, "skill captures are write-once", "import", "--skill", "run", "--update") + runSkillCLI(t, exitFailure, "requires a current project", "link", "--all", "--skill", "run") + runSkillCLI(t, exitAtRisk, "was not found", "import", "--all", "--skill", "run") + runSkillCLI(t, exitAtRisk, "no canonical skill", "link", "--skill", "run") + mustWriteT(t, filepath.Join(os.Getenv("XDG_CONFIG_HOME"), "agentsync", "config.toml"), "skills_root = '../outside'\n") + runSkillCLI(t, exitFailure, "skills_root must be a relative directory", "scan") + }) +} + +func TestSkillsAndAgentsMayShareAName(t *testing.T) { + repo, project := skillFixture(t) + agent := filepath.Join(repo, "agents", "build.md") + skill := filepath.Join(repo, "skills", "build") + mustWriteT(t, agent, "agent") + mustWriteT(t, filepath.Join(skill, "SKILL.md"), "skill") + report := runSkillCLI(t, exitOK, "\"skill_actions\"", "link", "--agent", "build", "--skill", "build", "--exclude-local", "--apply", "--json") + if !strings.Contains(report, "\"recorded\"") { + t.Fatalf("combined link omitted agent report: %s", report) + } + for path, expected := range map[string]string{ + filepath.Join(project, ".claude", "agents", "build.md"): agent, + filepath.Join(project, ".claude", "skills", "build"): skill, + } { + if actual, err := os.Readlink(path); err != nil || actual != expected { + t.Fatalf("link %s = %q, %v; want %s", path, actual, err, expected) + } + } +} + +func TestSkillsExcludeTheUserLevelDirectory(t *testing.T) { + repo, _ := skillFixture(t) + home := os.Getenv("HOME") + installed := filepath.Join(home, ".claude", "skills", "private") + mustWriteT(t, filepath.Join(installed, "SKILL.md"), "installed") + mustWriteT(t, filepath.Join(repo, "skills", "build", "SKILL.md"), "shared") + output := runSkillCLI(t, exitOK, "entries", "scan", "--all", "--json", "--fail-on-risk") + if strings.Contains(output, "private") { + t.Fatalf("scan included the user-level skill: %s", output) + } + runSkillCLI(t, exitAtRisk, "was not found", "import", "--all", "--skill", "private", "--apply") + runSkillCLI(t, exitAtRisk, "user-level skills", "link", "--all", "--project", "_home", "--skill", "build", "--apply") + if _, err := os.Lstat(filepath.Join(filepath.Dir(installed), "build")); !os.IsNotExist(err) { + t.Fatalf("link created a user-level skill: %v", err) + } + if _, err := os.Lstat(filepath.Join(repo, "project-skills")); !os.IsNotExist(err) { + t.Fatalf("import captured a user-level skill: %v", err) + } +} + +func TestSkillsKeepWhitespacePrefixedTrackedPaths(t *testing.T) { + repo, monorepo := skillFixture(t) + project := filepath.Join(monorepo, " package") + for _, name := range []string{"staged", "committed"} { + mustWriteT(t, filepath.Join(repo, "skills", name, "SKILL.md"), "canonical") + } + dir := filepath.Join(project, ".claude", "skills") + mustWriteT(t, filepath.Join(dir, "committed", "SKILL.md"), "HEAD") + testfs.Git(t, monorepo, "add", "--", " package") + testfs.Git(t, monorepo, "commit", "-qm", "committed skill") + testfs.Git(t, monorepo, "rm", "-rf", "--", " package/.claude/skills/committed") + mustWriteT(t, filepath.Join(dir, "staged", "SKILL.md"), "index") + testfs.Git(t, monorepo, "add", "--", " package") + if err := os.RemoveAll(filepath.Join(dir, "staged")); err != nil { + t.Fatal(err) + } + t.Chdir(project) + for _, name := range []string{"staged", "committed"} { + runSkillCLI(t, exitAtRisk, "index or HEAD", "link", "--skill", name, "--exclude-local", "--apply") + if _, err := os.Lstat(filepath.Join(dir, name)); !os.IsNotExist(err) { + t.Fatalf("link shadowed the tracked skill %s: %v", name, err) + } + } +} + +func TestSkillsBrokenConfigCannotSelectDefaultCatalog(t *testing.T) { + repo, project := skillFixture(t) + mustWriteT(t, filepath.Join(repo, "skills", "build", "SKILL.md"), "wrong default") + mustWriteT(t, filepath.Join(project, ".claude", "skills", "local", "SKILL.md"), "local") + cfg := filepath.Join(os.Getenv("XDG_CONFIG_HOME"), "agentsync", "config.toml") + mustWriteT(t, cfg, "skills_root = 'custom'\nroot = = invalid\n") + runSkillCLI(t, exitFailure, "config file", "link", "--all", "--project", "project", "--skill", "build", "--apply") + runSkillCLI(t, exitFailure, "config file", "import", "--skill", "local", "--apply") + runSkillCLI(t, exitFailure, "config file", "doctor", "--all") + for _, path := range []string{filepath.Join(project, ".claude", "skills", "build"), filepath.Join(repo, "project-skills")} { + if _, err := os.Lstat(path); !os.IsNotExist(err) { + t.Fatalf("broken config allowed a write at %s: %v", path, err) + } + } +} + +func TestSkillsDanglingLinksFollowCanonicalOwnership(t *testing.T) { + repo, project := skillFixture(t) + dir := filepath.Join(project, ".claude", "skills") + outside := t.TempDir() + mustMkdirT(t, dir) + mustMkdirT(t, filepath.Join(repo, "skills")) + canonicalAlias := filepath.Join(project, "canonical-alias") + escape := filepath.Join(repo, "escape") + dangling := filepath.Join(repo, "dangling") + for path, target := range map[string]string{ + canonicalAlias: filepath.Join(repo, "skills"), + escape: outside, + dangling: filepath.Join(outside, "removed"), + filepath.Join(dir, "canonical-missing"): filepath.Join(canonicalAlias, "missing", "nested"), + filepath.Join(dir, "foreign-missing"): filepath.Join(escape, "missing"), + filepath.Join(dir, "foreign-dangling"): dangling, + } { + if err := os.Symlink(target, path); err != nil { + t.Fatal(err) + } + } + output := runSkillCLI(t, exitAtRisk, "canonical-missing", "doctor", "--json") + var report skills.Report + if err := json.Unmarshal([]byte(output), &report); err != nil { + t.Fatal(err) + } + want := map[string]skills.State{"canonical-missing": skills.Broken, "foreign-missing": skills.Foreign, "foreign-dangling": skills.Foreign} + if len(report.Entries) != len(want) { + t.Fatalf("wrong skills reported: %+v", report) + } + for _, entry := range report.Entries { + if entry.State != want[entry.Name] { + t.Errorf("%s = %s, want %s", entry.Name, entry.State, want[entry.Name]) + } + } +} diff --git a/coverage-floors.txt b/coverage-floors.txt index 2f73348..9e36b2f 100644 --- a/coverage-floors.txt +++ b/coverage-floors.txt @@ -13,6 +13,7 @@ internal/override 98.3 internal/reconcile 91.3 internal/releasepolicy 96.4 internal/scan 91.6 +internal/skills 84.0 internal/state 89.9 internal/testfs 77.1 scripts/release-policy 90.7 diff --git a/docs/configuration.md b/docs/configuration.md index c25eb9b..85c1ae6 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -11,15 +11,19 @@ The usual place to set them is `config.toml`, which ```toml repo = "~/src/my-agents" root = "~/src" +# Optional, relative to the canonical checkout +skills_root = "skills" ``` It lives at `$XDG_CONFIG_HOME/agentsync/config.toml`, or at `~/.config/agentsync/config.toml` when `$XDG_CONFIG_HOME` is unset or not an absolute path. That holds on macOS too. -`repo` and `root` are the only keys. Each must be an absolute path, `~`, or start +`repo` and `root` must each be an absolute path, `~`, or start with `~/`; `~` expands against `$HOME`. A missing file is not an error, but a symlink there whose target is missing is a broken file. +`skills_root` defaults to `skills/` in the canonical checkout. A configured +directory and its resolved path must stay within that checkout. ## Environment @@ -48,9 +52,11 @@ and the root from the file. An empty or blank variable counts as unset. | 2 | config file | | 3 | refuse with exit 2 | -The file is read only when a setting is still unset after the environment. A -broken file therefore never blocks a run that sets both variables, which is how -you work around it while fixing it. +Repo and root resolution reads the file only for settings left unset by the +environment. Commands that inspect or provision skills also look up +`skills_root` in the file. A skill link, capture, or scan of skill directories +refuses a malformed or unreadable file, even when the environment supplies +the repo and root. Every run that reports prints a `Repo:` line and a `Root:` line naming where each value came from, including the config file's path. @@ -63,9 +69,10 @@ file: - it exists but cannot be read, is a directory, or is a dangling symlink; - it does not parse as TOML (the message gives the line), or a value has the wrong type, such as `repo = 5`; -- it holds a key other than `repo` or `root` (the message names the key); +- it holds a key other than `repo`, `root` or `skills_root` (the message names the key); - a key is present but empty; -- a value is not an absolute path after `~` expansion. +- `repo` or `root` is not an absolute path after `~` expansion; +- `skills_root` is not a checkout-relative directory path. If neither `$XDG_CONFIG_HOME` nor `$HOME` is a usable absolute path, the file cannot be located, and a run that needs it refuses with exit 2 saying it cannot @@ -79,6 +86,10 @@ project only). A project key is the project's path relative to `$HOME`, for example `src/api`. Projects outside `$HOME` are keyed `_outside-home/`. +Skills share these project keys. Agent Sync looks in +`project-skills///` first, then in +`//`. [Project skills](skills.md) + ## Which tree gets scanned Every command except `inventory` and `init` works on the project the working diff --git a/docs/skills.md b/docs/skills.md new file mode 100644 index 0000000..aa16bfe --- /dev/null +++ b/docs/skills.md @@ -0,0 +1,58 @@ +# Project skills + +A Claude project skill lives at `.claude/skills//` and has a regular +`SKILL.md`. The directory can also hold scripts, templates, and other support +files. Agent Sync compares a SHA-256 digest of the sorted relative paths and +file bytes, including nested files and empty directories. Captures preserve +executable bits. A nested symlink, special file, or unreadable tree prevents a +capture. + +Put shared skills in `skills//` in the canonical checkout, or set a +different checkout-relative `skills_root`. A captured skill in +`project-skills///` takes precedence for that project. The +project key is the same one Agent Sync uses for agents. [Configuration](configuration.md) + +## Link a canonical skill + +Run from inside the project. Named skills are selected independently of agent +files, even when they share a name. + +```bash +agentsync link --skill release-check --skill csv-format +agentsync link --skill release-check --skill csv-format --exclude-local --apply +``` + +Use `--all --project ` to select a project from the default tree. An +existing real directory, including one with matching content, is reported as +occupied. A healthy link to the selected counterpart reports a match. Broken, +foreign, or misdirected links stay in place for manual resolution. + +The Git gate refuses a path with an index or HEAD entry at or below it, or a +tracked ancestor file. If the skills directory contains tracked siblings, +`--exclude-local` allows a new symlink only after Git confirms that the +specific link path is ignored. Agent Sync adds a path-specific rule to +`info/exclude` when needed. In an otherwise visible skills directory, +`--exclude-local` keeps the link local. + +## Capture a project skill + +```bash +agentsync import --skill release-check +agentsync import --skill release-check --apply +``` + +The apply captures a complete directory under `project-skills/`. The source +stays in the project. Repeating the import reports matching content. If the +project capture already holds different bytes, review the differences and +resolve them manually. Use `--all` to select skills by name throughout the +default tree, or `--fail-on-pending` to check whether a preview would write a +capture. + +## Check existing skills + +`scan`, `doctor`, and `reconcile` report linked, broken, foreign, local-only, +canonical matches, and content drift. Their JSON includes `skills` and +`skill_problems` fields when applicable. With `--fail-on-risk`, scan returns 1 +for local-only skills, broken canonical links, drift, or an unclassifiable skill. +Doctor and reconcile report those findings for manual resolution. Reconcile +keeps skill directories and links in place, including with `--apply`. diff --git a/go.mod b/go.mod index dc65942..25ef084 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/agentic-tooling/agentsync go 1.25 -require github.com/BurntSushi/toml v1.6.0 +require ( + github.com/BurntSushi/toml v1.6.0 + golang.org/x/sys v0.41.0 +) diff --git a/go.sum b/go.sum index f74b269..e5969e0 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk= github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= +golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k= +golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= diff --git a/internal/config/config.go b/internal/config/config.go index e188eeb..0596175 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -18,8 +18,9 @@ import ( // Config holds the settings the file may carry. An empty field means the key // was absent: Load refuses a key that is present but empty. type Config struct { - Repo string `toml:"repo"` - Root string `toml:"root"` + Repo string `toml:"repo"` + Root string `toml:"root"` + SkillsRoot string `toml:"skills_root,omitempty"` } // Path is where the config file lives: under $XDG_CONFIG_HOME when that is @@ -39,16 +40,9 @@ func Path() (string, error) { // Load reads the file at path. A missing file is an empty Config, not an // error; every other problem with it is an error naming the file. func Load(path string) (Config, error) { - data, err := os.ReadFile(path) - if errors.Is(err, fs.ErrNotExist) { - // A link whose target is gone is a broken config, not an absent one. - if _, lerr := os.Lstat(path); lerr == nil { - return Config{}, fmt.Errorf("config file %s is a dangling symlink; fix or remove the link", path) - } - return Config{}, nil - } + data, err := readFile(path) if err != nil { - return Config{}, fmt.Errorf("config file %s: %w", path, err) + return Config{}, err } var c Config @@ -57,7 +51,7 @@ func Load(path string) (Config, error) { return Config{}, fmt.Errorf("config file %s: %w", path, err) } if undecoded := md.Undecoded(); len(undecoded) > 0 { - return Config{}, fmt.Errorf("config file %s: unknown key %q (known keys: repo, root)", path, undecoded[0].String()) + return Config{}, fmt.Errorf("config file %s: unknown key %q (known keys: repo, root, skills_root)", path, undecoded[0].String()) } for _, f := range []struct { @@ -73,9 +67,75 @@ func Load(path string) (Config, error) { } *f.val = v } + if md.IsDefined("skills_root") { + c.SkillsRoot, err = parseSkillsRoot(c.SkillsRoot) + if err != nil { + return Config{}, fmt.Errorf("config file %s: %w", path, err) + } + } return c, nil } +func readFile(path string) ([]byte, error) { + data, err := os.ReadFile(path) + if errors.Is(err, fs.ErrNotExist) { + if _, lerr := os.Lstat(path); lerr == nil { + return nil, fmt.Errorf("config file %s is a dangling symlink; fix or remove the link", path) + } + return nil, nil + } + if err != nil { + return nil, fmt.Errorf("config file %s: %w", path, err) + } + return data, nil +} + +func parseSkillsRoot(raw string) (string, error) { + value := strings.TrimSpace(raw) + if !filepath.IsLocal(value) || filepath.Clean(value) == "." || strings.HasPrefix(value, "~") || + strings.ContainsAny(value, "\\:\x00") || strings.Contains("/"+value+"/", "/../") { + return "", fmt.Errorf("skills_root must be a relative directory inside the canonical checkout, got %q", raw) + } + return filepath.Clean(value), nil +} + +// SkillsRoot reads the setting independently of repo and root. A command +// acting on skills must distinguish an omitted setting from a broken file. +func SkillsRoot(strict bool) (string, error) { + path, err := Path() + if err != nil { + return "", optionalConfigError(err, strict) + } + data, err := readFile(path) + if err != nil { + return "", optionalConfigError(err, strict) + } + var fields map[string]any + if _, err := toml.Decode(string(data), &fields); err != nil { + return "", optionalConfigError(fmt.Errorf("config file %s: %w", path, err), strict) + } + raw, exists := fields["skills_root"] + if !exists { + return "", nil + } + value, ok := raw.(string) + if !ok { + return "", fmt.Errorf("config file %s: skills_root must be a string", path) + } + root, err := parseSkillsRoot(value) + if err != nil { + return "", fmt.Errorf("config file %s: %w", path, err) + } + return root, nil +} + +func optionalConfigError(err error, strict bool) error { + if strict { + return err + } + return nil +} + // ParsePath checks one setting's value by the file's rules and returns it // expanded: non-empty, and absolute once a leading ~ or ~/ is expanded against // $HOME. key names the setting in the error. @@ -105,6 +165,11 @@ func Render(c Config) ([]byte, error) { if c.Root, err = portable("root", c.Root); err != nil { return nil, err } + if c.SkillsRoot != "" { + if c.SkillsRoot, err = parseSkillsRoot(c.SkillsRoot); err != nil { + return nil, err + } + } var buf bytes.Buffer err = toml.NewEncoder(&buf).Encode(c) return buf.Bytes(), err diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 1d538f6..7cfb8b7 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -327,3 +327,102 @@ func mustWriteFile(t *testing.T, path, content string) { t.Fatal(err) } } + +func TestSkillsRootConfiguration(t *testing.T) { + base := t.TempDir() + t.Setenv("XDG_CONFIG_HOME", base) + path, err := Path() + if err != nil { + t.Fatal(err) + } + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + for _, test := range []struct { + name, settings, want string + bad bool + }{ + {name: "absent", settings: "root = '/root'"}, + {name: "relative", settings: "skills_root = 'catalog/./shared'", want: "catalog/shared"}, + {name: "independent", settings: "repo = false\nskills_root = 'shared'", want: "shared"}, + {name: "invalid type", settings: "skills_root = false", bad: true}, + {name: "absolute", settings: "skills_root = '/outside'", bad: true}, + {name: "escape", settings: "skills_root = '../outside'", bad: true}, + {name: "traversal", settings: "skills_root = 'a/../b'", bad: true}, + {name: "home", settings: "skills_root = '~/outside'", bad: true}, + {name: "root", settings: "skills_root = '.'", bad: true}, + {name: "blank", settings: "skills_root = ' '", bad: true}, + {name: "invalid legacy config", settings: "root = = invalid"}, + } { + t.Run(test.name, func(t *testing.T) { + if err := os.WriteFile(path, []byte(test.settings), 0o600); err != nil { + t.Fatal(err) + } + got, err := SkillsRoot(false) + if got != test.want || (err != nil) != test.bad { + t.Fatalf("SkillsRoot = %q, %v; want %q, error=%v", got, err, test.want, test.bad) + } + }) + } + if err := os.Remove(path); err != nil { + t.Fatal(err) + } + if got, err := SkillsRoot(true); got != "" || err != nil { + t.Fatalf("missing optional settings file = %q, %v", got, err) + } + t.Setenv("XDG_CONFIG_HOME", "") + t.Setenv("HOME", "") + if got, err := SkillsRoot(false); got != "" || err != nil { + t.Fatalf("unavailable optional settings file = %q, %v", got, err) + } +} + +func TestSkillConfigurationRefusesBrokenFiles(t *testing.T) { + base := t.TempDir() + t.Setenv("XDG_CONFIG_HOME", base) + path, err := Path() + if err != nil { + t.Fatal(err) + } + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + if err := os.Symlink(filepath.Join(base, "missing"), path); err != nil { + t.Fatal(err) + } + if _, err := SkillsRoot(true); err == nil || !strings.Contains(err.Error(), "dangling symlink") { + t.Fatalf("dangling config error = %v", err) + } + if err := os.Remove(path); err != nil { + t.Fatal(err) + } + if err := os.Mkdir(path, 0o755); err != nil { + t.Fatal(err) + } + if _, err := SkillsRoot(true); err == nil || !strings.Contains(err.Error(), path) { + t.Fatalf("unreadable config error = %v", err) + } + t.Setenv("XDG_CONFIG_HOME", "") + t.Setenv("HOME", "") + if _, err := SkillsRoot(true); err == nil || !strings.Contains(err.Error(), "cannot locate") { + t.Fatalf("unavailable config location error = %v", err) + } +} + +func TestSkillsRootLoadAndRender(t *testing.T) { + settings := Config{Repo: "/repo", Root: "/projects", SkillsRoot: "catalog/shared"} + data, err := Render(settings) + if err != nil { + t.Fatal(err) + } + if got, err := Load(writeConfig(t, string(data))); err != nil || got != settings { + t.Fatalf("Load(Render()) = %+v, %v; want %+v", got, err, settings) + } + settings.SkillsRoot = "../outside" + if _, err := Render(settings); err == nil { + t.Fatal("Render accepted an escaping skills root") + } + if _, err := Load(writeConfig(t, "skills_root = '../outside'\n")); err == nil { + t.Fatal("Load accepted an escaping skills root") + } +} diff --git a/internal/linker/apply.go b/internal/linker/apply.go index e6d5a23..d6b98dd 100644 --- a/internal/linker/apply.go +++ b/internal/linker/apply.go @@ -12,6 +12,7 @@ import ( "github.com/agentic-tooling/agentsync/internal/scan" "github.com/agentic-tooling/agentsync/internal/state" + "golang.org/x/sys/unix" ) // Applied is what an Apply actually did. link has three kinds of write — the @@ -68,7 +69,7 @@ func (p Plan) Apply(repo *scan.Repo, st *state.File) (Applied, []error) { unexcluded := make(map[string]bool, len(p.Excludes)) for _, ex := range p.Excludes { - if err := writeExclude(ex); err != nil { + if err := WriteExclude(ex.File, ex.Pattern); err != nil { errs = append(errs, err) unexcluded[ex.Project] = true continue @@ -141,31 +142,30 @@ func record(st *state.File, links []Item, into *[]state.Record) []error { return errs } -// writeExclude appends the plan's pattern to a project's .git/info/exclude. -// -// info/exclude, not .gitignore: .gitignore is tracked and shared, so a rule -// written there is a decision committed on behalf of everyone who clones the -// project. info/exclude is local to this clone and does the same job for the -// person running this command. See the Exclude doc comment. -// -// The file is rewritten whole through a temporary and a rename rather than -// opened for append. An append that fails halfway leaves a partial line, and a -// partial line in an exclude file is a pattern nobody wrote — which could match -// paths nobody meant to hide. -func writeExclude(ex Exclude) error { - dir := filepath.Dir(ex.File) +// WriteExclude adds a rule once to a project's .git/info/exclude. It +// replaces the file atomically so a failed append cannot leave a partial rule. +func WriteExclude(file, pattern string) error { + dir := filepath.Dir(file) if err := os.MkdirAll(dir, 0o755); err != nil { return fmt.Errorf("creating %q: %w", dir, err) } + lock, err := os.Open(dir) + if err == nil { + defer func() { _ = lock.Close() }() + err = unix.Flock(int(lock.Fd()), unix.LOCK_EX) + } + if err != nil { + return fmt.Errorf("locking %q for exclusion update: %w", dir, err) + } - existing, err := os.ReadFile(ex.File) + existing, err := os.ReadFile(file) if err != nil && !errors.Is(err, fs.ErrNotExist) { - return fmt.Errorf("reading %q: %w", ex.File, err) + return fmt.Errorf("reading %q: %w", file, err) } // Re-checked here rather than trusted from the plan, so a concurrent run // or a hand edit between the two phases cannot produce a duplicate line. for line := range strings.SplitSeq(string(existing), "\n") { - if strings.TrimSpace(line) == ex.Pattern { + if strings.TrimSpace(line) == pattern { return nil } } @@ -178,10 +178,10 @@ func writeExclude(ex Exclude) error { // The comment survives in someone else's repository, so it says who wrote // the line and why rather than leaving an unexplained pattern behind. buf.WriteString("# agentsync: symlinks here point at an absolute path on this machine\n") - buf.WriteString(ex.Pattern + "\n") + buf.WriteString(pattern + "\n") - if err := writeAtomic(ex.File, buf.String()); err != nil { - return fmt.Errorf("excluding %s in %q: %w", ex.Pattern, ex.File, err) + if err := writeAtomic(file, buf.String()); err != nil { + return fmt.Errorf("excluding %s in %q: %w", pattern, file, err) } return nil } diff --git a/internal/linker/apply_test.go b/internal/linker/apply_test.go index 67b5568..f60599c 100644 --- a/internal/linker/apply_test.go +++ b/internal/linker/apply_test.go @@ -7,6 +7,7 @@ import ( "path/filepath" "strconv" "strings" + "sync" "syscall" "testing" @@ -275,7 +276,7 @@ func TestApply_UnreadableExcludeFileIsNotOverwritten(t *testing.T) { assertNoTemps(t, filepath.Dir(exclude), ".agentsync-exclude-") } -// TestApply_ExclusionAddedSinceThePlanIsNotDuplicated: writeExclude re-reads +// TestApply_ExclusionAddedSinceThePlanIsNotDuplicated: WriteExclude re-reads // the file instead of trusting the plan. func TestApply_ExclusionAddedSinceThePlanIsNotDuplicated(t *testing.T) { f, plan, path, exclude := excludePlan(t) @@ -323,7 +324,7 @@ func TestWriteExclude_FailedWriteIsReported(t *testing.T) { project := filepath.Dir(filepath.Dir(testfs.UnreadableAgentsDir(t, t.TempDir()))) ex := Exclude{Project: "p", File: filepath.Join(project, "exclude"), Pattern: "/.claude/agents/"} - err := writeExclude(ex) + err := WriteExclude(ex.File, ex.Pattern) if !errors.Is(err, syscall.ENAMETOOLONG) || !strings.Contains(err.Error(), "excluding") { t.Fatalf("err = %v, want the exclusion's ENAMETOOLONG", err) } @@ -541,3 +542,37 @@ func TestApply_StateSaveFailureIsReported(t *testing.T) { assertLink(t, path, filepath.Join(f.repoRoot, "agents", "shared.md")) assertNoTemps(t, f.repoRoot, ".agentsync-state-") } + +func TestWriteExclude_ConcurrentRulesAreRetained(t *testing.T) { + file := filepath.Join(t.TempDir(), "exclude") + if err := os.WriteFile(file, []byte(strings.Repeat("# existing rule\n", 32768)), 0o644); err != nil { + t.Fatal(err) + } + const count = 12 + results := make(chan error, count) + start := make(chan struct{}) + var wg sync.WaitGroup + for i := range count { + wg.Go(func() { + <-start + results <- WriteExclude(file, "/.claude/skills/skill-"+strconv.Itoa(i)) + }) + } + close(start) + wg.Wait() + close(results) + for err := range results { + if err != nil { + t.Fatal(err) + } + } + data, err := os.ReadFile(file) + if err != nil { + t.Fatal(err) + } + for i := range count { + if rule := "/.claude/skills/skill-" + strconv.Itoa(i) + "\n"; !strings.Contains(string(data), rule) { + t.Errorf("concurrent update lost rule %q", rule) + } + } +} diff --git a/internal/linker/plan_test.go b/internal/linker/plan_test.go index 5a8e846..cb1de9b 100644 --- a/internal/linker/plan_test.go +++ b/internal/linker/plan_test.go @@ -234,3 +234,19 @@ func TestNewPlan_UnhashableSourceIsAProblemNotAnItem(t *testing.T) { t.Errorf("items = %v, want the file dropped", plan.Items) } } + +func TestDeriveDirRefusesProjectKeyAliases(t *testing.T) { + f := newFixture(t) + f.gitInit("actual") + alias := filepath.Join(f.repo.Home, "alias") + actual := filepath.Join(f.repo.Home, "actual") + if err := os.MkdirAll(actual, 0o755); err != nil { + t.Fatal(err) + } + if err := os.Symlink(actual, alias); err != nil { + t.Fatal(err) + } + if dir, err := deriveDir(f.repo, "alias"); err == nil || !strings.Contains(err.Error(), "keys as") { + t.Fatalf("alias resolved a different project: %q, %v", dir, err) + } +} diff --git a/internal/scan/git.go b/internal/scan/git.go index 2395a3a..8d3e729 100644 --- a/internal/scan/git.go +++ b/internal/scan/git.go @@ -78,6 +78,12 @@ func runGit(dir string, args ...string) (string, gitAnswer) { // // The code is meaningful only when the answer is gitNo; it is 0 otherwise. func runGitCode(dir string, args ...string) (string, gitAnswer, int) { + out, answer, code := runGitRaw(dir, args...) + return strings.TrimSpace(out), answer, code +} + +// runGitRaw preserves the bytes in Git pathname records. +func runGitRaw(dir string, args ...string) (string, gitAnswer, int) { cmd := exec.Command("git", args...) // cmd.Dir rather than `git -C`: the working directory is the thing that // actually selects the repository, and it cannot be confused with a @@ -90,7 +96,7 @@ func runGitCode(dir string, args ...string) (string, gitAnswer, int) { err := cmd.Run() if err == nil { - return strings.TrimSpace(out.String()), gitYes, 0 + return out.String(), gitYes, 0 } var exitErr *exec.ExitError @@ -597,14 +603,27 @@ type DirGit struct { // falling through to a "no", because a caller about to write must be able to // tell "git says this is clean" from "git did not say". func InspectDir(dir string) DirGit { - d := DirGit{Dir: dir, Tracking: DirUnknown} + return inspectPath(dir, existingAncestor(dir), false) +} + +// InspectSkillPath checks a project-relative path without following its leaf. +// Both indexed and committed paths can be shadowed by a symlink. +func InspectSkillPath(project, rel string) DirGit { + return inspectPath(filepath.Join(project, rel), project, true) +} - probe := existingAncestor(dir) +func inspectPath(dir, probe string, skill bool) DirGit { + d := DirGit{Dir: dir, Tracking: DirUnknown} if probe == "" { return d } - top, ans := runGit(probe, "rev-parse", "--show-toplevel") + top, ans, _ := runGitRaw(probe, "rev-parse", "--show-toplevel") + if skill { + top = strings.TrimSuffix(top, "\n") + } else { + top = strings.TrimSpace(top) + } switch ans { case gitUnavailable: return d @@ -652,13 +671,20 @@ func InspectDir(dir string) DirGit { d.GitDir = gitDir } - rel, err := filepath.Rel(top, resolveMissing(dir)) + resolved := dir + if !skill { + resolved = resolveMissing(dir) + } + rel, err := filepath.Rel(top, resolved) if err != nil || !contains(rel) { // The directory is not under the root git handed us. We cannot form // a meaningful question, so we do not answer one. return d } d.Rel = filepath.ToSlash(rel) + if skill { + return inspectSkillGit(d) + } // An unborn HEAD is a repository that has committed nothing, so nothing // under this directory is tracked. That is a real answer — --quiet makes @@ -711,6 +737,68 @@ func InspectDir(dir string) DirGit { return d } +// SkillExcludePath asks Git for the shared info/exclude, including in worktrees. +func SkillExcludePath(root string) string { + path, answer := runGit(root, "rev-parse", "--path-format=absolute", "--git-path", "info/exclude") + if answer != gitYes || !filepath.IsAbs(path) { + return "" + } + return path +} + +func inspectSkillGit(d DirGit) DirGit { + // Query the first component as well so an indexed ancestor file or + // gitlink is found even when it disappeared from the working tree. + prefix, _, _ := strings.Cut(d.Rel, "/") + pathspec := []string{"--", prefix} + ignoreCase, answer, code := runGitCode(d.Root, "config", "--type=bool", "--get", "core.ignoreCase") + if answer != gitYes && !(answer == gitNo && code == gitSaidNo) { + return d + } + if ignoreCase == "true" { + // ls-tree has no case-insensitive pathspec. Filter its output below + // so case aliases cannot shadow an indexed or committed path. + pathspec = []string{"--"} + } + index, answer, _ := runGitRaw(d.Root, append([]string{"--literal-pathspecs", "ls-files", "-z"}, pathspec...)...) + if answer != gitYes { + return d + } + _, answer, code = runGitCode(d.Root, "rev-parse", "--verify", "--quiet", "HEAD") + switch { + case answer == gitYes: + tree, answer, _ := runGitRaw(d.Root, append([]string{"--literal-pathspecs", "ls-tree", "-r", "--name-only", "-z", "HEAD"}, pathspec...)...) + if answer != gitYes { + return d + } + index += tree + case answer == gitNo && code == gitSaidNo: + default: + return d + } + rel := d.Rel + if ignoreCase == "true" { + rel = strings.ToLower(rel) + } + for name := range strings.SplitSeq(index, "\x00") { + if ignoreCase == "true" { + name = strings.ToLower(name) + } + if name != "" && (name == rel || strings.HasPrefix(name, rel+"/") || strings.HasPrefix(rel, name+"/")) { + d.Tracking = DirTracked + return d + } + } + _, answer, code = runGitCode(d.Root, "check-ignore", "-q", "--", d.Rel) + switch { + case answer == gitYes: + d.Tracking = DirIgnored + case answer == gitNo && code == gitSaidNo: + d.Tracking = DirVisible + } + return d +} + // hasGitEntry reports whether dir or any ancestor holds a .git entry — a // directory in an ordinary checkout, a file in a worktree or submodule. // diff --git a/internal/scan/skill_git_test.go b/internal/scan/skill_git_test.go new file mode 100644 index 0000000..ab47d57 --- /dev/null +++ b/internal/scan/skill_git_test.go @@ -0,0 +1,121 @@ +package scan + +import ( + "os" + "path/filepath" + "testing" + + "github.com/agentic-tooling/agentsync/internal/testfs" +) + +func TestInspectSkillGit(t *testing.T) { + requireGit(t) + for _, test := range []struct { + name string + want DirTracking + prep func(*testing.T, string) + }{ + {"visible", DirVisible, nil}, + {"ignored", DirIgnored, func(t *testing.T, repo string) { + mustWrite(t, filepath.Join(repo, ".gitignore"), ".claude/skills/\n") + }}, + {"unborn", DirVisible, func(t *testing.T, repo string) { + if err := os.RemoveAll(filepath.Join(repo, ".git")); err != nil { + t.Fatal(err) + } + initGitRepo(t, repo) + }}, + {"index", DirTracked, func(t *testing.T, repo string) { + mustWrite(t, filepath.Join(repo, ".claude/skills/build/script"), "index") + testfs.Git(t, repo, "add", "--", ".claude") + }}, + {"HEAD only", DirTracked, func(t *testing.T, repo string) { + mustWrite(t, filepath.Join(repo, ".claude/skills/build/script"), "committed") + testfs.Git(t, repo, "add", "--", ".claude") + testfs.Git(t, repo, "commit", "-qm", "skill") + testfs.Git(t, repo, "rm", "-rf", "--", ".claude") + }}, + {"indexed ancestor", DirTracked, func(t *testing.T, repo string) { + mustWrite(t, filepath.Join(repo, ".claude"), "ancestor") + testfs.Git(t, repo, "add", "--", ".claude") + if err := os.Remove(filepath.Join(repo, ".claude")); err != nil { + t.Fatal(err) + } + }}, + {"not a prefix match", DirVisible, func(t *testing.T, repo string) { + mustWrite(t, filepath.Join(repo, ".claude/skills/builder"), "sibling") + testfs.Git(t, repo, "add", "--", ".claude") + }}, + {"case alias", DirTracked, func(t *testing.T, repo string) { + testfs.Git(t, repo, "config", "core.ignoreCase", "true") + mustWrite(t, filepath.Join(repo, ".CLAUDE/Skills/Build/script"), "committed") + testfs.Git(t, repo, "add", "--", ".CLAUDE") + testfs.Git(t, repo, "commit", "-qm", "skill") + testfs.Git(t, repo, "rm", "-rf", "--", ".CLAUDE") + }}, + {"unreadable config", DirUnknown, func(t *testing.T, repo string) { + testfs.Git(t, repo, "config", "core.ignoreCase", "invalid-boolean") + }}, + {"unreadable repository", DirUnknown, func(t *testing.T, repo string) { testfs.BreakGitfile(t, repo) }}, + {"unreadable tree", DirUnknown, func(t *testing.T, repo string) { testfs.DeleteHeadCommit(t, repo) }}, + {"unreadable refs", DirUnknown, func(t *testing.T, repo string) { testfs.CorruptPackedRefs(t, repo) }}, + {"unreadable index", DirUnknown, func(t *testing.T, repo string) { + mustWrite(t, filepath.Join(repo, ".git", "index"), "broken") + }}, + {"no repository", DirNoRepo, func(t *testing.T, repo string) { + if err := os.RemoveAll(filepath.Join(repo, ".git")); err != nil { + t.Fatal(err) + } + }}, + } { + t.Run(test.name, func(t *testing.T) { + repo, err := filepath.EvalSymlinks(t.TempDir()) + if err != nil { + t.Fatal(err) + } + testfs.GitRepo(t, repo, "README", "fixture") + if test.prep != nil { + test.prep(t, repo) + } + if got := InspectSkillPath(repo, ".claude/skills/build"); got.Tracking != test.want { + t.Fatalf("InspectSkillPath = %s; want %s", got.Tracking, test.want) + } + }) + } +} + +func TestSkillExcludePathUsesCommonGitDirectory(t *testing.T) { + requireGit(t) + repo, err := filepath.EvalSymlinks(t.TempDir()) + if err != nil { + t.Fatal(err) + } + testfs.GitRepo(t, repo, "README", "fixture") + worktree := filepath.Join(t.TempDir(), "worktree") + testfs.Git(t, repo, "worktree", "add", "-qb", "fixture-worktree", worktree) + want := filepath.Join(repo, ".git", "info", "exclude") + if got := SkillExcludePath(worktree); got != want { + t.Fatalf("SkillExcludePath = %q; want %q", got, want) + } + if got := SkillExcludePath(t.TempDir()); got != "" { + t.Fatalf("SkillExcludePath named a path without a repository: %q", got) + } +} + +func TestFindSkillDirsStopsAtSkillRoots(t *testing.T) { + root := t.TempDir() + skills := filepath.Join(root, "a", ".claude", "skills") + mustWrite(t, filepath.Join(skills, "name", "fixtures", ".claude", "agents", "test.md"), "nested") + linked := filepath.Join(root, "b", ".claude", "skills") + mustMkdir(t, filepath.Dir(linked)) + if err := os.Symlink(skills, linked); err != nil { + t.Fatal(err) + } + got, _, err := FindSkillDirs(root) + if err != nil || len(got) != 2 || got[0] != skills || got[1] != linked { + t.Fatalf("FindSkillDirs = %v, %v", got, err) + } + if got, _, err := FindAgentDirs(root); err != nil || len(got) != 0 { + t.Fatalf("agent finder walked a skill tree: %v, %v", got, err) + } +} diff --git a/internal/scan/state.go b/internal/scan/state.go index 08f7102..022a357 100644 --- a/internal/scan/state.go +++ b/internal/scan/state.go @@ -428,8 +428,20 @@ var skipDirs = map[string]bool{ // A hard error is returned only when the walk root itself cannot be read, // which makes the whole result meaningless. func FindAgentDirs(root string) ([]string, []ScanProblem, error) { + agents, _, problems, err := findClaudeDirs(root) + return agents, problems, err +} + +// FindSkillDirs finds project skill directories without walking skill trees. +func FindSkillDirs(root string) ([]string, []ScanProblem, error) { + _, skills, problems, err := findClaudeDirs(root) + return skills, problems, err +} + +func findClaudeDirs(root string) ([]string, []string, []ScanProblem, error) { var ( dirs []string + skills []string problems []ScanProblem ) @@ -445,23 +457,30 @@ func FindAgentDirs(root string) ([]string, []ScanProblem, error) { } return nil } + base := d.Name() + if filepath.Base(filepath.Dir(path)) == ".claude" && (base == "agents" || base == "skills") { + if base == "agents" && d.IsDir() { + dirs = append(dirs, path) + } + if base == "skills" { + skills = append(skills, path) + } + if d.IsDir() { + return fs.SkipDir + } + } if !d.IsDir() { return nil } - base := d.Name() if skipDirs[base] { return fs.SkipDir } - if base == "agents" && filepath.Base(filepath.Dir(path)) == ".claude" { - dirs = append(dirs, path) - return fs.SkipDir - } return nil }) if err != nil { - return nil, nil, fmt.Errorf("walking %q: %w", root, err) + return nil, nil, nil, fmt.Errorf("walking %q: %w", root, err) } - return dirs, problems, nil + return dirs, skills, problems, nil } // agentFiles lists the .md files directly inside one agents directory. @@ -754,8 +773,9 @@ func problem(path string, err error) ScanProblem { // Entries, Dirs, and Problems are always non-nil after a Scan, so the JSON form // carries empty arrays rather than nulls. type Result struct { - Entries []Entry `json:"entries"` - Dirs []string `json:"dirs"` + Entries []Entry `json:"entries"` + Dirs []string `json:"dirs"` + SkillDirs []string `json:"-"` // Problems lists paths the scan could not account for. A non-empty // Problems means the report is incomplete: "no at-risk files" is only a @@ -800,11 +820,12 @@ func (r *Repo) Scan(root string) (Result, error) { Problems: make([]ScanProblem, 0), } - dirs, problems, err := FindAgentDirs(root) + dirs, skillDirs, problems, err := findClaudeDirs(root) if err != nil { return res, err } res.Problems = append(res.Problems, problems...) + res.SkillDirs = skillDirs for _, dir := range dirs { // Compare ownership on the resolved path. The walk yields paths diff --git a/internal/skills/capture.go b/internal/skills/capture.go new file mode 100644 index 0000000..05111a0 --- /dev/null +++ b/internal/skills/capture.go @@ -0,0 +1,47 @@ +package skills + +import ( + "crypto/rand" + "errors" + "fmt" + "os" +) + +func capture(parent *os.Root, name string, content tree) error { + temporary := ".agentsync-skill-" + rand.Text() + if err := parent.Mkdir(temporary, 0o700); err != nil { + return err + } + defer func() { _ = parent.RemoveAll(temporary) }() + staged, err := openChild(parent, temporary, false) + if err != nil { + return err + } + defer func() { _ = staged.Close() }() + for _, n := range content { + if err := writeNode(staged, n); err != nil { + return fmt.Errorf("preparing %s: %w", n.path, err) + } + } + f, err := parent.Open(".") + if err != nil { + return err + } + defer func() { _ = f.Close() }() + // The exclusive rename is the destination reservation and publication. + // Even an empty destination created by a concurrent process wins. + return publish(int(f.Fd()), temporary, name) +} + +func writeNode(root *os.Root, n node) error { + if n.mode.IsDir() { + return root.Mkdir(n.path, 0o755) + } + mode := 0o644 | (n.mode.Perm() & 0o111) + f, err := root.OpenFile(n.path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600) + if err != nil { + return err + } + _, err = f.Write(n.data) + return errors.Join(err, f.Chmod(mode), f.Close()) +} diff --git a/internal/skills/provision.go b/internal/skills/provision.go new file mode 100644 index 0000000..bd1a8f4 --- /dev/null +++ b/internal/skills/provision.go @@ -0,0 +1,333 @@ +package skills + +import ( + "errors" + "io/fs" + "os" + "path/filepath" + "slices" + "strings" + + "github.com/agentic-tooling/agentsync/internal/linker" + "github.com/agentic-tooling/agentsync/internal/scan" +) + +type status string + +const ( + pending status = "pending" + applied status = "applied" + match status = "matching" + blocked status = "blocked" + failed status = "error" +) + +type Change struct { + Project string `json:"project"` + Name string `json:"name"` + Path string `json:"path,omitempty"` + Canonical string `json:"canonical,omitempty"` + Hash string `json:"hash,omitempty"` + Status status `json:"status"` + Reason string `json:"reason,omitempty"` + ExcludeFile string `json:"exclude_file,omitempty"` + Pattern string `json:"exclude_pattern,omitempty"` + + directory string + mode Mode +} + +type Plan []Change + +func (p Plan) Counts() (pendingCount, appliedCount, blockedCount, failedCount int) { + for _, item := range p { + switch item.Status { + case pending: + pendingCount++ + case applied: + appliedCount++ + case blocked: + blockedCount++ + case failed: + failedCount++ + } + } + return +} + +func (p Plan) Dirs() []string { + var dirs []string + for _, item := range p { + if item.directory != "" { + dir := filepath.Join(item.directory, projectDir) + if !slices.Contains(dirs, dir) { + dirs = append(dirs, dir) + } + } + } + return dirs +} + +type Mode string + +const ( + Link Mode = "link" + Import Mode = "import" +) + +// Plan chooses project/name pairs before checking their current filesystem and +// Git state. A wide import selects only projects where the requested name exists. +func (c *Catalog) Plan(mode Mode, keys, names []string, report Report, exclude bool) Plan { + if mode != Link && mode != Import { + return Plan{{Status: failed, Reason: "invalid skill operation"}} + } + plan := make(Plan, 0) + names = unique(names) + if len(keys) != 0 || mode == Link { + for _, key := range unique(keys) { + for _, name := range names { + plan = append(plan, c.check(mode, strings.TrimRight(key, "/"), name, exclude)) + } + } + return plan + } + for _, name := range names { + found := false + for _, dir := range report.Dirs { + path := filepath.Join(dir, name) + if _, err := os.Lstat(path); errors.Is(err, fs.ErrNotExist) { + continue + } + found = true + key := scan.ProjectKey(c.repo.Home, dir) + plan = append(plan, c.check(mode, key, name, exclude)) + } + if !found { + plan = append(plan, Change{Name: name, Status: blocked, Reason: "skill was not found in the selected projects"}) + } + } + return plan +} + +func unique(values []string) []string { + result := slices.Clone(values) + slices.Sort(result) + return slices.Compact(result) +} + +func (c *Catalog) check(mode Mode, key, name string, exclude bool) Change { + item := Change{Project: key, Name: name, mode: mode} + dir, err := scan.ProjectDir(c.repo.Home, key) + if err == nil { + dir, err = filepath.EvalSymlinks(dir) + } + if err != nil { + return item.refuse(blocked, err.Error()) + } + if _, inside := relative(c.repo.Root, dir); inside { + return item.refuse(blocked, "the project is inside the canonical checkout") + } + if dir == c.repo.Home { + return item.refuse(blocked, "the home directory holds user-level skills") + } + if scan.ProjectKey(c.repo.Home, filepath.Join(dir, projectDir)) != key { + return item.refuse(blocked, "project path does not match the selected key") + } + item.directory = dir + item.Path = filepath.Join(dir, projectDir, name) + project, err := openAbsolute(dir) + if err != nil { + return item.refuse(failed, err.Error()) + } + defer func() { _ = project.Close() }() + if mode == Link { + return c.checkLink(project, item, exclude) + } + item, _ = c.checkImport(project, item) + return item +} + +func (i Change) refuse(s status, reason string) Change { + i.Status, i.Reason = s, reason + return i +} + +func (c *Catalog) checkLink(project *os.Root, item Change, exclude bool) Change { + dir, err := openDir(project, projectDir, false) + if err == nil { + defer func() { _ = dir.Close() }() + if info, err := dir.Lstat(item.Name); err == nil { + if info.Mode()&fs.ModeSymlink != 0 { + entry, err := c.classifyLink(dir, Entry{Name: item.Name, Project: item.Project}) + if err != nil { + return item.refuse(failed, err.Error()) + } + if entry.State == Linked { + item.Canonical, item.Hash = entry.Counterpart, entry.Hash + return item.refuse(match, "already linked to the canonical skill") + } + } + return item.refuse(blocked, "a directory, file or link already occupies the project path") + } else if !errors.Is(err, fs.ErrNotExist) { + return item.refuse(failed, err.Error()) + } + } else if !errors.Is(err, fs.ErrNotExist) { + return item.refuse(failed, err.Error()) + } + item.Canonical, item.Hash, err = c.counterpart(item.Project, item.Name) + if err != nil { + return item.refuse(failed, err.Error()) + } + if item.Canonical == "" { + return item.refuse(blocked, "no canonical skill exists for this name") + } + return linkGate(item, exclude) +} + +func linkGate(item Change, exclude bool) Change { + item.ExcludeFile, item.Pattern = "", "" + target := scan.InspectSkillPath(item.directory, filepath.Join(projectDir, item.Name)) + switch target.Tracking { + case scan.DirUnknown: + return item.refuse(failed, "git could not inspect the skill path") + case scan.DirTracked: + return item.refuse(blocked, "git has an index or HEAD entry at or under the skill path, or an ancestor file") + case scan.DirNoRepo: + return item.refuse(pending, "create a skill directory symlink") + } + + parent := scan.InspectSkillPath(item.directory, projectDir) + if parent.Tracking == scan.DirUnknown { + return item.refuse(failed, "git could not inspect the project's skills directory") + } + if parent.Tracking == scan.DirTracked && !exclude { + return item.refuse(blocked, "git tracks the skills directory; use --exclude-local for an untracked sibling") + } + if exclude && target.Tracking == scan.DirVisible { + item.ExcludeFile = scan.SkillExcludePath(target.Root) + if item.ExcludeFile == "" { + return item.refuse(failed, "git did not resolve its local exclude file") + } + escaped := strings.NewReplacer("\\", "\\\\", "*", "\\*", "?", "\\?", "[", "\\[", "]", "\\]", " ", "\\ ") + if strings.ContainsAny(target.Rel, "\n\r") { + return item.refuse(failed, "git exclude rules cannot represent this path") + } + item.Pattern = "/" + escaped.Replace(target.Rel) + } + return item.refuse(pending, "create a skill directory symlink") +} + +func (c *Catalog) checkImport(project *os.Root, item Change) (Change, tree) { + dir, err := openDir(project, filepath.Join(projectDir, item.Name), false) + if errors.Is(err, fs.ErrNotExist) { + return item.refuse(blocked, "project skill directory is missing"), nil + } + if err != nil { + return item.refuse(failed, err.Error()), nil + } + defer func() { _ = dir.Close() }() + content, err := readTree(dir) + if err != nil { + return item.refuse(failed, err.Error()), nil + } + if scan.InspectSkillPath(item.directory, filepath.Join(projectDir, item.Name)).Tracking == scan.DirUnknown { + return item.refuse(failed, "git could not inspect the project skill"), nil + } + item.Hash = content.digest() + backup := filepath.Join(c.repo.Root, backupParent(item.Project), item.Name) + target, hash, err := c.counterpart(item.Project, item.Name) + if err != nil { + return item.refuse(failed, err.Error()), nil + } + item.Canonical = backup + if target != "" && item.Hash == hash { + item.Canonical = target + return item.refuse(match, "canonical skill already holds matching content"), nil + } + if target == backup { + return item.refuse(blocked, "the project capture already exists with different content"), nil + } + switch scan.InspectSkillPath(c.repo.Root, filepath.Join(backupParent(item.Project), item.Name)).Tracking { + case scan.DirUnknown: + return item.refuse(failed, "git could not inspect the canonical capture path"), nil + case scan.DirTracked: + return item.refuse(blocked, "the missing capture still has entries in the canonical index or HEAD"), nil + } + return item.refuse(pending, "capture the complete skill directory"), content +} + +func (c *Catalog) Apply(plan Plan, exclude bool) { + for i, item := range plan { + if item.Status != pending { + continue + } + plan[i] = c.applyOne(item, exclude) + } +} + +func (c *Catalog) applyOne(item Change, exclude bool) Change { + project, err := openAbsolute(item.directory) + if err != nil { + return item.refuse(failed, err.Error()) + } + defer func() { _ = project.Close() }() + var current Change + var content tree + switch item.mode { + case Link: + current = c.checkLink(project, item, exclude) + case Import: + current, content = c.checkImport(project, item) + default: + return item.refuse(failed, "invalid skill operation") + } + if current.Status != pending { + return current + } + if item.Hash != current.Hash || item.Canonical != current.Canonical { + return current.refuse(blocked, "skill changed since planning; run again to review the new content") + } + + if item.mode == Link { + err = applyLink(project, current, exclude) + } else { + err = c.applyCapture(current, content) + } + if errors.Is(err, fs.ErrExist) { + return current.refuse(blocked, "the destination appeared while applying") + } + if err != nil { + return current.refuse(failed, err.Error()) + } + return current.refuse(applied, current.Reason) +} + +func applyLink(project *os.Root, item Change, exclude bool) error { + dir, err := openDir(project, projectDir, true) + if err != nil { + return err + } + defer func() { _ = dir.Close() }() + if item.ExcludeFile != "" { + if err := linker.WriteExclude(item.ExcludeFile, item.Pattern); err != nil { + return err + } + } + current := linkGate(item, exclude) + if current.Status != pending { + return errors.New(current.Reason) + } + if current.ExcludeFile != "" { + return errors.New("git has not excluded the skill symlink") + } + return dir.Symlink(item.Canonical, item.Name) +} + +func (c *Catalog) applyCapture(item Change, content tree) error { + parent, err := openDir(c.root, backupParent(item.Project), true) + if err != nil { + return err + } + defer func() { _ = parent.Close() }() + return capture(parent, item.Name, content) +} diff --git a/internal/skills/publish_darwin.go b/internal/skills/publish_darwin.go new file mode 100644 index 0000000..8744113 --- /dev/null +++ b/internal/skills/publish_darwin.go @@ -0,0 +1,7 @@ +package skills + +import "golang.org/x/sys/unix" + +func publish(fd int, temporary, name string) error { + return unix.RenameatxNp(fd, temporary, fd, name, unix.RENAME_EXCL) +} diff --git a/internal/skills/publish_linux.go b/internal/skills/publish_linux.go new file mode 100644 index 0000000..786b6ea --- /dev/null +++ b/internal/skills/publish_linux.go @@ -0,0 +1,7 @@ +package skills + +import "golang.org/x/sys/unix" + +func publish(fd int, temporary, name string) error { + return unix.Renameat2(fd, temporary, fd, name, unix.RENAME_NOREPLACE) +} diff --git a/internal/skills/scan.go b/internal/skills/scan.go new file mode 100644 index 0000000..1d860fb --- /dev/null +++ b/internal/skills/scan.go @@ -0,0 +1,310 @@ +package skills + +import ( + "errors" + "fmt" + "io/fs" + "os" + "path/filepath" + "strings" + + "github.com/agentic-tooling/agentsync/internal/scan" +) + +const ( + Flag = "skill" + projectDir = ".claude/skills" + backupsDir = "project-skills" +) + +// State describes the relationship of a project skill to the canonical repo. +type State string + +const ( + LocalOnly State = "local-only" + Match State = "canonical-match" + Drift State = "content-drift" + Linked State = "linked" + Broken State = "broken" + Foreign State = "foreign" +) + +// Entry is a skill directory or directory symlink found in a project. +type Entry struct { + Project string `json:"project"` + Name string `json:"name"` + Path string `json:"path"` + State State `json:"state"` + Hash string `json:"hash,omitempty"` + Counterpart string `json:"counterpart,omitempty"` + LinkTarget string `json:"link_target,omitempty"` + Reason string `json:"reason,omitempty"` +} + +func (e Entry) NeedsHuman() bool { + return e.State == LocalOnly || e.State == Drift || e.State == Broken +} + +type Report struct { + Entries []Entry `json:"skills,omitempty"` + Problems []scan.ScanProblem `json:"skill_problems,omitempty"` + Dirs []string `json:"-"` +} + +func (r Report) AtRisk() Report { + out := Report{Problems: r.Problems} + for _, entry := range r.Entries { + if entry.NeedsHuman() { + out.Entries = append(out.Entries, entry) + } + } + return out +} + +// Catalog locates the shared and per-project trees beneath one checkout. +type Catalog struct { + repo *scan.Repo + root *os.Root + shared string +} + +func Open(repo *scan.Repo, shared string) (*Catalog, error) { + if shared == "" { + shared = "skills" + } + if resolved, err := filepath.EvalSymlinks(filepath.Join(repo.Root, shared)); err == nil { + var inside bool + shared, inside = relative(repo.Root, resolved) + if !inside || shared == "." { + return nil, fmt.Errorf("skills root %s must stay inside %s", resolved, repo.Root) + } + } + root, err := openAbsolute(repo.Root) + if err != nil { + return nil, err + } + dir, err := openDir(root, shared, false) + if err == nil { + _ = dir.Close() + } else if !errors.Is(err, fs.ErrNotExist) { + _ = root.Close() + return nil, fmt.Errorf("skills root: %w", err) + } + return &Catalog{repo: repo, root: root, shared: shared}, nil +} + +func (c *Catalog) Close() error { return c.root.Close() } + +func relative(root, path string) (string, bool) { + rel, err := filepath.Rel(root, path) + return rel, err == nil && filepath.IsLocal(rel) +} + +func backupParent(key string) string { + return filepath.Join(backupsDir, filepath.FromSlash(key)) +} + +func (c *Catalog) counterpart(key, name string) (string, string, error) { + for _, parent := range []string{backupParent(key), c.shared} { + path := filepath.Join(parent, name) + dir, err := openDir(c.root, path, false) + if errors.Is(err, fs.ErrNotExist) { + continue + } + if err != nil { + return "", "", err + } + content, err := readTree(dir) + _ = dir.Close() + if err != nil { + return "", "", fmt.Errorf("canonical skill %s: %w", path, err) + } + return filepath.Join(c.repo.Root, path), content.digest(), nil + } + return "", "", nil +} + +func (c *Catalog) Scan(root string) (Report, error) { + dirs, problems, err := scan.FindSkillDirs(root) + if err != nil { + return Report{}, err + } + report := c.ScanDirs(dirs) + report.Problems = append(report.Problems, problems...) + return report, nil +} + +func (c *Catalog) ScanDirs(dirs []string) Report { + var report Report + for _, path := range dirs { + project, err := filepath.EvalSymlinks(filepath.Dir(filepath.Dir(path))) + if err != nil { + report.addProblem(path, err) + continue + } + if _, inside := relative(c.repo.Root, project); inside || project == c.repo.Home { + continue + } + path = filepath.Join(project, projectDir) + report.Dirs = append(report.Dirs, path) + report.scanDir(c, path) + } + return report +} + +func (r *Report) scanDir(c *Catalog, path string) { + project := filepath.Dir(filepath.Dir(path)) + anchor, err := openAbsolute(project) + if err != nil { + r.addProblem(path, err) + return + } + defer func() { _ = anchor.Close() }() + dir, err := openDir(anchor, projectDir, false) + if err != nil { + r.addProblem(path, err) + return + } + defer func() { _ = dir.Close() }() + children, err := fs.ReadDir(dir.FS(), ".") + if err != nil { + r.addProblem(path, err) + return + } + key := scan.ProjectKey(c.repo.Home, path) + for _, child := range children { + if !child.IsDir() && child.Type()&fs.ModeSymlink == 0 { + continue + } + entry := Entry{Project: key, Name: child.Name(), Path: filepath.Join(path, child.Name())} + item, err := c.classify(dir, entry) + switch { + case err == errNoManifest: + case err != nil: + r.addProblem(entry.Path, err) + default: + r.Entries = append(r.Entries, item) + } + } +} + +func (r *Report) addProblem(path string, err error) { + r.Problems = append(r.Problems, scan.ScanProblem{Path: path, Err: err.Error()}) +} + +func (c *Catalog) classify(dir *os.Root, e Entry) (Entry, error) { + info, err := dir.Lstat(e.Name) + if err != nil { + return e, err + } + if info.Mode()&fs.ModeSymlink != 0 { + return c.classifyLink(dir, e) + } + child, err := openChild(dir, e.Name, false) + if err != nil { + return e, err + } + defer func() { _ = child.Close() }() + content, err := readTree(child) + if err != nil { + return e, err + } + e.Hash = content.digest() + var hash string + e.Counterpart, hash, err = c.counterpart(e.Project, e.Name) + switch { + case e.Counterpart == "": + e.State = LocalOnly + case e.Hash == hash: + e.State = Match + default: + e.State = Drift + } + return e, err +} + +func (c *Catalog) classifyLink(dir *os.Root, e Entry) (Entry, error) { + target, err := dir.Readlink(e.Name) + if err != nil { + return e, err + } + if !filepath.IsAbs(target) { + target = dir.Name() + string(filepath.Separator) + target + } + e.LinkTarget = target + resolved, missing, err := resolveTarget(target) + if err != nil { + return e, err + } + if missing { + e.State = Foreign + if _, inside := relative(c.repo.Root, resolved); inside { + e.State = Broken + } + return e, nil + } + if _, inside := relative(c.repo.Root, resolved); !inside { + e.State = Foreign + return e, nil + } + e.Counterpart, e.Hash, err = c.counterpart(e.Project, e.Name) + if resolved == e.Counterpart { + e.State = Linked + } else { + e.State = Drift + e.Reason = "link points at a different canonical path" + } + return e, err +} + +// resolveTarget retains the missing suffix while resolving existing ancestors +// and dangling intermediate links so ownership follows the intended path. +func resolveTarget(path string) (string, bool, error) { + suffix := "" + dangling := 0 + for { + resolved, err := filepath.EvalSymlinks(path) + if err == nil { + return filepath.Join(resolved, suffix), suffix != "" || dangling > 0, nil + } + if !errors.Is(err, fs.ErrNotExist) { + return "", false, err + } + probe := strings.TrimRight(path, string(filepath.Separator)) + parent, base := filepath.Split(probe) + if parent == "" { + return "", false, err + } + info, err := os.Lstat(probe) + if err != nil && !errors.Is(err, fs.ErrNotExist) { + return "", false, err + } + if info != nil && info.Mode()&fs.ModeSymlink != 0 { + ref, err := os.Readlink(probe) + if err != nil { + return "", false, err + } + if !filepath.IsAbs(ref) { + ref = parent + ref + } + path = ref + dangling++ + if dangling > 255 { + return "", false, errors.New("too many dangling symlink hops") + } + continue + } + suffix = filepath.Join(base, suffix) + path = parent + } +} + +// ValidateNames accepts directory basenames without normalizing path input. +func ValidateNames(names []string) error { + for _, name := range names { + if !filepath.IsLocal(name) || name == "." || strings.ContainsAny(name, "/\\\x00\n\r") || strings.HasPrefix(name, "-") { + return fmt.Errorf("invalid --skill name %q: expected a directory name", name) + } + } + return nil +} diff --git a/internal/skills/skills_test.go b/internal/skills/skills_test.go new file mode 100644 index 0000000..ff7a067 --- /dev/null +++ b/internal/skills/skills_test.go @@ -0,0 +1,372 @@ +package skills + +import ( + "errors" + "io/fs" + "os" + "os/exec" + "path/filepath" + "strings" + "sync" + "testing" + + "github.com/agentic-tooling/agentsync/internal/scan" + "github.com/agentic-tooling/agentsync/internal/testfs" + "golang.org/x/sys/unix" +) + +func skillRepo(t *testing.T, shared string) (*Catalog, string, string) { + t.Helper() + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git not available") + } + home, err := filepath.EvalSymlinks(t.TempDir()) + if err != nil { + t.Fatal(err) + } + canonical, project := filepath.Join(home, "catalog"), filepath.Join(home, "project") + testfs.GitRepo(t, canonical, "README", "canonical") + testfs.GitRepo(t, project, "README", "project") + t.Setenv("HOME", home) + t.Setenv("GIT_CONFIG_GLOBAL", os.DevNull) + t.Setenv("GIT_CONFIG_NOSYSTEM", "1") + repo, err := scan.NewRepo(canonical) + if err != nil { + t.Fatal(err) + } + c, err := Open(repo, shared) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = c.Close() }) + return c, canonical, project +} + +func write(t *testing.T, path, content string) { + t.Helper() + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte(content), 0o644); err != nil { + t.Fatal(err) + } +} + +func absent(t *testing.T, path string) { + t.Helper() + if _, err := os.Lstat(path); !errors.Is(err, fs.ErrNotExist) { + t.Fatalf("%s was created or could not be checked: %v", path, err) + } +} + +func TestSkillApplyRevalidatesBytes(t *testing.T) { + for _, mode := range []Mode{Link, Import} { + t.Run(string(mode), func(t *testing.T) { + c, repo, project := skillRepo(t, "") + source, dest := filepath.Join(repo, "skills", "build"), filepath.Join(project, projectDir, "build") + if mode == Import { + source, dest = dest, filepath.Join(repo, "project-skills", "project", "build") + } + write(t, filepath.Join(source, "SKILL.md"), "skill") + support := filepath.Join(source, "nested", "support") + write(t, support, "before") + plan := c.Plan(mode, []string{"project"}, []string{"build"}, Report{}, false) + if plan[0].Status != pending { + t.Fatalf("initial plan: %+v", plan) + } + write(t, support, "after") + c.Apply(plan, false) + if plan[0].Status != blocked || plan[0].Reason != "skill changed since planning; run again to review the new content" { + t.Fatalf("changed tree was applied: %+v", plan) + } + absent(t, dest) + }) + } +} + +func TestSkillLinkChecksIndexAgainAtApply(t *testing.T) { + c, repo, project := skillRepo(t, "") + write(t, filepath.Join(repo, "skills", "build", "SKILL.md"), "skill") + plan := c.Plan(Link, []string{"project"}, []string{"build"}, Report{}, true) + if plan[0].Status != pending { + t.Fatalf("initial plan: %+v", plan) + } + dest := filepath.Join(project, projectDir, "build") + write(t, filepath.Join(dest, "tracked"), "stage after planning") + testfs.Git(t, project, "add", "--", projectDir) + if err := os.RemoveAll(dest); err != nil { + t.Fatal(err) + } + c.Apply(plan, true) + if plan[0].Status != blocked { + t.Fatalf("staged path was allowed: %+v", plan) + } + absent(t, dest) +} + +func TestSkillCapturePublicationIsExclusive(t *testing.T) { + physical, err := filepath.EvalSymlinks(t.TempDir()) + if err != nil { + t.Fatal(err) + } + root, err := openAbsolute(physical) + if err != nil { + t.Fatal(err) + } + defer func() { _ = root.Close() }() + if err := root.Mkdir("existing", 0o755); err != nil { + t.Fatal(err) + } + contents := tree{{path: "SKILL.md", mode: 0o644, data: []byte("first")}} + if err := capture(root, "existing", contents); !errors.Is(err, fs.ErrExist) { + t.Fatalf("empty existing destination was replaced: %v", err) + } + absent(t, filepath.Join(physical, "existing", "SKILL.md")) + results := make(chan error, 2) + var wg sync.WaitGroup + for _, text := range []string{"first", "second"} { + wg.Go(func() { results <- capture(root, "contended", tree{{path: "SKILL.md", data: []byte(text)}}) }) + } + wg.Wait() + close(results) + succeeded, occupied := 0, 0 + for err := range results { + switch { + case err == nil: + succeeded++ + case errors.Is(err, fs.ErrExist): + occupied++ + default: + t.Fatal(err) + } + } + if succeeded != 1 || occupied != 1 { + t.Fatalf("exclusive captures: successful=%d, occupied=%d", succeeded, occupied) + } + children, err := fs.ReadDir(root.FS(), ".") + if err != nil || len(children) != 2 { + t.Fatalf("temporary captures left behind: %v, %v", children, err) + } +} + +func TestSkillTreesRejectNestedSpecialFiles(t *testing.T) { + c, repo, project := skillRepo(t, "") + source := filepath.Join(project, projectDir, "build") + write(t, filepath.Join(source, "SKILL.md"), "skill") + if err := unix.Mkfifo(filepath.Join(source, "pipe"), 0o600); err != nil { + t.Fatal(err) + } + plan := c.Plan(Import, []string{"project"}, []string{"build"}, Report{}, false) + c.Apply(plan, false) + if plan[0].Status != failed { + t.Fatalf("special file accepted: %+v", plan) + } + absent(t, filepath.Join(repo, "project-skills")) +} + +func TestSkillPathsRefuseAncestorSymlinks(t *testing.T) { + c, repo, project := skillRepo(t, "") + outside := t.TempDir() + write(t, filepath.Join(repo, "skills", "build", "SKILL.md"), "canonical") + if err := os.Symlink(outside, filepath.Join(project, ".claude")); err != nil { + t.Fatal(err) + } + plan := c.Plan(Link, []string{"project"}, []string{"build"}, Report{}, false) + c.Apply(plan, false) + if plan[0].Status != failed || !strings.Contains(plan[0].Reason, ".claude is not a real directory") { + t.Fatalf("escaped project path accepted: %+v", plan) + } + absent(t, filepath.Join(outside, "skills")) + if err := os.Remove(filepath.Join(project, ".claude")); err != nil { + t.Fatal(err) + } + write(t, filepath.Join(project, projectDir, "build", "SKILL.md"), "custom") + if err := os.Symlink(outside, filepath.Join(repo, "project-skills")); err != nil { + t.Fatal(err) + } + plan = c.Plan(Import, []string{"project"}, []string{"build"}, Report{}, false) + c.Apply(plan, false) + if plan[0].Status != failed || !strings.Contains(plan[0].Reason, "project-skills is not a real directory") { + t.Fatalf("escaped capture path accepted: %+v", plan) + } + absent(t, filepath.Join(outside, "project")) +} + +func TestSkillLinkConfirmsIgnoreRule(t *testing.T) { + c, repo, project := skillRepo(t, "") + write(t, filepath.Join(repo, "skills", "build", "SKILL.md"), "skill") + write(t, filepath.Join(project, ".gitignore"), "!/.claude/skills/build\n") + plan := c.Plan(Link, []string{"project"}, []string{"build"}, Report{}, true) + if plan[0].Status != pending { + t.Fatalf("initial plan: %+v", plan) + } + c.Apply(plan, true) + if plan[0].Status != failed || plan[0].Reason != "git has not excluded the skill symlink" { + t.Fatalf("negated ignore rule authorized link: %+v", plan) + } + absent(t, filepath.Join(project, projectDir, "build")) +} + +func TestSkillImportKeepsMissingTrackedCanonicalCapture(t *testing.T) { + c, repo, project := skillRepo(t, "") + write(t, filepath.Join(project, projectDir, "build", "SKILL.md"), "custom") + dest := filepath.Join(repo, "project-skills", "project", "build") + write(t, filepath.Join(dest, "SKILL.md"), "captured") + testfs.Git(t, repo, "add", "--", "project-skills") + testfs.Git(t, repo, "commit", "-qm", "capture") + testfs.Git(t, repo, "rm", "-rf", "--", "project-skills") + plan := c.Plan(Import, []string{"project"}, []string{"build"}, Report{}, false) + c.Apply(plan, false) + if plan[0].Status != blocked { + t.Fatalf("tracked canonical capture was replaced: %+v", plan) + } + absent(t, dest) +} + +func TestSkillScanClassifiesProjectDirectories(t *testing.T) { + c, repo, project := skillRepo(t, "") + base := filepath.Join(project, projectDir) + canonical := filepath.Join(repo, "skills", "matching") + write(t, filepath.Join(canonical, "SKILL.md"), "skill") + write(t, filepath.Join(base, "matching", "SKILL.md"), "skill") + write(t, filepath.Join(base, "standalone", "SKILL.md"), "local") + write(t, filepath.Join(base, "notes"), "ordinary file") + write(t, filepath.Join(base, "missing-manifest", "notes"), "ordinary directory") + write(t, filepath.Join(base, "invalid-manifest", "SKILL.md", "notes"), "directory manifest") + for name, target := range map[string]string{ + "broken": filepath.Join(repo, "absent"), "other": t.TempDir(), "wrong-name": canonical, + } { + if err := os.Symlink(target, filepath.Join(base, name)); err != nil { + t.Fatal(err) + } + } + foreign := filepath.Join(filepath.Dir(project), "foreign", projectDir) + if err := os.MkdirAll(filepath.Dir(foreign), 0o755); err != nil { + t.Fatal(err) + } + if err := os.Symlink(base, foreign); err != nil { + t.Fatal(err) + } + write(t, filepath.Join(repo, projectDir, "own-project", "SKILL.md"), "ignore canonical") + report, err := c.Scan(filepath.Dir(project)) + if err != nil { + t.Fatal(err) + } + want := map[string]State{"matching": Match, "standalone": LocalOnly, "broken": Broken, "other": Foreign, "wrong-name": Drift} + if len(report.Entries) != len(want) || len(report.Problems) != 2 || len(report.AtRisk().Entries) != 3 { + t.Fatalf("unexpected report: %+v", report) + } + for _, row := range report.Entries { + if row.State != want[row.Name] { + t.Errorf("%s: got %s, want %s", row.Name, row.State, want[row.Name]) + } + } +} + +func TestSkillLinkAndCaptureReportRepeatedRuns(t *testing.T) { + c, repo, project := skillRepo(t, "") + write(t, filepath.Join(repo, "skills", "shared", "SKILL.md"), "canonical") + link := c.Plan(Link, []string{"project"}, []string{"shared", "shared"}, Report{}, false) + c.Apply(link, false) + if pending, written, blocked, failed := link.Counts(); pending != 0 || written != 1 || blocked != 0 || failed != 0 { + t.Fatalf("link counts: %+v", link) + } + dest := filepath.Join(project, projectDir, "shared") + if target, err := os.Readlink(dest); err != nil || target != link[0].Canonical { + t.Fatalf("link = %q, %v; want %s", target, err, link[0].Canonical) + } + if repeat := c.Plan(Link, []string{"project"}, []string{"shared"}, Report{}, false); repeat[0].Status != match { + t.Fatalf("repeated link: %+v", repeat) + } + write(t, filepath.Join(project, projectDir, "local", "SKILL.md"), "local") + write(t, filepath.Join(project, projectDir, "local", "nested", "script"), "support") + report, err := c.Scan(filepath.Dir(project)) + if err != nil { + t.Fatal(err) + } + plan := c.Plan(Import, nil, []string{"local", "absent"}, report, false) + if pending, _, blocked, _ := plan.Counts(); pending != 1 || blocked != 1 || len(plan.Dirs()) != 1 { + t.Fatalf("wide import counts: %+v", plan) + } + c.Apply(plan, false) + if data, err := os.ReadFile(filepath.Join(repo, "project-skills", "project", "local", "nested", "script")); err != nil || string(data) != "support" { + t.Fatalf("captured script = %q, %v", data, err) + } + if repeat := c.Plan(Import, []string{"project"}, []string{"local"}, Report{}, false); repeat[0].Status != match { + t.Fatalf("repeated import: %+v", repeat) + } +} + +func TestSkillModeAndNamesRefuseInvalidInputs(t *testing.T) { + c, _, project := skillRepo(t, "") + plan := c.Plan("", []string{"project"}, []string{"bad"}, Report{}, false) + if len(plan) != 1 || plan[0].Status != failed { + t.Fatalf("invalid mode planned an operation: %+v", plan) + } + plan = Plan{{directory: project, Status: pending}} + c.Apply(plan, false) + if plan[0].Status != failed { + t.Fatalf("invalid mode authorized a write: %+v", plan) + } + for _, name := range []string{".", "../foreign", "some/name", "a\\b", "-apply"} { + if err := ValidateNames([]string{name}); err == nil { + t.Fatalf("accepted invalid name %q", name) + } + } + if err := ValidateNames([]string{"a name", "normal"}); err != nil { + t.Fatal(err) + } +} + +func TestSkillCapturePreparationFailureLeavesNoDestination(t *testing.T) { + dir, err := filepath.EvalSymlinks(t.TempDir()) + if err != nil { + t.Fatal(err) + } + root, err := openAbsolute(dir) + if err != nil { + t.Fatal(err) + } + defer func() { _ = root.Close() }() + files := tree{{path: "SKILL.md"}, {path: "absent/support"}} + if err := capture(root, "build", files); err == nil { + t.Fatal("failed capture published a partial directory") + } + if children, err := fs.ReadDir(root.FS(), "."); err != nil || len(children) != 0 { + t.Fatalf("failed capture left files visible: %v, %v", children, err) + } +} + +func TestResolveSkillLinkTargets(t *testing.T) { + base, err := filepath.EvalSymlinks(t.TempDir()) + if err != nil { + t.Fatal(err) + } + physical := filepath.Join(base, "outside", "nested") + if err := os.MkdirAll(physical, 0o755); err != nil { + t.Fatal(err) + } + for name, target := range map[string]string{ + "alias": physical, "relative-dangling": "outside/removed", "loop": "loop", + } { + if err := os.Symlink(target, filepath.Join(base, name)); err != nil { + t.Fatal(err) + } + } + for _, test := range []struct { + target, want string + missing bool + }{ + {"alias", physical, false}, + {"alias/../removed", filepath.Join(base, "outside", "removed"), true}, + {"relative-dangling/support", filepath.Join(base, "outside", "removed", "support"), true}, + } { + got, missing, err := resolveTarget(base + "/" + test.target) + if err != nil || got != test.want || missing != test.missing { + t.Fatalf("%s resolved to %q, missing=%v, %v; want %q, missing=%v", test.target, got, missing, err, test.want, test.missing) + } + } + if _, _, err := resolveTarget(filepath.Join(base, "loop")); err == nil { + t.Fatal("cyclic link resolved") + } +} diff --git a/internal/skills/tree.go b/internal/skills/tree.go new file mode 100644 index 0000000..f4ed36b --- /dev/null +++ b/internal/skills/tree.go @@ -0,0 +1,177 @@ +// Package skills inspects and provisions project-local Claude skill trees. +package skills + +import ( + "crypto/sha256" + "encoding/binary" + "encoding/hex" + "errors" + "fmt" + "io" + "io/fs" + "os" + "path/filepath" + "slices" + "strings" + + "golang.org/x/sys/unix" +) + +type node struct { + path string + mode fs.FileMode + data []byte +} + +type tree []node + +// openDir holds each opened directory while descending so resolution stays +// beneath the original root even if an ancestor is renamed during the walk. +func openDir(root *os.Root, path string, create bool) (*os.Root, error) { + parent := root + for component := range strings.SplitSeq(path, string(filepath.Separator)) { + next, err := openChild(parent, component, create) + if parent != root { + _ = parent.Close() + } + if err != nil { + return nil, err + } + parent = next + } + return parent, nil +} + +func openChild(parent *os.Root, name string, create bool) (*os.Root, error) { + info, err := parent.Lstat(name) + if create && errors.Is(err, fs.ErrNotExist) { + if err := parent.Mkdir(name, 0o755); err != nil && !errors.Is(err, fs.ErrExist) { + return nil, err + } + info, err = parent.Lstat(name) + } + if err != nil { + return nil, err + } + if !info.IsDir() { + return nil, fmt.Errorf("%s is not a real directory", filepath.Join(parent.Name(), name)) + } + child, err := parent.OpenRoot(name) + if err != nil { + return nil, err + } + current, err := child.Stat(".") + if err != nil || !os.SameFile(info, current) { + _ = child.Close() + return nil, fmt.Errorf("%s changed while opening it", child.Name()) + } + return child, nil +} + +var errNoManifest = errors.New("no regular SKILL.md") + +func readTree(root *os.Root) (tree, error) { + info, err := root.Lstat("SKILL.md") + if errors.Is(err, fs.ErrNotExist) { + return nil, errNoManifest + } + if err != nil { + return nil, err + } + if !info.Mode().IsRegular() { + return nil, fmt.Errorf("%s: %w", root.Name(), errNoManifest) + } + var result tree + if err := readNodes(root, "", &result); err != nil { + return nil, err + } + if !slices.ContainsFunc(result, func(n node) bool { return n.path == "SKILL.md" && n.mode.IsRegular() }) { + return nil, fmt.Errorf("%s changed while reading SKILL.md", root.Name()) + } + slices.SortFunc(result, func(a, b node) int { return strings.Compare(a.path, b.path) }) + return result, nil +} + +func openAbsolute(path string) (*os.Root, error) { + root, err := os.OpenRoot(string(filepath.Separator)) + if err != nil || path == string(filepath.Separator) { + return root, err + } + defer func() { _ = root.Close() }() + return openDir(root, strings.TrimPrefix(path, string(filepath.Separator)), false) +} + +func readNodes(root *os.Root, prefix string, result *tree) error { + entries, err := fs.ReadDir(root.FS(), ".") + if err != nil { + return fmt.Errorf("reading %s: %w", root.Name(), err) + } + for _, entry := range entries { + name := entry.Name() + n := node{path: filepath.Join(prefix, name)} + if entry.IsDir() { + child, err := openChild(root, name, false) + if err != nil { + return err + } + n.mode = fs.ModeDir + err = readNodes(child, n.path, result) + _ = child.Close() + if err != nil { + return err + } + } else { + info, err := entry.Info() + if err != nil { + return err + } + if !info.Mode().IsRegular() { + return fmt.Errorf("%s is not a regular file or directory", filepath.Join(root.Name(), name)) + } + n.mode = info.Mode() + n.data, err = readRegular(root, name, info) + if err != nil { + return fmt.Errorf("reading %s: %w", filepath.Join(root.Name(), name), err) + } + } + *result = append(*result, n) + } + return nil +} + +func readRegular(root *os.Root, name string, expected fs.FileInfo) ([]byte, error) { + // Nonblocking open keeps a FIFO swapped in after lstat from hanging a run. + f, err := root.OpenFile(name, os.O_RDONLY|unix.O_NOFOLLOW|unix.O_NONBLOCK, 0) + if err != nil { + return nil, err + } + defer func() { _ = f.Close() }() + info, err := f.Stat() + if err != nil { + return nil, err + } + if !info.Mode().IsRegular() || !os.SameFile(expected, info) { + return nil, errors.New("file changed while opening it") + } + return io.ReadAll(f) +} + +func (t tree) digest() string { + h := sha256.New() + var length [8]byte + for _, n := range t { + path := filepath.ToSlash(n.path) + kind := byte('f') + if n.mode.IsDir() { + kind = 'd' + } + _, _ = h.Write([]byte{kind}) + binary.BigEndian.PutUint64(length[:], uint64(len(path))) + _, _ = h.Write(length[:]) + _, _ = io.WriteString(h, path) + binary.BigEndian.PutUint64(length[:], uint64(len(n.data))) + _, _ = h.Write(length[:]) + _, _ = h.Write(n.data) + } + return hex.EncodeToString(h.Sum(nil)) +}