fix: keep the API account selected during agent setup

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 <noreply@paperclip.ing>
This commit is contained in:
DottaandPaperclip committed 2026-10-10 13:27:47 -05:00
1 parent 8f11a70b33
commit 0ee61b5002
2 files changed
+52 -2

No files matched your search

@@ -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<HTMLInputElement>('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");
@@ -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") {