Skip to content

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

fix(desktop): subscribe the subagent editor to image metadata reactively

koeriab84f779eb7ab
diff --git a/apps/desktop/src/components/settings/SubagentEditorSheet.tsx b/apps/desktop/src/components/settings/SubagentEditorSheet.tsx
index f97cf3cbe..6ac559f32 100644
--- a/apps/desktop/src/components/settings/SubagentEditorSheet.tsx
+++ b/apps/desktop/src/components/settings/SubagentEditorSheet.tsx
@@ -581,6 +581,8 @@ export function SubagentEditorSheet({
}) {
const { t } = useTranslation();
const providers = useAppStore((state) => state.providers);
+ const settings = useAppStore((state) => state.settings);
+ const providerModels = useAppStore((state) => state.providerModels);
const copiedPreset = Boolean(initialPresetId && findSubagentPreset(initialPresetId));
const [nameTouched, setNameTouched] = useState(!!editing || copiedPreset);
const [presetId, setPresetId] = useState<string | null>(
@@ -591,12 +593,20 @@ export function SubagentEditorSheet({
const pristine = !editing && !draft.name.trim() && !draft.description.trim();
const bytes = new TextEncoder().encode(draft.body).length;
const slug = draft.id || subagentSlug(draft.name);
+ // Subscribe to the settings slices and the discovered catalog reactively:
+ // the catalog slice swaps providerModels without replacing the providers
+ // array, so a getState() read here kept stale metadata while the sheet
+ // stayed open (e.g. image-only metadata arriving after mount).
+ const imageGenerationBindingsFromSettings = useMemo(
+ () => imageGenerationBindings(
+ settings?.imageGenerationModels,
+ settings?.imageGeneration,
+ ),
+ [settings?.imageGenerationModels, settings?.imageGeneration],
+ );
const modelChoices = useMemo(
- () => subagentModelChoices(providers, imageGenerationBindings(
- useAppStore.getState().settings?.imageGenerationModels,
- useAppStore.getState().settings?.imageGeneration,
- ), useAppStore.getState().providerModels),
- [providers],
+ () => subagentModelChoices(providers, imageGenerationBindingsFromSettings, providerModels),
+ [providers, imageGenerationBindingsFromSettings, providerModels],
);
const modelGroups = useMemo(
() => groupSubagentModelChoices(modelChoices),
diff --git a/apps/desktop/src/components/settings/SubagentFallbackModels.tsx b/apps/desktop/src/components/settings/SubagentFallbackModels.tsx
index f6699140a..e88ad5a83 100644
--- a/apps/desktop/src/components/settings/SubagentFallbackModels.tsx
+++ b/apps/desktop/src/components/settings/SubagentFallbackModels.tsx
@@ -1,3 +1,4 @@
+import { useMemo } from "react";
import { useTranslation } from "react-i18next";
import { imageGenerationBindings, type ProviderPublic } from "@pi-desktop/shared";
import { useAppStore } from "../../stores/app-store";
@@ -19,13 +20,22 @@ export function SubagentFallbackModels({ primary, values, choices, providers, on
onChange: (values: string[]) => void;
}) {
const { t } = useTranslation();
+ // Read the settings slices and the discovered catalog through reactive
+ // subscriptions: getState() here kept stale metadata when the catalog
+ // slice swapped providerModels without re-rendering this component.
+ const settings = useAppStore((state) => state.settings);
+ const providerModels = useAppStore((state) => state.providerModels);
+ const images = useMemo(
+ () => imageGenerationBindings(
+ settings?.imageGenerationModels,
+ settings?.imageGeneration,
+ ),
+ [settings?.imageGenerationModels, settings?.imageGeneration],
+ );
const selected = new Set([primary, ...values].map((pin) => subagentModelSelectValue(pin, choices)));
const available = choices.filter((choice) => !selected.has(choice.value));
const rows = values.map((pin) => {
- const choice = subagentModelDisplay(pin, providers, imageGenerationBindings(
- useAppStore.getState().settings?.imageGenerationModels,
- useAppStore.getState().settings?.imageGeneration,
- ), useAppStore.getState().providerModels);
+ const choice = subagentModelDisplay(pin, providers, images, providerModels);
return { choice, text: choice ? `${choice.providerName}/${choice.modelId}` : pin };
});
const move = (index: number, delta: number) => {
diff --git a/apps/desktop/test/subagent-editor-presets.test.mjs b/apps/desktop/test/subagent-editor-presets.test.mjs
index 37bb842b2..784aa0639 100644
--- a/apps/desktop/test/subagent-editor-presets.test.mjs
+++ b/apps/desktop/test/subagent-editor-presets.test.mjs
@@ -95,7 +95,7 @@ test("the editor can prefill a create draft from a catalog definition", () => {
test("the model picker uses the configured provider catalog", () => {
// The picker uses the shared provider catalog and preserves an existing
// orphan pin instead of silently changing it to session inheritance.
- assert.match(editorSource, /subagentModelChoices\(providers, imageGenerationBindings\(/);
+ assert.match(editorSource, /subagentModelChoices\(providers, imageGenerationBindings/);
assert.match(editorSource, /groupSubagentModelChoices\(modelChoices\)/);
assert.match(editorSource, /subagentModelOrphanPin\(draft\.model, modelChoices\)/);
});
@@ -116,7 +116,7 @@ test("the model picker keeps existing pins visible", async () => {
test("the model picker offers every configured provider model, with no free-text path", () => {
// The picker is the only way to set a model: every option comes from the
// configured provider catalog, so a saved pin is always resolvable.
- assert.match(editorSource, /subagentModelChoices\(providers, imageGenerationBindings\(/);
+ assert.match(editorSource, /subagentModelChoices\(providers, imageGenerationBindings/);
assert.match(editorSource, /groupSubagentModelChoices\(modelChoices\)/);
assert.match(editorSource, /subagentModelOrphanPin\(draft\.model, modelChoices\)/);
assert.doesNotMatch(editorSource, /CUSTOM_SUBAGENT_MODEL_VALUE/);
diff --git a/apps/desktop/test/subagent-models.test.mjs b/apps/desktop/test/subagent-models.test.mjs
index 8a09f26a0..5adcab1af 100644
--- a/apps/desktop/test/subagent-models.test.mjs
+++ b/apps/desktop/test/subagent-models.test.mjs
@@ -23,6 +23,7 @@ const {
subagentModelPinParts,
subagentModelSelectValue,
} = await import("../src/components/settings/subagent-models.ts");
+const { imageGenerationBindings } = await import("@pi-desktop/shared");
const binding = (id, availableForSubagents = true) => ({
id,
@@ -334,7 +335,7 @@ test("the editor model field is a configured-only searchable picker with an empt
// No hand-typed model id: the field never renders a free-text input.
assert.doesNotMatch(modelField, /<Input/);
assert.doesNotMatch(modelField, /modelPickCustom/);
- assert.match(source, /subagentModelChoices\(providers, imageGenerationBindings\(/);
+ assert.match(source, /subagentModelChoices\(providers, imageGenerationBindings/);
assert.match(source, /resetSubagentTemplate\(draft\)/);
});
@@ -370,6 +371,87 @@ test("the shared option-menu styles bound the list height and let it scroll", as
assert.match(menu[1], /max-height:/);
});
+test("model choices recompute when image metadata arrives while the sheet stays open", async () => {
+ // Regression for the maintainer re-review of #2304: the editor's
+ // modelChoices memo read settings.imageGenerationModels and providerModels
+ // through useAppStore.getState() while depending only on [providers]. The
+ // catalog slice swaps providerModels without replacing the providers array,
+ // so image-only metadata that arrived after the sheet opened never
+ // re-filtered the list until it was closed and reopened.
+ const editorSource = await readFile(
+ new URL("../src/components/settings/SubagentEditorSheet.tsx", import.meta.url),
+ "utf8",
+ );
+ const fallbackSource = await readFile(
+ new URL("../src/components/settings/SubagentFallbackModels.tsx", import.meta.url),
+ "utf8",
+ );
+ const start = editorSource.indexOf("const modelChoices = useMemo(");
+ const end = editorSource.indexOf("const modelGroups = useMemo(");
+ assert.ok(start !== -1 && end > start, "modelChoices memo block not found");
+ const memoBlock = editorSource.slice(start, end);
+ // The memo reads every input reactively: no getState() snapshot inside the
+ // block, and the dependency array covers providers, the settings-derived
+ // image bindings, and the discovered catalog.
+ assert.doesNotMatch(memoBlock, /useAppStore\.getState\(\)/);
+ assert.match(
+ memoBlock,
+ /\[providers, imageGenerationBindingsFromSettings, providerModels\]/,
+ );
+ // The settings-derived bindings memo subscribes to the image settings slices.
+ assert.match(
+ editorSource,
+ /\[settings\?\.imageGenerationModels, settings\?\.imageGeneration\]/,
+ );
+ // The fallback rows read the same slices through subscriptions, not snapshots.
+ assert.doesNotMatch(fallbackSource, /useAppStore\.getState\(\)/);
+ assert.match(fallbackSource, /useAppStore\(\(state\) => state\.providerModels\)/);
+
+ // Replay the maintainer's harness: with the providers reference unchanged,
+ // metadata arriving late (unknown -> output:["image"]) must change the memo
+ // result when the reactive inputs change. The memo callback is extracted
+ // verbatim from the component source and re-run against each store state.
+ const arrow = memoBlock.match(/useMemo\(\s*(\(\) => subagentModelChoices[\s\S]*?),\s*\[/);
+ assert.ok(arrow, "memo callback not extractable");
+ const runMemo = (imageGenerationBindingsFromSettings, providerModels) =>
+ new Function(
+ "subagentModelChoices",
+ "providers",
+ "imageGenerationBindingsFromSettings",
+ "providerModels",
+ `return (${arrow[1]})();`,
+ )(subagentModelChoices, providers, imageGenerationBindingsFromSettings, providerModels);
+
+ const lateProvider = provider({
+ id: "p2",
+ name: "My Gateway",
+ vendorKey: "custom",
+ models: [binding("custom-art")],
+ });
+ const providers = [provider(), lateProvider];
+ const settings = {
+ imageGenerationModels: [{ providerId: "p2", modelId: "custom-art" }],
+ imageGeneration: null,
+ };
+ // Metadata unknown: the image-marked custom model stays chat-eligible.
+ const before = runMemo(imageGenerationBindings(settings.imageGenerationModels, null), {});
+ assert.deepEqual(before.map((choice) => choice.value), [
+ "anthropic/claude-haiku-4-5",
+ "anthropic/claude-sonnet-4-6",
+ "My Gateway/custom-art",
+ ]);
+ // Metadata arrives while the sheet stays open: the same providers reference
+ // plus the new providerModels slice must drop the pure image endpoint.
+ const after = runMemo(
+ imageGenerationBindings(settings.imageGenerationModels, null),
+ { p2: [{ modelId: "custom-art", modalities: { output: ["image"] } }] },
+ );
+ assert.deepEqual(after.map((choice) => choice.value), [
+ "anthropic/claude-haiku-4-5",
+ "anthropic/claude-sonnet-4-6",
+ ]);
+});
+
test("the editor exposes the no-pass thinking option", async () => {
const source = await readFile(
new URL("../src/components/settings/SubagentEditorSheet.tsx", import.meta.url),