Skip to content

Commit a3af0ff

Browse files
fix(gateway): refresh model availability from persisted auth
1 parent b81666c commit a3af0ff

2 files changed

Lines changed: 87 additions & 16 deletions

File tree

‎src/gateway/server-methods/models-list-result.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
resolveDefaultAgentId,
99
} from "../../agents/agent-scope.js";
1010
import {
11-
ensureAuthProfileStoreWithoutExternalProfiles,
11+
loadAuthProfileStoreWithoutExternalProfiles,
1212
resolveAuthProfileOrder,
1313
type AuthProfileCredential,
1414
type AuthProfileStore,
@@ -170,10 +170,10 @@ function createModelsListProviderAuthChecker(params: {
170170
workspaceDir: string;
171171
}): ModelsListProviderAuthChecker {
172172
const agentDir = resolveAgentDir(params.cfg, params.agentId);
173-
const store = ensureAuthProfileStoreWithoutExternalProfiles(agentDir, {
173+
// Auth refreshes can be persisted by another CLI process while the gateway
174+
// keeps an older execution snapshot, so browse availability reads SQLite.
175+
const store = loadAuthProfileStoreWithoutExternalProfiles(agentDir, {
174176
allowKeychainPrompt: false,
175-
readOnly: true,
176-
syncExternalCli: false,
177177
});
178178
return createInFlightProviderAuthChecker(
179179
(provider, modelApi) =>

‎src/gateway/server-methods/models.test.ts‎

Lines changed: 83 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
// validation errors, and protocol response shapes.
33
import { describe, expect, it, vi } from "vitest";
44
import { ErrorCodes } from "../../../packages/gateway-protocol/src/index.js";
5+
import {
6+
clearRuntimeAuthProfileStoreSnapshots,
7+
replaceRuntimeAuthProfileStoreSnapshots,
8+
} from "../../agents/auth-profiles.js";
59
import type { OpenClawConfig } from "../../config/types.openclaw.js";
610
import { createDeferred } from "../../test-utils/deferred.js";
711
import { withEnvAsync } from "../../test-utils/env.js";
@@ -21,6 +25,21 @@ const withoutOpenAIEnvAuth = async <T>(run: () => Promise<T>): Promise<T> =>
2125
run,
2226
);
2327

28+
function createDemoOAuthStore(params: { access: string; expires: number }) {
29+
return {
30+
version: 1 as const,
31+
profiles: {
32+
"demo-provider:oauth": {
33+
type: "oauth" as const,
34+
provider: "demo-provider",
35+
access: params.access,
36+
refresh: "refresh-token",
37+
expires: params.expires,
38+
},
39+
},
40+
};
41+
}
42+
2443
function requestModelsList(params: {
2544
view: "configured" | "all";
2645
respond?: ReturnType<typeof vi.fn>;
@@ -488,18 +507,12 @@ describe("models.list", () => {
488507
agentEnv: "main",
489508
},
490509
async (state) => {
491-
await state.writeAuthProfiles({
492-
version: 1,
493-
profiles: {
494-
"demo-provider:expired": {
495-
type: "oauth",
496-
provider: "demo-provider",
497-
access: "expired-access",
498-
refresh: "refresh-token",
499-
expires: Date.now() - 60_000,
500-
},
501-
},
502-
});
510+
await state.writeAuthProfiles(
511+
createDemoOAuthStore({
512+
access: "expired-access",
513+
expires: Date.now() - 60_000,
514+
}),
515+
);
503516

504517
const { request, respond } = requestModelsList({
505518
view: "all",
@@ -528,6 +541,64 @@ describe("models.list", () => {
528541
);
529542
});
530543

544+
it("uses refreshed persisted OAuth when the runtime auth snapshot is stale", async () => {
545+
await withOpenClawTestState(
546+
{
547+
layout: "state-only",
548+
prefix: "openclaw-models-list-stale-runtime-profile-",
549+
agentEnv: "main",
550+
},
551+
async (state) => {
552+
const agentDir = state.agentDir();
553+
await state.writeAuthProfiles(
554+
createDemoOAuthStore({
555+
access: "refreshed-access",
556+
expires: Date.now() + 60 * 60_000,
557+
}),
558+
);
559+
replaceRuntimeAuthProfileStoreSnapshots([
560+
{
561+
agentDir,
562+
store: createDemoOAuthStore({
563+
access: "expired-access",
564+
expires: Date.now() - 60_000,
565+
}),
566+
},
567+
]);
568+
569+
try {
570+
const { request, respond } = requestModelsList({
571+
view: "all",
572+
loadGatewayModelCatalog: vi.fn(() =>
573+
Promise.resolve([
574+
{ id: "demo-model", name: "Demo Model", provider: "demo-provider" },
575+
]),
576+
),
577+
reqId: "req-models-list-stale-runtime-profile",
578+
});
579+
await request;
580+
581+
expect(respond).toHaveBeenCalledWith(
582+
true,
583+
{
584+
models: [
585+
{
586+
id: "demo-model",
587+
name: "Demo Model",
588+
provider: "demo-provider",
589+
available: true,
590+
},
591+
],
592+
},
593+
undefined,
594+
);
595+
} finally {
596+
clearRuntimeAuthProfileStoreSnapshots();
597+
}
598+
},
599+
);
600+
});
601+
531602
it("marks env SecretRef-backed auth profiles available", async () => {
532603
await withOpenClawTestState(
533604
{

0 commit comments

Comments
 (0)