Skip to content

Commit e1ec95d

Browse files
authored
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
1 parent 7a819da commit e1ec95d

53 files changed

Lines changed: 283 additions & 624 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎extensions/anthropic/register.runtime.ts‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import {
2222
type OpenClawConfig as ProviderAuthConfig,
2323
type ProviderAuthResult,
2424
suggestOAuthProfileIdForLegacyDefault,
25-
upsertAuthProfileWithLock,
2625
validateAnthropicSetupToken,
2726
} from "openclaw/plugin-sdk/provider-auth";
27+
import { upsertAuthProfileWithLockOrThrow } from "openclaw/plugin-sdk/provider-auth-api-key";
2828
import { buildOpenAICompatibleProviderCatalog } from "openclaw/plugin-sdk/provider-catalog-live-runtime";
2929
import {
3030
buildManifestModelProviderConfig,
@@ -90,7 +90,6 @@ function classifyAnthropicFailoverDescriptor(value: string | undefined) {
9090
return undefined;
9191
}
9292
}
93-
type UpsertAuthProfileParams = Parameters<typeof upsertAuthProfileWithLock>[0];
9493
const DEFAULT_ANTHROPIC_MODEL = "anthropic/claude-opus-5";
9594
const ANTHROPIC_OPUS_48_MODEL_ID = "claude-opus-4-8";
9695
const ANTHROPIC_OPUS_48_DOT_MODEL_ID = "claude-opus-4.8";
@@ -196,14 +195,6 @@ const CLAUDE_CLI_CANONICAL_ALLOWLIST_REFS = CLAUDE_CLI_DEFAULT_ALLOWLIST_REFS.ma
196195
: ref,
197196
);
198197

199-
async function upsertAuthProfileWithLockOrThrow(params: UpsertAuthProfileParams): Promise<void> {
200-
const updated = await upsertAuthProfileWithLock(params);
201-
if (!updated) {
202-
throw new Error(
203-
"Failed to update auth profile store; the auth store lock may be busy. Wait a moment and retry.",
204-
);
205-
}
206-
}
207198
function normalizeAnthropicSetupTokenInput(value: string): string {
208199
return value.replaceAll(/\s+/g, "").trim();
209200
}

‎extensions/cloudflare-ai-gateway/index.ts‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import {
1111
listProfilesForProvider,
1212
normalizeApiKeyInput,
1313
normalizeOptionalSecretInput,
14-
upsertAuthProfileWithLock,
1514
validateApiKeyInput,
1615
} from "openclaw/plugin-sdk/provider-auth";
16+
import { upsertAuthProfileWithLockOrThrow } from "openclaw/plugin-sdk/provider-auth-api-key";
1717
import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
1818
import { buildCloudflareAiGatewayCatalogProvider } from "./catalog-provider.js";
1919
import { CLOUDFLARE_AI_GATEWAY_DEFAULT_MODEL_REF } from "./models.js";
@@ -23,17 +23,6 @@ import { wrapCloudflareAiGatewayProviderStream } from "./stream-wrappers.js";
2323
const PROVIDER_ID = "cloudflare-ai-gateway";
2424
const PROVIDER_ENV_VAR = "CLOUDFLARE_AI_GATEWAY_API_KEY";
2525
const PROFILE_ID = "cloudflare-ai-gateway:default";
26-
type UpsertAuthProfileParams = Parameters<typeof upsertAuthProfileWithLock>[0];
27-
28-
async function upsertAuthProfileWithLockOrThrow(params: UpsertAuthProfileParams): Promise<void> {
29-
const updated = await upsertAuthProfileWithLock(params);
30-
if (!updated) {
31-
throw new Error(
32-
"Failed to update auth profile store; the auth store lock may be busy. Wait a moment and retry.",
33-
);
34-
}
35-
}
36-
3726
function readRequiredTextInput(value: unknown): string {
3827
return typeof value === "string" ? value.trim() : "";
3928
}

‎extensions/discord/src/monitor/exec-approvals.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,9 @@ describe("discord exec approval monitor helpers", () => {
271271
approvalId: "abc",
272272
approvalKind,
273273
decision: "allow-once",
274+
channel: "discord",
275+
senderId: "default",
274276
gatewayUrl: "ws://127.0.0.1:18789",
275-
clientDisplayName: "Discord approval (default)",
276277
});
277278
},
278279
);

