fix(ui): offer only replace for a changed off switch, and name its project

A project's off switch that was edited by hand in the Agent's file shows
up as a conflict with "Import from Agent" next to it. Import reads the
Agent's global file, which holds the server and never the project's
switch, so it brought in something unrelated and the conflict stayed.
Only replacing the entry settles it, so that is the one button offered.

The conflict row also says which project folder it is in: one server
can conflict in several, and two such rows shared a React key.

Refs #289
This commit is contained in:
Willie
2026-09-22 02:01:36 +08:00
parent f1f9ef13ed
commit 626d48fe0a
4 changed files with 20 additions and 7 deletions
+3 -3
View File
@@ -6,7 +6,7 @@ import Badge from '../Badge';
import Button from '../Button';
import { Checkbox } from '../Checkbox';
import { useT } from '../../i18n';
import { countActions, describeMessage, groupByFile, isResolvable, statusVariant } from './mcpView';
import { canImportConflict, countActions, describeMessage, groupByFile, isResolvable, statusVariant } from './mcpView';
export type MCPResolve = (target: string, name: string, action: 'import' | 'replace') => void;
@@ -49,9 +49,9 @@ export default function MCPPreview({ plan, onResolve, busy = false }: Props) {
{change.message ? <span className="text-sm text-pencil-light">{describeMessage(t, change.message)}</span> : null}
</div>
{onResolve && isResolvable(change) ? <div className="flex flex-wrap items-center gap-2">
<Button size="sm" variant="secondary" disabled={busy} onClick={() => onResolve(change.target, change.name, 'import')}>{t('mcp.importFromAgent', { target: change.target })}</Button>
{canImportConflict(change) ? <Button size="sm" variant="secondary" disabled={busy} onClick={() => onResolve(change.target, change.name, 'import')}>{t('mcp.importFromAgent', { target: change.target })}</Button> : null}
<Button size="sm" variant="secondary" loading={busy} onClick={() => onResolve(change.target, change.name, 'replace')}>{t('mcp.replace')}</Button>
<span className="text-xs text-pencil-light">{t('mcp.resolveHint', { target: change.target })}</span>
{canImportConflict(change) ? <span className="text-xs text-pencil-light">{t('mcp.resolveHint', { target: change.target })}</span> : null}
</div> : null}
</li>)}
</ul>
+9 -1
View File
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
import type { MCPPlan } from '../../api/mcp';
import { buildMatrix, describeError, describeMessage, groupByFile, isResolvable, joinCommand, splitCommand, switchTargets, targetLabel } from './mcpView';
import { buildMatrix, describeError, describeMessage, canImportConflict, groupByFile, isResolvable, joinCommand, splitCommand, switchTargets, targetLabel } from './mcpView';
import { mcpTargets } from '../../api/mcp';
const change = (name: string, target: string, action: string, message?: string) => ({ name, target, action, message, path: `/${target}.json` });
@@ -81,3 +81,11 @@ describe('groupByFile', () => {
expect(groups.map((g) => g.offListFor)).toEqual([undefined, '/work/app', undefined]);
});
});
describe('canImportConflict', () => {
// Import reads the Agent's global file, which holds the server and never the project's switch.
it('offers no import for a changed switch, which only a replace can settle', () => {
const changed = { target: 'opencode', path: '/work/app/opencode.json', name: 'docs', action: 'conflict', message: 'Agent configuration changed; import it or explicitly replace this entry' };
expect([canImportConflict(changed), canImportConflict({ ...changed, root: '/work/app', switch: true })]).toEqual([true, false]);
});
});
+3
View File
@@ -111,6 +111,9 @@ export const isResolvable = (change: MCPChange) =>
change.action === 'conflict' &&
['mcp.conflictChanged', 'mcp.conflictUnmanaged', 'mcp.conflictOrphaned'].includes(conflictKeys[conflictPrefix(change.message ?? '')]);
/** Whether importing can settle a conflict. It reads the Agent's global file, which never holds a project's switch. */
export const canImportConflict = (change: MCPChange) => isResolvable(change) && !change.switch;
/** Agents a project can turn a global server off for: those it uses, that the server reaches and that have a switch. */
export const switchTargets = (server: MCPServer, defaults: string[], projectTargets: readonly string[]) =>
(server.targets ?? defaults).filter((x) => mcpOffTargets.includes(x) && projectTargets.includes(x) && (x !== 'pi' || server.piExtension === 'pi-mcp-adapter'));
+5 -3
View File
@@ -22,7 +22,7 @@ import { MCPConfigDialog } from '../components/mcp/MCPConfigView';
import MCPRemoveDialog from '../components/mcp/MCPRemoveDialog';
import MCPRestoreDialog from '../components/mcp/MCPRestoreDialog';
import MCPServerDialog from '../components/mcp/MCPServerDialog';
import { buildMatrix, describeError, describeMessage, isShadowed, isResolvable, targetLabel, type MCPChange } from '../components/mcp/mcpView';
import { buildMatrix, canImportConflict, describeError, describeMessage, isShadowed, isResolvable, projectOf, targetLabel, type MCPChange } from '../components/mcp/mcpView';
import { useT } from '../i18n';
import { shortenHome } from '../lib/paths';
import { queryKeys } from '../lib/queryKeys';
@@ -214,13 +214,15 @@ export default function MCPPage() {
<AlertCircle size={16} className="self-start mt-0.5" />
<div className="flex flex-1 flex-col gap-2">
{conflicts.map((c, i) => (
<div key={`${c.target}:${c.name}`} className="flex items-center gap-3">
<div key={`${c.path}:${c.target}:${c.name}`} className="flex items-center gap-3">
<span className="flex-1">
{i === 0 && <b>{t(conflicts.length === 1 ? 'mcp.conflictLead.one' : 'mcp.conflictLead.other', { count: conflicts.length })} </b>}
{conflictText(c)}
{/* One server can conflict in several folders, so a project's row says which. */}
{projectOf(roots, c) && <span className="text-ink-2"> · {shortenHome(projectOf(roots, c)!)}</span>}
</span>
{isResolvable(c) && <>
<Button size="sm" variant="secondary" disabled={busy} onClick={() => void resolve(c.target, c.name, 'import')}>{t('mcp.importFromAgent', { target: targetLabel(c.target) })}</Button>
{canImportConflict(c) && <Button size="sm" variant="secondary" disabled={busy} onClick={() => void resolve(c.target, c.name, 'import')}>{t('mcp.importFromAgent', { target: targetLabel(c.target) })}</Button>}
<Button size="sm" variant="secondary" disabled={busy} onClick={() => void resolve(c.target, c.name, 'replace')}>{t('mcp.replace')}</Button>
</>}
</div>