Skip to content

Commit a7da742

Browse files
committed
fix: avoid replaying stale Goose provider lookups
Signed-off-by: Philip Azar <[email protected]>
1 parent 39bb35c commit a7da742

3 files changed

Lines changed: 138 additions & 1 deletion

File tree

‎src/bundled/agents/AgentModelPicker.test.tsx‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,50 @@ it("loads Goose models on provider selection, retires stale results, and retries
9797
}
9898
});
9999

100+
it("does not replay a failed Goose provider selection when the picker remounts", async () => {
101+
const f = controlFixture();
102+
const run = vi.fn(async () => {
103+
throw Error("Provider sign-in failed");
104+
});
105+
f.host.models = { begin: async () => 1, run, cancel: async () => {} };
106+
const control = createAgentControl(f.host);
107+
const draft = {
108+
...agentDraft(f.agent),
109+
command: "/local/goose-acp",
110+
provider: "anthropic",
111+
model: "",
112+
};
113+
const picker = (providerSelection: number) => (
114+
<AgentModelPicker
115+
providerSelection={providerSelection}
116+
draft={draft}
117+
control={control}
118+
defaults={undefined}
119+
onChange={() => {}}
120+
/>
121+
);
122+
const first = render(picker(0));
123+
try {
124+
first.rerender(picker(1));
125+
await waitFor(() => expect(run).toHaveBeenCalledOnce());
126+
await screen.findByText(
127+
"Could not load models. Retry explicitly; your model entry is unchanged.",
128+
);
129+
first.unmount();
130+
131+
const remounted = render(picker(1));
132+
try {
133+
await act(async () => {});
134+
expect(run).toHaveBeenCalledOnce();
135+
} finally {
136+
remounted.unmount();
137+
}
138+
} finally {
139+
first.unmount();
140+
control.dispose();
141+
}
142+
});
143+
100144
it("Goose Databricks v2 browses live IDs and flags an unlisted short name", async () => {
101145
const f = controlFixture();
102146
const run = vi.fn(async () => ({

‎src/bundled/agents/AgentModelPicker.tsx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ export function AgentModelPicker({
7373
const [query, setQuery] = useState<string | null>(null);
7474
const [open, setOpen] = useState(false);
7575
const attempted = useRef<string | null>(null);
76+
// The settings form survives while presets temporarily unmount this picker.
77+
// Treat its current counter as already consumed when mounting so only a new
78+
// committed provider choice can initiate Goose discovery.
79+
const consumedProviderSelection = useRef(providerSelection);
7680
// Native resolves absolute executables and write-only provider overrides.
7781
const supported = !!control.models;
7882
const highlighted = useRef<ModelCatalog["models"][number] | null>(null);
@@ -254,6 +258,8 @@ export function AgentModelPicker({
254258
// that request but wait for Browse or Retry, never signing in per keystroke.
255259
// biome-ignore lint/correctness/useExhaustiveDependencies: only selecting a Goose provider triggers automatic discovery.
256260
useEffect(() => {
261+
if (providerSelection === consumedProviderSelection.current) return;
262+
consumedProviderSelection.current = providerSelection;
257263
if (providerSelection && goose && draft.provider) void run("connect");
258264
}, [providerSelection]);
259265
const fresh = catalog?.key === key ? catalog.data : null;

‎src/bundled/agents/AgentSettingsFields.test.tsx‎

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import "@testing-library/jest-dom/vitest";
33
import { useState } from "react";
44
import { afterEach, expect, it, vi } from "vitest";
5-
import { cleanup, render, screen, waitFor } from "@testing-library/react";
5+
import { act, cleanup, render, screen, waitFor } from "@testing-library/react";
66
import userEvent from "@testing-library/user-event";
77
import { AgentSettingsFields } from "./AgentSettingsFields";
88
import { agentDraft, agentEdit, type AgentDraft } from "./agent-edit";
@@ -572,6 +572,93 @@ it("uses a masked OpenAI key for Goose model lookup and discards unsaved keys on
572572
}
573573
});
574574

575+
it("does not replay a failed Goose provider choice after a preset custom-path round trip", async () => {
576+
const fixture = controlFixture();
577+
fixture.data.harnessOptions = [
578+
{
579+
command: "/usr/local/bin/goose-acp",
580+
label: "Goose",
581+
defaultArgs: ["acp"],
582+
providers: [{ value: "anthropic", label: "Anthropic" }],
583+
configurationPolicy: {
584+
authentication: "harnessWithOverrides",
585+
provider: "selector",
586+
supportedModes: [],
587+
model: "optional",
588+
effortDiscovery: "unknown",
589+
selectorEnvironment: {
590+
model: "GOOSE_MODEL",
591+
provider: "GOOSE_PROVIDER",
592+
},
593+
},
594+
},
595+
];
596+
const run = vi.fn(async () => {
597+
throw Error("Goose sign-in cancelled");
598+
});
599+
fixture.host.models = { begin: async () => 1, run, cancel: async () => {} };
600+
const control = createAgentControl(fixture.host);
601+
const user = userEvent.setup();
602+
function Editor() {
603+
const [draft, setDraft] = useState(() => ({
604+
...agentDraft(fixture.agent),
605+
command: "/custom/goose-acp",
606+
args: '["acp"]',
607+
provider: "",
608+
model: "",
609+
environment: {},
610+
}));
611+
return (
612+
<AgentSettingsFields
613+
draft={draft}
614+
control={control}
615+
state={{
616+
status: "ready",
617+
data: fixture.data,
618+
busy: false,
619+
error: null,
620+
}}
621+
disabled={false}
622+
onChange={(patch) => setDraft((current) => ({ ...current, ...patch }))}
623+
/>
624+
);
625+
}
626+
const view = render(<Editor />);
627+
try {
628+
await user.click(screen.getByRole("combobox", { name: "LLM Provider" }));
629+
await user.click(await screen.findByRole("option", { name: "Anthropic" }));
630+
await waitFor(() => expect(run).toHaveBeenCalledOnce());
631+
await waitFor(() =>
632+
expect(
633+
screen.getByRole("combobox", { name: "Model" }),
634+
).not.toHaveAttribute("aria-busy", "true"),
635+
);
636+
637+
await user.click(screen.getByRole("combobox", { name: "Harness" }));
638+
await user.click(
639+
await screen.findByRole("option", {
640+
name: "Custom executable / current value",
641+
}),
642+
);
643+
const executable = screen.getByRole("textbox", { name: "Executable" });
644+
await user.clear(executable);
645+
await user.paste("/custom/hermes-acp");
646+
expect(screen.queryByLabelText("Model")).toBeNull();
647+
648+
await user.clear(screen.getByRole("textbox", { name: "Executable" }));
649+
await user.paste("/custom/goose-acp");
650+
await waitFor(() =>
651+
expect(screen.getByRole("combobox", { name: "Model" })).toBeVisible(),
652+
);
653+
expect(screen.getByLabelText("Anthropic API key")).toBeVisible();
654+
await act(async () => {});
655+
expect(run).toHaveBeenCalledOnce();
656+
} finally {
657+
view.unmount();
658+
control.dispose();
659+
}
660+
});
661+
575662
it("adds a Pi provider API key for lookup and drops it when the provider changes", async () => {
576663
const fixture = controlFixture();
577664
fixture.data.harnessOptions = [

0 commit comments

Comments
 (0)