Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(settings): read and write a global capability row in the same scope

liuzixiang1a8996a10b1d
diff --git a/apps/desktop/src/components/settings/AgentMcpPage.tsx b/apps/desktop/src/components/settings/AgentMcpPage.tsx
index 1a16c74e2..6cfa8b2cf 100644
--- a/apps/desktop/src/components/settings/AgentMcpPage.tsx
+++ b/apps/desktop/src/components/settings/AgentMcpPage.tsx
@@ -7,6 +7,7 @@ import {
type McpServerStatus,
} from "@pi-desktop/shared";
import { api } from "../../lib/api";
+import { capabilityQuery } from "../../lib/capability-scope";
import { useAppStore } from "../../stores/app-store";
import { useHostCollection } from "../../hooks/use-host-collection";
import { useRowBusy } from "../../hooks/use-row-busy";
@@ -83,11 +84,11 @@ export function AgentMcpPage() {
const showToast = useAppStore((state) => state.showToast);
const { selectedProjectPath, setSelectedProjectPath, options } = useAgentProjects();
const fetchServers = useCallback(async (): Promise<McpCollection> => {
+ // The global list omits the selected project on purpose: its rows read the
+ // global default, the same scope the switch writes, so the two can never
+ // disagree (#1957). Only project rows are per-project state.
const [global, project] = await Promise.all([
- api.listMcpServers({
- level: "global",
- ...(selectedProjectPath ? { projectPath: selectedProjectPath } : {}),
- }),
+ api.listMcpServers({ level: "global" }),
selectedProjectPath
? api.listMcpServers({ level: "project", projectPath: selectedProjectPath })
: Promise.resolve({
@@ -133,10 +134,8 @@ export function AgentMcpPage() {
const { armed, setArmed } = useArmedDelete();
const rowKey = (level: AgentCapabilityLevel, id: string) => `${level}:${id}`;
- const levelQuery = (level: AgentCapabilityLevel) => ({
- level,
- ...(selectedProjectPath ? { projectPath: selectedProjectPath } : {}),
- });
+ const levelQuery = (level: AgentCapabilityLevel) =>
+ capabilityQuery(level, selectedProjectPath);
const patchRow = (
level: AgentCapabilityLevel,
@@ -216,17 +215,9 @@ export function AgentMcpPage() {
// on a switch reads as a broken control.
patchRow(level, server.id, { enabled: next });
try {
- // The switch writes the state for the row's own level. A global server's
- // toggle must record the global default - carrying the selected project
- // here would store a per-project override while the switch reads as
- // global, so one project's "off" silently left every other project on.
- // Only project rows need the path: the host scans that project's
- // directory to find the definition.
- await api.setMcpServerEnabled(
- server.id,
- next,
- level === "global" ? { level } : levelQuery(level),
- );
+ // The switch writes the row's own level scope: a global server records
+ // the global default, a project row records that project's override.
+ await api.setMcpServerEnabled(server.id, next, levelQuery(level));
showToast(
t(next ? "settings.capabilityEnabled" : "settings.capabilityDisabled", {
name: server.label || server.id,
diff --git a/apps/desktop/src/components/settings/AgentSkillsPage.tsx b/apps/desktop/src/components/settings/AgentSkillsPage.tsx
index 230222d21..1a7bcb0ce 100644
--- a/apps/desktop/src/components/settings/AgentSkillsPage.tsx
+++ b/apps/desktop/src/components/settings/AgentSkillsPage.tsx
@@ -7,6 +7,7 @@ import {
type UserSkillRecord,
} from "@pi-desktop/shared";
import { api } from "../../lib/api";
+import { capabilityQuery } from "../../lib/capability-scope";
import { useAppStore } from "../../stores/app-store";
import { useHostCollection } from "../../hooks/use-host-collection";
import { useRowBusy } from "../../hooks/use-row-busy";
@@ -76,10 +77,10 @@ export function AgentSkillsPage() {
const fetchSkills = useCallback(async (): Promise<SkillCollection> => {
const [builtins, global, project] = await Promise.all([
api.listBuiltinSkills(),
- api.listUserSkills({
- level: "global",
- ...(selectedProjectPath ? { projectPath: selectedProjectPath } : {}),
- }),
+ // The global list omits the selected project on purpose: its rows read
+ // the global default, the same scope the switch writes, so the two can
+ // never disagree (#1957). Only project rows are per-project state.
+ api.listUserSkills({ level: "global" }),
selectedProjectPath
? api.listUserSkills({ level: "project", projectPath: selectedProjectPath })
: Promise.resolve({ skills: [] as UserSkillRecord[] }),
@@ -130,10 +131,8 @@ export function AgentSkillsPage() {
}));
};
- const levelQuery = (level: AgentCapabilityLevel) => ({
- level,
- ...(selectedProjectPath ? { projectPath: selectedProjectPath } : {}),
- });
+ const levelQuery = (level: AgentCapabilityLevel) =>
+ capabilityQuery(level, selectedProjectPath);
/**
* The switch flips locally first and only reverts if the host refuses, so one
@@ -145,17 +144,9 @@ export function AgentSkillsPage() {
const next = !skill.enabled;
patchRow(level, skill.id, { enabled: next });
try {
- // The switch writes the state for the row's own level. A global skill's
- // toggle must record the global default - carrying the selected project
- // here would store a per-project override while the switch reads as
- // global, so one project's "off" silently left every other project on.
- // Only project rows need the path: the host resolves the definition
- // through that project's directory.
- await api.setUserSkillEnabled(
- skill.id,
- next,
- level === "global" ? { level } : levelQuery(level),
- );
+ // The switch writes the row's own level scope: a global skill records
+ // the global default, a project row records that project's override.
+ await api.setUserSkillEnabled(skill.id, next, levelQuery(level));
showToast(
t(next ? "settings.capabilityEnabled" : "settings.capabilityDisabled", {
name: skill.name || skill.id,
diff --git a/apps/desktop/src/lib/capability-scope.ts b/apps/desktop/src/lib/capability-scope.ts
new file mode 100644
index 000000000..10f0d89d8
--- /dev/null
+++ b/apps/desktop/src/lib/capability-scope.ts
@@ -0,0 +1,24 @@
+import type { AgentCapabilityLevel, AgentCapabilityQuery } from "@pi-desktop/shared";
+
+/**
+ * The query a capability request carries for one row of its own level.
+ *
+ * A global row's switch reads and writes the global default, so its query must
+ * stay scope-free even when a project is selected: appending the selection
+ * would write a per-project override while the switch still reads as global,
+ * and a reader with a legacy override under one project could then never turn
+ * the global default off from there (#1957). Project rows are the one case
+ * that needs the path - the host resolves a project definition through that
+ * project's directory, so without it the row cannot be found at all.
+ */
+export function capabilityQuery(
+ level: AgentCapabilityLevel,
+ selectedProjectPath: string | null,
+): AgentCapabilityQuery {
+ return {
+ level,
+ ...(level === "project" && selectedProjectPath
+ ? { projectPath: selectedProjectPath }
+ : {}),
+ };
+}
diff --git a/apps/desktop/test/agent-capability-settings.test.mjs b/apps/desktop/test/agent-capability-settings.test.mjs
index 560b24e77..e69a3a526 100644
--- a/apps/desktop/test/agent-capability-settings.test.mjs
+++ b/apps/desktop/test/agent-capability-settings.test.mjs
@@ -260,26 +260,24 @@ test("toggling a capability flips locally and reverts only if the host refuses",
});
test("a global row's toggle records the global default, never a per-project override", () => {
- // The selected project legitimately scopes the list calls (effective
- // per-project state), but the switch itself writes the state for the row's
- // own level: a global toggle carrying projectPath stored an override, so
- // one project's "off" left every other project on (#1957).
- for (const [source, method] of [
- [skills, "setUserSkillEnabled"],
- [mcp, "setMcpServerEnabled"],
- ]) {
- const toggle = source.slice(
- source.indexOf("const toggle = async"),
- source.indexOf("catch", source.indexOf(`api.${method}`)),
- );
- assert.notEqual(toggle, "", `${method} toggle should be present`);
+ // The selected project legitimately scopes the project-list calls, but the
+ // switch and the global list must speak the global default's scope: the old
+ // helper attached the project everywhere, so toggling stored an override
+ // while the switch read as global (#1957). The pages delegate every query
+ // to the scope module, which attaches the path to project rows only.
+ for (const source of [skills, mcp]) {
assert.match(
- toggle,
- new RegExp(
- `api\\.${method}\\([\\s\\S]*?level === "global" \\? \\{ level \\} : levelQuery\\(level\\)`,
- ),
+ source,
+ /const levelQuery = \(level: AgentCapabilityLevel\) =>\s*\n\s*capabilityQuery\(level, selectedProjectPath\)/,
);
}
+ // The global list reads the global default: same scope the switch writes,
+ // so the row and the switch can never disagree.
+ assert.match(mcp, /api\.listMcpServers\(\{ level: "global" \}\)/);
+ assert.match(skills, /api\.listUserSkills\(\{ level: "global" \}\)/);
+ // The scope rule itself: the path rides only on project-level rows.
+ const scope = read("../src/lib/capability-scope.ts");
+ assert.match(scope, /level === "project" && selectedProjectPath/);
// Subagents are global-only: no project scope exists to mix in.
assert.doesNotMatch(subagents, /projectPath/);
});
diff --git a/apps/desktop/test/capability-scope.test.mjs b/apps/desktop/test/capability-scope.test.mjs
new file mode 100644
index 000000000..4593f7f11
--- /dev/null
+++ b/apps/desktop/test/capability-scope.test.mjs
@@ -0,0 +1,26 @@
+import assert from "node:assert/strict";
+import { register } from "node:module";
+import test from "node:test";
+register(new URL("./helpers/ts-import-hooks.mjs", import.meta.url));
+const { capabilityQuery } = await import("../src/lib/capability-scope.ts");
+
+test("a global capability query stays scope-free even with a project selected", () => {
+ // #1957 regression: with project A selected and a legacy per-project
+ // override underneath (the old UI wrote those), the switch must still write
+ // the global default. A query carrying projectPath here would write an
+ // override instead - a no-op click that reverted on the next refresh and
+ // left the global default unreachable from any project.
+ assert.deepEqual(capabilityQuery("global", "/projects/a"), { level: "global" });
+ assert.deepEqual(capabilityQuery("global", null), { level: "global" });
+});
+
+test("a project capability query carries the selected project's path", () => {
+ // The host resolves a project definition by scanning that project's
+ // directory, so without the path the row cannot be found at all.
+ assert.deepEqual(capabilityQuery("project", "/projects/a"), {
+ level: "project",
+ projectPath: "/projects/a",
+ });
+ // No selection, no path to attach.
+ assert.deepEqual(capabilityQuery("project", null), { level: "project" });
+});