From 6eb6f86892540dc31a253a9bd9bc697a9dfc146f Mon Sep 17 00:00:00 2001 From: Winston Chang Date: Wed, 11 Nov 2026 23:50:21 -0600 Subject: [PATCH 1/3] Add checkGoogleVertexSignIn and userInitiated Vertex status updates Add a Vertex counterpart to checkAwsSignIn so hosts can ask "does this provider need sign-in now?" before a send or after a failed turn. It mints a token from the same credential source the client factory uses and classifies failures with the model list's isAuthError + authErrorGuidance, returning sign-in-required (with a userInitiated adc_expired update) only for expired ADC; other failures are unknown (fail open) and brokered tokens are ok. Name the Vertex status update type (GoogleVertexProviderStatusUpdate) and give it the same userInitiated flag as Bedrock's. Export the check, its result type, and the update type from ai-provider-bridge/providers. --- packages/ai-provider-bridge/src/providers.ts | 7 +- .../__tests__/google-vertex-provider.test.ts | 61 +++++++++- .../src/providers/google-vertex-provider.ts | 114 +++++++++++++----- 3 files changed, 153 insertions(+), 29 deletions(-) diff --git a/packages/ai-provider-bridge/src/providers.ts b/packages/ai-provider-bridge/src/providers.ts index 29cc887..6dabb3a 100644 --- a/packages/ai-provider-bridge/src/providers.ts +++ b/packages/ai-provider-bridge/src/providers.ts @@ -45,6 +45,7 @@ export { } from "./providers/foundry-provider"; export { registerCustomGeminiProvider, registerGeminiProvider } from "./providers/gemini-provider"; export { + checkGoogleVertexSignIn, registerCustomGoogleVertexProvider, registerGoogleVertexProvider, } from "./providers/google-vertex-provider"; @@ -53,7 +54,11 @@ export { resolveGoogleVertexCredentialSource, } from "./google-vertex-credentials"; export type { GoogleVertexCredentialSource } from "./google-vertex-credentials"; -export type { GoogleVertexProviderCallbacks } from "./providers/google-vertex-provider"; +export type { + GoogleVertexProviderCallbacks, + GoogleVertexProviderStatusUpdate, + GoogleVertexSignInCheck, +} from "./providers/google-vertex-provider"; export { registerCustomLitellmProvider, registerLitellmProvider, diff --git a/packages/ai-provider-bridge/src/providers/__tests__/google-vertex-provider.test.ts b/packages/ai-provider-bridge/src/providers/__tests__/google-vertex-provider.test.ts index ba7600c..2ae684a 100644 --- a/packages/ai-provider-bridge/src/providers/__tests__/google-vertex-provider.test.ts +++ b/packages/ai-provider-bridge/src/providers/__tests__/google-vertex-provider.test.ts @@ -29,8 +29,9 @@ import { getEffectiveLocation, isVertexAnthropicModel, } from "../../model-clients/GoogleVertexClient"; -import type { Logger } from "../../types"; +import type { GoogleCloudCredentials, Logger } from "../../types"; import { + checkGoogleVertexSignIn, claudeDisplayName, registerCustomGoogleVertexProvider, registerGoogleVertexProvider, @@ -507,6 +508,64 @@ describe("resolveGoogleVertexAccessToken", () => { }); }); +describe("checkGoogleVertexSignIn", () => { + const credentials: GoogleCloudCredentials = { type: "google-cloud", project: "my-project" }; + + beforeEach(() => { + vi.clearAllMocks(); + vi.stubEnv("GOOGLE_APPLICATION_CREDENTIALS", undefined); + authMocks.getClient.mockResolvedValue({ getAccessToken: authMocks.getAccessToken }); + // A `function` (not an arrow) so the code under test can call it with + // `new GoogleAuth(...)`; returning an object makes it the instance. + authMocks.googleAuth.mockImplementation(function () { + return { getClient: authMocks.getClient }; + }); + }); + + afterEach(() => { + vi.unstubAllEnvs(); + }); + + it("requires sign-in when ADC is expired, returning a userInitiated adc_expired update", async () => { + authMocks.getAccessToken.mockRejectedValueOnce(new Error("invalid_grant")); + + const result = await checkGoogleVertexSignIn("custom-vertex", credentials, mockLogger, {}); + + expect(result).toEqual({ + status: "sign-in-required", + update: expect.objectContaining({ + providerId: "custom-vertex", + authMethodId: "google-cloud", + status: "auth_error", + error: expect.objectContaining({ code: "adc_expired" }), + userInitiated: true, + }), + }); + }); + + it("is unknown when GOOGLE_APPLICATION_CREDENTIALS points at a missing file", async () => { + authMocks.getClient.mockRejectedValueOnce(new Error("Could not load the default credentials.")); + + await expect( + checkGoogleVertexSignIn("google-vertex", credentials, mockLogger, { + GOOGLE_APPLICATION_CREDENTIALS: "/no/such/adc.json", + }), + ).resolves.toEqual({ status: "unknown" }); + }); + + it("is ok for a brokered token without minting", async () => { + await expect( + checkGoogleVertexSignIn( + "google-vertex", + { ...credentials, accessToken: "brokered-token" }, + mockLogger, + undefined, + ), + ).resolves.toEqual({ status: "ok" }); + expect(authMocks.googleAuth).not.toHaveBeenCalled(); + }); +}); + describe("describeGoogleVertexCredentialSource", () => { let dir: string; diff --git a/packages/ai-provider-bridge/src/providers/google-vertex-provider.ts b/packages/ai-provider-bridge/src/providers/google-vertex-provider.ts index ffa09c5..4cd66d3 100644 --- a/packages/ai-provider-bridge/src/providers/google-vertex-provider.ts +++ b/packages/ai-provider-bridge/src/providers/google-vertex-provider.ts @@ -15,28 +15,37 @@ import { resolveGoogleVertexCredentialSource, } from "../google-vertex-credentials"; import { GoogleVertexClient } from "../model-clients/GoogleVertexClient"; -import type { Logger, ModelInfo, ProviderCredentials } from "../types"; +import type { GoogleCloudCredentials, Logger, ModelInfo, ProviderCredentials } from "../types"; import { NOTIFICATION_ACTIONS } from "../types"; import type { ClientFactory, ProviderRegistry } from "./ProviderRegistry"; +/** A Google Vertex provider status update, with full provider context. */ +export interface GoogleVertexProviderStatusUpdate { + providerId: string; + authMethodId: string; + status: "ok" | "auth_error" | "network_error"; + error?: { + code: string; + message: string; + action?: { + label: string; + commandId: string; + }; + }; + /** + * Set when a user action (a send-time sign-in check) produced the update rather than model + * discovery. Hosts re-show the prompt even if it was already shown or dismissed, and must not + * count the update as discovery evidence (e.g. as a reconnect verification verdict). + */ + userInitiated?: true; +} + /** * Optional callbacks for platform-specific Google Vertex provider status updates. * Same interface shape as BedrockProviderCallbacks. */ export interface GoogleVertexProviderCallbacks { - onProviderStatusChange?: (update: { - providerId: string; - authMethodId: string; - status: "ok" | "auth_error" | "network_error"; - error?: { - code: string; - message: string; - action?: { - label: string; - commandId: string; - }; - }; - }) => Promise; + onProviderStatusChange?: (update: GoogleVertexProviderStatusUpdate) => Promise; } /** @@ -103,6 +112,69 @@ function authErrorGuidance(source: GoogleVertexCredentialSource): { return AUTH_ERROR_GUIDANCE[source.kind]; } +function vertexAuthErrorUpdate( + providerId: string, + guidance: { code: string; message: string }, +): GoogleVertexProviderStatusUpdate { + return { + providerId, + authMethodId: "google-cloud", + status: "auth_error", + error: { + code: guidance.code, + message: guidance.message, + action: { + label: "Reload model list", + commandId: NOTIFICATION_ACTIONS.REFRESH_MODELS, + }, + }, + }; +} + +/** Result of {@link checkGoogleVertexSignIn}. */ +export type GoogleVertexSignInCheck = + | { status: "ok" } + | { status: "unknown" } + | { status: "sign-in-required"; update: GoogleVertexProviderStatusUpdate }; + +/** + * Mint a Vertex access token and report whether the user must sign in again. Publishes nothing: + * on `sign-in-required` the result carries the `adc_expired` status update (the model list's copy, + * flagged `userInitiated`) for the caller to deliver to its status callbacks. Any other failure, + * including other auth errors (a missing key file, rejected inline credentials), is `unknown`. + * A brokered token is returned as-is by minting, so it is `ok` without being validated. + * `credentialEnvironment` is the same captured environment the client factory receives + * (undefined reads `process.env`). + */ +export async function checkGoogleVertexSignIn( + providerId: string, + credentials: GoogleCloudCredentials, + logger: Logger, + credentialEnvironment: Readonly> | undefined, +): Promise { + const credentialSource = resolveGoogleVertexCredentialSource( + credentials.accessToken, + credentialEnvironment, + ); + try { + await mintGoogleVertexAccessToken(credentialSource); + return { status: "ok" }; + } catch (error) { + const guidance = isAuthError(error) ? authErrorGuidance(credentialSource) : undefined; + if (guidance?.code !== "adc_expired") { + const errorMsg = error instanceof Error ? error.message : String(error); + logger.warn( + `[GoogleVertex] Sign-in check for ${providerId} could not mint a token from ${describeGoogleVertexCredentialSource(credentialSource)}: ${errorMsg}`, + ); + return { status: "unknown" }; + } + return { + status: "sign-in-required", + update: { ...vertexAuthErrorUpdate(providerId, guidance), userInitiated: true }, + }; + } +} + /** * Fetch models from a single Vertex AI publisher endpoint. */ @@ -393,19 +465,7 @@ function createGoogleVertexModelFetcher( `[GoogleVertex] ${guidance.message} Credential source: ${sourceDescription}. Error: ${errorMsg}`, ); - await callbacks?.onProviderStatusChange?.({ - providerId, - authMethodId: "google-cloud", - status: "auth_error", - error: { - code: guidance.code, - message: guidance.message, - action: { - label: "Reload model list", - commandId: NOTIFICATION_ACTIONS.REFRESH_MODELS, - }, - }, - }); + await callbacks?.onProviderStatusChange?.(vertexAuthErrorUpdate(providerId, guidance)); // Clear cache so subsequent requests don't short-circuit cachedModels = null; From ea8fe371dabc93a44de3489f3d66119880d47e2e Mon Sep 17 00:00:00 2001 From: Winston Chang Date: Thu, 12 Nov 2026 00:09:16 -0600 Subject: [PATCH 2/3] Reword userInitiated docs: sign-in check also runs after a failed turn --- .../ai-provider-bridge/src/providers/bedrock-provider.ts | 6 +++--- .../src/providers/google-vertex-provider.ts | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/ai-provider-bridge/src/providers/bedrock-provider.ts b/packages/ai-provider-bridge/src/providers/bedrock-provider.ts index 1969cde..13460e0 100644 --- a/packages/ai-provider-bridge/src/providers/bedrock-provider.ts +++ b/packages/ai-provider-bridge/src/providers/bedrock-provider.ts @@ -56,9 +56,9 @@ export interface BedrockProviderStatusUpdate { }; }; /** - * Set when a user action (a send-time sign-in check) produced the update rather than model - * discovery. Hosts re-show the prompt even if it was already shown or dismissed, and must not - * count the update as discovery evidence (e.g. as a reconnect verification verdict). + * Set when a sign-in check (before a send or after a failed turn) produced the update rather + * than model discovery. Hosts re-show the prompt even if it was already shown or dismissed, and + * must not count the update as discovery evidence (e.g. as a reconnect verification verdict). */ userInitiated?: true; } diff --git a/packages/ai-provider-bridge/src/providers/google-vertex-provider.ts b/packages/ai-provider-bridge/src/providers/google-vertex-provider.ts index 4cd66d3..868d723 100644 --- a/packages/ai-provider-bridge/src/providers/google-vertex-provider.ts +++ b/packages/ai-provider-bridge/src/providers/google-vertex-provider.ts @@ -33,9 +33,9 @@ export interface GoogleVertexProviderStatusUpdate { }; }; /** - * Set when a user action (a send-time sign-in check) produced the update rather than model - * discovery. Hosts re-show the prompt even if it was already shown or dismissed, and must not - * count the update as discovery evidence (e.g. as a reconnect verification verdict). + * Set when a sign-in check (before a send or after a failed turn) produced the update rather + * than model discovery. Hosts re-show the prompt even if it was already shown or dismissed, and + * must not count the update as discovery evidence (e.g. as a reconnect verification verdict). */ userInitiated?: true; } From ac400c1055397453656f229f168ba4c14d8c4af6 Mon Sep 17 00:00:00 2001 From: Winston Chang Date: Thu, 12 Nov 2026 01:01:51 -0600 Subject: [PATCH 3/3] Don't require sign-in when a GOOGLE_APPLICATION_CREDENTIALS file is rejected An explicit key file overrides gcloud's well-known file, so a gcloud login can't repair a rejected one (e.g. a revoked service-account key). The model list labels it adc_expired, but checkGoogleVertexSignIn now returns unknown for it so hosts don't refuse sends with a sign-in prompt that can't help. --- .../src/google-vertex-credentials.ts | 8 ++++++++ .../__tests__/google-vertex-provider.test.ts | 19 +++++++++++++++++++ .../src/providers/google-vertex-provider.ts | 6 ++++-- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/ai-provider-bridge/src/google-vertex-credentials.ts b/packages/ai-provider-bridge/src/google-vertex-credentials.ts index 2de9b20..ee080bf 100644 --- a/packages/ai-provider-bridge/src/google-vertex-credentials.ts +++ b/packages/ai-provider-bridge/src/google-vertex-credentials.ts @@ -168,6 +168,14 @@ function adcKeyFile(source: GoogleVertexCredentialSource & { kind: "adc" }): str ); } +/** + * The `GOOGLE_APPLICATION_CREDENTIALS` path ADC reads, or undefined when it + * uses gcloud's file or the metadata server (or the source isn't ADC). + */ +export function googleVertexKeyFile(source: GoogleVertexCredentialSource): string | undefined { + return source.kind === "adc" ? adcKeyFile(source) : undefined; +} + /** * The `GOOGLE_APPLICATION_CREDENTIALS` path when it points at a file that does * not exist. google-auth-library does not fall back to gcloud's file then, so diff --git a/packages/ai-provider-bridge/src/providers/__tests__/google-vertex-provider.test.ts b/packages/ai-provider-bridge/src/providers/__tests__/google-vertex-provider.test.ts index 2ae684a..7c35ea1 100644 --- a/packages/ai-provider-bridge/src/providers/__tests__/google-vertex-provider.test.ts +++ b/packages/ai-provider-bridge/src/providers/__tests__/google-vertex-provider.test.ts @@ -553,6 +553,25 @@ describe("checkGoogleVertexSignIn", () => { ).resolves.toEqual({ status: "unknown" }); }); + it("is unknown when an existing GOOGLE_APPLICATION_CREDENTIALS file is rejected", async () => { + // A gcloud login writes gcloud's own file, which this explicit file + // overrides, so prompting to sign in again could not fix it. + const dir = mkdtempSync(join(tmpdir(), "vertex-key-")); + try { + const keyFile = join(dir, "service-account.json"); + writeFileSync(keyFile, "{}"); + authMocks.getAccessToken.mockRejectedValueOnce(new Error("invalid_grant")); + + await expect( + checkGoogleVertexSignIn("google-vertex", credentials, mockLogger, { + GOOGLE_APPLICATION_CREDENTIALS: keyFile, + }), + ).resolves.toEqual({ status: "unknown" }); + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }); + it("is ok for a brokered token without minting", async () => { await expect( checkGoogleVertexSignIn( diff --git a/packages/ai-provider-bridge/src/providers/google-vertex-provider.ts b/packages/ai-provider-bridge/src/providers/google-vertex-provider.ts index 868d723..9c20a50 100644 --- a/packages/ai-provider-bridge/src/providers/google-vertex-provider.ts +++ b/packages/ai-provider-bridge/src/providers/google-vertex-provider.ts @@ -11,6 +11,7 @@ import { type GoogleVertexCredentialSource, isInlineServiceAccountError, mintGoogleVertexAccessToken, + googleVertexKeyFile, missingGoogleVertexKeyFile, resolveGoogleVertexCredentialSource, } from "../google-vertex-credentials"; @@ -142,7 +143,8 @@ export type GoogleVertexSignInCheck = * on `sign-in-required` the result carries the `adc_expired` status update (the model list's copy, * flagged `userInitiated`) for the caller to deliver to its status callbacks. Any other failure, * including other auth errors (a missing key file, rejected inline credentials), is `unknown`. - * A brokered token is returned as-is by minting, so it is `ok` without being validated. + * So is a rejected `GOOGLE_APPLICATION_CREDENTIALS` file: it overrides gcloud's file, so signing + * in again with gcloud could not repair it. A brokered token is returned as-is by minting, so it is `ok` without being validated. * `credentialEnvironment` is the same captured environment the client factory receives * (undefined reads `process.env`). */ @@ -161,7 +163,7 @@ export async function checkGoogleVertexSignIn( return { status: "ok" }; } catch (error) { const guidance = isAuthError(error) ? authErrorGuidance(credentialSource) : undefined; - if (guidance?.code !== "adc_expired") { + if (guidance?.code !== "adc_expired" || googleVertexKeyFile(credentialSource)) { const errorMsg = error instanceof Error ? error.message : String(error); logger.warn( `[GoogleVertex] Sign-in check for ${providerId} could not mint a token from ${describeGoogleVertexCredentialSource(credentialSource)}: ${errorMsg}`,