diff --git a/ai_docs/tests/project_command_runbook.md b/ai_docs/tests/project_command_runbook.md index f4b4d64b6..b74dff02f 100644 --- a/ai_docs/tests/project_command_runbook.md +++ b/ai_docs/tests/project_command_runbook.md @@ -3,7 +3,8 @@ ## Scope Verify the global `project` CLI: defaults, selection, resource settings, previews, -JSON failures, saved partial sync and retry, and merge/copy/symlink removal. +JSON failures, saved partial sync and retry, merge/copy/symlink removal, and +registering Git checkouts with `--scan`. Tests use local fixture skills and agents; no network or external repositories. Native Windows link behavior is outside this Linux acceptance runbook. @@ -460,8 +461,39 @@ Expected: - exit_code: 0 - jq: .registration == "removed" and (.detach.removed | length) > 0 +### 15. Scan registers checkouts once and a rescan only adds + +```bash +set -euo pipefail +ss() { /workspace/bin/skillshare "$@"; } +fixture=$(mktemp -d "$HOME/project-command-XXXXXX") +suite="$fixture/suite" +mkdir -p "$suite/app-a/.git" "$suite/app-b/.git" "$suite/docs" "$suite/.worktrees/app-a" "$fixture/skills/team-alpha" "$XDG_CONFIG_HOME/skillshare" +printf 'gitdir: %s\n' "$suite/app-a/.git/worktrees/app-a" > "$suite/.worktrees/app-a/.git" +printf '%s\n' '---' 'name: team-alpha' 'description: Team fixture' '---' '# Team' > "$fixture/skills/team-alpha/SKILL.md" +cat > "$XDG_CONFIG_HOME/skillshare/config.yaml" </dev/null +test "$before" = "$(cat "$XDG_CONFIG_HOME/skillshare/config.yaml")" +ss "${args[@]}" --sync --json >/dev/null +test -L "$suite/.worktrees/app-a/.agents/skills/team-alpha" +grep -q 'name: worktrees-app-a' "$XDG_CONFIG_HOME/skillshare/config.yaml" +ss project edit app-b --target codex,claude --json >/dev/null +mkdir -p "$suite/app-c/.git" +ss "${args[@]}" --json +``` + +Expected: +- exit_code: 0 +- jq: ([.scan[] | select(.result == "added")] | length) == 1 and (.scan[] | select(.root | endswith("/app-c")) | .result) == "added" and (.scan[] | select(.root | endswith("/app-b")) | .result) == "skipped" and ([.scan[] | select(.root | endswith("/docs"))] | length) == 0 + ## Pass Criteria -All 14 mdproof steps pass with zero failures or skips. JSON steps satisfy their +All 15 mdproof steps pass with zero failures or skips. JSON steps satisfy their jq assertions, expected rejection/partial-failure steps exit 1, and filesystem checks prove preview safety, sync scope, retry, detach behavior, and source survival. diff --git a/cmd/skillshare/completion_bash.go b/cmd/skillshare/completion_bash.go index 82c760e53..ae70270c3 100644 --- a/cmd/skillshare/completion_bash.go +++ b/cmd/skillshare/completion_bash.go @@ -90,11 +90,12 @@ _skillshare() { # Project command if [[ "${cmd}" == project ]]; then local project_subcmds="list show add edit sync remove localize share" - local project_flags="--target --name --skills --no-skills --agents --no-agents --skills-mode --agents-mode --skills-naming --skills-include --skills-exclude --agents-include --agents-exclude --machine-local --shared --sync --dry-run -n --yes --json --help -h --global -g" + local project_flags="--target --name --skills --no-skills --agents --no-agents --skills-mode --agents-mode --skills-naming --skills-include --skills-exclude --agents-include --agents-exclude --scan --machine-local --shared --sync --dry-run -n --yes --json --help -h --global -g" case "${prev}" in --skills-mode|--agents-mode) COMPREPLY=($(compgen -W "merge symlink copy" -- "${cur}")); return ;; --skills-naming) COMPREPLY=($(compgen -W "flat standard prefixed" -- "${cur}")); return ;; --target) COMPREPLY=($(compgen -W "adal aiderdesk amp antigravity antigravity-cli astrbot augment autohand-code bob claude cline codearts codebuddy codestudio codex comate commandcode continue copilot cortex crush cursor deepagents deepseek-harness devin dexto droid firebender forgecode fx gemini gitlab-duo goose grok hermes iflow jazz junie kilocode kimchi kimi kimi-code kiro kode letta lingma mcpjam mux neovate omp ona openclaw opencode openhands pi pochi posit-assistant purecode qoder qoder-cn qwen reasonix replit roo rovodev tabnine trae trae-cn universal verdent vibe warp windsurf witsy xcode-claude xcode-codex zcode zed zencoder" -- "${cur}")); return ;; + --scan) COMPREPLY=($(compgen -d -- "${cur}")); return ;; --name|--skills-include|--skills-exclude|--agents-include|--agents-exclude) return ;; esac if [[ ${cword} -eq 2 ]]; then diff --git a/cmd/skillshare/completion_fish.go b/cmd/skillshare/completion_fish.go index 1429a9653..95ceaf7a8 100644 --- a/cmd/skillshare/completion_fish.go +++ b/cmd/skillshare/completion_fish.go @@ -37,6 +37,7 @@ complete -c skillshare -n '__fish_skillshare_using_command project' -l agents-mo complete -c skillshare -n '__fish_skillshare_using_command project' -l skills-naming -r -a 'flat standard prefixed' complete -c skillshare -n '__fish_skillshare_using_command project' -l skills-include -r complete -c skillshare -n '__fish_skillshare_using_command project' -l skills-exclude -r +complete -c skillshare -n '__fish_skillshare_using_command project' -l scan -x -a '(__fish_complete_directories)' -d 'Also add Git checkouts below this folder' complete -c skillshare -n '__fish_skillshare_using_command project' -l machine-local -d 'Save a new project on this machine only' complete -c skillshare -n '__fish_skillshare_using_command project' -l shared -d 'Save in config.yaml, or also remove its copy' complete -c skillshare -n '__fish_skillshare_using_command project' -l agents-include -r diff --git a/cmd/skillshare/completion_nushell.go b/cmd/skillshare/completion_nushell.go index f341613a7..4de9045bb 100644 --- a/cmd/skillshare/completion_nushell.go +++ b/cmd/skillshare/completion_nushell.go @@ -77,6 +77,7 @@ export extern "skillshare project" [ --skills-exclude: string --agents-include: string --agents-exclude: string + --scan: path --machine-local --shared --sync diff --git a/cmd/skillshare/completion_powershell.go b/cmd/skillshare/completion_powershell.go index bbfdc17b3..135503eeb 100644 --- a/cmd/skillshare/completion_powershell.go +++ b/cmd/skillshare/completion_powershell.go @@ -267,7 +267,7 @@ $_skillshareCompleter = { @{ Name = 'localize'; Desc = 'Keep a copy on this machine only' } @{ Name = 'share'; Desc = 'Move a machine-only project into config.yaml' } ) - $flags['project'] = '--target', '--name', '--skills', '--no-skills', '--agents', '--no-agents', '--skills-mode', '--agents-mode', '--skills-naming', '--skills-include', '--skills-exclude', '--agents-include', '--agents-exclude', '--machine-local', '--shared', '--sync', '--dry-run', '-n', '--yes', '--json', '--help', '-h', '--global', '-g' + $flags['project'] = '--target', '--name', '--skills', '--no-skills', '--agents', '--no-agents', '--skills-mode', '--agents-mode', '--skills-naming', '--skills-include', '--skills-exclude', '--agents-include', '--agents-exclude', '--scan', '--machine-local', '--shared', '--sync', '--dry-run', '-n', '--yes', '--json', '--help', '-h', '--global', '-g' if ($cmd -eq 'project' -and $previousIndex -ge 0) { $values = switch ($elements[$previousIndex]) { '--skills-mode' { @('merge', 'symlink', 'copy') } diff --git a/cmd/skillshare/completion_zsh.go b/cmd/skillshare/completion_zsh.go index 99d98129d..5ecabb8f6 100644 --- a/cmd/skillshare/completion_zsh.go +++ b/cmd/skillshare/completion_zsh.go @@ -417,6 +417,7 @@ _skillshare() { '*--skills-exclude[Replace skills exclude filters]:pattern: ' \ '*--agents-include[Replace agents include filters]:pattern: ' \ '*--agents-exclude[Replace agents exclude filters]:pattern: ' \ + '*--scan[Also add Git checkouts below this folder]:folder:_directories' \ '--machine-local[Save a new project on this machine only]' \ '--shared[Save in config.yaml, or also remove its copy]' \ '--sync[Sync after add or edit]' \ diff --git a/cmd/skillshare/log_format.go b/cmd/skillshare/log_format.go index 2d261e9b7..e8998baed 100644 --- a/cmd/skillshare/log_format.go +++ b/cmd/skillshare/log_format.go @@ -433,6 +433,12 @@ func formatProjectLogPairs(args map[string]any) []logDetailPair { if targets, ok := logArgStringSlice(args, "targets"); ok && len(targets) > 0 { pairs = append(pairs, logDetailPair{key: "targets", isList: true, listValues: targets}) } + if scan, ok := logArgStringSlice(args, "scan"); ok && len(scan) > 0 { + pairs = append(pairs, logDetailPair{key: "scan", isList: true, listValues: scan}) + } + if added, ok := logArgInt(args, "added"); ok { + pairs = append(pairs, logDetailPair{key: "added", value: strconv.Itoa(added)}) + } if scope, ok := logArgString(args, "scope"); ok && scope != "" { pairs = append(pairs, logDetailPair{key: "scope", value: scope}) } @@ -451,6 +457,12 @@ func formatProjectLogDetail(args map[string]any) string { if targets, ok := logArgStringSlice(args, "targets"); ok && len(targets) > 0 { parts = append(parts, "targets="+strings.Join(targets, ", ")) } + if scan, ok := logArgStringSlice(args, "scan"); ok && len(scan) > 0 { + parts = append(parts, "scan="+strings.Join(scan, ", ")) + } + if added, ok := logArgInt(args, "added"); ok { + parts = append(parts, "added="+strconv.Itoa(added)) + } if scope, ok := logArgString(args, "scope"); ok && scope != "" { parts = append(parts, "scope="+scope) } diff --git a/cmd/skillshare/log_format_test.go b/cmd/skillshare/log_format_test.go index bf1955bb8..37d52a528 100644 --- a/cmd/skillshare/log_format_test.go +++ b/cmd/skillshare/log_format_test.go @@ -426,6 +426,14 @@ func TestFormatProjectLog_ShowsActionAndRoot(t *testing.T) { } } +func TestFormatProjectLog_ShowsScan(t *testing.T) { + // Args as read back from operations.log: JSON numbers decode as float64. + e := oplog.Entry{Command: "project", Args: map[string]any{"action": "add", "root": "~/work/suite", "scan": []any{".", ".worktrees"}, "added": float64(3), "scope": "cli"}} + if detail := formatLogDetail(e, false); detail != "add ~/work/suite, scan=., .worktrees, added=3, scope=cli" { + t.Fatalf("unexpected scan detail: %q", detail) + } +} + func TestFormatSyncLog_ShowsProjectRoot(t *testing.T) { e := oplog.Entry{Command: "sync", Args: map[string]any{"action": "sync", "root": "~/work/app", "targets_total": 2, "scope": "cli"}} diff --git a/cmd/skillshare/project.go b/cmd/skillshare/project.go index 41b6d1bb1..ac312e55f 100644 --- a/cmd/skillshare/project.go +++ b/cmd/skillshare/project.go @@ -10,7 +10,7 @@ import ( ssync "skillshare/internal/sync" ) -var projectValueFlags = []string{"--target", "--name", "--skills-mode", "--skills-naming", "--skills-include", "--skills-exclude", "--agents-mode", "--agents-include", "--agents-exclude"} +var projectValueFlags = []string{"--target", "--name", "--skills-mode", "--skills-naming", "--skills-include", "--skills-exclude", "--agents-mode", "--agents-include", "--agents-exclude", "--scan"} type projectOptions struct { action, token string @@ -167,6 +167,9 @@ func parseProjectArgs(args []string) (projectOptions, error) { if want == 2 { o.token = operands[1] } + if _, scan := o.values["--scan"]; scan && o.action != "add" { + return o, fmt.Errorf("--scan requires project add") + } if o.action != "add" && o.action != "edit" && (len(o.values) > 0 || len(o.toggles) > 0 || o.sync) { return o, fmt.Errorf("settings and --sync require project add or edit") } @@ -210,10 +213,10 @@ func parseProjectArgs(args []string) (projectOptions, error) { func printProjectHelp() { printHelp("skillshare project [list|show|add|edit|sync|remove|localize|share] [project] [options]", "Manage the project folders the global config syncs to. A project is saved in config.yaml unless it is kept on this machine only. No project config is changed.", helpGroup{title: "Commands", rows: []helpRow{ - {"list", "List registered projects (default)"}, {"show ", "Show one project by name or root"}, {"add --target T", "Add an existing project folder"}, {"edit ", "Change only the specified settings, where they are saved"}, {"localize ", "Keep a copy on this machine only; config.yaml stays"}, {"share ", "Move a machine-only project into config.yaml"}, {"sync ", "Sync skills and agents to this project"}, {"remove ", "Detach skills and remove the project; agents stay"}, + {"list", "List registered projects (default)"}, {"show ", "Show one project by name or root"}, {"add --target T", "Add an existing project folder"}, {"add --scan DIR --target T", "Also add each Git checkout below DIR, with the same settings"}, {"edit ", "Change only the specified settings, where they are saved"}, {"localize ", "Keep a copy on this machine only; config.yaml stays"}, {"share ", "Move a machine-only project into config.yaml"}, {"sync ", "Sync skills and agents to this project"}, {"remove ", "Detach skills and remove the project; agents stay"}, }}, helpGroup{title: "Options", rows: []helpRow{ - {"--target T", "Replace targets; repeat or use a comma list"}, {"--name N", "Set name; --name= restores the basename"}, {"--skills, --no-skills", "Turn skills on (add default) or off"}, {"--agents, --no-agents", "Turn agents on or off (add default)"}, {"--skills-mode, --agents-mode MODE", "merge, symlink or copy; empty inherits"}, {"--skills-naming NAMING", "flat, standard or prefixed; empty inherits"}, {"--skills-include, --skills-exclude P", "Replace filter list; repeat; empty clears"}, {"--agents-include, --agents-exclude P", "Replace filter list; repeat; empty clears"}, {"--machine-local", "Add: save on this machine only, not in config.yaml"}, {"--shared", "Add: save in config.yaml despite projects_storage; remove: also remove config.yaml's copy"}, {"--sync", "Sync after add/edit; force is off"}, {"-n, --dry-run", "Preview without writing or prompting"}, {"--yes", "Skip the remove or share confirmation (remove --json requires it)"}, {"--json", "Output JSON"}, {"-g, --global", "Use the global config (always)"}, + {"--target T", "Replace targets; repeat or use a comma list"}, {"--name N", "Set name; --name= restores the basename"}, {"--skills, --no-skills", "Turn skills on (add default) or off"}, {"--agents, --no-agents", "Turn agents on or off (add default)"}, {"--skills-mode, --agents-mode MODE", "merge, symlink or copy; empty inherits"}, {"--skills-naming NAMING", "flat, standard or prefixed; empty inherits"}, {"--skills-include, --skills-exclude P", "Replace filter list; repeat; empty clears"}, {"--agents-include, --agents-exclude P", "Replace filter list; repeat; empty clears"}, {"--machine-local", "Add: save on this machine only, not in config.yaml"}, {"--shared", "Add: save in config.yaml despite projects_storage; remove: also remove config.yaml's copy"}, {"--scan DIR", "Add: also add Git checkouts below this folder of root; repeat; registered ones stay as they are"}, {"--sync", "Sync after add/edit; force is off"}, {"-n, --dry-run", "Preview without writing or prompting"}, {"--yes", "Skip the remove or share confirmation (remove --json requires it)"}, {"--json", "Output JSON"}, {"-g, --global", "Use the global config (always)"}, }}, ) } diff --git a/cmd/skillshare/project_handlers.go b/cmd/skillshare/project_handlers.go index e48879586..48737129d 100644 --- a/cmd/skillshare/project_handlers.go +++ b/cmd/skillshare/project_handlers.go @@ -151,6 +151,9 @@ func runProject(o projectOptions) error { } else if o.shared { storage = projects.OriginShared } + if _, scan := o.values["--scan"]; scan { + return runProjectScan(o, cfg, service, root, entry, storage, release, start) + } if _, exists := cfg.Projects[root]; create && exists { // Adding the same project again is a no-op, so scripts can rerun it. if info, err := os.Stat(config.ExpandPath(root)); err != nil || !info.IsDir() { diff --git a/cmd/skillshare/project_render.go b/cmd/skillshare/project_render.go index c48a7e458..e50e1503f 100644 --- a/cmd/skillshare/project_render.go +++ b/cmd/skillshare/project_render.go @@ -122,24 +122,7 @@ func renderProjectResult(o projectOptions, r projectResult, err error, start tim ui.Row(ui.MarkNone, "Registration", r.Registration, ui.RowWidth("Registration")) } if r.Sync != nil { - for _, unmatched := range r.Sync.Unmatched { - for _, pattern := range unmatched.Patterns { - ui.Warning("%s: %s", unmatched.Target, (ssync.UnmatchedInclude{Pattern: pattern, Suggestions: unmatched.Suggestions}).Warning()) - } - } - var targets map[string]config.TargetConfig - var managed map[string]config.ManagedProject - if r.Expanded != nil { - targets = r.Expanded.Targets - managed = r.Expanded.Projects - } - printSyncFolderConflicts(r.Sync.FolderConflicts, r.Sync.PathOverlap, false, targets, managed) - for _, row := range r.Sync.Results { - ui.Row(ui.MarkOK, row.Target, fmt.Sprintf("%d linked, %d updated, %d skipped, %d pruned", len(row.Linked), len(row.Updated), len(row.Skipped), len(row.Pruned)), ui.RowWidth(row.Target)) - } - for _, failure := range r.Sync.Failed { - ui.Row(ui.MarkFail, failure.Target, failure.Error, ui.RowWidth(failure.Target)) - } + renderProjectSync(r.Sync, r.Expanded) } for _, warning := range r.Warnings { ui.Warning("%s", warning) @@ -159,6 +142,29 @@ func renderProjectResult(o projectOptions, r projectResult, err error, start tim } return err } + +// renderProjectSync prints one project's sync, as project sync does. +func renderProjectSync(synced *ssync.ExecuteResult, expanded *config.Config) { + for _, unmatched := range synced.Unmatched { + for _, pattern := range unmatched.Patterns { + ui.Warning("%s: %s", unmatched.Target, (ssync.UnmatchedInclude{Pattern: pattern, Suggestions: unmatched.Suggestions}).Warning()) + } + } + var targets map[string]config.TargetConfig + var managed map[string]config.ManagedProject + if expanded != nil { + targets = expanded.Targets + managed = expanded.Projects + } + printSyncFolderConflicts(synced.FolderConflicts, synced.PathOverlap, false, targets, managed) + for _, row := range synced.Results { + ui.Row(ui.MarkOK, row.Target, fmt.Sprintf("%d linked, %d updated, %d skipped, %d pruned", len(row.Linked), len(row.Updated), len(row.Skipped), len(row.Pruned)), ui.RowWidth(row.Target)) + } + for _, failure := range synced.Failed { + ui.Row(ui.MarkFail, failure.Target, failure.Error, ui.RowWidth(failure.Target)) + } +} + func renderProjectItem(item projects.Item) { width := ui.RowWidth("Project", "Root", "Origin", "Origin file", "Targets", "Skills", "Agents") ui.Row(ui.MarkNone, "Project", item.Name, width) diff --git a/cmd/skillshare/project_scan.go b/cmd/skillshare/project_scan.go new file mode 100644 index 000000000..cd615fe3a --- /dev/null +++ b/cmd/skillshare/project_scan.go @@ -0,0 +1,126 @@ +package main + +import ( + "fmt" + "slices" + "time" + + "skillshare/internal/config" + "skillshare/internal/projects" + ssync "skillshare/internal/sync" + "skillshare/internal/ui" +) + +// projectScanRow adds a scanned project's sync to its scan result. +type projectScanRow struct { + projects.ScanRow + Sync *projectSyncJSON `json:"sync,omitempty"` + Error string `json:"error,omitempty"` + synced *ssync.ExecuteResult +} + +type projectScanJSON struct { + projectResultJSON + Scan []projectScanRow `json:"scan"` +} + +// runProjectScan runs project add --scan, then syncs the root and every added +// or kept checkout when --sync is set. release unlocks the config before syncing. +func runProjectScan(o projectOptions, cfg *config.Config, service *projects.Service, root string, entry config.ManagedProject, storage string, release func(), start time.Time) error { + scan, err := service.Scan(root, entry, o.values["--scan"], projects.PutOptions{DryRun: o.dryRun, Storage: storage}) + if release != nil { + release() + } + if err != nil { + return err + } + result := projectResult{Result: scan.Result} + for _, w := range scan.Warnings { + result.Warnings = append(result.Warnings, w.String()) + } + if !o.dryRun && result.Registration == "updated" { + logProject(cfg, "project", "add", root, "ok", nil, start, map[string]any{"scan": o.values["--scan"], "added": scan.Added()}) + } + + rows := make([]projectScanRow, len(scan.Rows)) + failed := 0 + for i, scanned := range scan.Rows { + row := &rows[i] + row.ScanRow = scanned + if !o.sync || scanned.Item == nil { + continue + } + synced, err := syncProject(result.Expanded, scanned.Item.Root, o.dryRun) + err = projectSyncError(synced, err, o.dryRun) + if synced != nil { + row.synced = synced + row.Sync = projectSyncPayload(synced, result.Expanded.EffectiveAgentsSource()) + for _, w := range synced.Warnings { + if !slices.Contains(result.Warnings, w) { + result.Warnings = append(result.Warnings, w) + } + } + } + if err != nil { + failed++ + row.Error = err.Error() + } + if !o.dryRun { + status := projectSyncStatus(projectResult{Result: projects.Result{Item: scanned.Item, Expanded: result.Expanded}, Sync: synced}, err) + logProject(result.Expanded, "sync", "sync", scanned.Item.Root, status, err, start, nil) + } + } + var opErr error + if failed > 0 { + opErr = fmt.Errorf("sync failed for %d of the scanned projects", failed) + } + return renderProjectScan(o, result, rows, opErr, start) +} + +func renderProjectScan(o projectOptions, r projectResult, rows []projectScanRow, err error, start time.Time) error { + unchanged := len(r.Affected) == 0 + if o.json { + applied := !o.dryRun && r.Registration == "updated" + p := projectScanJSON{projectResultJSON: projectResultJSON{Action: "add", Project: r.Item, Applied: &applied, Unchanged: unchanged, Affected: projectSlice(r.Affected), Warnings: projectSlice(r.Warnings), Registration: r.Registration}, Scan: rows} + if p.Project != nil { + item := projectJSONItem(*p.Project) + p.Project = &item + } + if err != nil { + p.Error = err.Error() + } + return writeJSONResult(p, err) + } + width := ui.RowWidth("Skipped") + for _, row := range rows { + mark, label := ui.MarkOK, "Added" + switch row.Result { + case projects.ScanKept: + mark, label = ui.MarkNone, "Kept" + case projects.ScanSkipped: + mark, label = ui.MarkWarn, "Skipped" + } + value := row.Root + if row.Reason != "" { + value += " " + row.Reason + } + ui.Row(mark, label, value, width) + if row.synced != nil { + renderProjectSync(row.synced, r.Expanded) + } + } + for _, warning := range r.Warnings { + ui.Warning("%s", warning) + } + if o.dryRun { + ui.DryRun() + } else if unchanged && !o.sync { + ui.Done(ui.MarkNone, "Projects unchanged", time.Since(start)) + } else if err == nil { + ui.Done(ui.MarkOK, "Project add complete", time.Since(start)) + } + if err == nil && !o.dryRun { + ui.Next("skillshare project list", "Inspect registered projects") + } + return err +} diff --git a/internal/projects/scan.go b/internal/projects/scan.go new file mode 100644 index 000000000..42e958e03 --- /dev/null +++ b/internal/projects/scan.go @@ -0,0 +1,382 @@ +package projects + +import ( + "errors" + "fmt" + "io/fs" + "os" + "path" + "path/filepath" + "slices" + "strings" + + "skillshare/internal/config" + "skillshare/internal/projectdir" + "skillshare/internal/utils" +) + +// Results of a scanned folder. +const ( + ScanAdded = "added" + ScanKept = "kept" + ScanSkipped = "skipped" +) + +// Why a scanned folder was skipped, for callers that word the reason themselves. +const ( + SkipDifferent = "different" + SkipStorage = "storage" + SkipRemoved = "removed" + SkipOwnConfig = "own-config" + SkipNameTaken = "name-taken" + SkipBadName = "bad-name" +) + +// ScanRow is one folder a scan considered: the root or a checkout below it. +// Path is relative to the root ("." for the root itself). Item is the +// registered project, except for a skipped folder. +type ScanRow struct { + Root string `json:"root"` + Path string `json:"path"` + Name string `json:"name"` + Result string `json:"result"` + Skip string `json:"skip,omitempty"` + Reason string `json:"reason,omitempty"` + Item *Item `json:"-"` +} + +// Kinds of scan warnings. +const ( + ScanMissing = "missing" + ScanUnreadable = "unreadable" +) + +// ScanWarning is a scan folder that does not exist, or a folder below one that +// could not be read. Folder is relative to the root. +type ScanWarning struct { + Kind string `json:"kind"` + Folder string `json:"folder"` + Detail string `json:"detail,omitempty"` +} + +func (w ScanWarning) String() string { + if w.Kind == ScanMissing { + return fmt.Sprintf("scan folder %s does not exist", w.Folder) + } + return fmt.Sprintf("skipped %s, which could not be read: %s", w.Folder, w.Detail) +} + +// ScanResult is a scan's registration. Item is the root. +type ScanResult struct { + Result + Rows []ScanRow + Warnings []ScanWarning +} + +// Added counts the folders the scan registered, or would in a dry run. +func (r ScanResult) Added() int { + n := 0 + for _, row := range r.Rows { + if row.Result == ScanAdded { + n++ + } + } + return n +} + +// Scan registers root and each Git checkout below its scan folders with the +// same settings, in one validated write. It only adds: a registered folder +// stays as it is, so a rerun registers only new checkouts. +func (s *Service) Scan(root string, entry config.ManagedProject, dirs []string, opts PutOptions) (ScanResult, error) { + cfg, err := s.load() + if err != nil { + return ScanResult{}, err + } + abs := config.ExpandPath(root) + if info, err := os.Stat(abs); err != nil || !info.IsDir() { + return ScanResult{}, fmt.Errorf("%w: %s", ErrFolderNotFound, root) + } + storage := opts.Storage + if storage == "" { + storage = cfg.NewProjectOrigin() + } + if err := checkStorage(storage); err != nil { + return ScanResult{}, err + } + found, warnings, err := ScanCheckouts(abs, dirs) + if err != nil { + return ScanResult{}, err + } + cwd, err := os.Getwd() + if err != nil { + return ScanResult{}, err + } + home, err := os.UserHomeDir() + if err != nil { + return ScanResult{}, err + } + rows, adds, err := planScan(cfg, root, entry, found, opts.Storage, cwd, home) + if err != nil { + return ScanResult{}, err + } + result := ScanResult{Result: Result{Expanded: cfg, Registration: "kept"}, Rows: rows, Warnings: warnings} + if len(adds) > 0 { + // Entries share the settings' slices; each gets its own copy. + c, err := layerEntries(s.path, cfg, cloneProjects(adds), storage) + if err != nil { + return ScanResult{}, err + } + result.Affected = c.Affected + if err := c.Save.Check(); err != nil { + return result, err + } + if err := commit(c, opts.DryRun, &result.Result, "updated"); err != nil { + return result, err + } + result.Expanded = c.Save.Expanded + } + for i := range result.Rows { + if result.Rows[i].Result != ScanSkipped { + v := resultItem(result.Expanded, result.Rows[i].Root) + result.Rows[i].Item = &v + } + } + // The root is reported even when it was skipped. + root = result.Rows[0].Root + item := resultItem(result.Expanded, root) + result.Item = &item + return result, nil +} + +// planScan decides each folder's result and the entries to add. storage is the +// layer the caller asked for, if any. +func planScan(cfg *config.Config, root string, entry config.ManagedProject, found []string, storage, cwd, home string) ([]ScanRow, map[string]config.ManagedProject, error) { + taken := map[string]bool{} + for key, p := range cfg.Projects { + taken[p.ProjectName(key)] = true + } + adds := map[string]config.ManagedProject{} + rootRow, err := registeredRow(cfg, root, entry, false, storage, cwd, home) + if err != nil { + return nil, nil, err + } + if rootRow == nil { + rootRow = &ScanRow{Root: root, Name: entry.ProjectName(root), Result: ScanAdded} + } + rootRow.Path = "." + if rootRow.Result == ScanAdded { + adds[root] = entry + } + taken[rootRow.Name] = true + rows := []ScanRow{*rootRow} + + var fresh []string + bases := map[string]int{} + for _, rel := range found { + key := filepath.Join(root, filepath.FromSlash(rel)) + row, err := registeredRow(cfg, key, entry, true, storage, cwd, home) + if err != nil { + return nil, nil, err + } + if row == nil && ownsConfig(config.ExpandPath(key)) { + row = &ScanRow{Root: key, Name: path.Base(rel), Result: ScanSkipped, Skip: SkipOwnConfig, Reason: "has its own .skillshare/ project config"} + } + if row != nil { + row.Path = rel + rows = append(rows, *row) + continue + } + fresh = append(fresh, rel) + bases[path.Base(rel)]++ + } + + // A checkout keeps its folder name unless another project or a sibling + // checkout has it. Then the checkout directly under root keeps it and the + // others are named from their path. Folder names are claimed first, so + // the result does not depend on scan order. + derived := map[string]bool{} + for _, rel := range fresh { + base := path.Base(rel) + derived[rel] = taken[base] || (bases[base] > 1 && rel != base) + } + for _, pass := range []bool{false, true} { + for _, rel := range fresh { + if derived[rel] != pass { + continue + } + key := filepath.Join(root, filepath.FromSlash(rel)) + child := entry + child.Name = "" + name := path.Base(rel) + if pass { + if name = scanName(rel); name != path.Base(rel) { + child.Name = name + } + } + row := ScanRow{Root: key, Path: rel, Name: name, Result: ScanAdded} + switch { + case taken[name]: + row.Result, row.Skip, row.Reason = ScanSkipped, SkipNameTaken, fmt.Sprintf("name %q is already used; add it on its own with --name", name) + case name == "" || strings.ContainsAny(name, config.ProjectTargetSep+`/\`) || ValidateName(name) != nil: + row.Result, row.Skip, row.Reason = ScanSkipped, SkipBadName, fmt.Sprintf("%q cannot be a project name; add it on its own with --name", name) + default: + taken[name] = true + adds[key] = child + } + rows = append(rows, row) + } + } + slices.SortFunc(rows[1:], func(a, b ScanRow) int { return strings.Compare(a.Root, b.Root) }) + return rows, adds, nil +} + +// registeredRow reports a folder that is already registered, nil otherwise. +// The root keeps its own name; a checkout's settings are compared without it. +func registeredRow(cfg *config.Config, key string, want config.ManagedProject, checkout bool, storage, cwd, home string) (*ScanRow, error) { + match, err := FindRoot(cfg.Projects, key, cwd, home) + if errors.Is(err, ErrNotFound) { + if checkout && cfg.HasSharedProject(key) { + return &ScanRow{Root: key, Name: want.ProjectName(key), Result: ScanSkipped, Skip: SkipRemoved, Reason: "was removed on this machine; add it on its own to restore it"}, nil + } + return nil, nil + } + if err != nil { + return nil, err + } + have := cfg.Projects[match] + row := &ScanRow{Root: match, Name: have.ProjectName(match), Result: ScanKept} + if checkout { + want.Name = have.Name + } + origin, _ := cfg.ProjectOrigin(match) + if !config.SameManagedProject(have, want) { + row.Result, row.Skip, row.Reason = ScanSkipped, SkipDifferent, "has different settings; change it with: skillshare project edit "+row.Name + } else if storage != "" && storage != origin { + verb := map[string]string{OriginLocal: "localize", OriginShared: "share"}[storage] + where := map[string]string{OriginLocal: "on this machine only", OriginShared: "in config.yaml"}[origin] + row.Result, row.Skip, row.Reason = ScanSkipped, SkipStorage, fmt.Sprintf("is saved %s; move it with: skillshare project %s %s", where, verb, row.Name) + } + return row, nil +} + +// ownsConfig includes a .skillshare/ whose config.yaml is gitignored, as a +// clone of a shared skills repository has it. +func ownsConfig(dir string) bool { + _, full := projectdir.Find(dir) + _, partial := projectdir.FindPartial(dir) + return full || partial +} + +// scanName names a checkout from its path under the root: +// .worktrees/app-a becomes worktrees-app-a. +func scanName(rel string) string { + var parts []string + for _, part := range strings.Split(rel, "/") { + if part = strings.TrimLeft(part, "."); part != "" { + parts = append(parts, part) + } + } + return strings.Join(parts, "-") +} + +// ScanCheckouts finds Git checkouts below the scan folders of root, an absolute +// project folder. A checkout has a .git folder, or a .git file for a linked +// worktree, and is never searched further. Hidden folders below a scan folder, +// node_modules and links are skipped. Found checkouts are slash-separated and +// relative to root. A scan folder that does not exist, or a folder that cannot +// be read, is skipped with a warning, so one such folder never stops the scan. +func ScanCheckouts(root string, dirs []string) (found []string, warnings []ScanWarning, err error) { + // A root reached through a link is walked at its target, so the walk can + // start there; links below the root are still skipped. + if resolved, err := filepath.EvalSymlinks(root); err == nil { + root = resolved + } + seen := map[string]bool{} + for _, dir := range dirs { + rel := filepath.FromSlash(dir) + if !filepath.IsLocal(rel) { + return nil, nil, fmt.Errorf("--scan %q must be a folder inside %s", dir, root) + } + scanRoot := filepath.Join(root, rel) + exists, err := localFolder(root, scanRoot) + if errors.Is(err, fs.ErrPermission) { + warnings = append(warnings, ScanWarning{Kind: ScanUnreadable, Folder: dir, Detail: fs.ErrPermission.Error()}) + continue + } + if err != nil { + return nil, nil, fmt.Errorf("--scan %q: %w", dir, err) + } + if !exists { + warnings = append(warnings, ScanWarning{Kind: ScanMissing, Folder: dir}) + continue + } + err = filepath.WalkDir(scanRoot, func(path string, entry fs.DirEntry, err error) error { + if err != nil { + folder, _ := filepath.Rel(root, path) + detail := err + if pathErr, ok := err.(*fs.PathError); ok { + detail = pathErr.Err + } + warnings = append(warnings, ScanWarning{Kind: ScanUnreadable, Folder: filepath.ToSlash(folder), Detail: detail.Error()}) + return filepath.SkipDir + } + if !entry.IsDir() { + return nil + } + if utils.IsLinkMode(path, entry.Type()) { + return filepath.SkipDir + } + if path != scanRoot && (strings.HasPrefix(entry.Name(), ".") || entry.Name() == "node_modules") { + return filepath.SkipDir + } + if path == root || !isCheckout(path) { + return nil + } + checkout, _ := filepath.Rel(root, path) + if checkout = filepath.ToSlash(checkout); !seen[checkout] { + seen[checkout] = true + found = append(found, checkout) + } + return filepath.SkipDir + }) + if err != nil { + return nil, nil, err + } + } + slices.Sort(found) + return found, warnings, nil +} + +// localFolder reports whether path, inside root, is a folder reached without +// crossing a link. A missing folder is not an error. +func localFolder(root, path string) (bool, error) { + rel, _ := filepath.Rel(root, path) + if rel == "." { + return true, nil + } + at := root + for _, part := range strings.Split(rel, string(filepath.Separator)) { + at = filepath.Join(at, part) + info, err := os.Lstat(at) + if errors.Is(err, fs.ErrNotExist) { + return false, nil + } + if err != nil { + return false, err + } + if utils.IsLinkMode(at, info.Mode()) { + return false, fmt.Errorf("%s is a link", at) + } + if !info.IsDir() { + return false, fmt.Errorf("%s is not a folder", at) + } + } + return true, nil +} + +// isCheckout uses Lstat, unlike install.IsGitRepo, to reject a linked .git, +// which could point at another checkout. +func isCheckout(path string) bool { + info, err := os.Lstat(filepath.Join(path, ".git")) + return err == nil && (info.IsDir() || info.Mode().IsRegular()) +} diff --git a/internal/projects/scan_test.go b/internal/projects/scan_test.go new file mode 100644 index 000000000..6543cae9a --- /dev/null +++ b/internal/projects/scan_test.go @@ -0,0 +1,124 @@ +package projects + +import ( + "maps" + "os" + "path/filepath" + "runtime" + "slices" + "testing" + + "skillshare/internal/config" +) + +func TestScanCheckoutsSkipsLinks(t *testing.T) { + root := t.TempDir() + outside := t.TempDir() + for _, dir := range []string{filepath.Join(root, "app", ".git"), filepath.Join(outside, "other", ".git"), filepath.Join(root, "linked-git")} { + if err := os.MkdirAll(dir, 0755); err != nil { + t.Fatal(err) + } + } + // A linked checkout folder and a linked .git could both point at another checkout. + if err := os.Symlink(filepath.Join(outside, "other"), filepath.Join(root, "linked")); err != nil { + t.Skip("symlinks unavailable:", err) + } + if err := os.Symlink(filepath.Join(outside, "other", ".git"), filepath.Join(root, "linked-git", ".git")); err != nil { + t.Fatal(err) + } + found, _, err := ScanCheckouts(root, []string{".", "app"}) + if err != nil { + t.Fatal(err) + } + if !slices.Equal(found, []string{"app"}) { + t.Fatalf("found %v, want [app]", found) + } + if _, _, err := ScanCheckouts(root, []string{"linked"}); err == nil { + t.Fatal("a linked scan folder was accepted") + } +} + +func TestScanCheckoutsThroughLinkedRoot(t *testing.T) { + real := filepath.Join(t.TempDir(), "suite") + if err := os.MkdirAll(filepath.Join(real, "app", ".git"), 0755); err != nil { + t.Fatal(err) + } + link := filepath.Join(t.TempDir(), "suite") + if err := os.Symlink(real, link); err != nil { + t.Skip("symlinks unavailable:", err) + } + found, _, err := ScanCheckouts(link, []string{"."}) + if err != nil { + t.Fatal(err) + } + if !slices.Equal(found, []string{"app"}) { + t.Fatalf("found %v, want [app]", found) + } +} + +func TestPlanScanNamesClashingCheckoutsFromTheirPath(t *testing.T) { + home := t.TempDir() + root := filepath.Join(home, "suite") + cfg := &config.Config{Projects: map[string]config.ManagedProject{ + filepath.Join(home, "other", "worktrees-app-b"): {Targets: []string{"codex"}}, + }} + entry := config.ManagedProject{Targets: []string{"codex"}} + // Sorted as a scan returns them: the worktrees come before their checkouts. + found := []string{".worktrees/app-a", ".worktrees/app-b", "app-a", "app-b"} + rows, adds, err := planScan(cfg, root, entry, found, "", home, home) + if err != nil { + t.Fatal(err) + } + names := map[string]string{} + for _, row := range rows { + rel, _ := filepath.Rel(root, row.Root) + names[filepath.ToSlash(rel)] = row.Result + ":" + row.Name + } + want := map[string]string{".": "added:suite", "app-a": "added:app-a", "app-b": "added:app-b", ".worktrees/app-a": "added:worktrees-app-a", ".worktrees/app-b": "skipped:worktrees-app-b"} + if !maps.Equal(names, want) { + t.Fatalf("rows %v, want %v", names, want) + } + if adds[filepath.Join(root, "app-a")].Name != "" || adds[filepath.Join(root, ".worktrees", "app-a")].Name != "worktrees-app-a" { + t.Fatalf("saved names: %+v", adds) + } +} + +func TestPlanScanSkipsCheckoutWithItsOwnSkillshare(t *testing.T) { + root := t.TempDir() + // A clone of a shared skills repository: config.yaml is gitignored. + if err := os.MkdirAll(filepath.Join(root, "tools", ".skillshare", "skills"), 0755); err != nil { + t.Fatal(err) + } + rows, adds, err := planScan(&config.Config{}, root, config.ManagedProject{Targets: []string{"codex"}}, []string{"tools"}, "", root, root) + if err != nil { + t.Fatal(err) + } + if len(adds) != 1 || rows[1].Result != ScanSkipped { + t.Fatalf("rows %+v, adds %v", rows, adds) + } +} + +func TestScanCheckoutsSkipsUnreadableFolders(t *testing.T) { + if os.Geteuid() == 0 || runtime.GOOS == "windows" { + t.Skip("test requires POSIX directory permissions and a non-root user") + } + root := t.TempDir() + for _, dir := range []string{filepath.Join(root, "app", ".git"), filepath.Join(root, "private", "secret", ".git")} { + if err := os.MkdirAll(dir, 0755); err != nil { + t.Fatal(err) + } + } + private := filepath.Join(root, "private") + if err := os.Chmod(private, 0); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = os.Chmod(private, 0755) }) + // The scan folder itself sits below the unreadable one. + found, warnings, err := ScanCheckouts(root, []string{".", "private/secret"}) + if err != nil { + t.Fatal(err) + } + if !slices.Equal(found, []string{"app"}) || len(warnings) != 2 || warnings[0].Folder != "private" || warnings[1].Folder != "private/secret" { + t.Fatalf("found %v, warnings %v", found, warnings) + } +} diff --git a/internal/projects/service.go b/internal/projects/service.go index 8a5c486a9..e5b52b8b6 100644 --- a/internal/projects/service.go +++ b/internal/projects/service.go @@ -176,19 +176,10 @@ func (s *Service) Put(root string, entry config.ManagedProject, opts PutOptions) } else if storage == "" { storage = cfg.NewProjectOrigin() } - if storage != OriginShared && storage != OriginLocal { - return Result{}, fmt.Errorf("invalid project storage %q (valid: %s, %s)", storage, OriginShared, OriginLocal) + if err := checkStorage(storage); err != nil { + return Result{}, err } - c, err := PrepareLayers(s.path, cfg, func(l *config.ProjectLayers) error { - if storage == OriginLocal { - l.SetLocal(root, entry) - } else { - // A re-added project is no longer hidden on this machine. - l.SetShared(root, entry) - l.ClearLocal(root) - } - return nil - }) + c, err := layerEntries(s.path, cfg, map[string]config.ManagedProject{root: entry}, storage) if err != nil { return Result{}, err } @@ -213,6 +204,29 @@ func (s *Service) Put(root string, entry config.ManagedProject, opts PutOptions) return result, nil } +func checkStorage(storage string) error { + if storage != OriginShared && storage != OriginLocal { + return fmt.Errorf("invalid project storage %q (valid: %s, %s)", storage, OriginShared, OriginLocal) + } + return nil +} + +// layerEntries saves each entry in storage. An entry saved in config.yaml is +// no longer hidden on this machine. +func layerEntries(path string, cfg *config.Config, entries map[string]config.ManagedProject, storage string) (*Candidate, error) { + return PrepareLayers(path, cfg, func(l *config.ProjectLayers) error { + for root, entry := range entries { + if storage == OriginLocal { + l.SetLocal(root, entry) + } else { + l.SetShared(root, entry) + l.ClearLocal(root) + } + } + return nil + }) +} + // detachDeparting compares paths, rather than target names, so renamed and // shared destinations stay owned. Effective candidates include alias changes. func (s *Service) detachDeparting(before, after *config.Config, affected []string, dryRun bool) (DetachReport, error) { diff --git a/internal/server/handler_projects.go b/internal/server/handler_projects.go index 25ca7cf34..0a2849052 100644 --- a/internal/server/handler_projects.go +++ b/internal/server/handler_projects.go @@ -110,14 +110,9 @@ func (s *Server) handleSaveProject(w http.ResponseWriter, r *http.Request) { writeError(w, http.StatusBadRequest, "root is required") return } - for _, rc := range []*projectResource{body.Skills, body.Agents} { - if rc == nil { - continue - } - if _, err := ssync.FilterSkills(nil, rc.Include, rc.Exclude); err != nil { - writeError(w, http.StatusBadRequest, "invalid include/exclude pattern: "+err.Error()) - return - } + if err := body.checkFilters(); err != nil { + writeError(w, http.StatusBadRequest, err.Error()) + return } s.mu.Lock() @@ -165,6 +160,80 @@ func (s *Server) handleSaveProject(w http.ResponseWriter, r *http.Request) { writeJSON(w, map[string]any{"success": true, "root": root, "origin": result.Item.Origin, "originPath": result.Item.OriginPath}) } +// handleScanProjects adds a folder and each Git checkout below its scan folders +// with the same settings, like project add --scan. It only adds; a dry run +// reports each folder's result and writes nothing. +func (s *Server) handleScanProjects(w http.ResponseWriter, r *http.Request) { + start := time.Now() + var body struct { + projectBody + Scan []string `json:"scan"` + DryRun bool `json:"dryRun"` + } + if err := decodeJSON(w, r, &body, defaultJSONBodyLimit); err != nil { + if !errors.Is(err, errBodyTooLarge) { + writeError(w, http.StatusBadRequest, "invalid JSON body") + } + return + } + if body.Root == "" || len(body.Scan) == 0 { + writeError(w, http.StatusBadRequest, "root and at least one scan folder are required") + return + } + if err := body.checkFilters(); err != nil { + writeError(w, http.StatusBadRequest, err.Error()) + return + } + + s.mu.Lock() + defer s.mu.Unlock() + release, err := projects.LockConfig(r.Context(), s.configPath()) + if err != nil { + writeError(w, http.StatusInternalServerError, err.Error()) + return + } + defer release() + if err := s.reloadProjectsReadOnly(); err != nil { + writeError(w, http.StatusInternalServerError, err.Error()) + return + } + + entry := config.ManagedProject{Name: body.Name, Targets: body.Targets, Skills: body.Skills.config(), Agents: body.Agents.config()} + result, err := s.projectService().Scan(body.Root, entry, body.Scan, projects.PutOptions{DryRun: body.DryRun, Storage: body.Storage}) + if err != nil { + status := http.StatusBadRequest + if errors.Is(err, projects.ErrSave) { + status = http.StatusInternalServerError + } + writeError(w, status, err.Error()) + return + } + // A dry run's Expanded is the candidate config, so only a write installs it. + applied := !body.DryRun && result.Registration == "updated" + if applied { + s.installGlobalConfig(result.Expanded) + s.writeOpsLog("project", "ok", start, map[string]any{"action": "add", "root": body.Root, "scan": body.Scan, "added": result.Added(), "scope": "ui"}, "") + } + warnings := result.Warnings + if warnings == nil { + warnings = []projects.ScanWarning{} + } + writeJSON(w, map[string]any{"applied": applied, "rows": result.Rows, "warnings": warnings}) +} + +// checkFilters rejects include/exclude patterns a sync could not use. +func (b projectBody) checkFilters() error { + for _, rc := range []*projectResource{b.Skills, b.Agents} { + if rc == nil { + continue + } + if _, err := ssync.FilterSkills(nil, rc.Include, rc.Exclude); err != nil { + return fmt.Errorf("invalid include/exclude pattern: %w", err) + } + } + return nil +} + func (s *Server) handleRemoveProject(w http.ResponseWriter, r *http.Request) { start := time.Now() s.mu.Lock() diff --git a/internal/server/handler_projects_test.go b/internal/server/handler_projects_test.go index 57e081aa5..5720fb816 100644 --- a/internal/server/handler_projects_test.go +++ b/internal/server/handler_projects_test.go @@ -12,6 +12,7 @@ import ( "testing" "skillshare/internal/config" + "skillshare/internal/projects" ssync "skillshare/internal/sync" ) @@ -373,3 +374,87 @@ func TestSaveProject_StorageFollowsSettingByDefault(t *testing.T) { t.Fatalf("project is not machine-local: %s", rr.Body) } } + +func TestScanProjects_PreviewThenAdd(t *testing.T) { + s, src := newTestServer(t) + suite := filepath.Join(filepath.Dir(src), "suite") + for _, dir := range []string{"app-a/.git", ".worktrees/app-a", "docs"} { + if err := os.MkdirAll(filepath.Join(suite, dir), 0755); err != nil { + t.Fatal(err) + } + } + if err := os.WriteFile(filepath.Join(suite, ".worktrees", "app-a", ".git"), []byte("gitdir: /elsewhere\n"), 0644); err != nil { + t.Fatal(err) + } + body := func(dryRun bool) string { + return `{"root":"` + suite + `","scan":[".",".worktrees","later"],"targets":["claude"],"skills":{"mode":"","include":[],"exclude":[]},"dryRun":` + map[bool]string{true: "true", false: "false"}[dryRun] + `}` + } + type scanReply struct { + Applied bool `json:"applied"` + Rows []map[string]string `json:"rows"` + Warnings []projects.ScanWarning `json:"warnings"` + } + listed := func() int { + var list struct { + Projects []projectItem `json:"projects"` + } + json.Unmarshal(projectRequest(t, s, http.MethodGet, "/api/projects", "").Body.Bytes(), &list) + return len(list.Projects) + } + + rr := projectRequest(t, s, http.MethodPost, "/api/projects/scan", body(true)) + var preview scanReply + if err := json.Unmarshal(rr.Body.Bytes(), &preview); err != nil || rr.Code != http.StatusOK { + t.Fatalf("preview: %d %s", rr.Code, rr.Body) + } + if preview.Applied || len(preview.Rows) != 3 || listed() != 0 { + t.Fatalf("preview wrote or missed rows: %+v, %d listed", preview, listed()) + } + if len(preview.Warnings) != 1 || preview.Warnings[0].Kind != projects.ScanMissing || preview.Warnings[0].Folder != "later" { + t.Fatalf("warnings %+v", preview.Warnings) + } + + rr = projectRequest(t, s, http.MethodPost, "/api/projects/scan", body(false)) + var added scanReply + json.Unmarshal(rr.Body.Bytes(), &added) + if !added.Applied || listed() != 3 { + t.Fatalf("apply: %s, %d listed", rr.Body, listed()) + } + paths := map[string]string{} + for _, row := range added.Rows { + paths[row["path"]] = row["name"] + } + if paths["."] != "suite" || paths["app-a"] != "app-a" || paths[".worktrees/app-a"] != "worktrees-app-a" { + t.Fatalf("rows %+v", added.Rows) + } + + // A rescan, with the root given with a trailing separator, only keeps. + rr = projectRequest(t, s, http.MethodPost, "/api/projects/scan", strings.Replace(body(false), `"root":"`+suite, `"root":"`+suite+`/`, 1)) + var again scanReply + json.Unmarshal(rr.Body.Bytes(), &again) + for _, row := range again.Rows { + if row["result"] != projects.ScanKept { + t.Fatalf("rescan: %s", rr.Body) + } + } + if again.Applied || listed() != 3 { + t.Fatalf("rescan: %s, %d listed", rr.Body, listed()) + } +} + +func TestScanProjects_RejectsBadRequests(t *testing.T) { + s, src := newTestServer(t) + suite := filepath.Join(filepath.Dir(src), "suite") + if err := os.MkdirAll(suite, 0755); err != nil { + t.Fatal(err) + } + for name, body := range map[string]string{ + "no scan folder": `{"root":"` + suite + `","scan":[],"targets":["claude"]}`, + "folder outside root": `{"root":"` + suite + `","scan":["../other"],"targets":["claude"]}`, + "bad filter": `{"root":"` + suite + `","scan":["."],"targets":["claude"],"skills":{"mode":"","include":["["],"exclude":[]}}`, + } { + if rr := projectRequest(t, s, http.MethodPost, "/api/projects/scan", body); rr.Code != http.StatusBadRequest { + t.Errorf("%s: got %d %s", name, rr.Code, rr.Body) + } + } +} diff --git a/internal/server/server.go b/internal/server/server.go index 825f96192..e8736bd5a 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -549,6 +549,7 @@ func (s *Server) registerRoutes() { s.mux.HandleFunc("PUT /api/projects", s.requireGlobalProjects(s.handleSaveProject)) s.mux.HandleFunc("DELETE /api/projects", s.requireGlobalProjects(s.handleRemoveProject)) s.mux.HandleFunc("POST /api/projects/convert", s.requireGlobalProjects(s.handleConvertProject)) + s.mux.HandleFunc("POST /api/projects/scan", s.requireGlobalProjects(s.handleScanProjects)) // Sync matrix s.mux.HandleFunc("GET /api/sync-matrix", s.handleSyncMatrix) diff --git a/internal/sourcewalk/allowlist.json b/internal/sourcewalk/allowlist.json index f7996f2ad..d529274ad 100644 --- a/internal/sourcewalk/allowlist.json +++ b/internal/sourcewalk/allowlist.json @@ -679,6 +679,14 @@ "reason": "plugin", "note": "Validate plugin source links" }, + { + "file": "internal/projects/scan.go", + "function": "ScanCheckouts", + "callee": "filepath.WalkDir", + "root": "scanRoot", + "reason": "project", + "note": "Find Git checkouts below a project folder for project add --scan" + }, { "file": "internal/resource/agent.go", "function": "(AgentKind).Discover", diff --git a/skills/skillshare/SKILL.md b/skills/skillshare/SKILL.md index fcf3206e7..2f5d062c4 100644 --- a/skills/skillshare/SKILL.md +++ b/skills/skillshare/SKILL.md @@ -158,6 +158,7 @@ skillshare project list --json skillshare project localize ~/work/existing --dry-run skillshare project add ~/work/app --target claude,codex --skills-mode copy --dry-run skillshare project add ~/work/app --target claude,codex --skills-mode copy --sync +skillshare project add ~/work/suite --scan . --scan .worktrees --target codex --dry-run skillshare project edit app --skills-include 'frontend-*' skillshare project sync app --json skillshare project remove app --dry-run --json @@ -168,7 +169,9 @@ skillshare project remove app --dry-run --json `--machine-local` or `projects_storage: machine-local` (`--shared` overrides the setting); `localize` and `share` move one between the two. Removing a local copy of a `config.yaml` project keeps `config.yaml`'s version unless `--shared` is given. Edit filters replace lists. Removal detaches skills and keeps agent files; -JSON removal requires `--yes` unless previewing. Read [targets.md](references/targets.md) +JSON removal requires `--yes` unless previewing. `--scan DIR` also adds each Git checkout +below DIR with the same settings and only adds; rerun it after a new clone or worktree. +Read [targets.md](references/targets.md) for saved-then-sync failures, retries, and partial removal results. ### MCP settings diff --git a/skills/skillshare/references/targets.md b/skills/skillshare/references/targets.md index 607ca52e3..61d39d0da 100644 --- a/skills/skillshare/references/targets.md +++ b/skills/skillshare/references/targets.md @@ -144,6 +144,7 @@ skillshare project remove shop-web --yes --json skillshare project add ~/work/scratch --target claude --machine-local --dry-run skillshare project localize shop-web --dry-run --json skillshare project share scratch --dry-run --json +skillshare project add ~/work/suite --scan . --scan .worktrees --target codex --dry-run --json ``` Always global (explicit `-g` accepted, `-p` rejected), even inside a project. Add requires @@ -167,6 +168,16 @@ exit 1 does not undo the saved registration: inspect `sync.failed` and retry wit mutation JSON includes `action`, `project`, `applied`, `affected`, and `warnings`. No-op edits report `applied: false, unchanged: true`. +`--scan DIR` (add only, repeatable, relative to the root) also registers each Git checkout +below DIR (`.git` folder or worktree `.git` file) as an ordinary project with the same +settings. Hidden folders below DIR, `node_modules` and links are skipped; missing or +unreadable folders are skipped with a warning. It only adds: +registered folders (root included) are `kept` or, with different settings, `skipped`; +checkouts with their own `.skillshare/` are `skipped`. A name clash names the checkout +from its path (`.worktrees/app-a` → `worktrees-app-a`). JSON adds a `scan` array of +`root`, `path` (`.` for the root), `name`, `result`, and for a skipped folder +`skip` and `reason`; with `--sync`, each row's `sync` and `error`. Rerun it after a new clone or worktree. + Removal asks for confirmation unless `--yes` or previewing; JSON removal requires `--yes` unless previewing. Whole-folder links are removed; merge links become local copies while source content exists; copy files remain and their manifest is removed. diff --git a/tests/integration/completion_test.go b/tests/integration/completion_test.go index ce9c66095..bee04c1e4 100644 --- a/tests/integration/completion_test.go +++ b/tests/integration/completion_test.go @@ -47,6 +47,11 @@ func TestCompletion_ProjectLocalize_AllShells(t *testing.T) { result.AssertOutputContains(t, "localize") result.AssertOutputContains(t, "share") result.AssertOutputContains(t, "machine-local") + scan := "--scan" + if shell == "fish" { + scan = "-l scan" + } + result.AssertOutputContains(t, scan) } } diff --git a/tests/integration/project_scan_test.go b/tests/integration/project_scan_test.go new file mode 100644 index 000000000..3eefcd302 --- /dev/null +++ b/tests/integration/project_scan_test.go @@ -0,0 +1,239 @@ +//go:build !online + +package integration + +import ( + "os" + "path/filepath" + "strings" + "testing" + + "skillshare/internal/config" + "skillshare/internal/testutil" +) + +// scanFixture builds a workspace of checkouts, a linked worktree, a plain +// folder and folders the scan must ignore. +func scanFixture(t *testing.T) (*testutil.Sandbox, string) { + t.Helper() + sb, _ := projectFixture(t) + suite := filepath.Join(sb.Home, "suite") + for _, dir := range []string{"app-a", "app-b", "node_modules/pkg", "nested/deep/lib"} { + mkCheckout(t, filepath.Join(suite, dir)) + } + mkWorktree(t, filepath.Join(suite, ".worktrees", "app-a-fix")) + if err := os.MkdirAll(filepath.Join(suite, "docs"), 0755); err != nil { + t.Fatal(err) + } + return sb, suite +} + +func mkCheckout(t *testing.T, dir string) { + t.Helper() + if err := os.MkdirAll(filepath.Join(dir, ".git"), 0755); err != nil { + t.Fatal(err) + } +} + +// A linked worktree has a .git file instead of a directory. +func mkWorktree(t *testing.T, dir string) { + t.Helper() + if err := os.MkdirAll(dir, 0755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, ".git"), []byte("gitdir: /elsewhere\n"), 0644); err != nil { + t.Fatal(err) + } +} + +func scanRows(t *testing.T, payload map[string]any) map[string]map[string]any { + t.Helper() + rows := map[string]map[string]any{} + list, _ := payload["scan"].([]any) + for _, v := range list { + row := v.(map[string]any) + rows[row["root"].(string)] = row + } + return rows +} + +func loadProjects(t *testing.T, sb *testutil.Sandbox) map[string]config.ManagedProject { + t.Helper() + cfg, err := config.LoadReadOnlyPath(sb.ConfigPath) + if err != nil { + t.Fatal(err) + } + return cfg.Projects +} + +func TestProjectScan_RegistersCheckouts(t *testing.T) { + sb, suite := scanFixture(t) + r := sb.RunCLI("project", "add", suite, "--scan", ".", "--scan", ".worktrees", "--target", "codex", "--skills-include", "team-*", "--json") + r.AssertSuccess(t) + rows := scanRows(t, projectJSON(t, r)) + + projects := loadProjects(t, sb) + for _, rel := range []string{"", "app-a", "app-b", ".worktrees/app-a-fix", "nested/deep/lib"} { + root := filepath.Join(suite, rel) + p, ok := projects[root] + if !ok { + t.Fatalf("%s not registered; projects: %v", root, projects) + } + if p.Name != "" || p.Skills == nil || len(p.Skills.Include) != 1 { + t.Fatalf("%s registered with %+v", root, p) + } + if rows[root]["result"] != "added" { + t.Fatalf("%s row: %v", root, rows[root]) + } + } + for _, rel := range []string{"docs", "node_modules/pkg"} { + if _, ok := projects[filepath.Join(suite, rel)]; ok { + t.Fatalf("%s must not be registered", rel) + } + } +} + +func TestProjectScan_DryRunWritesNothing(t *testing.T) { + sb, suite := scanFixture(t) + before := sb.ReadFile(sb.ConfigPath) + r := sb.RunCLI("project", "add", suite, "--scan", ".", "--target", "codex", "-n", "--json") + r.AssertSuccess(t) + payload := projectJSON(t, r) + if payload["applied"] != false || scanRows(t, payload)[filepath.Join(suite, "app-a")]["result"] != "added" { + t.Fatalf("preview: %s", r.Stdout) + } + if sb.ReadFile(sb.ConfigPath) != before { + t.Fatal("dry run wrote config.yaml") + } +} + +func TestProjectScan_RescanOnlyAdds(t *testing.T) { + sb, suite := scanFixture(t) + args := []string{"project", "add", suite, "--scan", ".", "--target", "codex", "--json"} + sb.RunCLI(args...).AssertSuccess(t) + sb.RunCLI("project", "edit", filepath.Join(suite, "app-b"), "--target", "codex,claude").AssertSuccess(t) + mkCheckout(t, filepath.Join(suite, "tools")) + sb.WriteFile(filepath.Join(suite, "tools", ".skillshare", "config.yaml"), "targets: {}\n") + mkCheckout(t, filepath.Join(suite, "app-c")) + + r := sb.RunCLI(args...) + r.AssertSuccess(t) + rows := scanRows(t, projectJSON(t, r)) + want := map[string]string{"": "kept", "app-a": "kept", "app-b": "skipped", "tools": "skipped", "app-c": "added"} + for rel, result := range want { + if row := rows[filepath.Join(suite, rel)]; row["result"] != result { + t.Fatalf("%q: got %v, want %s", rel, row, result) + } + } + if reason, _ := rows[filepath.Join(suite, "app-b")]["reason"].(string); !strings.Contains(reason, "skillshare project edit app-b") { + t.Fatalf("app-b reason: %q", reason) + } + if reason, _ := rows[filepath.Join(suite, "tools")]["reason"].(string); !strings.Contains(reason, ".skillshare") { + t.Fatalf("tools reason: %q", reason) + } + projects := loadProjects(t, sb) + if got := projects[filepath.Join(suite, "app-b")].Targets; len(got) != 2 { + t.Fatalf("rescan changed app-b's targets: %v", got) + } + if _, ok := projects[filepath.Join(suite, "tools")]; ok { + t.Fatal("tools has its own config and must not be registered") + } + + before := sb.ReadFile(sb.ConfigPath) + sb.RunCLI(args...).AssertSuccess(t) + if sb.ReadFile(sb.ConfigPath) != before { + t.Fatal("a rescan with nothing new rewrote config.yaml") + } +} + +func TestProjectScan_LeavesRegisteredRootAlone(t *testing.T) { + sb, suite := scanFixture(t) + sb.RunCLI("project", "add", suite, "--target", "claude").AssertSuccess(t) + r := sb.RunCLI("project", "add", suite, "--scan", ".", "--target", "codex", "--json") + r.AssertSuccess(t) + payload := projectJSON(t, r) + if row := scanRows(t, payload)[suite]; row["result"] != "skipped" { + t.Fatalf("root row: %v", row) + } + if project, _ := payload["project"].(map[string]any); project["root"] != suite { + t.Fatalf("project must be the root: %v", payload["project"]) + } + projects := loadProjects(t, sb) + if got := projects[suite].Targets; len(got) != 1 || got[0] != "claude" { + t.Fatalf("root targets changed: %v", got) + } + if got := projects[filepath.Join(suite, "app-a")].Targets; len(got) != 1 || got[0] != "codex" { + t.Fatalf("app-a targets: %v", got) + } +} + +func TestProjectScan_MachineLocal(t *testing.T) { + sb, suite := scanFixture(t) + before := sb.ReadFile(sb.ConfigPath) + sb.RunCLI("project", "add", suite, "--scan", ".", "--target", "codex", "--machine-local").AssertSuccess(t) + if sb.ReadFile(sb.ConfigPath) != before { + t.Fatal("--machine-local changed config.yaml") + } + if local := localRegistry(t, sb.ConfigPath); !strings.Contains(local, filepath.Join(suite, "app-a")) { + t.Fatalf("local registry lacks app-a:\n%s", local) + } +} + +func TestProjectScan_SyncsEachCheckout(t *testing.T) { + sb, suite := scanFixture(t) + sb.CreateSkill("alpha", map[string]string{"SKILL.md": "---\nname: alpha\n---\n# Alpha\n"}) + r := sb.RunCLI("project", "add", suite, "--scan", ".", "--target", "claude", "--sync") + r.AssertSuccess(t) + for _, rel := range []string{"", "app-a", "app-b"} { + if _, err := os.Stat(filepath.Join(suite, rel, ".claude", "skills", "alpha", "SKILL.md")); err != nil { + t.Fatalf("%q not synced: %v\n%s", rel, err, r.Stdout) + } + } +} + +func TestProjectScan_SyncFailureStopsNoOtherCheckout(t *testing.T) { + sb, suite := scanFixture(t) + sb.CreateSkill("alpha", map[string]string{"SKILL.md": "---\nname: alpha\n---\n# Alpha\n"}) + // A file where app-a's skills folder belongs makes that target invalid. + sb.WriteFile(filepath.Join(suite, "app-a", ".claude", "skills"), "blocked") + args := []string{"project", "add", suite, "--scan", ".", "--target", "claude", "--sync", "--json"} + + before := sb.ReadFile(sb.ConfigPath) + preview := sb.RunCLI(append(args, "-n")...) + if preview.ExitCode == 0 || sb.ReadFile(sb.ConfigPath) != before { + t.Fatalf("preview: exit %d, config changed %v: %s", preview.ExitCode, sb.ReadFile(sb.ConfigPath) != before, preview.Stdout) + } + if _, err := os.Stat(filepath.Join(suite, "app-b", ".claude")); err == nil { + t.Fatal("preview synced app-b") + } + + r := sb.RunCLI(args...) + if r.ExitCode == 0 { + t.Fatalf("a failed checkout sync must exit 1: %s", r.Stdout) + } + rows := scanRows(t, projectJSON(t, r)) + if rows[filepath.Join(suite, "app-a")]["error"] == nil || rows[filepath.Join(suite, "app-b")]["error"] != nil { + t.Fatalf("row errors: %v", rows) + } + if _, err := os.Stat(filepath.Join(suite, "app-b", ".claude", "skills", "alpha", "SKILL.md")); err != nil { + t.Fatalf("app-b not synced after app-a failed: %v", err) + } +} + +func TestProjectScan_Folders(t *testing.T) { + sb, suite := scanFixture(t) + for _, dir := range []string{"..", filepath.Join(sb.Home, "other")} { + r := sb.RunCLI("project", "add", suite, "--scan", dir, "--target", "codex") + if r.ExitCode == 0 { + t.Fatalf("--scan %s accepted", dir) + } + } + r := sb.RunCLI("project", "add", suite, "--scan", ".worktrees", "--scan", "later", "--target", "codex", "--json") + r.AssertSuccess(t) + if warnings, _ := projectJSON(t, r)["warnings"].([]any); len(warnings) != 1 || !strings.Contains(warnings[0].(string), "later") { + t.Fatalf("missing folder warning: %s", r.Stdout) + } + if r := sb.RunCLI("project", "edit", suite, "--scan", "."); r.ExitCode == 0 { + t.Fatal("--scan accepted by project edit") + } +} diff --git a/ui/src/api/targets.ts b/ui/src/api/targets.ts index 09742a4ef..dcd86a620 100644 --- a/ui/src/api/targets.ts +++ b/ui/src/api/targets.ts @@ -4,6 +4,8 @@ import type { ProjectInput, ProjectList, ProjectMutation, + ProjectScan, + ProjectScanInput, SkillsDetach, SkillsOffPreview, Target, @@ -21,6 +23,8 @@ export const targetsApi = { http.delete( `/projects?root=${encodeURIComponent(root)}${shared ? '&shared=true' : ''}`, ), + /** Adds root and each Git checkout below the scan folders; a dry run writes nothing. */ + scanProjects: (input: ProjectScanInput) => http.post('/projects/scan', input), convertProject: (root: string) => http.post('/projects/convert', { root }), /** skillsEnabled false adds a target that skillshare writes no skills to, and creates no skills folder. */ addTarget: ( diff --git a/ui/src/api/types/targets.ts b/ui/src/api/types/targets.ts index 762f7613d..dba4dd0c8 100644 --- a/ui/src/api/types/targets.ts +++ b/ui/src/api/types/targets.ts @@ -35,6 +35,37 @@ export type ProjectInput = Pick & { scan: string[]; dryRun: boolean }; + +/** One folder a scan considered: the picked folder or a checkout below it. */ +export interface ProjectScanRow { + /** The stored root key */ + root: string; + /** Relative to the picked folder; "." for the folder itself */ + path: string; + name: string; + result: 'added' | 'kept' | 'skipped'; + /** Why the folder was skipped, as a code */ + skip?: string; + /** The CLI's English reason, shown for a skip code the dashboard does not know */ + reason?: string; +} + +export interface ProjectScanWarning { + kind: 'missing' | 'unreadable'; + folder: string; + detail?: string; +} + +export interface ProjectScan { + /** False for a dry run, or when nothing new was found */ + applied: boolean; + rows: ProjectScanRow[]; + /** Scan folders that do not exist, and folders that could not be read */ + warnings: ProjectScanWarning[]; +} + export interface ProjectMutation { success: boolean; root: string; diff --git a/ui/src/components/projects/AddProjectDialog.test.tsx b/ui/src/components/projects/AddProjectDialog.test.tsx index 42ff8feac..94a9b7eb8 100644 --- a/ui/src/components/projects/AddProjectDialog.test.tsx +++ b/ui/src/components/projects/AddProjectDialog.test.tsx @@ -2,12 +2,18 @@ import { render, screen, waitFor } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import { api } from '../../api/client'; +import { mcpApi } from '../../api/mcp'; import { I18nProvider } from '../../i18n'; import AddProjectDialog from './AddProjectDialog'; vi.mock('../../api/client', async (load) => ({ ...(await load()), - api: { saveProject: vi.fn() }, + api: { saveProject: vi.fn(), scanProjects: vi.fn() }, +})); + +vi.mock('../../api/mcp', async (load) => ({ + ...(await load()), + mcpApi: { save: vi.fn() }, })); vi.mock('./ProjectTools', () => ({ @@ -25,6 +31,8 @@ const storagePaths = { local: '/home/me/.local/state/skillshare/projects/a.yaml', }; +const onScanned = vi.fn(); + const open = (defaultStorage?: 'shared' | 'local') => render( @@ -36,6 +44,7 @@ const open = (defaultStorage?: 'shared' | 'local') => existing={[]} onClose={() => {}} onAdded={() => {}} + onScanned={onScanned} /> , ); @@ -85,4 +94,72 @@ describe('Add project dialog', () => { expect(api.saveProject).toHaveBeenCalledWith(expect.objectContaining({ storage: 'local' })), ); }); + + it('writes checkouts only after their review is confirmed', async () => { + const row = (path: string) => ({ + root: `~/work/docs/${path}`, + path, + name: path, + result: 'added' as const, + }); + vi.mocked(api.scanProjects) + .mockResolvedValueOnce({ applied: false, rows: [row('.'), row('app-a')], warnings: [] }) + .mockResolvedValueOnce({ applied: true, rows: [row('.'), row('app-a')], warnings: [] }); + open(); + await userEvent.click(screen.getByRole('checkbox', { name: /Also add each checkout/ })); + await userEvent.type(screen.getByLabelText('Look in'), '.worktrees{Enter}'); + await userEvent.type(screen.getByLabelText('Project folder'), '~/work/docs'); + await userEvent.click(screen.getByRole('button', { name: 'pick claude' })); + await userEvent.click(screen.getByRole('button', { name: 'Review' })); + await screen.findByRole('button', { name: 'Add 2 projects' }); + expect(api.scanProjects).toHaveBeenCalledTimes(1); + expect(api.scanProjects).toHaveBeenLastCalledWith( + expect.objectContaining({ root: '~/work/docs', scan: ['.', '.worktrees'], dryRun: true }), + ); + + await userEvent.click(screen.getByRole('button', { name: 'Add 2 projects' })); + await waitFor(() => expect(onScanned).toHaveBeenCalledWith(2)); + expect(api.scanProjects).toHaveBeenLastCalledWith( + expect.objectContaining({ scan: ['.', '.worktrees'], dryRun: false }), + ); + // The default storage is left to the server, as the CLI does without a flag. + expect(vi.mocked(api.scanProjects).mock.lastCall?.[0].storage).toBeUndefined(); + expect(api.saveProject).not.toHaveBeenCalled(); + }); + + it('stays open when the write finds nothing left to add', async () => { + const row = (path: string, result: 'added' | 'kept') => ({ + root: `~/work/docs/${path}`, + path, + name: path, + result, + }); + vi.mocked(api.scanProjects) + .mockResolvedValueOnce({ applied: false, rows: [row('.', 'added')], warnings: [] }) + .mockResolvedValueOnce({ applied: false, rows: [row('.', 'kept')], warnings: [] }); + open(); + await userEvent.click(screen.getByRole('checkbox', { name: /Also add each checkout/ })); + await userEvent.type(screen.getByLabelText('Project folder'), '~/work/docs'); + await userEvent.click(screen.getByRole('button', { name: 'pick claude' })); + await userEvent.click(screen.getByRole('button', { name: 'Review' })); + await userEvent.click(await screen.findByRole('button', { name: 'Add 1 project' })); + await screen.findByText('Nothing new to add.'); + expect(onScanned).not.toHaveBeenCalled(); + }); + + it('returns to the form with the error when MCP fails after the write', async () => { + const row = { root: '~/work/docs', path: '.', name: 'docs', result: 'added' as const }; + vi.mocked(api.scanProjects).mockResolvedValue({ applied: true, rows: [row], warnings: [] }); + vi.mocked(mcpApi.save).mockRejectedValue(new Error('mcp save failed')); + open(); + await userEvent.click(screen.getByRole('checkbox', { name: /^MCP/ })); + await userEvent.click(screen.getByRole('checkbox', { name: /Also add each checkout/ })); + await userEvent.type(screen.getByLabelText('Project folder'), '~/work/docs'); + await userEvent.click(screen.getByRole('button', { name: 'pick claude' })); + await userEvent.click(screen.getByRole('button', { name: 'Review' })); + await userEvent.click(await screen.findByRole('button', { name: 'Add 1 project' })); + await screen.findByText('mcp save failed'); + expect(screen.getByRole('button', { name: 'Review' })).toBeInTheDocument(); + expect(onScanned).not.toHaveBeenCalled(); + }); }); diff --git a/ui/src/components/projects/AddProjectDialog.tsx b/ui/src/components/projects/AddProjectDialog.tsx index ec1aff6c1..023d952bc 100644 --- a/ui/src/components/projects/AddProjectDialog.tsx +++ b/ui/src/components/projects/AddProjectDialog.tsx @@ -1,6 +1,7 @@ import { useState } from 'react'; -import { Check, Folder, X } from 'lucide-react'; -import { api, type ProjectList, type ProjectStorage } from '../../api/client'; +import { Link } from 'react-router-dom'; +import { ArrowLeft, Check, Folder, TriangleAlert, X } from 'lucide-react'; +import { api, type ProjectList, type ProjectScan, type ProjectStorage } from '../../api/client'; import { hooksApi } from '../../api/hooks'; import { mcpApi, mcpTargets } from '../../api/mcp'; import Button from '../Button'; @@ -8,8 +9,10 @@ import { Select } from '../Select'; import DialogShell from '../DialogShell'; import { useT, plural } from '../../i18n'; import { shortenHome } from '../../lib/paths'; +import PatternInput from '../targets/PatternInput'; import ProjectTools from './ProjectTools'; -import { toolGroups } from './projectView'; +import { baseName, projectUrl, toolGroups } from './projectView'; +import { opensProject, renamed, scanCounts, scanNote, scanWarning } from './scanView'; interface Props { tools: ProjectList['tools']; @@ -22,12 +25,28 @@ interface Props { existing: string[]; onClose: () => void; onAdded: (root: string) => void; + /** After adding a folder's checkouts too; count is how many projects were added */ + onScanned: (count: number) => void; } const PARTS = ['skills', 'agents', 'mcp', 'hooks'] as const; type PartName = (typeof PARTS)[number]; +const PART_LABEL: Record = { + skills: 'Skills', + agents: 'Agents', + mcp: 'MCP', + hooks: 'Hooks', +}; +const RESULT = { + added: { label: 'Added', tone: 'ok' }, + kept: { label: 'Kept', tone: 'off' }, + skipped: { label: 'Skipped', tone: 'warn' }, +} as const; -/** Add one folder to projects. Filters and MCP servers are edited on the project's page. */ +/** + * Add one folder to projects, or that folder and each Git checkout inside it + * after a review. Filters and MCP servers are edited on the project's page. + */ export default function AddProjectDialog({ tools, common, @@ -36,6 +55,7 @@ export default function AddProjectDialog({ existing, onClose, onAdded, + onScanned, }: Props) { const t = useT(); const [path, setPath] = useState(''); @@ -47,19 +67,38 @@ export default function AddProjectDialog({ hooks: false, }); const [storage, setStorage] = useState(defaultStorage); + const [scan, setScan] = useState(false); + const [scanDirs, setScanDirs] = useState(['.']); + // The dry run being reviewed; nothing is written until it is confirmed. + const [review, setReview] = useState(null); const [error, setError] = useState(''); const [saving, setSaving] = useState(false); // Trailing separators aside, this is the key config.yaml will hold. const root = path.trim().replace(/(?<=.)[\\/]+$/, ''); const absolute = /^(~($|[\\/])|\/|[A-Za-z]:[\\/])/.test(root); - const taken = existing.some((x) => x === root || shortenHome(x) === root); + const project = (key: string) => existing.find((x) => x === key || shortenHome(x) === key); + // A scan keeps a folder that is already a project, so only a single add refuses it. + const taken = !scan && Boolean(project(root)); const pathError = root && !absolute ? t('projects.add.folderHint') : taken ? t('projects.add.taken') : ''; + // The server refuses these too, but in CLI words. + const scanError = + scan && scanDirs.some((d) => /^([\\/]|[A-Za-z]:)/.test(d) || d.split(/[\\/]/).includes('..')) + ? t('projects.scan.inside') + : ''; const agentTools = targets.filter((x) => tools.find((tool) => tool.name === x)?.agentsPath); const folders = toolGroups(tools, targets).length; + const counts = scanCounts(review?.rows ?? []); const canSave = - Boolean(root) && !pathError && targets.length > 0 && PARTS.some((p) => parts[p]) && !saving; + Boolean(root) && + !pathError && + targets.length > 0 && + PARTS.some((p) => parts[p]) && + (!scan || scanDirs.length > 0) && + !scanError && + (!review || counts.added > 0) && + !saving; const partHint = (p: PartName) => { if (p === 'agents' && targets.length > 0) { @@ -93,30 +132,62 @@ export default function AddProjectDialog({ }, ]; + // MCP and hooks are not part of a project entry; each new project gets them here. + const addParts = async (roots: string[]) => { + const clients = mcpTargets.filter((x) => targets.includes(x)); + for (const project of roots) { + if (parts.mcp) { + await mcpApi.save({ + project, + settings: { targets: clients.length > 0 ? clients : undefined }, + }); + } + // An empty hooks block: the project's own hooks are added on its Hooks tab. + if (parts.hooks) await hooksApi.save({ project }); + } + }; + const save = async () => { if (!canSave) return; setSaving(true); setError(''); const all = { mode: '', include: [], exclude: [] }; + const settings = { + // Like the CLI without a flag, the default storage keeps a project saved in the other place. + storage: scan && storage === defaultStorage ? undefined : storage, + root, + targets, + skills: parts.skills ? all : null, + agents: parts.agents ? all : null, + }; try { - const saved = await api.saveProject({ - create: true, - storage, - root, - targets, - skills: parts.skills ? all : null, - agents: parts.agents ? all : null, - }); - if (parts.mcp) { - const clients = mcpTargets.filter((x) => targets.includes(x)); - await mcpApi.save({ - project: root, - settings: { targets: clients.length > 0 ? clients : undefined }, - }); + if (!scan) { + const saved = await api.saveProject({ create: true, ...settings }); + await addParts([saved.root]); + onAdded(saved.root); + return; } - // An empty hooks block: the project's own hooks are added on its Hooks tab. - if (parts.hooks) await hooksApi.save({ project: saved.root }); - onAdded(saved.root); + const result = await api.scanProjects({ ...settings, scan: scanDirs, dryRun: !review }); + if (!review) { + setReview(result); + setSaving(false); + return; + } + // The write scans again, so count what it added, not what was reviewed. + const added = result.rows.filter((r) => r.result === 'added').map((r) => r.root); + if (added.length === 0) { + setReview(result); + setSaving(false); + return; + } + try { + await addParts(added); + } catch (e) { + // The projects are saved; a new review would only show them as kept. + setReview(null); + throw e; + } + onScanned(added.length); } catch (e) { setError((e as Error).message); setSaving(false); @@ -132,8 +203,21 @@ export default function AddProjectDialog({ ariaLabel={t('projects.add.title')} className="!max-w-[640px]" > -
-

{t('projects.add.title')}

+
+ {review && ( + + )} +

+ {t(review ? 'projects.scan.reviewTitle' : 'projects.add.title')} +

+ ))} +
+
+
+ {t('projects.scan.title')} +
+ - ))} -
-
+ + {t('projects.scan.toggle')} + {t('projects.scan.toggleHint')} + + + {scan && ( +
+ + + + {scanError || t('projects.scan.foldersHint')} + +
+ )} + + + + )} {error && (
{error}
)} -
- +
+ {t('projects.add.savedTo')}