From 113d410da7afffa3e82e31ca509331ebdfb2bf30 Mon Sep 17 00:00:00 2001 From: Willie Date: Mon, 5 Oct 2026 22:03:47 +0800 Subject: [PATCH] fix(hub): apply the source follow policy to local discovery Hub index, built-in search, and draft candidates discovered only the source path, dropping the follow policy used by the inventory. Add optional walk options to the shared Hub functions and pass the operation policy from dashboard handlers and CLI indexing. Explicit CLI sources use the active config policy while retaining configuration-free indexing. Cover global/project modes, enabled/disabled following, all three handlers, and CLI source overrides; document the behavior in all website locales and built-in guidance. --- cmd/skillshare/hub.go | 39 +++++--- cmd/skillshare/search_hub_test.go | 95 ++++++++++++++++++- internal/hub/draft_candidates.go | 5 +- internal/hub/index.go | 5 +- internal/server/handler_hub.go | 2 +- internal/server/handler_hub_drafts.go | 2 +- internal/server/handler_hub_test.go | 75 +++++++++++++++ internal/server/handler_search.go | 7 +- skills/skillshare/SKILL.md | 3 + website/docs/reference/commands/hub.md | 2 + .../current/reference/commands/hub.md | 2 + .../current/reference/commands/hub.md | 2 + .../current/reference/commands/hub.md | 2 + .../current/reference/commands/hub.md | 2 + 14 files changed, 218 insertions(+), 25 deletions(-) diff --git a/cmd/skillshare/hub.go b/cmd/skillshare/hub.go index e97f8161f..65be36225 100644 --- a/cmd/skillshare/hub.go +++ b/cmd/skillshare/hub.go @@ -8,6 +8,7 @@ import ( "skillshare/internal/config" "skillshare/internal/hub" + "skillshare/internal/sourcewalk" "skillshare/internal/ui" ) @@ -149,13 +150,10 @@ func cmdHubIndex(args []string) error { i++ } - // Resolve source path - if sourcePath == "" { - resolved, err := resolveSourcePath(mode, cwd) - if err != nil { - return err - } - sourcePath = resolved + // Resolve the source and its operation policy, including an explicit source override. + sourcePath, walk, err := resolveHubSource(mode, cwd, sourcePath) + if err != nil { + return err } // Resolve output path @@ -165,7 +163,7 @@ func cmdHubIndex(args []string) error { start := time.Now() sp := ui.StartSpinner("Scanning source directory...") - idx, err := hub.BuildIndex(sourcePath, full, auditSkills) + idx, err := hub.BuildIndex(sourcePath, full, auditSkills, walk) sp.Stop() if err != nil { return fmt.Errorf("failed to build index: %w", err) @@ -191,21 +189,32 @@ func cmdHubIndex(args []string) error { return nil } -// resolveSourcePath determines the source directory based on mode. -func resolveSourcePath(mode runMode, cwd string) (string, error) { +// resolveHubSource uses the active config's policy, retaining config-free --source use. +func resolveHubSource(mode runMode, cwd, sourcePath string) (string, sourcewalk.Options, error) { if mode == modeProject { rt, err := loadProjectRuntime(cwd) if err != nil { - return "", fmt.Errorf("failed to load project config: %w", err) + if sourcePath != "" { + return sourcePath, sourcewalk.Options{}, nil + } + return "", sourcewalk.Options{}, fmt.Errorf("failed to load project config: %w", err) } - return rt.sourcePath, nil + if sourcePath == "" { + sourcePath = rt.sourcePath + } + return sourcePath, config.SkillsWalk(rt.config.FollowSourceLinks, sourcePath, rt.targets), nil } - cfg, err := config.Load() if err != nil { - return "", fmt.Errorf("failed to load config: %w", err) + if sourcePath != "" { + return sourcePath, sourcewalk.Options{}, nil + } + return "", sourcewalk.Options{}, fmt.Errorf("failed to load config: %w", err) } - return cfg.EffectiveSkillsSource(), nil + if sourcePath == "" { + sourcePath = cfg.EffectiveSkillsSource() + } + return sourcePath, config.SkillsWalk(cfg.FollowSourceLinks, sourcePath, cfg.Targets), nil } func printHubHelp() { diff --git a/cmd/skillshare/search_hub_test.go b/cmd/skillshare/search_hub_test.go index bd3a506d5..2b6d095b6 100644 --- a/cmd/skillshare/search_hub_test.go +++ b/cmd/skillshare/search_hub_test.go @@ -1,6 +1,15 @@ package main -import "testing" +import ( + "encoding/json" + "fmt" + "os" + "path/filepath" + "testing" + + "skillshare/internal/config" + "skillshare/internal/hub" +) func TestLooksLikeURLOrPath_SSH(t *testing.T) { cases := map[string]bool{ @@ -18,3 +27,87 @@ func TestLooksLikeURLOrPath_SSH(t *testing.T) { } } } + +func TestHubIndexFollowedSourceLinks(t *testing.T) { + for _, project := range []bool{false, true} { + for _, follow := range []bool{false, true} { + for _, explicit := range []bool{false, true} { + t.Run(fmt.Sprintf("project=%t/follow=%t/explicit=%t", project, follow, explicit), func(t *testing.T) { + root := t.TempDir() + t.Chdir(root) + t.Setenv("SKILLSHARE_CONFIG", filepath.Join(root, "config.yaml")) + source := filepath.Join(root, "skills") + cfg := &config.Config{Source: source, FollowSourceLinks: follow} + if err := cfg.Save(); err != nil { + t.Fatal(err) + } + if project { + cfg := &config.ProjectConfig{Sources: config.ProjectSources{Skills: source}, FollowSourceLinks: follow} + if err := cfg.Save(root); err != nil { + t.Fatal(err) + } + } + if explicit { + source = filepath.Join(root, "custom-source") + } + checkout := t.TempDir() + for _, dir := range []string{source, filepath.Join(checkout, "linked-skill")} { + if err := os.MkdirAll(dir, 0755); err != nil { + t.Fatal(err) + } + } + if err := os.WriteFile(filepath.Join(checkout, "linked-skill", "SKILL.md"), []byte("# Linked skill\n"), 0644); err != nil { + t.Fatal(err) + } + if err := os.Symlink(checkout, filepath.Join(source, "linked")); err != nil { + t.Fatal(err) + } + output := filepath.Join(root, "index.json") + args := []string{"-g", "-o", output} + if project { + args[0] = "-p" + } + if explicit { + args = append(args, "--source", source) + } + captureStdoutStderr(t, func() { + if err := cmdHubIndex(args); err != nil { + t.Errorf("hub index: %v", err) + } + }) + data, err := os.ReadFile(output) + if err != nil { + t.Fatal(err) + } + var index hub.Index + if err := json.Unmarshal(data, &index); err != nil { + t.Fatal(err) + } + want := 0 + if follow { + want = 1 + } + if len(index.Skills) != want || (follow && index.Skills[0].Name != "linked-skill") { + t.Fatalf("follow=%t index=%s", follow, data) + } + }) + } + } + } +} + +func TestHubIndexExplicitSourceWithoutConfig(t *testing.T) { + root := t.TempDir() + t.Setenv("SKILLSHARE_CONFIG", filepath.Join(root, "missing.yaml")) + if err := os.WriteFile(filepath.Join(root, "SKILL.md"), []byte("# Skill\n"), 0644); err != nil { + t.Fatal(err) + } + captureStdoutStderr(t, func() { + if err := cmdHubIndex([]string{"-g", "--source", root}); err != nil { + t.Errorf("explicit source requires config: %v", err) + } + }) + if _, err := os.Stat(filepath.Join(root, "skillshare-hub.json")); err != nil { + t.Fatal(err) + } +} diff --git a/internal/hub/draft_candidates.go b/internal/hub/draft_candidates.go index 9f200319b..742c3a36c 100644 --- a/internal/hub/draft_candidates.go +++ b/internal/hub/draft_candidates.go @@ -6,14 +6,15 @@ import ( "sort" "skillshare/internal/install" + "skillshare/internal/sourcewalk" ssync "skillshare/internal/sync" "skillshare/internal/utils" ) // DraftCandidates offers installed skills without treating local relative paths // as GitHub shorthand. Unknown origins stay local until the author supplies one. -func DraftCandidates(sourcePath string) ([]DraftEntry, error) { - discovered, err := ssync.DiscoverSourceSkills(sourcePath) +func DraftCandidates(sourcePath string, walk ...sourcewalk.Options) ([]DraftEntry, error) { + discovered, err := ssync.DiscoverSourceSkills(sourcePath, walk...) if err != nil { return nil, err } diff --git a/internal/hub/index.go b/internal/hub/index.go index 2bc3657de..20bb1c3c7 100644 --- a/internal/hub/index.go +++ b/internal/hub/index.go @@ -12,6 +12,7 @@ import ( "skillshare/internal/audit" "skillshare/internal/install" + "skillshare/internal/sourcewalk" ssync "skillshare/internal/sync" ) @@ -53,13 +54,13 @@ type SkillEntry struct { // name, description, source are populated. // If auditSkills is true, each skill is scanned with audit.ScanSkill // and risk fields are populated. -func BuildIndex(sourcePath string, full bool, auditSkills bool) (*Index, error) { +func BuildIndex(sourcePath string, full bool, auditSkills bool, walk ...sourcewalk.Options) (*Index, error) { // Fail fast if source directory does not exist. if _, err := os.Stat(sourcePath); err != nil { return nil, fmt.Errorf("source directory: %w", err) } - discovered, err := ssync.DiscoverSourceSkills(sourcePath) + discovered, err := ssync.DiscoverSourceSkills(sourcePath, walk...) if err != nil { return nil, err } diff --git a/internal/server/handler_hub.go b/internal/server/handler_hub.go index 52564ac4a..6abd43fbc 100644 --- a/internal/server/handler_hub.go +++ b/internal/server/handler_hub.go @@ -12,7 +12,7 @@ func (s *Server) handleHubIndex(w http.ResponseWriter, r *http.Request) { sourcePath := s.skillsSource() s.mu.RUnlock() - idx, err := hub.BuildIndex(sourcePath, false, false) + idx, err := hub.BuildIndex(sourcePath, false, false, s.skillsWalk()) if err != nil { writeError(w, http.StatusInternalServerError, err.Error()) return diff --git a/internal/server/handler_hub_drafts.go b/internal/server/handler_hub_drafts.go index 66f41994b..00b831e1d 100644 --- a/internal/server/handler_hub_drafts.go +++ b/internal/server/handler_hub_drafts.go @@ -56,7 +56,7 @@ func (s *Server) handleHubDraftCandidates(w http.ResponseWriter, r *http.Request s.mu.RLock() source := s.skillsSource() s.mu.RUnlock() - entries, err := hub.DraftCandidates(source) + entries, err := hub.DraftCandidates(source, s.skillsWalk()) if err != nil { draftError(w, err) return diff --git a/internal/server/handler_hub_test.go b/internal/server/handler_hub_test.go index 4b81cbd6f..0b2b43f96 100644 --- a/internal/server/handler_hub_test.go +++ b/internal/server/handler_hub_test.go @@ -2,8 +2,11 @@ package server import ( "encoding/json" + "fmt" "net/http" "net/http/httptest" + "os" + "path/filepath" "testing" ) @@ -38,3 +41,75 @@ func TestHandleHubIndex_WithSkills(t *testing.T) { t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String()) } } + +func TestHubHandlersFollowSourceLinks(t *testing.T) { + for _, project := range []bool{false, true} { + for _, enabled := range []bool{false, true} { + t.Run(fmt.Sprintf("project=%t/follow=%t", project, enabled), func(t *testing.T) { + s, source := newUpdateFollowServer(t, project, enabled) + addSkill(t, source, "healthy") + checkout := t.TempDir() + addSkill(t, checkout, "linked-skill") + if err := os.Symlink(checkout, filepath.Join(source, "linked")); err != nil { + t.Fatal(err) + } + for _, surface := range []string{"index", "drafts", "search"} { + t.Run(surface, func(t *testing.T) { + rr := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodGet, "/api/search?q=linked-skill&hub=@builtin", nil) + switch surface { + case "index": + s.handleHubIndex(rr, req) + case "drafts": + s.handleHubDraftCandidates(rr, req) + case "search": + s.handleSearch(rr, req) + } + if rr.Code != http.StatusOK { + t.Fatalf("status=%d: %s", rr.Code, rr.Body.String()) + } + var names []string + switch surface { + case "drafts": + var entries []struct { + ID string `json:"id"` + } + if err := json.Unmarshal(rr.Body.Bytes(), &entries); err != nil { + t.Fatal(err) + } + for _, entry := range entries { + names = append(names, filepath.Base(entry.ID)) + } + default: + var resp struct { + Skills []struct { + Name string `json:"name"` + } `json:"skills"` + Results []struct { + Name string `json:"name"` + } `json:"results"` + } + if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil { + t.Fatal(err) + } + for _, entry := range resp.Skills { + names = append(names, entry.Name) + } + for _, entry := range resp.Results { + names = append(names, entry.Name) + } + } + found, healthy := false, false + for _, name := range names { + found = found || name == "linked-skill" + healthy = healthy || name == "healthy" + } + if found != enabled || (surface != "search" && !healthy) { + t.Fatalf("follow=%t inventory=%v: %s", enabled, names, rr.Body.String()) + } + }) + } + }) + } + } +} diff --git a/internal/server/handler_search.go b/internal/server/handler_search.go index b50b1cdaf..2354c72fa 100644 --- a/internal/server/handler_search.go +++ b/internal/server/handler_search.go @@ -10,6 +10,7 @@ import ( "skillshare/internal/hub" "skillshare/internal/install" "skillshare/internal/search" + "skillshare/internal/sourcewalk" ) func (s *Server) handleSearch(w http.ResponseWriter, r *http.Request) { @@ -37,7 +38,7 @@ func (s *Server) handleSearch(w http.ResponseWriter, r *http.Request) { var err error switch { case hubParam == "@builtin": - results, err = searchBuiltinIndex(source, query, limit) + results, err = searchBuiltinIndex(source, query, limit, s.skillsWalk()) case hubParam != "": // SSH hub sources trigger a git clone using the host's SSH credentials. // In the web server, restrict that to URLs the user has explicitly saved @@ -104,8 +105,8 @@ func savedHubURLSet(hub config.HubConfig) map[string]bool { } // searchBuiltinIndex builds the hub index from local skills and searches it in-memory. -func searchBuiltinIndex(sourcePath, query string, limit int) ([]search.SearchResult, error) { - idx, err := hub.BuildIndex(sourcePath, false, false) +func searchBuiltinIndex(sourcePath, query string, limit int, walk ...sourcewalk.Options) ([]search.SearchResult, error) { + idx, err := hub.BuildIndex(sourcePath, false, false, walk...) if err != nil { return nil, err } diff --git a/skills/skillshare/SKILL.md b/skills/skillshare/SKILL.md index 2926960d7..132a65cca 100644 --- a/skills/skillshare/SKILL.md +++ b/skills/skillshare/SKILL.md @@ -146,6 +146,9 @@ skillshare hub default my-hub # Set default hub skillshare hub remove my-hub # Remove a hub skillshare hub index --source ~/.config/skillshare/skills/ --full --audit # Build hub index ``` +The active `follow_source_links` setting also applies to `hub index` (including +`--source`), built-in dashboard search, and Hub draft candidates. + ## References Read the matching file for command flags, examples, and limitations. Avoid loading all diff --git a/website/docs/reference/commands/hub.md b/website/docs/reference/commands/hub.md index 3940824e9..c0877a0fb 100644 --- a/website/docs/reference/commands/hub.md +++ b/website/docs/reference/commands/hub.md @@ -103,6 +103,8 @@ skillshare hub default --reset # Clear default → community hub Build a `skillshare-hub.json` index file from installed skills. The generated index can be consumed by [`search --hub`](./search.md#private-index-search) for private, offline skill discovery. +With [`follow_source_links: true`](../targets/configuration.md#follow_source_links) in the active config, the index includes skills behind first-level source links, including with `--source`. The dashboard's built-in search and Hub draft candidates use the same discovery policy. + ### Usage ```bash diff --git a/website/i18n/ja/docusaurus-plugin-content-docs/current/reference/commands/hub.md b/website/i18n/ja/docusaurus-plugin-content-docs/current/reference/commands/hub.md index 7a0099408..015414b02 100644 --- a/website/i18n/ja/docusaurus-plugin-content-docs/current/reference/commands/hub.md +++ b/website/i18n/ja/docusaurus-plugin-content-docs/current/reference/commands/hub.md @@ -103,6 +103,8 @@ skillshare hub default --reset # デフォルトをクリア → community インストール済みの Skill から `skillshare-hub.json` インデックスファイルを構築します。生成されたインデックスは、プライベートかつオフラインでの Skill 発見のために [`search --hub`](./search.md#private-index-search) で利用できます。 +有効な設定で [`follow_source_links: true`](../targets/configuration.md#follow_source_links) を指定すると、`--source` 使用時も第1階層の Source リンクの背後にある Skill がインデックスに含まれます。ダッシュボードの組み込み検索と Hub ドラフト候補も同じ検出ポリシーを使います。 + ### 使い方 ```bash diff --git a/website/i18n/ko/docusaurus-plugin-content-docs/current/reference/commands/hub.md b/website/i18n/ko/docusaurus-plugin-content-docs/current/reference/commands/hub.md index 67a32a879..f56fdb82a 100644 --- a/website/i18n/ko/docusaurus-plugin-content-docs/current/reference/commands/hub.md +++ b/website/i18n/ko/docusaurus-plugin-content-docs/current/reference/commands/hub.md @@ -103,6 +103,8 @@ skillshare hub default --reset # Clear default → community hub 설치된 skill로부터 `skillshare-hub.json` index 파일을 빌드합니다. 생성된 index는 비공개 오프라인 skill 검색을 위해 [`search --hub`](./search.md#private-index-search)에서 사용할 수 있습니다. +활성 설정에서 [`follow_source_links: true`](../targets/configuration.md#follow_source_links)를 지정하면 `--source`를 사용할 때도 첫 번째 수준 source 링크 뒤의 skill이 인덱스에 포함됩니다. 대시보드의 기본 검색과 Hub 초안 후보도 같은 발견 정책을 사용합니다. + ### Usage ```bash diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/reference/commands/hub.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/reference/commands/hub.md index 23095db53..4d7c39f8b 100644 --- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/reference/commands/hub.md +++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/reference/commands/hub.md @@ -103,6 +103,8 @@ skillshare hub default --reset # 清除默认值 → 社区 hub 从已安装的 skills 构建一个 `skillshare-hub.json` 索引文件。生成的索引可以被 [`search --hub`](./search.md#private-index-search) 消费,用于私有、离线的 skill 发现。 +在当前配置中设置 [`follow_source_links: true`](../targets/configuration.md#follow_source_links) 后,索引会包含第一层 source 链接背后的 skill,包括使用 `--source` 时。dashboard 的内置搜索和 Hub 草稿候选项也使用相同的发现策略。 + ### 用法 ```bash diff --git a/website/i18n/zh-Hant/docusaurus-plugin-content-docs/current/reference/commands/hub.md b/website/i18n/zh-Hant/docusaurus-plugin-content-docs/current/reference/commands/hub.md index 8d53308a0..07e041791 100644 --- a/website/i18n/zh-Hant/docusaurus-plugin-content-docs/current/reference/commands/hub.md +++ b/website/i18n/zh-Hant/docusaurus-plugin-content-docs/current/reference/commands/hub.md @@ -103,6 +103,8 @@ skillshare hub default --reset # 清除預設值 → community hub 從已安裝的 skills 建立一個 `skillshare-hub.json` 索引檔案。產生的索引可供 [`search --hub`](./search.md#private-index-search) 使用,進行私有、離線的 skill 探索。 +在目前配置中設定 [`follow_source_links: true`](../targets/configuration.md#follow_source_links) 後,索引會包含第一層 source 連結背後的 skill,包含使用 `--source` 時。dashboard 的內建搜尋與 Hub 草稿候選項也使用相同的 discovery policy。 + ### 用法 ```bash