From 48a1b07097713c1b3668fb51a3cb5decee0484b4 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Fri, 9 Jan 2026 05:36:14 +0000 Subject: [PATCH] fix: promote selected auth profile order --- src/commands/onboard-auth.test.ts | 30 +++++++++++++++++++++++++++++- src/commands/onboard-auth.ts | 17 ++++++++++++++--- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/commands/onboard-auth.test.ts b/src/commands/onboard-auth.test.ts index f50ab2823..5def9a6ef 100644 --- a/src/commands/onboard-auth.test.ts +++ b/src/commands/onboard-auth.test.ts @@ -5,7 +5,10 @@ import path from "node:path"; import type { OAuthCredentials } from "@mariozechner/pi-ai"; import { afterEach, describe, expect, it } from "vitest"; -import { writeOAuthCredentials } from "./onboard-auth.js"; +import { + applyAuthProfileConfig, + writeOAuthCredentials, +} from "./onboard-auth.js"; describe("writeOAuthCredentials", () => { const previousStateDir = process.env.CLAWDBOT_STATE_DIR; @@ -77,3 +80,28 @@ describe("writeOAuthCredentials", () => { ).rejects.toThrow(); }); }); + +describe("applyAuthProfileConfig", () => { + it("promotes the newly selected profile to the front of auth.order", () => { + const next = applyAuthProfileConfig( + { + auth: { + profiles: { + "anthropic:default": { provider: "anthropic", mode: "api_key" }, + }, + order: { anthropic: ["anthropic:default"] }, + }, + }, + { + profileId: "anthropic:claude-cli", + provider: "anthropic", + mode: "oauth", + }, + ); + + expect(next.auth?.order?.anthropic).toEqual([ + "anthropic:claude-cli", + "anthropic:default", + ]); + }); +}); diff --git a/src/commands/onboard-auth.ts b/src/commands/onboard-auth.ts index a4d367d48..e1116a98d 100644 --- a/src/commands/onboard-auth.ts +++ b/src/commands/onboard-auth.ts @@ -53,6 +53,7 @@ export function applyAuthProfileConfig( provider: string; mode: "api_key" | "oauth"; email?: string; + preferProfileFirst?: boolean; }, ): ClawdbotConfig { const profiles = { @@ -67,13 +68,23 @@ export function applyAuthProfileConfig( // Only maintain `auth.order` when the user explicitly configured it. // Default behavior: no explicit order -> resolveAuthProfileOrder can round-robin by lastUsed. const existingProviderOrder = cfg.auth?.order?.[params.provider]; + const preferProfileFirst = params.preferProfileFirst ?? true; + const reorderedProviderOrder = + existingProviderOrder && preferProfileFirst + ? [ + params.profileId, + ...existingProviderOrder.filter( + (profileId) => profileId !== params.profileId, + ), + ] + : existingProviderOrder; const order = existingProviderOrder !== undefined ? { ...cfg.auth?.order, - [params.provider]: existingProviderOrder.includes(params.profileId) - ? existingProviderOrder - : [...existingProviderOrder, params.profileId], + [params.provider]: reorderedProviderOrder?.includes(params.profileId) + ? reorderedProviderOrder + : [...(reorderedProviderOrder ?? []), params.profileId], } : cfg.auth?.order; return {