From 5a6873ec967477c5d4b6a3045de8074ec1e54e09 Mon Sep 17 00:00:00 2001 From: Willie Date: Mon, 16 Nov 2026 15:48:20 +0800 Subject: [PATCH] fix(mcp): stop conflict resolution looping between Agents (#919) When Agents hold different definitions of one server, importing one Agent's version was refused with "unresolved conflicts prevented adoption", because any conflict in the plan blocked it, including another Agent's. The source was saved anyway, so the Agents that had matched the old definition now conflicted, and importing from them flipped the source back. Replace with source sent one resolution at a time, so with two conflicts the plan stayed blocked and the dialog could never apply. Gate adoption only on the resolved entries' own conflicts, in the API save and in the interactive CLI import. Replace with source now settles every conflict of that server at once. After a conflict import, a translated hint names the Agents in the same scope that still need Replace with source, and the remaining adoption error is translated. --- cmd/skillshare/mcp_interactive.go | 9 +++-- cmd/skillshare/mcp_tui_test.go | 24 +++++++++++++ internal/mcp/mutate.go | 13 ++++++- internal/mcp/sync_test.go | 34 +++++++++++++++++++ .../components/mcp/MCPImportDialog.test.tsx | 3 +- ui/src/components/mcp/MCPImportDialog.tsx | 17 ++++++++-- ui/src/components/mcp/mcpView.test.ts | 23 ++++++++++++- ui/src/components/mcp/mcpView.ts | 5 +++ ui/src/i18n/locales/de.json | 2 ++ ui/src/i18n/locales/en.json | 2 ++ ui/src/i18n/locales/es.json | 2 ++ ui/src/i18n/locales/fa.json | 2 ++ ui/src/i18n/locales/fr.json | 2 ++ ui/src/i18n/locales/id.json | 2 ++ ui/src/i18n/locales/ja.json | 2 ++ ui/src/i18n/locales/ko.json | 2 ++ ui/src/i18n/locales/pt-BR.json | 2 ++ ui/src/i18n/locales/zh-CN.json | 2 ++ ui/src/i18n/locales/zh-TW.json | 2 ++ ui/src/pages/MCPPage.test.tsx | 21 ++++++++++++ ui/src/pages/MCPPage.tsx | 6 +++- .../docs/how-to/daily-tasks/sharing-mcp.md | 6 +++- website/docs/reference/commands/mcp.md | 6 +++- .../current/how-to/daily-tasks/sharing-mcp.md | 5 ++- .../current/reference/commands/mcp.md | 4 +++ .../current/how-to/daily-tasks/sharing-mcp.md | 5 ++- .../current/reference/commands/mcp.md | 4 +++ .../current/how-to/daily-tasks/sharing-mcp.md | 5 ++- .../current/reference/commands/mcp.md | 5 ++- .../current/how-to/daily-tasks/sharing-mcp.md | 5 ++- .../current/reference/commands/mcp.md | 5 ++- 31 files changed, 209 insertions(+), 18 deletions(-) diff --git a/cmd/skillshare/mcp_interactive.go b/cmd/skillshare/mcp_interactive.go index 20c0957ea..dbe50ea51 100644 --- a/cmd/skillshare/mcp_interactive.go +++ b/cmd/skillshare/mcp_interactive.go @@ -128,10 +128,13 @@ func reviewMCPMutations(service *mcp.Service, mutations []mcp.Mutation, o mcpOpt } choices := []checklistItemData{{label: "Save only", desc: "Update the source; leave Agent files unchanged"}} if p.Blocked { + // Another Agent's conflict does not stop saving an import; only the imported entry's own does. + var resolutions []mcp.Resolution for _, mutation := range mutations { - if len(mutation.Resolutions) > 0 { - return fmt.Errorf("resolve MCP conflicts before importing; no settings saved") - } + resolutions = append(resolutions, mutation.Resolutions...) + } + if p.ResolutionConflicts(resolutions) { + return fmt.Errorf("resolve MCP conflicts before importing; no settings saved") } } else { choices = append([]checklistItemData{{label: "Save and sync", desc: "Apply the previewed changes to Agent files"}}, choices...) diff --git a/cmd/skillshare/mcp_tui_test.go b/cmd/skillshare/mcp_tui_test.go index e4ae3f0eb..e9cd1f67a 100644 --- a/cmd/skillshare/mcp_tui_test.go +++ b/cmd/skillshare/mcp_tui_test.go @@ -113,6 +113,30 @@ func TestMCPBatchImportCancelAndSaveOnly(t *testing.T) { } } +// Codex holds a different docs than Claude. Importing Claude's still saves, so the user can +// replace Codex's next instead of being told to resolve a conflict the import cannot settle. +func TestMCPImportSavesDespiteAnotherAgentsConflict(t *testing.T) { + s := mcpTUIService(t) + if err := os.WriteFile(s.ConfigPath, []byte("mcp: {targets: [claude, codex], servers: {}}\n"), 0600); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(s.Home, ".claude.json"), []byte(`{"mcpServers":{"docs":{"command":"a"}}}`), 0600); err != nil { + t.Fatal(err) + } + if err := os.MkdirAll(filepath.Join(s.Home, ".codex"), 0700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(s.Home, ".codex", "config.toml"), []byte("[mcp_servers.docs]\ncommand = \"b\"\n"), 0600); err != nil { + t.Fatal(err) + } + candidates := []mcp.Candidate{{Name: "docs", Server: mcp.Server{Command: "a"}, From: "claude"}} + // The server, both Agents, then Save only: the one choice a blocked plan offers. + prompts := &scriptedMCPPrompts{choices: [][]int{{0}, {0, 1}, {0}}} + if err := mcpBatchImportWizard(s, candidates, mcpOptions{}, prompts); err != nil { + t.Fatalf("another Agent's conflict refused the import: %v", err) + } +} + func TestMCPReviewDryRunNeverPromptsOrWrites(t *testing.T) { s := mcpTUIService(t) before, _ := os.ReadFile(s.ConfigPath) diff --git a/internal/mcp/mutate.go b/internal/mcp/mutate.go index 858d0eb4d..5b59b6ead 100644 --- a/internal/mcp/mutate.go +++ b/internal/mcp/mutate.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "path/filepath" + "slices" "gopkg.in/yaml.v3" "skillshare/internal/utils" @@ -173,6 +174,14 @@ func (s *Service) PreviewMutations(mutations []Mutation) (*Plan, error) { return s.previewResolved(source, batchResolutions(mutations)) } +// ResolutionConflicts reports whether an entry one of resolutions names is still a conflict. +// Other entries may conflict without stopping a save that only adopts or replaces these. +func (p *Plan) ResolutionConflicts(resolutions []Resolution) bool { + return slices.ContainsFunc(p.Changes, func(c Change) bool { + return c.Action == "conflict" && slices.ContainsFunc(resolutions, func(r Resolution) bool { return r.Target == c.Target && r.Name == c.Name }) + }) +} + func batchResolutions(mutations []Mutation) []Resolution { var resolutions []Resolution for _, m := range mutations { @@ -419,7 +428,9 @@ func (s *Service) MutateBatch(mutations []Mutation, revision string, sync bool) if !sync { // Saving an explicit import also records the native baseline. The next // sync can update it, but still detects any edits made in the meantime. - if len(resolutions) > 0 && preview.Blocked { + // Only the resolved entries have to settle: another Agent that holds a + // different definition of the server stays a conflict of its own. + if len(resolutions) > 0 && preview.ResolutionConflicts(resolutions) { return nil, fmt.Errorf("source saved; unresolved conflicts prevented adoption") } // Stopping to manage a server drops those records instead, so sync leaves its entries be. diff --git a/internal/mcp/sync_test.go b/internal/mcp/sync_test.go index 4e9bcdd62..5d35dc56b 100644 --- a/internal/mcp/sync_test.go +++ b/internal/mcp/sync_test.go @@ -171,6 +171,40 @@ func TestTickingTheImportedAgentLaterTakesItsEntryOver(t *testing.T) { } } +// Two Agents hold different definitions of one server. Importing one of them takes that +// entry over even though the other is still a conflict, so the user can replace the other +// next instead of importing back and forth. +func TestImportAdoptsDespiteAnotherAgentsConflict(t *testing.T) { + s := testService(t) + claude := filepath.Join(s.Home, ".claude.json") + // Laid out as sync writes it, so taking it over leaves nothing else to do. + if err := os.WriteFile(claude, []byte("{\n \"mcpServers\": {\n \"docs\": {\n \"command\": \"a\"\n }\n }\n}\n"), 0600); err != nil { + t.Fatal(err) + } + codex := filepath.Join(s.Home, ".codex", "config.toml") + if err := os.MkdirAll(filepath.Dir(codex), 0700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(codex, []byte("[mcp_servers.docs]\ncommand = \"b\"\n"), 0600); err != nil { + t.Fatal(err) + } + server := Server{Command: "a"} + adopt := []Resolution{{Target: "claude", Name: "docs", Action: "adopt"}} + if _, err := s.Mutate(Mutation{Name: "docs", Server: &server, Replace: true, Resolutions: adopt}, "", false); err != nil { + t.Fatalf("another Agent's conflict refused the import: %v", err) + } + plan, err := s.Preview() + if err != nil { + t.Fatal(err) + } + if c := changeFor(plan, claude, "docs"); c == nil || c.Action != "unchanged" { + t.Fatalf("the imported entry was not taken over: %+v", c) + } + if c := changeFor(plan, codex, "docs"); c == nil || c.Action != "conflict" { + t.Fatalf("the other Agent's entry must stay a conflict: %+v", c) + } +} + func TestAgentDefaultsAreNotDrift(t *testing.T) { s := testService(t) if _, err := s.Apply(""); err != nil { diff --git a/ui/src/components/mcp/MCPImportDialog.test.tsx b/ui/src/components/mcp/MCPImportDialog.test.tsx index 66456d40b..86f96ba58 100644 --- a/ui/src/components/mcp/MCPImportDialog.test.tsx +++ b/ui/src/components/mcp/MCPImportDialog.test.tsx @@ -12,7 +12,7 @@ import { MCPTargetOrder } from './targetOrder'; vi.mock('../CodeEditor', () => ({ default: ({ value, onChange, ariaLabel }: { value: string; onChange: (v: string) => void; ariaLabel: string }) =>