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 model-provider and plugin rows their own busy guards (#1928)
Liu Zixiangfeaf61ad2d42
diff --git a/apps/desktop/src/components/settings/ModelConfigPage.tsx b/apps/desktop/src/components/settings/ModelConfigPage.tsxindex f061d73c3..ac41c47bb 100644--- a/apps/desktop/src/components/settings/ModelConfigPage.tsx+++ b/apps/desktop/src/components/settings/ModelConfigPage.tsx@@ -17,6 +17,7 @@ import {} from "@pi-desktop/shared";import { useAppStore } from "../../stores/app-store";import { api } from "../../lib/api";+import { useRowBusy } from "../../hooks/use-row-busy";import { Button } from "../ui";import {IconConfig,@@ -103,8 +104,11 @@ export function ModelConfigPage() {const [jevSetup, setJevSetup] = useState(false);// Bumped when that dialog stored a key, so the card re-reads what exists.const [jevStatusRevision, setJevStatusRevision] = useState(0);- const [busyId, setBusyId] = useState<string | null>(null);- const [testingId, setTestingId] = useState<string | null>(null);+ // One busy key per provider row: a settle elsewhere on the page must not+ // re-arm a row whose own request is still in flight, and a busy row does+ // not lock the others (the same contract the capability pages pin).+ const rowBusy = useRowBusy();+ const testingRows = useRowBusy();const [changingImageModel, setChangingImageModel] = useState(false);const [refreshingCatalog, setRefreshingCatalog] = useState(false);const [catalogStatus, setCatalogStatus] = useState<CatalogStatus | null>(null);@@ -290,7 +294,7 @@ export function ModelConfigPage() {};const toggleEnabled = async (provider: ProviderPublic) => {- setBusyId(provider.id);+ if (!rowBusy.begin(provider.id)) return;try {await api.updateProvider({ id: provider.id, enabled: !provider.enabled });await refreshProviders();@@ -303,12 +307,12 @@ export function ModelConfigPage() {variant: "error",});} finally {- setBusyId(null);+ rowBusy.end(provider.id);}};const removeProvider = async (provider: ProviderPublic) => {- setBusyId(provider.id);+ if (!rowBusy.begin(provider.id)) return;try {await api.deleteProvider(provider.id);await refreshProviders();@@ -318,13 +322,13 @@ export function ModelConfigPage() {variant: "error",});} finally {- setBusyId(null);+ rowBusy.end(provider.id);}};/** Resolves true once the key is stored, so the row can close its entry. */const saveProviderKey = async (provider: ProviderPublic, value: string) => {- setBusyId(provider.id);+ if (!rowBusy.begin(provider.id)) return false;try {await api.setProviderSecret({ id: provider.id, secretValue: value });await refreshProviders();@@ -339,7 +343,7 @@ export function ModelConfigPage() {});return false;} finally {- setBusyId(null);+ rowBusy.end(provider.id);}};@@ -348,7 +352,8 @@ export function ModelConfigPage() {};const testProvider = async (provider: ProviderPublic) => {- setTestingId(provider.id);+ if (!rowBusy.begin(provider.id)) return;+ testingRows.begin(provider.id);try {const result = (await api.testProvider(provider.id)) as {ok?: boolean;@@ -371,7 +376,8 @@ export function ModelConfigPage() {variant: "error",});} finally {- setTestingId(null);+ rowBusy.end(provider.id);+ testingRows.end(provider.id);}};@@ -465,16 +471,14 @@ export function ModelConfigPage() {providers={providers}accountFor={accountFor}busy={- busyId !== null ||- testingId !== null ||setupFor !== null ||editingAccountId !== null ||busyAccountId !== null ||savingAccount ||login !== null}- isRowBusy={(id) => busyId === id || testingId === id || busyAccountId === id}- testingId={testingId}+ isRowBusy={(id) => rowBusy.isBusy(id) || busyAccountId === id}+ isTesting={testingRows.isBusy}onEdit={(provider) =>serviceRowKind(provider) === "account"? setEditingAccountId(provider.id)diff --git a/apps/desktop/src/components/settings/ServiceList.tsx b/apps/desktop/src/components/settings/ServiceList.tsxindex 847b82010..e266c5e31 100644--- a/apps/desktop/src/components/settings/ServiceList.tsx+++ b/apps/desktop/src/components/settings/ServiceList.tsx@@ -21,7 +21,7 @@ export type ServiceListProps = {/** A page-level write or dialog is in flight; card order is locked meanwhile. */busy: boolean;isRowBusy: (providerId: string) => boolean;- testingId: string | null;+ isTesting: (providerId: string) => boolean;onEdit: (provider: ProviderPublic) => void;onTest: (provider: ProviderPublic) => void;onCopy: (provider: ProviderPublic) => void;@@ -36,7 +36,7 @@ export function ServiceList({accountFor,busy,isRowBusy,- testingId,+ isTesting,onEdit,onTest,onCopy,@@ -161,7 +161,7 @@ export function ServiceList({provider={provider}entry={accountFor(provider.id)}busy={rowBusy}- testing={testingId === provider.id}+ testing={isTesting(provider.id)}dragging={reorder.draggingId === provider.id}menuItems={menuItems(provider)}menuOpen={openMenuId === provider.id}diff --git a/apps/desktop/src/features/plugins/InstalledPluginsPanel.tsx b/apps/desktop/src/features/plugins/InstalledPluginsPanel.tsxindex 2fba42045..eda1e4cdb 100644--- a/apps/desktop/src/features/plugins/InstalledPluginsPanel.tsx+++ b/apps/desktop/src/features/plugins/InstalledPluginsPanel.tsx@@ -30,13 +30,12 @@ export function InstalledPluginsPanel({setInstalledQuery,rowMenu,setRowMenu,- busyId,+ isPluginBusy,queueInstall,projects,currentProjectPath,run,refreshPlugins,- reloadingId,reloadPlugin,setSettingsPlugin,servicesByPlugin,@@ -187,7 +186,7 @@ export function InstalledPluginsPanel({<Buttonvariant="primary"size="sm"- disabled={busyId === plugin.id}+ disabled={isPluginBusy(plugin.id)}onClick={() =>queueInstall({id: plugin.id,@@ -198,7 +197,7 @@ export function InstalledPluginsPanel({})}>- {busyId === plugin.id+ {isPluginBusy(plugin.id)? t("plugins.updating"): t("plugins.updateTo", { version: update.version })}</Button>@@ -256,10 +255,7 @@ export function InstalledPluginsPanel({<buttontype="button"role="menuitem"- disabled={- busyId === plugin.id ||- reloadingId === plugin.id- }+ disabled={isPluginBusy(plugin.id)}onClick={() => {setRowMenu(null);void reloadPlugin(plugin.id);diff --git a/apps/desktop/src/features/plugins/MarketplacePanel.tsx b/apps/desktop/src/features/plugins/MarketplacePanel.tsxindex 38657dae8..ea2e32d13 100644--- a/apps/desktop/src/features/plugins/MarketplacePanel.tsx+++ b/apps/desktop/src/features/plugins/MarketplacePanel.tsx@@ -24,7 +24,7 @@ export function MarketplacePanel({installedById,selectedId,openDetail,- busyId,+ isPluginBusy,queueInstall,reviewSummaries,reviewTickers,@@ -185,7 +185,7 @@ export function MarketplacePanel({<Buttonvariant="primary"size="sm"- disabled={busyId === item.id || packagePending}+ disabled={isPluginBusy(item.id) || packagePending}title={packagePending? t("plugins.packagePendingHint", {@@ -204,7 +204,7 @@ export function MarketplacePanel({>{packagePending? t("plugins.packagePending")- : busyId === item.id+ : isPluginBusy(item.id)? t("plugins.installing"): upgradable? t("plugins.updateNow")diff --git a/apps/desktop/src/features/plugins/PluginDetailSheet.tsx b/apps/desktop/src/features/plugins/PluginDetailSheet.tsxindex ad0fd30c3..14d471a97 100644--- a/apps/desktop/src/features/plugins/PluginDetailSheet.tsx+++ b/apps/desktop/src/features/plugins/PluginDetailSheet.tsx@@ -45,7 +45,7 @@ export function PluginDetailSheet({detailUpToDate,detailPermissions,installedDetail,- busyId,+ isPluginBusy,queueInstall,setSelectedVersion,reviewSummaries,@@ -164,7 +164,7 @@ export function PluginDetailSheet({) : (<Buttonvariant="primary"- disabled={busyId === detail.id || detailPackagePending}+ disabled={isPluginBusy(detail.id) || detailPackagePending}onClick={() =>queueInstall({id: detail.id,@@ -178,7 +178,7 @@ export function PluginDetailSheet({? t("plugins.withdrawn"): detailPackagePending? t("plugins.packagePending")- : busyId === detail.id+ : isPluginBusy(detail.id)? t("plugins.installing"): installedDetail? t("plugins.updateNow")diff --git a/apps/desktop/src/features/plugins/PluginDialogs.tsx b/apps/desktop/src/features/plugins/PluginDialogs.tsxindex 055c5358e..d90d8baf1 100644--- a/apps/desktop/src/features/plugins/PluginDialogs.tsx+++ b/apps/desktop/src/features/plugins/PluginDialogs.tsx@@ -32,7 +32,7 @@ export function PluginDialogs({confirmReview,autoUpdate,setAutoUpdate,- busyId,+ isPluginBusy,confirmInstall,installJob,cancelInstallDownload,@@ -93,10 +93,10 @@ export function PluginDialogs({</Button><Buttonvariant="primary"- disabled={busyId === pendingReview.id}+ disabled={isPluginBusy(pendingReview.id)}onClick={() => void confirmReview()}>- {busyId === pendingReview.id+ {isPluginBusy(pendingReview.id)? t("plugins.devReviewLoading"): t("plugins.devReviewAccept")}</Button>@@ -153,10 +153,10 @@ export function PluginDialogs({</Button><Buttonvariant="primary"- disabled={busyId === pendingInstall.id}+ disabled={isPluginBusy(pendingInstall.id)}onClick={() => void confirmInstall()}>- {busyId === pendingInstall.id+ {isPluginBusy(pendingInstall.id)? t("plugins.installing"): t("plugins.acceptInstall")}</Button>diff --git a/apps/desktop/src/features/plugins/usePluginsPage.ts b/apps/desktop/src/features/plugins/usePluginsPage.tsindex af3dfe938..67d959f55 100644--- a/apps/desktop/src/features/plugins/usePluginsPage.ts+++ b/apps/desktop/src/features/plugins/usePluginsPage.ts@@ -2,6 +2,7 @@ import { useEffect, useMemo, useRef, useState } from "react";import { useTranslation } from "react-i18next";import { useAppStore } from "../../stores/app-store";import { api } from "../../lib/api";+import { useRowBusy } from "../../hooks/use-row-busy";import { usePluginBrowseState } from "./browse-state";import type {MarketPluginDetail,@@ -54,7 +55,10 @@ export function usePluginsPage() {const [projects, setProjects] = useState<ProjectRecord[]>([]);const [market, setMarket] = useState<MarketPluginSummary[]>([]);const [marketLoading, setMarketLoading] = useState(false);- const [busyId, setBusyId] = useState<string | null>(null);+ // One busy key per plugin row: the first request to settle must not re-arm+ // a row that is still in flight, and a busy row does not lock the others+ // (the same contract the capability pages pin).+ const rowBusy = useRowBusy();const [installJob, setInstallJob] = useState<PluginInstallJob | null>(null);/*** The job, and the reading a transfer speed is measured against, are mirrored@@ -495,6 +499,7 @@ export function usePluginsPage() {const reloadPlugin = (id: string) =>run(async () => {+ if (!rowBusy.begin(id)) return;setReloadingId(id);try {const result = await api.reloadPlugin(id);@@ -507,6 +512,7 @@ export function usePluginsPage() {await refreshPlugins();showToast(t("plugins.reloadDone"), { variant: "success" });} finally {+ rowBusy.end(id);setReloadingId(null);}});@@ -576,7 +582,7 @@ export function usePluginsPage() {const confirmReview = async () => {const review = pendingReview;if (!review) return;- setBusyId(review.id);+ if (!rowBusy.begin(review.id)) return;try {if (review.kind === "reload") {await api.confirmReloadPlugin({@@ -597,7 +603,7 @@ export function usePluginsPage() {} catch (e) {showToast(e instanceof Error ? e.message : String(e), { variant: "error" });} finally {- setBusyId(null);+ rowBusy.end(review.id);}};@@ -645,11 +651,11 @@ export function usePluginsPage() {* first step is visible, and the request's own answer is what ends it.*/const runInstall = async (request: PluginInstallRequest) => {+ if (!rowBusy.begin(request.id)) return;installSampleRef.current = null;const job = newInstallJob(request);installJobRef.current = job;setInstallJob(job);- setBusyId(request.id);try {await api.marketInstall({id: request.id,@@ -688,7 +694,7 @@ export function usePluginsPage() {// be reported where the user is looking.if (!shown) showToast(message, { variant: "error" });} finally {- setBusyId(null);+ rowBusy.end(request.id);}};@@ -778,7 +784,7 @@ export function usePluginsPage() {setCategory,market,marketLoading,- busyId,+ isPluginBusy: rowBusy.isBusy,reloadingId,pendingInstall,setPendingInstall,diff --git a/apps/desktop/test/plugin-runtime-risk.test.mjs b/apps/desktop/test/plugin-runtime-risk.test.mjsindex 497d92599..27bdcf44c 100644--- a/apps/desktop/test/plugin-runtime-risk.test.mjs+++ b/apps/desktop/test/plugin-runtime-risk.test.mjs@@ -187,8 +187,8 @@ test("marketplace detail pane renders readme changelog and versions", () => {test("marketplace blocks install for a version with no published package", () => {assert.match(pageSrc, /function versionInstallable\(/);assert.match(pageSrc, /const packagePending = item\.installable === false/);- assert.match(pageSrc, /disabled=\{busyId === item\.id \|\| packagePending\}/);- assert.match(pageSrc, /disabled=\{busyId === detail\.id \|\| detailPackagePending\}/);+ assert.match(pageSrc, /disabled=\{isPluginBusy\(item\.id\) \|\| packagePending\}/);+ assert.match(pageSrc, /disabled=\{isPluginBusy\(detail\.id\) \|\| detailPackagePending\}/);assert.match(pageSrc, /t\("plugins\.packagePending"\)/);assert.match(pageSrc, /t\("plugins\.packagePendingHint", \{/);diff --git a/apps/desktop/test/row-busy-pages.test.mjs b/apps/desktop/test/row-busy-pages.test.mjsnew file mode 100644index 000000000..eafdae5b9--- /dev/null+++ b/apps/desktop/test/row-busy-pages.test.mjs@@ -0,0 +1,88 @@+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");+const [+ modelsSource,+ serviceListSource,+ pluginsHookSource,+ installedSource,+ marketplaceSource,+ detailSource,+ dialogsSource,+] = await Promise.all([+ read("../src/components/settings/ModelConfigPage.tsx"),+ read("../src/components/settings/ServiceList.tsx"),+ read("../src/features/plugins/usePluginsPage.ts"),+ read("../src/features/plugins/InstalledPluginsPanel.tsx"),+ read("../src/features/plugins/MarketplacePanel.tsx"),+ read("../src/features/plugins/PluginDetailSheet.tsx"),+ read("../src/features/plugins/PluginDialogs.tsx"),+]);++test("model config rows keep independent busy guards", () => {+ assert.match(modelsSource, /import \{ useRowBusy \} from "\.\.\/\.\.\/hooks\/use-row-busy"/);+ assert.match(modelsSource, /const rowBusy = useRowBusy\(\)/);+ // Every row-keyed operation claims its own row before the first await and+ // releases it in a finally, so a settle elsewhere cannot re-arm it.+ for (const name of ["toggleEnabled", "removeProvider", "saveProviderKey", "testProvider"]) {+ const start = modelsSource.indexOf(`const ${name} = async`);+ assert.ok(start >= 0, `${name} missing`);+ const body = modelsSource.slice(start, start + 1600);+ assert.match(body, /if \(!rowBusy\.begin\(provider\.id\)\) return/, `${name} must claim its row`);+ assert.match(body, /rowBusy\.end\(provider\.id\)/, `${name} must release its row`);+ }+ // A busy row does not lock the page: the list-wide gate keeps only the+ // modal-dialog terms, and row business lives in isRowBusy.+ assert.doesNotMatch(modelsSource, /busyId/);+ assert.doesNotMatch(modelsSource, /busy=\{\s*busyId/);+ assert.match(modelsSource, /isRowBusy=\{\(id\) => rowBusy\.isBusy\(id\) \|\| busyAccountId === id\}/);+});++test("provider test spinners remain independent while requests overlap", () => {+ assert.match(modelsSource, /const testingRows = useRowBusy\(\)/);+ const start = modelsSource.indexOf("const testProvider = async");+ assert.ok(start >= 0, "testProvider missing");+ const body = modelsSource.slice(start, start + 1800);+ assert.match(body, /testingRows\.begin\(provider\.id\)/);+ assert.match(body, /testingRows\.end\(provider\.id\)/);+ assert.match(modelsSource, /isTesting=\{testingRows\.isBusy\}/);+ assert.match(serviceListSource, /isTesting: \(providerId: string\) => boolean/);+ assert.match(serviceListSource, /testing=\{isTesting\(provider\.id\)\}/);+ assert.doesNotMatch(modelsSource, /testingId|setTestingId/);+});++test("plugins page rows keep independent busy guards", () => {+ assert.match(pluginsHookSource, /import \{ useRowBusy \} from "\.\.\/\.\.\/hooks\/use-row-busy"/);+ assert.match(pluginsHookSource, /const rowBusy = useRowBusy\(\)/);+ assert.doesNotMatch(pluginsHookSource, /setBusyId/);+ for (const [name, key] of [+ ["confirmReview", "review.id"],+ ["runInstall", "request.id"],+ ["reloadPlugin", "id"],+ ]) {+ const start = pluginsHookSource.indexOf(`const ${name}`);+ assert.ok(start >= 0, `${name} missing`);+ const body = pluginsHookSource.slice(start, start + 2200);+ assert.match(body, new RegExp(`rowBusy\\.begin\\(${key.replace(".", "\\.")}\\)`), `${name} must claim its row`);+ assert.match(body, new RegExp(`rowBusy\\.end\\(${key.replace(".", "\\.")}\\)`), `${name} must release its row`);+ }+ // The page model exposes the per-row predicate; the single busyId slot is gone.+ assert.match(pluginsHookSource, /isPluginBusy: rowBusy\.isBusy,/);+ assert.doesNotMatch(pluginsHookSource, /\bbusyId\b/);+});++test("plugin surfaces consume the per-row busy predicate", () => {+ for (const [label, source, key] of [+ ["installed", installedSource, "plugin.id"],+ ["marketplace", marketplaceSource, "item.id"],+ ["detail", detailSource, "detail.id"],+ ]) {+ assert.doesNotMatch(source, /\bbusyId\b/, `${label} must not read the removed slot`);+ assert.match(source, new RegExp(`isPluginBusy\\(${key.replace(".", "\\.")}\\)`), `${label} must read the predicate`);+ }+ assert.match(dialogsSource, /isPluginBusy\(pendingReview\.id\)/);+ assert.match(dialogsSource, /isPluginBusy\(pendingInstall\.id\)/);+ assert.doesNotMatch(dialogsSource, /\bbusyId\b/);+});