refactor(auth): dedupe profile upserts and approval resolvers (#120831)

* refactor(auth): dedupe profile upserts and approval resolvers

* test(auth): mock canonical locked upsert

* test(auth): mock locked upsert during onboarding
This commit is contained in:
Peter Steinberger
2026-08-08 21:14:16 -07:00
committed by GitHub
parent 7a819da6fc
commit e1ec95dcbf
53 changed files with 283 additions and 624 deletions
+1 -12
View File
@@ -1,7 +1,7 @@
/** Builds API-key provider auth methods that write profiles and config updates. */
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
import { normalizeUniqueStringEntries } from "@openclaw/normalization-core/string-normalization";
import { upsertAuthProfileWithLock } from "../agents/auth-profiles/profiles.js";
import { upsertAuthProfileWithLockOrThrow } from "../agents/auth-profiles/profiles.js";
import { resolveAgentModelPrimaryValue } from "../config/model-input.js";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import type { SecretInput } from "../config/types.secrets.js";
@@ -39,8 +39,6 @@ type ProviderApiKeyAuthMethodOptions = {
applyConfig?: (cfg: OpenClawConfig) => OpenClawConfig;
};
type UpsertAuthProfileParams = Parameters<typeof upsertAuthProfileWithLock>[0];
const loadProviderApiKeyAuthRuntime = createLazyRuntimeSurface(
() => import("./provider-api-key-auth.runtime.js"),
({ providerApiKeyAuthRuntime }) => providerApiKeyAuthRuntime,
@@ -66,15 +64,6 @@ function resolveProfileIds(params: {
return [resolveProfileId(params)];
}
async function upsertAuthProfileWithLockOrThrow(params: UpsertAuthProfileParams): Promise<void> {
const updated = await upsertAuthProfileWithLock(params);
if (!updated) {
throw new Error(
"Failed to update auth profile store; the auth store lock may be busy. Wait a moment and retry.",
);
}
}
async function applyApiKeyConfig(params: {
ctx: ProviderAuthMethodNonInteractiveContext;
providerId: string;
+1 -11
View File
@@ -5,7 +5,7 @@ import {
resolveAgentDir,
resolveAgentWorkspaceDir,
} from "../agents/agent-scope.js";
import { upsertAuthProfileWithLock } from "../agents/auth-profiles.js";
import { upsertAuthProfileWithLockOrThrow } from "../agents/auth-profiles.js";
import { formatLiteralProviderPrefixedModelRef } from "../agents/model-ref-shared.js";
import { resolveDefaultAgentWorkspaceDir } from "../agents/workspace.js";
import { normalizeAgentModelRefForConfig } from "../config/model-input.js";
@@ -31,8 +31,6 @@ import type {
ProviderPlugin,
} from "./types.js";
type UpsertAuthProfileParams = Parameters<typeof upsertAuthProfileWithLock>[0];
type ApplyProviderAuthChoiceParams = {
authChoice: string;
config: OpenClawConfig;
@@ -643,11 +641,3 @@ export async function applyAuthChoiceLoadedPluginProvider(
...(prepared.retrySelection ? { retrySelection: true } : {}),
};
}
async function upsertAuthProfileWithLockOrThrow(params: UpsertAuthProfileParams): Promise<void> {
const updated = await upsertAuthProfileWithLock(params);
if (!updated) {
throw new Error(
"Failed to update auth profile store; the auth store lock may be busy. Wait a moment and retry.",
);
}
}
+5 -12
View File
@@ -4,7 +4,11 @@ import path from "node:path";
import { uniqueStrings } from "@openclaw/normalization-core/string-normalization";
import { resolveDefaultAgentDir } from "../agents/agent-scope-config.js";
import { buildAuthProfileId } from "../agents/auth-profiles/identity.js";
import { upsertAuthProfile, upsertAuthProfileWithLock } from "../agents/auth-profiles/profiles.js";
import {
upsertAuthProfile,
upsertAuthProfileWithLock,
upsertAuthProfileWithLockOrThrow,
} from "../agents/auth-profiles/profiles.js";
import { resolveProviderIdForAuth } from "../agents/provider-auth-aliases.js";
import { resolveStateDir } from "../config/paths.js";
import type { OpenClawConfig } from "../config/types.openclaw.js";
@@ -22,8 +26,6 @@ import { isValidSecretRef } from "../secrets/ref-contract.js";
import { normalizeSecretInput } from "../utils/normalize-secret-input.js";
import type { SecretInputMode } from "./provider-auth-types.js";
type UpsertAuthProfileParams = Parameters<typeof upsertAuthProfileWithLock>[0];
const resolveAuthAgentDir = (agentDir?: string, config?: OpenClawConfig) =>
agentDir ?? resolveDefaultAgentDir(config ?? {});
@@ -140,15 +142,6 @@ export function upsertApiKeyProfile(params: {
return profileId;
}
async function upsertAuthProfileWithLockOrThrow(params: UpsertAuthProfileParams): Promise<void> {
const updated = await upsertAuthProfileWithLock(params);
if (!updated) {
throw new Error(
"Failed to update auth profile store; the auth store lock may be busy. Wait a moment and retry.",
);
}
}
export function applyAuthProfileConfig(
cfg: OpenClawConfig,
params: {