Skip to content

Commit fbc479f

Browse files
committed
fix(gateway): bind usage refresh to runtime config
1 parent 447393d commit fbc479f

3 files changed

Lines changed: 32 additions & 4 deletions

File tree

‎src/gateway/server-methods/models-auth-status-usage-cache.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export type ProviderUsageStatus = Pick<
3636

3737
type ProviderUsageCacheEntry = {
3838
agentDir: string;
39-
configRef: object;
39+
configRef: OpenClawConfig;
4040
credentialKey: string;
4141
providerKey: string;
4242
refreshedAt: number;
@@ -46,7 +46,7 @@ type ProviderUsageCacheEntry = {
4646

4747
type ProviderUsageRefresh = {
4848
agentDir: string;
49-
configRef: object;
49+
configRef: OpenClawConfig;
5050
credentialKey: string;
5151
providerKey: string;
5252
promise: Promise<UsageSummary>;
@@ -158,7 +158,7 @@ function mapProviderUsage(usage: Awaited<ReturnType<typeof loadProviderUsageSumm
158158
function scheduleProviderUsageRefresh(params: {
159159
agentId: string;
160160
agentDir: string;
161-
configRef: object;
161+
configRef: OpenClawConfig;
162162
credentialKey: string;
163163
providerIds: UsageProviderId[];
164164
providerKey: string;
@@ -176,6 +176,7 @@ function scheduleProviderUsageRefresh(params: {
176176
const promise = loadProviderUsageSummary({
177177
providers: params.providerIds,
178178
agentDir: params.agentDir,
179+
config: params.configRef,
179180
timeoutMs: 3500,
180181
})
181182
.then((usage) => {
@@ -222,7 +223,7 @@ function scheduleProviderUsageRefresh(params: {
222223
type ProviderUsageCacheParams = {
223224
agentId: string;
224225
agentDir: string;
225-
configRef: object;
226+
configRef: OpenClawConfig;
226227
credentialKey: string;
227228
forceRefresh?: boolean;
228229
providerIds: UsageProviderId[];

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,8 @@ describe("models.authStatus", () => {
10101010
});
10111011

10121012
it("routes claude-cli OAuth profiles to Anthropic usage with plan and billing", async () => {
1013+
const runtimeConfig = {};
1014+
mocks.getRuntimeConfig.mockReturnValue(runtimeConfig);
10131015
const profile = {
10141016
profileId: "claude-cli",
10151017
provider: "claude-cli",
@@ -1044,6 +1046,7 @@ describe("models.authStatus", () => {
10441046
expect(mocks.loadProviderUsageSummary).toHaveBeenCalledWith({
10451047
providers: ["anthropic"],
10461048
agentDir: "/tmp/agent",
1049+
config: runtimeConfig,
10471050
timeoutMs: 3500,
10481051
});
10491052
let result: ModelAuthStatusResult | undefined;
@@ -1087,6 +1090,7 @@ describe("models.authStatus", () => {
10871090
expect(mocks.loadProviderUsageSummary).toHaveBeenCalledWith({
10881091
providers: ["deepseek"],
10891092
agentDir: "/tmp/agent",
1093+
config: expect.any(Object),
10901094
timeoutMs: 3500,
10911095
});
10921096
let result: ModelAuthStatusResult | undefined;
@@ -1225,6 +1229,7 @@ describe("models.authStatus", () => {
12251229
expect(mocks.loadProviderUsageSummary).toHaveBeenLastCalledWith({
12261230
providers: ["openai"],
12271231
agentDir: "/tmp/rebound-agent",
1232+
config: expect.any(Object),
12281233
timeoutMs: 3500,
12291234
});
12301235
});

‎src/gateway/server-methods/usage.status-cache.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,28 @@ describe("usage.status provider usage cache", () => {
114114
vi.restoreAllMocks();
115115
});
116116

117+
it("loads the cached provider snapshot from the exact runtime config", async () => {
118+
mocks.loadProviderUsageSummary.mockImplementation(async (options) => ({
119+
updatedAt: now,
120+
providers:
121+
options.config === config
122+
? [
123+
{
124+
provider: "openai",
125+
displayName: "OpenAI",
126+
windows: [{ label: "5h", usedPercent: 25 }],
127+
accountEmail: "[email protected]",
128+
},
129+
]
130+
: [],
131+
}));
132+
133+
const result = (await runUsageStatus()) as {
134+
providers: Array<{ accountEmail?: string }>;
135+
};
136+
expect(result.providers[0]?.accountEmail).toBe("[email protected]");
137+
});
138+
117139
it("reuses byte-identical results within 60s and refreshes stale data in the background", async () => {
118140
const first = await runUsageStatus();
119141
const repeated = await runUsageStatus();

0 commit comments

Comments
 (0)