Skip to content

Commit 20fe561

Browse files
fix(cli): refresh running gateway auth state after models auth order set/clear (#119946)
* fix(cli): refresh running gateway auth state after models auth order set/clear (#114989) The order set/clear commands wrote the agent auth-profile store from the CLI process but never notified a running gateway, which keeps serving its pre-mutation auth snapshot until restart. Sibling auth mutations (login/logout/profile add) already call refreshRunningGatewayAuthState; align order set/clear with that contract. * style: format auth-order.test.ts with oxfmt
1 parent 8c059f9 commit 20fe561

2 files changed

Lines changed: 120 additions & 0 deletions

File tree

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Covers `models auth order set/clear`: store writes and running-gateway refresh.
2+
import { beforeEach, describe, expect, it, vi } from "vitest";
3+
import type { AuthProfileStore } from "../../agents/auth-profiles.js";
4+
import type { OpenClawConfig } from "../../config/types.openclaw.js";
5+
import type { RuntimeEnv } from "../../runtime.js";
6+
7+
const mocks = vi.hoisted(() => ({
8+
ensureAuthProfileStore: vi.fn(),
9+
setAuthProfileOrder: vi.fn(),
10+
loadModelsConfig: vi.fn(),
11+
refreshRunningGatewayAuthState: vi.fn(async () => undefined),
12+
}));
13+
14+
vi.mock("../../agents/auth-profiles.js", () => ({
15+
ensureAuthProfileStore: mocks.ensureAuthProfileStore,
16+
setAuthProfileOrder: mocks.setAuthProfileOrder,
17+
externalCliDiscoveryForProviderAuth: () => undefined,
18+
resolveAuthStatePathForDisplay: (agentDir: string) => `${agentDir}/auth-profiles.json`,
19+
}));
20+
21+
vi.mock("./load-config.js", () => ({
22+
loadModelsConfig: mocks.loadModelsConfig,
23+
}));
24+
25+
vi.mock("./shared.js", async (importOriginal) => {
26+
const actual = await importOriginal<typeof import("./shared.js")>();
27+
return {
28+
...actual,
29+
resolveModelsTargetAgent: (_cfg: OpenClawConfig, rawAgentId?: string) => ({
30+
agentId: rawAgentId ?? "main",
31+
agentDir: `/tmp/agent-${rawAgentId ?? "main"}`,
32+
}),
33+
};
34+
});
35+
36+
vi.mock("./auth-refresh.js", () => ({
37+
refreshRunningGatewayAuthState: mocks.refreshRunningGatewayAuthState,
38+
}));
39+
40+
const { modelsAuthOrderClearCommand, modelsAuthOrderSetCommand } = await import("./auth-order.js");
41+
42+
function createRuntime(): RuntimeEnv & { logs: string[] } {
43+
const logs: string[] = [];
44+
return {
45+
logs,
46+
log: (message: string) => {
47+
logs.push(message);
48+
},
49+
error: () => {},
50+
} as unknown as RuntimeEnv & { logs: string[] };
51+
}
52+
53+
function storeWith(profileIds: string[], order?: string[]): AuthProfileStore {
54+
return {
55+
version: 1,
56+
profiles: Object.fromEntries(
57+
profileIds.map((profileId) => [
58+
profileId,
59+
{ type: "oauth" as const, provider: profileId.split(":")[0] ?? "anthropic", access: "tok" },
60+
]),
61+
),
62+
...(order ? { order: { anthropic: order } } : {}),
63+
} as unknown as AuthProfileStore;
64+
}
65+
66+
describe("models auth order", () => {
67+
beforeEach(() => {
68+
vi.clearAllMocks();
69+
mocks.loadModelsConfig.mockResolvedValue({} as OpenClawConfig);
70+
mocks.ensureAuthProfileStore.mockReturnValue(
71+
storeWith(["anthropic:a", "anthropic:b"], ["anthropic:a"]),
72+
);
73+
mocks.setAuthProfileOrder.mockResolvedValue(
74+
storeWith(["anthropic:a", "anthropic:b"], ["anthropic:b", "anthropic:a"]),
75+
);
76+
});
77+
78+
it("set writes the store order and refreshes a running gateway", async () => {
79+
const runtime = createRuntime();
80+
await modelsAuthOrderSetCommand(
81+
{ provider: "anthropic", order: ["anthropic:b", "anthropic:a"] },
82+
runtime,
83+
);
84+
85+
expect(mocks.setAuthProfileOrder).toHaveBeenCalledWith({
86+
agentDir: "/tmp/agent-main",
87+
provider: "anthropic",
88+
order: ["anthropic:b", "anthropic:a"],
89+
});
90+
expect(mocks.refreshRunningGatewayAuthState).toHaveBeenCalledTimes(1);
91+
expect(runtime.logs).toContain("Auth profile order override: anthropic:b, anthropic:a");
92+
});
93+
94+
it("clear removes the store order and refreshes a running gateway", async () => {
95+
const runtime = createRuntime();
96+
await modelsAuthOrderClearCommand({ provider: "anthropic" }, runtime);
97+
98+
expect(mocks.setAuthProfileOrder).toHaveBeenCalledWith({
99+
agentDir: "/tmp/agent-main",
100+
provider: "anthropic",
101+
order: null,
102+
});
103+
expect(mocks.refreshRunningGatewayAuthState).toHaveBeenCalledTimes(1);
104+
expect(runtime.logs.some((line) => line.includes("Auth profile order override cleared"))).toBe(
105+
true,
106+
);
107+
});
108+
109+
it("does not refresh the gateway when the store update fails", async () => {
110+
mocks.setAuthProfileOrder.mockResolvedValue(null);
111+
112+
await expect(
113+
modelsAuthOrderSetCommand({ provider: "anthropic", order: ["anthropic:a"] }, createRuntime()),
114+
).rejects.toThrow("Failed to update auth state");
115+
expect(mocks.refreshRunningGatewayAuthState).not.toHaveBeenCalled();
116+
});
117+
});

‎src/commands/models/auth-order.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { formatCliCommand } from "../../cli/command-format.js";
1313
import type { OpenClawConfig } from "../../config/types.openclaw.js";
1414
import { type RuntimeEnv, writeRuntimeJson } from "../../runtime.js";
1515
import { shortenHomePath } from "../../utils.js";
16+
import { refreshRunningGatewayAuthState } from "./auth-refresh.js";
1617
import { loadModelsConfig } from "./load-config.js";
1718
import { resolveModelsTargetAgent } from "./shared.js";
1819

@@ -103,6 +104,7 @@ export async function modelsAuthOrderClearCommand(
103104
runtime.log(`Agent: ${agentId}`);
104105
runtime.log(`Provider: ${provider}`);
105106
runtime.log(`Auth profile order override cleared; ${describeOrderFallback(cfg, provider)}.`);
107+
await refreshRunningGatewayAuthState();
106108
}
107109

108110
/** Sets the provider auth profile priority order after validating each profile id. */
@@ -149,4 +151,5 @@ export async function modelsAuthOrderSetCommand(
149151
runtime.log(`Agent: ${agentId}`);
150152
runtime.log(`Provider: ${provider}`);
151153
runtime.log(`Auth profile order override: ${describeOrder(updated, provider).join(", ")}`);
154+
await refreshRunningGatewayAuthState();
152155
}

0 commit comments

Comments
 (0)