惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
G
Google Developers Blog
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
J
Java Code Geeks
P
Proofpoint News Feed
V
Visual Studio Blog
爱范儿
爱范儿
The Cloudflare Blog
博客园 - 叶小钗
V
V2EX
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
M
MIT News - Artificial intelligence
Microsoft Security Blog
Microsoft Security Blog
博客园 - 聂微东
H
Help Net Security
B
Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 【当耐特】
量子位
宝玉的分享
宝玉的分享
WordPress大学
WordPress大学
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(google): bound vertex adc token cache expiry · opencl...
steipete · 2026-05-30 · via Recent Commits to openclaw:main

@@ -2,7 +2,11 @@ import { existsSync, readFileSync } from "node:fs";

22

import { readFile } from "node:fs/promises";

33

import os from "node:os";

44

import path from "node:path";

5-

import { resolveExpiresAtMsFromDurationSeconds } from "openclaw/plugin-sdk/number-runtime";

5+

import {

6+

asDateTimestampMs,

7+

resolveExpiresAtMsFromDurationMs,

8+

resolveExpiresAtMsFromDurationSeconds,

9+

} from "openclaw/plugin-sdk/number-runtime";

610

import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";

711812

type GoogleAuthorizedUserCredentials = {

@@ -32,6 +36,7 @@ const GOOGLE_VERTEX_OAUTH_SCOPE = "https://www.googleapis.com/auth/cloud-platfor

3236

// leaves the gateway.

3337

const GOOGLE_VERTEX_TOKEN_EXPIRY_BUFFER_MS = 60_000;

3438

const GOOGLE_VERTEX_DEFAULT_TOKEN_LIFETIME_SECONDS = 3600;

39+

const GOOGLE_VERTEX_AUTHLIB_TOKEN_CACHE_MS = 5 * 60_000;

35403641

let cachedGoogleVertexAuthorizedUserToken: GoogleVertexAuthorizedUserToken | undefined;

3742

let cachedGoogleAuthClient:

@@ -43,18 +48,36 @@ let cachedGoogleAuthClient:

4348

| undefined;

4449

let cachedGoogleVertexAdcToken: GoogleVertexAdcToken | undefined;

455046-

function resolveAuthorizedUserTokenExpiresAtMs(value: unknown, nowMs: number): number {

47-

if (typeof value === "number" && Number.isFinite(value)) {

48-

return (

49-

resolveExpiresAtMsFromDurationSeconds(Math.max(1, value), { nowMs }) ??

50-

nowMs - GOOGLE_VERTEX_TOKEN_EXPIRY_BUFFER_MS

51-

);

51+

function isGoogleVertexTokenFresh(expiresAtMsRaw: number, nowRaw = Date.now()): boolean {

52+

const expiresAtMs = asDateTimestampMs(expiresAtMsRaw);

53+

const nowMs = asDateTimestampMs(nowRaw);

54+

if (expiresAtMs === undefined || nowMs === undefined) {

55+

return false;

5256

}

53-

return (

54-

resolveExpiresAtMsFromDurationSeconds(GOOGLE_VERTEX_DEFAULT_TOKEN_LIFETIME_SECONDS, {

55-

nowMs,

56-

}) ?? nowMs - GOOGLE_VERTEX_TOKEN_EXPIRY_BUFFER_MS

57+

const minFreshExpiresAtMs = resolveExpiresAtMsFromDurationMs(

58+

GOOGLE_VERTEX_TOKEN_EXPIRY_BUFFER_MS,

59+

{ nowMs },

5760

);

61+

return minFreshExpiresAtMs !== undefined && expiresAtMs > minFreshExpiresAtMs;

62+

}

63+64+

function resolveAuthorizedUserTokenExpiresAtMs(value: unknown, nowRaw: number): number | undefined {

65+

const nowMs = asDateTimestampMs(nowRaw);

66+

if (nowMs === undefined) {

67+

return undefined;

68+

}

69+

const lifetimeSeconds =

70+

typeof value === "number" && Number.isFinite(value)

71+

? Math.max(1, value)

72+

: GOOGLE_VERTEX_DEFAULT_TOKEN_LIFETIME_SECONDS;

73+

return resolveExpiresAtMsFromDurationSeconds(lifetimeSeconds, { nowMs }) ?? nowMs;

74+

}

75+76+

function resolveGoogleAuthLibraryTokenExpiresAtMs(nowRaw = Date.now()): number | undefined {

77+

const nowMs = asDateTimestampMs(nowRaw);

78+

return nowMs === undefined

79+

? undefined

80+

: resolveExpiresAtMsFromDurationMs(GOOGLE_VERTEX_AUTHLIB_TOKEN_CACHE_MS, { nowMs });

5881

}

59826083

export function resetGoogleVertexAuthorizedUserTokenCacheForTest(): void {

@@ -177,7 +200,7 @@ async function refreshGoogleVertexAuthorizedUserAccessToken(params: {

177200

if (

178201

cached?.credentialsPath === params.credentialsPath &&

179202

cached.refreshToken === refreshToken &&

180-

cached.expiresAtMs - Date.now() > GOOGLE_VERTEX_TOKEN_EXPIRY_BUFFER_MS

203+

isGoogleVertexTokenFresh(cached.expiresAtMs)

181204

) {

182205

return cached.token;

183206

}

@@ -208,12 +231,15 @@ async function refreshGoogleVertexAuthorizedUserAccessToken(params: {

208231

throw new Error("Google Vertex ADC token refresh response did not include an access_token.");

209232

}

210233

const nowMs = Date.now();

211-

cachedGoogleVertexAuthorizedUserToken = {

212-

token,

213-

expiresAtMs: resolveAuthorizedUserTokenExpiresAtMs(payload?.expires_in, nowMs),

214-

credentialsPath: params.credentialsPath,

215-

refreshToken,

216-

};

234+

const expiresAtMs = resolveAuthorizedUserTokenExpiresAtMs(payload?.expires_in, nowMs);

235+

if (expiresAtMs !== undefined) {

236+

cachedGoogleVertexAuthorizedUserToken = {

237+

token,

238+

expiresAtMs,

239+

credentialsPath: params.credentialsPath,

240+

refreshToken,

241+

};

242+

}

217243

return token;

218244

}

219245

@@ -238,7 +264,7 @@ async function resolveGoogleVertexAccessTokenViaGoogleAuth(): Promise<string> {

238264

const auth = await cachedGoogleAuthClient.promise;

239265240266

const cached = cachedGoogleVertexAdcToken;

241-

if (cached && cached.expiresAtMs - Date.now() > GOOGLE_VERTEX_TOKEN_EXPIRY_BUFFER_MS) {

267+

if (cached && isGoogleVertexTokenFresh(cached.expiresAtMs)) {

242268

return cached.token;

243269

}

244270

@@ -255,10 +281,13 @@ async function resolveGoogleVertexAccessTokenViaGoogleAuth(): Promise<string> {

255281

// `getAccessToken()` return type, so we cache for a conservative 5 minutes.

256282

// The library itself already refreshes well before its own internal expiry,

257283

// so this cache is mainly to avoid hot-loop calls into the auth client.

258-

cachedGoogleVertexAdcToken = {

259-

token: normalized,

260-

expiresAtMs: Date.now() + 5 * 60_000,

261-

};

284+

const expiresAtMs = resolveGoogleAuthLibraryTokenExpiresAtMs();

285+

if (expiresAtMs !== undefined) {

286+

cachedGoogleVertexAdcToken = {

287+

token: normalized,

288+

expiresAtMs,

289+

};

290+

}

262291

return normalized;

263292

}

264293