Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(settings): give every capability row its own busy guard
liuzixiangf3e5cc310e1b
diff --git a/apps/desktop/src/components/settings/AgentMcpPage.tsx b/apps/desktop/src/components/settings/AgentMcpPage.tsxindex 45e1b909e..091998c51 100644--- a/apps/desktop/src/components/settings/AgentMcpPage.tsx+++ b/apps/desktop/src/components/settings/AgentMcpPage.tsx@@ -9,6 +9,7 @@ import {import { api } from "../../lib/api";import { useAppStore } from "../../stores/app-store";import { useHostCollection } from "../../hooks/use-host-collection";+import { useRowBusy } from "../../hooks/use-row-busy";import { mcpOAuthErrorTranslationKey } from "../../lib/mcp-oauth-error";import {AgentCapabilityPage,@@ -114,7 +115,7 @@ export function AgentMcpPage() {const [filter, setFilter] = useState<CapabilityFilter>("all");const [importOpen, setImportOpen] = useState(false);const [search, setSearch] = useState("");- const [busyId, setBusyId] = useState<string | null>(null);+ const rowBusy = useRowBusy();const [menuFor, setMenuFor] = useState<string | null>(null);const [editor, setEditor] = useState<McpEditorState | null>(null);const [saving, setSaving] = useState(false);@@ -209,9 +210,8 @@ export function AgentMcpPage() {const toggle = async (server: McpServerRecord, level: AgentCapabilityLevel) => {const key = rowKey(level, server.id);- if (busyId) return;+ if (!rowBusy.begin(key)) return;const next = !server.enabled;- setBusyId(key);// Flip locally first: the host is the authority, but a round-trip of latency// on a switch reads as a broken control.patchRow(level, server.id, { enabled: next });@@ -227,7 +227,7 @@ export function AgentMcpPage() {patchRow(level, server.id, { enabled: server.enabled });showToast(error instanceof Error ? error.message : String(error), { variant: "error" });} finally {- setBusyId(null);+ rowBusy.end(key);}};@@ -329,7 +329,7 @@ export function AgentMcpPage() {const remove = async (server: McpServerRecord, level: AgentCapabilityLevel) => {const key = rowKey(level, server.id);- setBusyId(key);+ if (!rowBusy.begin(key)) return;try {await api.removeMcpServer(server.id, levelQuery(level));await load();@@ -339,7 +339,7 @@ export function AgentMcpPage() {} catch (error) {showToast(error instanceof Error ? error.message : String(error), { variant: "error" });} finally {- setBusyId(null);+ rowBusy.end(key);setArmed(null);}};@@ -397,7 +397,7 @@ export function AgentMcpPage() {return;}const key = rowKey(level, server.id);- setBusyId(key);+ if (!rowBusy.begin(key)) return;try {const result = await api.transferMcpServer({id: server.id,@@ -420,7 +420,7 @@ export function AgentMcpPage() {} catch (error) {showToast(error instanceof Error ? error.message : String(error), { variant: "error" });} finally {- setBusyId(null);+ rowBusy.end(key);}};@@ -428,7 +428,7 @@ export function AgentMcpPage() {const key = rowKey(level, server.id);const name = server.label || server.id;const status = statusFor(statuses, server);- const busy = busyId === key;+ const busy = rowBusy.isBusy(key);const testing = testingId === server.id;const authorizing = authorizingId === server.id;const isArmed = armed === key;diff --git a/apps/desktop/src/components/settings/AgentSkillsPage.tsx b/apps/desktop/src/components/settings/AgentSkillsPage.tsxindex 2f5d3de2c..451bc950a 100644--- a/apps/desktop/src/components/settings/AgentSkillsPage.tsx+++ b/apps/desktop/src/components/settings/AgentSkillsPage.tsx@@ -9,6 +9,7 @@ import {import { api } from "../../lib/api";import { useAppStore } from "../../stores/app-store";import { useHostCollection } from "../../hooks/use-host-collection";+import { useRowBusy } from "../../hooks/use-row-busy";import {AgentCapabilityPage,AgentProjectPicker,@@ -101,7 +102,7 @@ export function AgentSkillsPage() {const [filter, setFilter] = useState<CapabilityFilter>("all");const [importOpen, setImportOpen] = useState(false);const [search, setSearch] = useState("");- const [busyId, setBusyId] = useState<string | null>(null);+ const rowBusy = useRowBusy();const [menuFor, setMenuFor] = useState<string | null>(null);const [editor, setEditor] = useState<SkillEditorState | null>(null);const [view, setView] = useState<"skills" | "market">("skills");@@ -140,9 +141,8 @@ export function AgentSkillsPage() {*/const toggle = async (skill: UserSkillRecord, level: AgentCapabilityLevel) => {const key = rowKey(level, skill.id);- if (busyId === key) return;+ if (!rowBusy.begin(key)) return;const next = !skill.enabled;- setBusyId(key);patchRow(level, skill.id, { enabled: next });try {await api.setUserSkillEnabled(skill.id, next, levelQuery(level));@@ -156,15 +156,14 @@ export function AgentSkillsPage() {patchRow(level, skill.id, { enabled: skill.enabled });showToast(error instanceof Error ? error.message : String(error), { variant: "error" });} finally {- setBusyId(null);+ rowBusy.end(key);}};const toggleBuiltin = async (skill: BuiltinSkillRecord) => {const key = builtinRowKey(skill.id);- if (busyId === key) return;+ if (!rowBusy.begin(key)) return;const next = !skill.enabled;- setBusyId(key);patchBuiltin(skill.id, { enabled: next });try {await api.setBuiltinSkillEnabled(skill.id, next);@@ -178,7 +177,7 @@ export function AgentSkillsPage() {patchBuiltin(skill.id, { enabled: skill.enabled });showToast(error instanceof Error ? error.message : String(error), { variant: "error" });} finally {- setBusyId(null);+ rowBusy.end(key);}};@@ -205,7 +204,7 @@ export function AgentSkillsPage() {const openEdit = async (skill: UserSkillRecord, level: AgentCapabilityLevel) => {const key = rowKey(level, skill.id);- setBusyId(key);+ if (!rowBusy.begin(key)) return;try {const result = await api.readUserSkill(skill.id, levelQuery(level));setEditor({@@ -216,7 +215,7 @@ export function AgentSkillsPage() {} catch (error) {showToast(error instanceof Error ? error.message : String(error), { variant: "error" });} finally {- setBusyId(null);+ rowBusy.end(key);}};@@ -260,7 +259,7 @@ export function AgentSkillsPage() {const remove = async (skill: UserSkillRecord, level: AgentCapabilityLevel) => {const key = rowKey(level, skill.id);- setBusyId(key);+ if (!rowBusy.begin(key)) return;try {await api.removeUserSkill(skill.id, levelQuery(level));await load();@@ -270,7 +269,7 @@ export function AgentSkillsPage() {} catch (error) {showToast(error instanceof Error ? error.message : String(error), { variant: "error" });} finally {- setBusyId(null);+ rowBusy.end(key);setArmed(null);}};@@ -283,7 +282,8 @@ export function AgentSkillsPage() {showToast(t("settings.selectProjectFirst"), { variant: "error" });return;}- setBusyId(sourceKind === "dir" ? "import-dir" : "import");+ const importKey = sourceKind === "dir" ? "import-dir" : "import";+ if (!rowBusy.begin(importKey)) return;try {const result = await api.importUserSkill({level,@@ -316,7 +316,7 @@ export function AgentSkillsPage() {} catch (error) {showToast(error instanceof Error ? error.message : String(error), { variant: "error" });} finally {- setBusyId(null);+ rowBusy.end(importKey);}};@@ -370,7 +370,7 @@ export function AgentSkillsPage() {return;}const key = rowKey(level, skill.id);- setBusyId(key);+ if (!rowBusy.begin(key)) return;try {const result = await api.transferUserSkill({id: skill.id,@@ -393,14 +393,14 @@ export function AgentSkillsPage() {} catch (error) {showToast(error instanceof Error ? error.message : String(error), { variant: "error" });} finally {- setBusyId(null);+ rowBusy.end(key);}};const renderRow = (skill: UserSkillRecord, level: AgentCapabilityLevel) => {const key = rowKey(level, skill.id);const name = skill.name || skill.id;- const busy = busyId === key;+ const busy = rowBusy.isBusy(key);const isArmed = armed === key;const items: CapabilityMenuItem[] = [{@@ -507,7 +507,7 @@ export function AgentSkillsPage() {const renderBuiltinRow = (skill: BuiltinSkillRecord) => {const key = builtinRowKey(skill.id);const name = skill.name || skill.id;- const busy = busyId === key;+ const busy = rowBusy.isBusy(key);return (<CapabilityRowkey={key}@@ -541,7 +541,7 @@ export function AgentSkillsPage() {const importButton = (level: AgentCapabilityLevel) => (<><CapabilityButton- busy={busyId === "import"}+ busy={rowBusy.isBusy("import")}title={level === "project"? t("settings.capabilityImportToProject")@@ -553,7 +553,7 @@ export function AgentSkillsPage() {{t("settings.importSkillFile")}</CapabilityButton><CapabilityButton- busy={busyId === "import-dir"}+ busy={rowBusy.isBusy("import-dir")}title={level === "project"? t("settings.capabilityImportToProject")diff --git a/apps/desktop/src/components/settings/AgentSubagentsPage.tsx b/apps/desktop/src/components/settings/AgentSubagentsPage.tsxindex 721467853..296c112c4 100644--- a/apps/desktop/src/components/settings/AgentSubagentsPage.tsx+++ b/apps/desktop/src/components/settings/AgentSubagentsPage.tsx@@ -4,6 +4,7 @@ import type { SubagentDefinition, UserSubagentRecord } from "@pi-desktop/shared"import { api } from "../../lib/api";import { useAppStore } from "../../stores/app-store";import { useHostCollection } from "../../hooks/use-host-collection";+import { useRowBusy } from "../../hooks/use-row-busy";import {AgentCapabilityPage,CapabilityButton,@@ -71,7 +72,7 @@ export function AgentSubagentsPage() {showToast(error instanceof Error ? error.message : String(error), { variant: "error" }),);const [search, setSearch] = useState("");- const [busyId, setBusyId] = useState<string | null>(null);+ const rowBusy = useRowBusy();const [menuFor, setMenuFor] = useState<string | null>(null);const [editor, setEditor] = useState<SubagentEditorState | null>(null);const [saving, setSaving] = useState(false);@@ -83,9 +84,8 @@ export function AgentSubagentsPage() {* row's request never blanks the list or freezes the others.*/const toggle = async (subagent: UserSubagentRecord) => {- if (busyId === subagent.id) return;+ if (!rowBusy.begin(subagent.id)) return;const next = !subagent.enabled;- setBusyId(subagent.id);setSubagents((current) => ({...current,owned: current.owned.map((row) =>@@ -109,7 +109,7 @@ export function AgentSubagentsPage() {}));showToast(error instanceof Error ? error.message : String(error), { variant: "error" });} finally {- setBusyId(null);+ rowBusy.end(subagent.id);}};@@ -120,9 +120,8 @@ export function AgentSubagentsPage() {*/const toggleBuiltin = async (builtin: BuiltinSubagentRow) => {const handle = builtin.name;- if (busyId === `builtin:${handle}`) return;+ if (!rowBusy.begin(`builtin:${handle}`)) return;const next = !builtin.enabled;- setBusyId(`builtin:${handle}`);setSubagents((current) => ({...current,builtins: current.builtins.map((row) =>@@ -146,12 +145,12 @@ export function AgentSubagentsPage() {}));showToast(error instanceof Error ? error.message : String(error), { variant: "error" });} finally {- setBusyId(null);+ rowBusy.end(`builtin:${handle}`);}};const openEdit = async (subagent: UserSubagentRecord) => {- setBusyId(subagent.id);+ if (!rowBusy.begin(subagent.id)) return;try {const result = await api.readUserSubagent(subagent.id);setEditor({@@ -161,7 +160,7 @@ export function AgentSubagentsPage() {} catch (error) {showToast(error instanceof Error ? error.message : String(error), { variant: "error" });} finally {- setBusyId(null);+ rowBusy.end(subagent.id);}};@@ -218,7 +217,7 @@ export function AgentSubagentsPage() {};const remove = async (subagent: UserSubagentRecord) => {- setBusyId(subagent.id);+ if (!rowBusy.begin(subagent.id)) return;try {await api.removeUserSubagent(subagent.id);await load();@@ -228,7 +227,7 @@ export function AgentSubagentsPage() {} catch (error) {showToast(error instanceof Error ? error.message : String(error), { variant: "error" });} finally {- setBusyId(null);+ rowBusy.end(subagent.id);setArmed(null);}};@@ -263,7 +262,7 @@ export function AgentSubagentsPage() {const handle = definition.name;const name = builtinDisplayName(handle, t);const canCopy = !ownedHandles.has(handle);- const busy = busyId === `builtin:${handle}`;+ const busy = rowBusy.isBusy(`builtin:${handle}`);return (<CapabilityRowkey={`builtin:${handle}`}@@ -310,7 +309,7 @@ export function AgentSubagentsPage() {const renderRow = (subagent: UserSubagentRecord) => {const name = subagent.name || subagent.id;- const busy = busyId === subagent.id;+ const busy = rowBusy.isBusy(subagent.id);const isArmed = armed === subagent.id;const items: CapabilityMenuItem[] = [{diff --git a/apps/desktop/src/hooks/use-row-busy.ts b/apps/desktop/src/hooks/use-row-busy.tsnew file mode 100644index 000000000..53db7c33e--- /dev/null+++ b/apps/desktop/src/hooks/use-row-busy.ts@@ -0,0 +1,43 @@+import { useCallback, useRef, useState } from "react";+import { createRowBusyGuard, type RowBusyGuard } from "../lib/row-busy-guard";++/**+ * Per-row busy guard for row-keyed async operations.+ *+ * A single shared `busyId` state breaks in both directions: whichever request+ * settles first clears the guard of rows still in flight (so a second click+ * can double-fire them), and guarding with `if (busyId)` serializes unrelated+ * rows into one global queue. One independent key per row keeps each row+ * double-click-safe while the others stay responsive. The guard logic itself+ * lives in `lib/row-busy-guard` so tests can drive it without a renderer.+ */+export function useRowBusy(): {+ isBusy: (key: string) => boolean;+ begin: (key: string) => boolean;+ end: (key: string) => void;+} {+ const guardRef = useRef<RowBusyGuard | null>(null);+ if (!guardRef.current) guardRef.current = createRowBusyGuard();+ const guard = guardRef.current;+ const [revision, setRevision] = useState(0);+ const bump = useCallback(() => setRevision((count) => count + 1), []);+ const begin = useCallback(+ (key: string) => {+ const claimed = guard.begin(key);+ if (claimed) bump();+ return claimed;+ },+ [guard, bump],+ );+ const end = useCallback(+ (key: string) => {+ guard.end(key);+ bump();+ },+ [guard, bump],+ );+ // `revision` is a dependency on purpose: consumers re-render on every claim+ // and release without this hook exposing its Set.+ const isBusy = useCallback((key: string) => guard.isBusy(key), [guard, revision]);+ return { isBusy, begin, end };+}diff --git a/apps/desktop/src/lib/row-busy-guard.ts b/apps/desktop/src/lib/row-busy-guard.tsnew file mode 100644index 000000000..297002a17--- /dev/null+++ b/apps/desktop/src/lib/row-busy-guard.ts@@ -0,0 +1,32 @@+/**+ * Busy-key bookkeeping for row-keyed operations, free of React so tests can+ * drive the guard directly.+ *+ * One shared `busyId` slot cannot guard rows that run concurrently: the first+ * request to settle clears the slot while other rows are still in flight, so+ * those rows lose both their busy marker and their double-click guard. One+ * independent key per row keeps every row protected without serializing the+ * page.+ */+export type RowBusyGuard = {+ /** Claim one row's slot; false when that row is already busy. */+ begin: (key: string) => boolean;+ /** Release one row's slot; safe to call when nothing claimed it. */+ end: (key: string) => void;+ isBusy: (key: string) => boolean;+};++export function createRowBusyGuard(): RowBusyGuard {+ const busy = new Set<string>();+ return {+ begin: (key) => {+ if (busy.has(key)) return false;+ busy.add(key);+ return true;+ },+ end: (key) => {+ busy.delete(key);+ },+ isBusy: (key) => busy.has(key),+ };+}diff --git a/apps/desktop/test/row-busy-guard.test.mjs b/apps/desktop/test/row-busy-guard.test.mjsnew file mode 100644index 000000000..47882cacb--- /dev/null+++ b/apps/desktop/test/row-busy-guard.test.mjs@@ -0,0 +1,56 @@+import assert from "node:assert/strict";+import { readFile } from "node:fs/promises";+import test from "node:test";++const read = (path) => readFile(new URL(path, import.meta.url), "utf8");++test("a row busy guard tracks rows independently", async () => {+ const { createRowBusyGuard } = await import("../src/lib/row-busy-guard.ts");++ const guard = createRowBusyGuard();+ assert.equal(guard.begin("skills:s1"), true);+ assert.equal(guard.isBusy("skills:s1"), true);++ // The same row cannot double-claim — this is the double-click guard.+ assert.equal(guard.begin("skills:s1"), false);++ // Another row claims freely: one busy row never blocks or clears another.+ assert.equal(guard.begin("skills:s2"), true);+ assert.equal(guard.isBusy("skills:s2"), true);++ // Releasing the first row leaves the second one's guard in place — the+ // single-slot regression this guard replaces cleared every row here.+ guard.end("skills:s1");+ assert.equal(guard.isBusy("skills:s1"), false);+ assert.equal(guard.begin("skills:s1"), true);+ assert.equal(guard.isBusy("skills:s2"), true);++ guard.end("skills:s2");+ guard.end("skills:s2"); // releasing twice stays safe+ assert.equal(guard.isBusy("skills:s2"), false);+});++test("capability pages guard rows with useRowBusy instead of one shared slot", async () => {+ const pages = [+ "../src/components/settings/AgentSkillsPage.tsx",+ "../src/components/settings/AgentSubagentsPage.tsx",+ "../src/components/settings/AgentMcpPage.tsx",+ ];+ for (const page of pages) {+ const source = await read(page);+ assert.match(source, /useRowBusy\(\)/, `${page} must use the row busy guard`);+ assert.doesNotMatch(+ source,+ /useState<string \| null>\(null\).*busy/,+ `${page} must not keep a single busy slot`,+ );+ }+});++test("the MCP toggle no longer freezes every row while one is busy", async () => {+ const source = await read("../src/components/settings/AgentMcpPage.tsx");+ const toggle = source.match(/const toggle = async \(server: McpServerRecord[\s\S]*?\n \};/)?.[0] ?? "";+ assert.ok(toggle.length > 0, "MCP toggle implementation not found");+ assert.doesNotMatch(toggle, /if \(busyId\) return;/);+ assert.match(toggle, /if \(!rowBusy\.begin\(key\)\) return;/);+});