‎extensions/discord/src/monitor/exec-approvals.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,9 @@ export function createDiscordExecApprovalButtonContext(params: {
204204
approvalId,
205205
approvalKind,
206206
decision,
207+
channel: "discord",
208+
senderId: params.accountId,
207209
gatewayUrl: params.gatewayUrl,
208-
clientDisplayName: `Discord approval (${params.accountId})`,
209210
});
210211
return { ok: true, resolution };
211212
} catch (err) {

‎extensions/github-copilot/login.ts‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {
1212
applyAuthProfileConfig,
1313
ensureAuthProfileStore,
1414
normalizeGithubCopilotDomain,
15-
upsertAuthProfileWithLock,
1615
} from "openclaw/plugin-sdk/provider-auth";
16+
import { upsertAuthProfileWithLockOrThrow } from "openclaw/plugin-sdk/provider-auth-api-key";
1717
import { readProviderJsonResponse } from "openclaw/plugin-sdk/provider-http";
1818
import type { RuntimeEnv } from "openclaw/plugin-sdk/runtime";
1919
import { fetchWithSsrFGuard, type SsrFPolicy } from "openclaw/plugin-sdk/ssrf-runtime";
@@ -63,8 +63,6 @@ type DeviceTokenResponse =
6363
const GITHUB_DEVICE_ACCESS_DENIED = Symbol("github-device-access-denied");
6464
const GITHUB_DEVICE_EXPIRED = Symbol("github-device-expired");
6565

66-
type UpsertAuthProfileParams = Parameters<typeof upsertAuthProfileWithLock>[0];
67-
6866
class GitHubDeviceFlowError extends Error {
6967
readonly kind: symbol;
7068
constructor(kind: symbol, message: string) {
@@ -74,15 +72,6 @@ class GitHubDeviceFlowError extends Error {
7472
}
7573
}
7674

77-
async function upsertAuthProfileWithLockOrThrow(params: UpsertAuthProfileParams): Promise<void> {
78-
const updated = await upsertAuthProfileWithLock(params);
79-
if (!updated) {
80-
throw new Error(
81-
"Failed to update auth profile store; the auth store lock may be busy. Wait a moment and retry.",
82-
);
83-
}
84-
}
85-
8675
function isGitHubDeviceAccessDeniedError(err: unknown): boolean {
8776
return err instanceof GitHubDeviceFlowError && err.kind === GITHUB_DEVICE_ACCESS_DENIED;
8877
}

‎extensions/googlechat/src/approval-card-click.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ describe("maybeHandleGoogleChatApprovalCardClick", () => {
164164
approvalId: "approval-1",
165165
approvalKind: "exec",
166166
decision: "allow-once",
167+
channel: "googlechat",
167168
senderId: "users/123",
168-
clientDisplayName: "Google Chat approval (users/123)",
169169
});
170170
expect(updateGoogleChatMessage).toHaveBeenCalledWith({
171171
account: target.account,

‎extensions/googlechat/src/approval-card-click.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ export async function maybeHandleGoogleChatApprovalCardClick(params: {
8686
approvalId: consumed.approvalId,
8787
approvalKind: consumed.approvalKind,
8888
decision: consumed.decision,
89+
channel: "googlechat",
8990
senderId: actor,
90-
clientDisplayName: `Google Chat approval (${actor?.trim() || "unknown"})`,
9191
});
9292
await updateGoogleChatMessage({
9393
account: params.target.account,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { resolveApprovalOverGateway } from "openclaw/plugin-sdk/approval-gateway-runtime";
2+
3+
export type IMessageApprovalGatewayRuntime = NonNullable<
4+
Parameters<typeof resolveApprovalOverGateway>[0]["gatewayRuntime"]
5+
>;

‎extensions/imessage/src/approval-polls.test.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ const resolverMocks = vi.hoisted(() => ({
1414
isApprovalNotFoundError: vi.fn(() => false),
1515
}));
1616

17-
vi.mock("./approval-resolver.js", () => ({
18-
resolveIMessageApproval: resolverMocks.resolveIMessageApproval,
17+
vi.mock("openclaw/plugin-sdk/approval-gateway-runtime", () => ({
18+
resolveApprovalOverGateway: resolverMocks.resolveIMessageApproval,
19+
}));
20+
vi.mock("openclaw/plugin-sdk/error-runtime", () => ({
1921
isApprovalNotFoundError: resolverMocks.isApprovalNotFoundError,
2022
}));
2123

‎extensions/imessage/src/approval-polls.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import {
99
} from "openclaw/plugin-sdk/approval-reaction-runtime";
1010
import type { ExecApprovalReplyDecision } from "openclaw/plugin-sdk/approval-reply-runtime";
1111
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
12+
import { isApprovalNotFoundError } from "openclaw/plugin-sdk/error-runtime";
1213
import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime";
1314
import { asDateTimestampMs } from "openclaw/plugin-sdk/number-runtime";
1415
import { createPluginStateErrorReporter } from "openclaw/plugin-sdk/plugin-state-runtime";
1516
import { getIMessageApprovalApprovers, imessageApprovalAuth } from "./approval-auth.js";
16-
import type { IMessageApprovalGatewayRuntime } from "./approval-resolver.js";
17+
import type { IMessageApprovalGatewayRuntime } from "./approval-gateway-types.js";
1718
import {
1819
buildIMessageApprovalConversationKeyForInbound,
1920
enumerateApprovalTargetKeys,
@@ -52,7 +53,9 @@ type IMessageApprovalPollTarget = {
5253

5354
type IMessageApprovalPollTombstone = { approvalId: string };
5455

55-
const loadApprovalResolver = createLazyRuntimeModule(() => import("./approval-resolver.js"));
56+
const loadApprovalResolver = createLazyRuntimeModule(
57+
() => import("openclaw/plugin-sdk/approval-gateway-runtime"),
58+
);
5659

5760
const reportPersistentError = createPluginStateErrorReporter(
5861
getOptionalIMessageRuntime,
@@ -531,13 +534,14 @@ export async function maybeResolveIMessageApprovalPollVote(params: {
531534
return true;
532535
}
533536

534-
const { isApprovalNotFoundError, resolveIMessageApproval } = await loadApprovalResolver();
537+
const { resolveApprovalOverGateway } = await loadApprovalResolver();
535538
try {
536-
const result = await resolveIMessageApproval({
539+
const result = await resolveApprovalOverGateway({
537540
cfg: params.cfg,
538541
approvalId: target.approvalId,
539542
approvalKind: target.approvalKind,
540543
decision,
544+
channel: "imessage",
541545
senderId: event.actorHandle,
542546
gatewayUrl: params.gatewayUrl,
543547
...(params.gatewayRuntime ? { gatewayRuntime: params.gatewayRuntime } : {}),

0 commit comments

Comments
 (0)