From d1371db7ea2f924541b24b4bff8022975f91ecad Mon Sep 17 00:00:00 2001 From: Willie Date: Mon, 5 Oct 2026 11:29:39 +0800 Subject: [PATCH] fix(audit): keep the skills source when auditing agents only (#412) * fix(audit): keep the skills source when auditing agents only `skillshare audit --agents` replaced sourcePath with the agents source before building auditTUIContext, so the TUI's Skills tab discovered skills inside the agents directory. The agents-only branches now read agentsSourcePath themselves and the skills source is left alone. Also from the #390 review, independent of .skillfollow: - /api/skillignore reads the source through skillsSource() like the other routes (equal in project mode today; consistency only). - scripts/windows/e2e-file-links.ps1 checked doctor output for a "Config:" line that no longer exists and could not match the ~-shortened path, so the run always aborted. It now checks status --json source.path, as e2e-skillfollow.ps1 does. - windows-test runs internal/sourcewalk; GOOS=windows vet passes and the job on this PR is the first real run. Refs #404, #407. * fix(audit): relativize agent names against the agents source With discovery moved to the agents source, auditInstalled and auditFiltered still printed the header and computed SkillName against the skills source, so `audit agents --json` reported names such as `../agents/helper.md` when the two sources are siblings (Codex review on #412). auditScanRoot now picks the root per kind for discovery, header, and relativization; the skills source is kept only for TUI tab switching. Also drops internal/sourcewalk from windows-test again: the first run on windows-latest fails TestWalkParity, TestWalkDirParity and TestSymlinkRoot (callback paths come from the canonical root, so filepath.Rel against the test's root yields `..\..` chains). Logged on #344. --- cmd/skillshare/audit.go | 43 +++++++++------------ cmd/skillshare/audit_agents_source_test.go | 42 ++++++++++++++++++++ internal/server/handler_skillignore.go | 4 +- internal/server/handler_skillignore_test.go | 18 +++++++++ scripts/windows/e2e-file-links.ps1 | 11 +++--- 5 files changed, 87 insertions(+), 31 deletions(-) create mode 100644 cmd/skillshare/audit_agents_source_test.go diff --git a/cmd/skillshare/audit.go b/cmd/skillshare/audit.go index 5cfeb7ef9..2b0c4853c 100644 --- a/cmd/skillshare/audit.go +++ b/cmd/skillshare/audit.go @@ -205,11 +205,6 @@ func cmdAudit(args []string) error { cfgPath = config.ConfigPath() } - // When kind is agents-only, override sourcePath to the agents source directory. - if kind == kindAgents && agentsSourcePath != "" { - sourcePath = agentsSourcePath - } - policy := audit.ResolvePolicy(audit.PolicyInputs{ Profile: opts.Profile, Threshold: opts.Threshold, @@ -241,7 +236,7 @@ func cmdAudit(args []string) error { case isSinglePath: results, summary, err = auditPath(opts.Targets[0], modeString(mode), projectRoot, threshold, opts.Format, opts.PolicyLine, registry) case isSingleName: - results, summary, err = auditSkillByName(sourcePath, opts.Targets[0], modeString(mode), projectRoot, threshold, opts.Format, opts.PolicyLine, kind, registry) + results, summary, err = auditSkillByName(auditScanRoot(kind, sourcePath, agentsSourcePath), opts.Targets[0], modeString(mode), projectRoot, threshold, opts.Format, opts.PolicyLine, kind, registry) default: results, summary, err = auditFiltered(sourcePath, agentsSourcePath, opts.Targets, opts.Groups, modeString(mode), projectRoot, threshold, kind, opts, registry) } @@ -499,6 +494,16 @@ func discoverForKind(kind resourceKindFilter, sourcePath string) ([]auditSkillRe return collectInstalledSkillPaths(sourcePath) } +// auditScanRoot picks the source a kind-filtered scan reads and relativizes +// against. The skills source is kept apart so the TUI can scan the other kind +// when switching tabs. +func auditScanRoot(kind resourceKindFilter, sourcePath, agentsSourcePath string) string { + if kind == kindAgents { + return agentsSourcePath + } + return sourcePath +} + func toInputsForKind(kind resourceKindFilter, items []auditSkillRef) []audit.SkillInput { return toAuditInputs(items, kind == kindAgents) } @@ -531,13 +536,8 @@ func auditInstalled(sourcePath, agentsSourcePath, mode, projectRoot, threshold s if !jsonOutput { spinner = ui.StartSpinner(fmt.Sprintf("Discovering %s...", kind.Noun(2))) } - var skillPaths []auditSkillRef - var err error - if kind == kindAgents { - skillPaths, err = collectInstalledAgentPaths(sourcePath) - } else { - skillPaths, err = collectInstalledSkillPaths(sourcePath) - } + scanRoot := auditScanRoot(kind, sourcePath, agentsSourcePath) + skillPaths, err := discoverForKind(kind, scanRoot) if err != nil { if spinner != nil { spinner.Fail("Discovery failed") @@ -570,7 +570,7 @@ func auditInstalled(sourcePath, agentsSourcePath, mode, projectRoot, threshold s // Print the header before scan so user sees context while waiting. if !jsonOutput { - printAuditHeader(mode, sourcePath, threshold, opts.PolicyLine) + printAuditHeader(mode, scanRoot, threshold, opts.PolicyLine) } // Phase 1: parallel scan with progress bar. @@ -606,7 +606,7 @@ func auditInstalled(sourcePath, agentsSourcePath, mode, projectRoot, threshold s sr.Result.IsBlocked = sr.Result.HasSeverityAtOrAbove(threshold) sr.Result.Kind = kind.SingularNoun() // Use relative path so TUI shows group hierarchy (e.g. "frontend/vue/skill"). - if rel, err := filepath.Rel(sourcePath, sr.Result.ScanTarget); err == nil && rel != sr.Result.SkillName { + if rel, err := filepath.Rel(scanRoot, sr.Result.ScanTarget); err == nil && rel != sr.Result.SkillName { sr.Result.SkillName = rel } results = append(results, sr.Result) @@ -649,13 +649,8 @@ func auditFiltered(sourcePath, agentsSourcePath string, names, groups []string, Threshold: threshold, } - var allSkills []auditSkillRef - var err error - if kind == kindAgents { - allSkills, err = collectInstalledAgentPaths(sourcePath) - } else { - allSkills, err = collectInstalledSkillPaths(sourcePath) - } + scanRoot := auditScanRoot(kind, sourcePath, agentsSourcePath) + allSkills, err := discoverForKind(kind, scanRoot) if err != nil { return nil, base, err } @@ -715,7 +710,7 @@ func auditFiltered(sourcePath, agentsSourcePath string, names, groups []string, // Print the header before scan so user sees context while waiting. scanStart := time.Now() if !jsonOutput { - printAuditHeader(mode, sourcePath, threshold, opts.PolicyLine) + printAuditHeader(mode, scanRoot, threshold, opts.PolicyLine) } // Phase 1: parallel scan with progress bar. @@ -750,7 +745,7 @@ func auditFiltered(sourcePath, agentsSourcePath string, names, groups []string, sr.Result.Threshold = threshold sr.Result.IsBlocked = sr.Result.HasSeverityAtOrAbove(threshold) sr.Result.Kind = kind.SingularNoun() - if rel, err := filepath.Rel(sourcePath, sr.Result.ScanTarget); err == nil && rel != sr.Result.SkillName { + if rel, err := filepath.Rel(scanRoot, sr.Result.ScanTarget); err == nil && rel != sr.Result.SkillName { sr.Result.SkillName = rel } results = append(results, sr.Result) diff --git a/cmd/skillshare/audit_agents_source_test.go b/cmd/skillshare/audit_agents_source_test.go new file mode 100644 index 000000000..5fd2890c1 --- /dev/null +++ b/cmd/skillshare/audit_agents_source_test.go @@ -0,0 +1,42 @@ +package main + +import ( + "os" + "path/filepath" + "strings" + "testing" + + "skillshare/internal/audit" +) + +// The agents-only audit must read the agents source itself: the caller keeps +// the skills source untouched so the TUI's Skills tab scans skills, not agents. +func TestAuditInstalled_AgentsKindScansAgentsSource(t *testing.T) { + skills := t.TempDir() + agents := t.TempDir() + if err := os.MkdirAll(filepath.Join(skills, "demo"), 0755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(skills, "demo", "SKILL.md"), []byte("---\nname: demo\n---\n# demo\n"), 0644); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(agents, "helper.md"), []byte("---\nname: helper\n---\n# helper\n"), 0644); err != nil { + t.Fatal(err) + } + + var results []*audit.Result + captureStdout(t, func() { + var err error + results, _, err = auditInstalled(skills, agents, "global", "", "medium", kindAgents, auditOptions{Format: formatJSON}, audit.DefaultRegistry()) + if err != nil { + t.Errorf("auditInstalled: %v", err) + } + }) + + if len(results) != 1 || results[0].Kind != "agent" || !strings.HasPrefix(results[0].ScanTarget, agents) { + t.Fatalf("expected the one agent from the agents source, got %+v", results) + } + if results[0].SkillName != "helper.md" { + t.Fatalf("expected the name relative to the agents source, got %q", results[0].SkillName) + } +} diff --git a/internal/server/handler_skillignore.go b/internal/server/handler_skillignore.go index 85ffbd217..258ab989b 100644 --- a/internal/server/handler_skillignore.go +++ b/internal/server/handler_skillignore.go @@ -45,7 +45,7 @@ func readIgnoreLocal(ignorePath string) *ignoreLocalFile { func (s *Server) handleGetSkillignore(w http.ResponseWriter, r *http.Request) { // Snapshot source path under RLock, then release before I/O. s.mu.RLock() - source := s.cfg.EffectiveSkillsSource() + source := s.skillsSource() s.mu.RUnlock() ignorePath := filepath.Join(source, ".skillignore") @@ -96,7 +96,7 @@ func (s *Server) handlePutSkillignore(w http.ResponseWriter, r *http.Request) { return } - source := s.cfg.EffectiveSkillsSource() + source := s.skillsSource() ignorePath := filepath.Join(source, ".skillignore") if body.Raw == "" { diff --git a/internal/server/handler_skillignore_test.go b/internal/server/handler_skillignore_test.go index d52ed5fe3..5b4e5fadb 100644 --- a/internal/server/handler_skillignore_test.go +++ b/internal/server/handler_skillignore_test.go @@ -6,6 +6,7 @@ import ( "net/http/httptest" "os" "path/filepath" + "strings" "testing" ) @@ -41,3 +42,20 @@ func TestHandleGetSkillignore_NoLocalFile(t *testing.T) { t.Fatalf("expected no local file, got %+v", resp.Local) } } + +// In project mode the route serves the project skills source; the handler now +// reads it through skillsSource() like the other source-reading routes. +func TestHandleGetSkillignore_ProjectModeUsesProjectSource(t *testing.T) { + s, projectRoot := newTestProjectServerWithExtras(t, nil) + + rr := httptest.NewRecorder() + s.handler.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "/api/skillignore", nil)) + + var resp skillignoreResponse + if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil { + t.Fatalf("decode: %v (body=%s)", err, rr.Body.String()) + } + if !strings.HasPrefix(resp.Path, projectRoot+string(filepath.Separator)) { + t.Fatalf("expected .skillignore under the project source %s, got %s", projectRoot, resp.Path) + } +} diff --git a/scripts/windows/e2e-file-links.ps1 b/scripts/windows/e2e-file-links.ps1 index 87af26b11..b501c8812 100644 --- a/scripts/windows/e2e-file-links.ps1 +++ b/scripts/windows/e2e-file-links.ps1 @@ -90,11 +90,12 @@ extras: - path: '$H\.codex' "@ | Set-Content "$env:APPDATA\skillshare\config.yaml" -Section 'isolation check (doctor Config line)' -$doc = & $Exe doctor 2>&1 | ForEach-Object { "$_" } -$cfgLine = ($doc | Select-String 'Config:' | Select-Object -First 1) -join '' -Log $cfgLine.Trim() -if ($cfgLine -notlike "*$Root*") { Log 'ABORT: config is not under the test root'; Log 'DONE'; exit 1 } +# status --json reports the source from the loaded config; it matches only the config written above. +# (doctor prints the config path shortened to ~, so its text cannot prove the path is under $Root.) +Section 'isolation check (status --json source.path)' +$st = try { (& $Exe status --json 2> "$env:TEMP\stderr.txt" | Out-String) | ConvertFrom-Json } catch { $null } +Log "source.path=$($st.source.path)" +if (-not $st -or ($st.source.path.TrimEnd('\') -ine "$S\skills")) { Log 'ABORT: config is not under the test root'; Log 'DONE'; exit 1 } $paths = @("$H\.claude\skills\demo-skill", "$H\.claude\agents\demo-agent.md", "$H\.claude\rules\style.md", "$H\.codex\AGENTS.md") $manifests = @("$H\.claude\skills", "$H\.claude\agents", "$H\.claude\rules", "$H\.codex")