Skip to content

Commit b8c4f7f

Browse files
chenxiaoyu209claude
andcommitted
fix(gateway): bound usage.cost all-agent fan-out concurrency to prevent SQLite starvation
Replace unbounded Promise.all in loadAllAgentCostUsageSummary with runTasksWithConcurrency capped at SESSIONS_USAGE_AGENT_LOAD_CONCURRENCY (12), matching the existing sessions.usage pattern in the same module. Fixes #101552 Co-Authored-By: Claude <[email protected]>
1 parent 36a91ac commit b8c4f7f

2 files changed

Lines changed: 45 additions & 12 deletions

File tree

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,4 +519,30 @@ describe("gateway usage helpers", () => {
519519
totals: { totalTokens: 10, totalCost: 1 },
520520
});
521521
});
522+
523+
it("caps usage.cost all-agent fan-out concurrency", async () => {
524+
let inFlight = 0;
525+
let peakConcurrency = 0;
526+
vi.mocked(loadCostUsageSummaryFromCache).mockImplementation(async () => {
527+
inFlight += 1;
528+
peakConcurrency = Math.max(peakConcurrency, inFlight);
529+
await new Promise<void>((r) => {
530+
setTimeout(r, 5);
531+
});
532+
inFlight -= 1;
533+
return costSummary({ totalTokens: 1, totalCost: 0 });
534+
});
535+
536+
const respond = vi.fn();
537+
const agents = Array.from({ length: 30 }, (_, i) => ({ id: `agent-${i}` }));
538+
await usageHandlers["usage.cost"]({
539+
respond,
540+
params: { startDate: "2026-02-01", endDate: "2026-02-02", agentScope: "all" },
541+
context: { getRuntimeConfig: () => ({ agents: { list: agents } }) },
542+
} as unknown as Parameters<(typeof usageHandlers)["usage.cost"]>[0]);
543+
544+
expect(respond).toHaveBeenCalledWith(true, expect.any(Object), undefined);
545+
expect(peakConcurrency).toBeLessThanOrEqual(12);
546+
expect(respond.mock.calls[0]?.[1]?.totals?.totalTokens).toBe(30);
547+
});
522548
});

‎src/gateway/server-methods/usage.ts‎

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -864,19 +864,26 @@ async function loadAllAgentCostUsageSummary(params: {
864864
const agentIds = listAgentsForGateway(params.config).agents.map((agent) =>
865865
normalizeAgentId(agent.id),
866866
);
867-
const summaries = await Promise.all(
868-
agentIds.map((agentId) =>
869-
loadCostUsageSummaryFromCache({
870-
startMs: params.startMs,
871-
endMs: params.endMs,
872-
dailyUtcOffsetMinutes: params.dailyUtcOffsetMinutes,
873-
config: params.config,
874-
agentId,
875-
requestRefresh: true,
876-
refreshMode: "background",
877-
}),
867+
const result = await runTasksWithConcurrency({
868+
tasks: agentIds.map(
869+
(agentId) => async () =>
870+
loadCostUsageSummaryFromCache({
871+
startMs: params.startMs,
872+
endMs: params.endMs,
873+
dailyUtcOffsetMinutes: params.dailyUtcOffsetMinutes,
874+
config: params.config,
875+
agentId,
876+
requestRefresh: true,
877+
refreshMode: "background",
878+
}),
878879
),
879-
);
880+
limit: SESSIONS_USAGE_AGENT_LOAD_CONCURRENCY,
881+
errorMode: "stop",
882+
});
883+
if (result.hasError) {
884+
throw result.firstError;
885+
}
886+
const summaries = result.results;
880887
const dailyByDate = new Map<string, CostUsageTotals & { date: string }>();
881888
const totals = createEmptyCostUsageTotals();
882889
let cacheStatus: UsageCacheStatus | undefined;

0 commit comments

Comments
 (0)