From 0ee61b5002f8c01d882ed92a7caa78d62245b123 Mon Sep 17 00:00:00 2001 From: Dotta Date: Sat, 10 Oct 2026 13:26:57 -0500 Subject: [PATCH] fix: keep the API account selected during agent setup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bind the account returned by credential creation explicitly instead of silently resolving the user’s existing provider default. Preserve that binding on connection-test retries without creating another account or changing defaults. Co-Authored-By: Paperclip --- .../AgentProviderConnection.test.tsx | 41 +++++++++++++++++++ .../new-agent/AgentProviderConnection.tsx | 13 +++++- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/ui/src/components/new-agent/AgentProviderConnection.test.tsx b/ui/src/components/new-agent/AgentProviderConnection.test.tsx index cb77c83b163..c2b5950c76b 100644 --- a/ui/src/components/new-agent/AgentProviderConnection.test.tsx +++ b/ui/src/components/new-agent/AgentProviderConnection.test.tsx @@ -21,6 +21,7 @@ const managedApi = vi.hoisted(() => ({ checkLocalLogin: vi.fn(async () => ({ status: "sign_in_required" as "ready" | "sign_in_required" })), cancelLocalLogin: vi.fn(async () => ({})), create: vi.fn(async () => ({ connectionId: "managed-connection", grantId: "managed-grant" })), + setDefault: vi.fn(), })); vi.mock("@/api/ai-connections", () => ({ aiConnectionsApi: managedApi })); vi.mock("@/api/agents", () => ({ @@ -410,6 +411,46 @@ describe("AgentProviderConnection reuse", () => { expect(host.textContent).not.toContain("Use saved subscription"); }); + it.each(["claude_local", "codex_local"] as const)("tests and retries the newly entered API account without replacing the subscription default: %s", async adapterType => { + const provider = adapterType === "claude_local" ? "anthropic" : "openai"; + managedApi.list.mockResolvedValue({ currentUserId: "user-1", connections: [{ + id: "subscription-account", grantId: "subscription-grant", companyId: "c1", provider, + method: "subscription", name: "My subscription", ownership: "personal", + ownerUserId: "user-1", isDefault: true, status: "connected", + }] } as never); + const { test, connected, key } = await mount(adapterType, false, true, false, false); + test.mockResolvedValueOnce(false).mockResolvedValueOnce(true); + click("Use API key instead"); + openProvider(); + flushSync(() => { + const input = host.querySelector('input[type="password"]')!; + Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!.call(input, "fixture-api-key"); + input.dispatchEvent(new Event("input", { bubbles: true })); + }); + click("Connect"); + const expected = { + env: {}, + aiConnection: { + provider, method: "api_key", mode: "delegated", + connectionId: "managed-connection", grantId: "managed-grant", + }, + }; + await vi.waitFor(() => expect(host.textContent).toContain("The provider did not respond")); + expect(test).toHaveBeenCalledExactlyOnceWith(expected); + expect(connected).not.toHaveBeenCalled(); + expect(managedApi.create).toHaveBeenCalledExactlyOnceWith("c1", { + provider, method: "api_key", name: `My ${provider === "openai" ? "OpenAI" : "Claude"} API`, + ownership: "personal", apiKey: "fixture-api-key", agentIds: [], allAgents: true, + }); + expect(host.querySelector('input[type="password"]')?.value ?? "").toBe(""); + click("Connect"); + await vi.waitFor(() => expect(connected).toHaveBeenCalledExactlyOnceWith(expected)); + expect(test).toHaveBeenNthCalledWith(2, expected); + expect(managedApi.create).toHaveBeenCalledTimes(1); + expect(managedApi.setDefault).not.toHaveBeenCalled(); + expect(connected.mock.calls[0][0].credentials?.[key]).toBeUndefined(); + }); + it("defaults to subscription when no saved credentials exist", async () => { await mount("claude_local", false, true, false, false); expect(host.textContent).toContain("Use API key instead"); diff --git a/ui/src/components/new-agent/AgentProviderConnection.tsx b/ui/src/components/new-agent/AgentProviderConnection.tsx index 75aee7460c8..16df3e2e26b 100644 --- a/ui/src/components/new-agent/AgentProviderConnection.tsx +++ b/ui/src/components/new-agent/AgentProviderConnection.tsx @@ -201,8 +201,17 @@ export function AgentProviderConnection({ connection = { env: {}, aiConnection: { provider: aiProvider, method: "subscription", mode: "responsible_user" } }; } if (connection.credentials) { - await aiConnectionsApi.create(companyId, { provider: aiProvider, method: "api_key", name: `My ${provider} API`, ownership: "personal", apiKey: connection.credentials[envKey], agentIds: [], allAgents: true }); - connection = { env: {}, aiConnection: { provider: aiProvider, method: "api_key", mode: "responsible_user" } }; + const account = await aiConnectionsApi.create(companyId, { provider: aiProvider, method: "api_key", name: `My ${provider} API`, ownership: "personal", apiKey: connection.credentials[envKey], agentIds: [], allAgents: true }); + connection = { + env: {}, + aiConnection: { + provider: aiProvider, + method: "api_key", + mode: "delegated", + connectionId: account.connectionId, + grantId: account.grantId, + }, + }; } if (run !== epoch.current) return; if (method === "api") {