From 7a4103b9a8f3538c61407f275cb2bf90c40b30dc Mon Sep 17 00:00:00 2001 From: Willie Date: Thu, 8 Oct 2026 16:01:35 +0800 Subject: [PATCH] refactor(extras): validate every mode change and share special extra names cmdExtrasMode only ran ValidateExtraConfig behind a growing list of conditions, so each new restriction needed another clause. For a plain folder extra the full check adds only the mode-name and flatten+symlink rules, which sync already rejects, so validating unconditionally cannot refuse a config that syncs today. The agents and memory extra names were string literals in sync, the server handlers, the memory store, and the CLI. They now live in internal/config, and the rename guard moves to ValidateExtraRename so a future CLI rename reuses it instead of copying the check. Refs #489 --- cmd/skillshare/extras.go | 8 +++++-- cmd/skillshare/extras_mode.go | 9 +++----- cmd/skillshare/sync_extras.go | 7 ++----- internal/config/extras_validate.go | 19 +++++++++++++++++ internal/memory/store.go | 4 ++-- internal/server/handler_extras.go | 2 +- internal/server/handler_extras_edit.go | 11 +++------- internal/sync/extras_run.go | 4 ++-- tests/integration/extras_test.go | 29 ++++++++++++++++++++++++++ 9 files changed, 67 insertions(+), 26 deletions(-) diff --git a/cmd/skillshare/extras.go b/cmd/skillshare/extras.go index ab0af75ec..d00ec4575 100644 --- a/cmd/skillshare/extras.go +++ b/cmd/skillshare/extras.go @@ -1,6 +1,10 @@ package main -import "fmt" +import ( + "fmt" + + "skillshare/internal/config" +) func cmdExtras(args []string) error { if len(args) == 0 { @@ -35,7 +39,7 @@ func cmdExtras(args []string) error { return cmdExtrasCollect(rest) case "source": return cmdExtrasSource(rest) - case "memory": + case config.MemoryExtraName: return cmdExtrasMemory(rest) case "--help", "-h": printExtrasHelp() diff --git a/cmd/skillshare/extras_mode.go b/cmd/skillshare/extras_mode.go index 42e1a2b39..09cbf3d75 100644 --- a/cmd/skillshare/extras_mode.go +++ b/cmd/skillshare/extras_mode.go @@ -181,12 +181,9 @@ func cmdExtrasMode(args []string) error { return filterErr } } - // Filters rule out single files and symlink mode, so check them whenever - // either side changes. - if _, updated := findExtraByName(extras, name); updated.File != "" || config.ManagedExtraMode(syncMode) || syncMode == "symlink" || filters.hasUpdates() { - if err := config.ValidateExtraConfig(updated); err != nil { - return err - } + _, updated := findExtraByName(extras, name) + if err := config.ValidateExtraConfig(updated); err != nil { + return err } if err := validateFn(); err != nil { diff --git a/cmd/skillshare/sync_extras.go b/cmd/skillshare/sync_extras.go index 272cacf23..2c3be7b52 100644 --- a/cmd/skillshare/sync_extras.go +++ b/cmd/skillshare/sync_extras.go @@ -14,9 +14,6 @@ import ( "skillshare/internal/ui" ) -// extrasAgentsName is the extras entry name that may overlap with the agents sync system. -const extrasAgentsName = "agents" - type syncExtrasJSONOutput struct { Extras []syncExtrasJSONEntry `json:"extras"` Duration string `json:"duration"` @@ -110,7 +107,7 @@ func cmdSyncExtrasGlobal(dryRun, force, jsonOutput, partOfAll bool, start time.T // Detect overlap between extras "agents" and the agents sync system var agentTargetPaths map[string]bool for _, extra := range cfg.Extras { - if extra.Name == extrasAgentsName { + if extra.Name == config.AgentsExtraName { agentTargetPaths = collectAgentTargetPathsGlobal(cfg) break } @@ -220,7 +217,7 @@ func cmdSyncExtrasProject(cwd string, dryRun, force, jsonOutput, partOfAll bool, // Detect overlap between extras "agents" and the agents sync system var agentTargetPaths map[string]bool for _, extra := range projCfg.Extras { - if extra.Name == extrasAgentsName { + if extra.Name == config.AgentsExtraName { agentTargetPaths = collectAgentTargetPathsProject(cwd) break } diff --git a/internal/config/extras_validate.go b/internal/config/extras_validate.go index d07ace425..548e3fa1e 100644 --- a/internal/config/extras_validate.go +++ b/internal/config/extras_validate.go @@ -79,6 +79,25 @@ func ValidateExtraNameUnique(name string, existing []ExtraConfig) error { return nil } +// Extras with these names get special handling: sync skips agent target +// folders for the extra named agents, and the memory notes live in the extra +// named memory. +const ( + AgentsExtraName = "agents" + MemoryExtraName = "memory" +) + +// ValidateExtraRename rejects renaming an extra to or from a name with +// special handling, ignoring case. +func ValidateExtraRename(oldName, newName string) error { + for _, special := range []string{AgentsExtraName, MemoryExtraName} { + if strings.EqualFold(oldName, special) || strings.EqualFold(newName, special) { + return fmt.Errorf("an extra cannot be renamed to or from %s", special) + } + } + return nil +} + // ValidateExtraConfig checks an extra's single-file settings and target modes. // file and as must be plain filenames; as, import mode, and the absence of // flatten are tied to file; an extension cannot transform a single file. diff --git a/internal/memory/store.go b/internal/memory/store.go index c4dcab5aa..a0e8949c6 100644 --- a/internal/memory/store.go +++ b/internal/memory/store.go @@ -36,14 +36,14 @@ type Note struct { // Extra reuses the existing named source, including its per-extra override. func Extra(extras []config.ExtraConfig) (config.ExtraConfig, bool, error) { for _, extra := range extras { - if strings.EqualFold(extra.Name, "memory") { + if strings.EqualFold(extra.Name, config.MemoryExtraName) { if extra.File != "" { return extra, true, fmt.Errorf("extra %q must be a folder to manage memory notes", extra.Name) } return extra, true, nil } } - return config.ExtraConfig{Name: "memory"}, false, nil + return config.ExtraConfig{Name: config.MemoryExtraName}, false, nil } func GlobalRoot(cfg *config.Config) (string, error) { diff --git a/internal/server/handler_extras.go b/internal/server/handler_extras.go index 8a0626a33..fada9b245 100644 --- a/internal/server/handler_extras.go +++ b/internal/server/handler_extras.go @@ -697,7 +697,7 @@ func (s *Server) syncExtras(name string, dryRun, force bool) []extraSyncResult { ResolveExtension: s.resolveExtensionSpec, } for _, extra := range s.extrasConfig() { - if extra.Name == "agents" { + if extra.Name == config.AgentsExtraName { opts.AgentTargetPaths = s.extrasAgentTargetPaths() break } diff --git a/internal/server/handler_extras_edit.go b/internal/server/handler_extras_edit.go index dd14c90b0..99a038fd8 100644 --- a/internal/server/handler_extras_edit.go +++ b/internal/server/handler_extras_edit.go @@ -7,7 +7,6 @@ import ( "os" "path/filepath" "slices" - "strings" "time" "skillshare/internal/config" @@ -199,13 +198,9 @@ func (s *Server) handleExtrasEdit(w http.ResponseWriter, r *http.Request) { next := extra if body.Name != "" && body.Name != name { - // Sync skips agent target folders for the extra named agents, and the - // memory notes live in the extra named memory. - for _, special := range []string{"agents", "memory"} { - if strings.EqualFold(name, special) || strings.EqualFold(body.Name, special) { - writeError(w, http.StatusBadRequest, "an extra cannot be renamed to or from "+special) - return - } + if err := config.ValidateExtraRename(name, body.Name); err != nil { + writeError(w, http.StatusBadRequest, err.Error()) + return } if err := config.ValidateExtraName(body.Name); err != nil { writeError(w, http.StatusBadRequest, err.Error()) diff --git a/internal/sync/extras_run.go b/internal/sync/extras_run.go index b5ceea37d..4ccc0395b 100644 --- a/internal/sync/extras_run.go +++ b/internal/sync/extras_run.go @@ -65,8 +65,8 @@ func RunExtraTargets(extra config.ExtraConfig, sourceDir string, opts ExtraRunOp func runExtraTarget(extra config.ExtraConfig, t config.ExtraTargetConfig, sourceDir string, opts ExtraRunOptions) ExtraTargetRun { tr := ExtraTargetRun{Target: t, Path: opts.ResolvePath(t.Path), Mode: EffectiveMode(t.Mode)} - if extra.Name == "agents" && opts.AgentTargetPaths[filepath.Clean(tr.Path)] { - tr.SkippedBy = "agents" + if extra.Name == config.AgentsExtraName && opts.AgentTargetPaths[filepath.Clean(tr.Path)] { + tr.SkippedBy = config.AgentsExtraName return tr } diff --git a/tests/integration/extras_test.go b/tests/integration/extras_test.go index 00237b759..ccf7eea02 100644 --- a/tests/integration/extras_test.go +++ b/tests/integration/extras_test.go @@ -901,6 +901,35 @@ extras: } } +// TestExtras_Mode_FlattenFolderExtra verifies that a flatten-only change on a +// plain folder extra passes the full extra validation. +func TestExtras_Mode_FlattenFolderExtra(t *testing.T) { + sb := testutil.NewSandbox(t) + defer sb.Cleanup() + + rulesTarget := filepath.Join(sb.Home, ".claude", "rules") + os.MkdirAll(rulesTarget, 0755) + + claudeTarget := sb.CreateTarget("claude") + sb.WriteConfig(`source: ` + sb.SourcePath + ` +targets: + claude: + path: ` + claudeTarget + ` +extras: + - name: rules + targets: + - path: ` + rulesTarget + ` + mode: copy +`) + + result := sb.RunCLI("extras", "rules", "--flatten", "-g") + + result.AssertSuccess(t) + if configContent := sb.ReadFile(sb.ConfigPath); !strings.Contains(configContent, "flatten: true") { + t.Errorf("expected config to contain 'flatten: true', got:\n%s", configContent) + } +} + // TestExtras_Mode_MultipleTargets_NoTarget verifies that "extras --mode" errors // when the extra has multiple targets and --target is not specified. func TestExtras_Mode_MultipleTargets_NoTarget(t *testing.T) {