-
Notifications
You must be signed in to change notification settings - Fork 3
feat(migration): auto-migrate configs after CLI upgrade #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+409
−1
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| package migration | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/josephgoksu/TaskWing/internal/bootstrap" | ||
| "github.com/josephgoksu/TaskWing/internal/mcpcfg" | ||
| ) | ||
|
|
||
| // CheckAndMigrate runs a post-upgrade migration if the CLI version has changed | ||
| // since the last run in this project. It silently regenerates local configs and | ||
| // returns warnings for issues that require manual intervention (e.g., global MCP). | ||
| // | ||
| // This is designed to be called from PersistentPreRunE and must be: | ||
| // - Sub-millisecond on the happy path (version matches) | ||
| // - Non-fatal on all error paths (never blocks user commands) | ||
| func CheckAndMigrate(projectDir, currentVersion string) (warnings []string, err error) { | ||
| taskwingDir := filepath.Join(projectDir, ".taskwing") | ||
| versionFile := filepath.Join(taskwingDir, "version") | ||
|
|
||
| // Not bootstrapped or inaccessible — nothing to migrate | ||
| if _, err := os.Stat(taskwingDir); err != nil { | ||
| return nil, nil | ||
| } | ||
|
|
||
| stored, err := os.ReadFile(versionFile) | ||
| if err != nil { | ||
| // Version file missing (pre-migration bootstrap). Write current and return. | ||
| if werr := os.WriteFile(versionFile, []byte(currentVersion), 0644); werr != nil { | ||
| fmt.Fprintf(os.Stderr, "⚠️ taskwing: could not write version stamp (%v); migration will re-run next time\n", werr) | ||
| } | ||
| return nil, nil | ||
| } | ||
josephgoksu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| storedVersion := strings.TrimSpace(string(stored)) | ||
|
|
||
| // Happy path: version matches — no-op | ||
| if storedVersion == currentVersion { | ||
| return nil, nil | ||
| } | ||
|
|
||
| // Skip dev builds to avoid constant re-runs during development | ||
| if currentVersion == "dev" { | ||
| return nil, nil | ||
| } | ||
|
|
||
| // --- Version mismatch: run migration --- | ||
|
|
||
| // 1. Silent local migration: regenerate slash commands for managed AIs | ||
| migrateLocalConfigs(projectDir) | ||
|
|
||
| // 2. Global MCP check: warn about legacy server names | ||
| warnings = checkGlobalMCPLegacy() | ||
|
|
||
| // 3. Write current version | ||
| if werr := os.WriteFile(versionFile, []byte(currentVersion), 0644); werr != nil { | ||
| fmt.Fprintf(os.Stderr, "⚠️ taskwing: could not write version stamp (%v); migration will re-run next time\n", werr) | ||
| } | ||
|
|
||
| return warnings, nil | ||
| } | ||
|
|
||
| // migrateLocalConfigs detects which AIs have managed markers and regenerates | ||
| // their slash commands (which internally prunes stale tw-* files). | ||
| func migrateLocalConfigs(projectDir string) { | ||
| for _, aiName := range bootstrap.ValidAINames() { | ||
| cfg, ok := bootstrap.AIHelperByName(aiName) | ||
| if !ok { | ||
| continue | ||
| } | ||
|
|
||
| // Check if this AI has a managed marker | ||
| if cfg.SingleFile { | ||
| // Single-file AIs (e.g., Copilot) embed the marker in file content. | ||
| // Check for the embedded marker before regenerating. | ||
| filePath := filepath.Join(projectDir, cfg.CommandsDir, cfg.SingleFileName) | ||
| content, err := os.ReadFile(filePath) | ||
| if err != nil || !strings.Contains(string(content), "<!-- TASKWING_MANAGED -->") { | ||
| continue | ||
| } | ||
| } else { | ||
| markerPath := filepath.Join(projectDir, cfg.CommandsDir, bootstrap.TaskWingManagedFile) | ||
| if _, err := os.Stat(markerPath); err != nil { | ||
| continue | ||
| } | ||
| } | ||
|
|
||
| // Regenerate (this prunes stale files and creates new ones) | ||
| initializer := bootstrap.NewInitializer(projectDir) | ||
| if err := initializer.CreateSlashCommands(aiName, false); err != nil { | ||
| fmt.Fprintf(os.Stderr, "⚠️ taskwing: could not regenerate %s commands: %v\n", aiName, err) | ||
| } | ||
| } | ||
josephgoksu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // checkGlobalMCPLegacy reads Claude's global MCP config and warns if legacy | ||
| // server names are present. | ||
| func checkGlobalMCPLegacy() []string { | ||
| home, err := os.UserHomeDir() | ||
| if err != nil { | ||
| return nil | ||
| } | ||
| configPath := filepath.Join(home, ".claude", "claude_desktop_config.json") | ||
| return checkGlobalMCPLegacyAt(configPath) | ||
| } | ||
|
|
||
| // checkGlobalMCPLegacyAt checks a specific config file path for legacy server names. | ||
| func checkGlobalMCPLegacyAt(configPath string) []string { | ||
| content, err := os.ReadFile(configPath) | ||
| if err != nil { | ||
| return nil | ||
| } | ||
|
|
||
| var config struct { | ||
| MCPServers map[string]json.RawMessage `json:"mcpServers"` | ||
| } | ||
| if err := json.Unmarshal(content, &config); err != nil { | ||
| return nil | ||
| } | ||
|
|
||
| var warnings []string | ||
| for name := range config.MCPServers { | ||
| if mcpcfg.IsLegacyServerName(name) { | ||
| warnings = append(warnings, fmt.Sprintf("Global MCP config has legacy server name %q. Run: taskwing doctor --fix --yes", name)) | ||
| } | ||
| } | ||
|
|
||
| return warnings | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.