Repository navigation
fix(gateway): aggregate usage.cost across agents - #42063
stablegenius49 wants to merge 1 commit into
Conversation
Greptile SummaryThis PR fixes the Key changes and observations:
Confidence Score: 4/5
Last reviewed commit: da3d7d3 |
| expect(summary.totals).toEqual({ | ||
| input: 6, | ||
| output: 8, | ||
| cacheRead: 10, | ||
| cacheWrite: 12, | ||
| totalTokens: 40, | ||
| totalCost: 4, | ||
| inputCost: 0.7, | ||
| outputCost: 1, | ||
| cacheReadCost: 1.1, | ||
| cacheWriteCost: 1.2000000000000002, | ||
| missingCostEntries: 1, | ||
| }); | ||
| expect(summary.daily).toEqual([ | ||
| { | ||
| date: "2026-02-05", | ||
| input: 6, | ||
| output: 8, | ||
| cacheRead: 10, | ||
| cacheWrite: 12, | ||
| totalTokens: 40, | ||
| totalCost: 4, | ||
| inputCost: 0.7, | ||
| outputCost: 1, | ||
| cacheReadCost: 1.1, | ||
| cacheWriteCost: 1.2000000000000002, | ||
| missingCostEntries: 1, | ||
| }, | ||
| ]); |
There was a problem hiding this comment.
Hardcoded floating-point precision artifact
The test explicitly encodes the floating-point precision artefact cacheWriteCost: 1.2000000000000002 (i.e., 0.4 + 0.8 in IEEE 754). While this documents current behaviour, it means the same imprecise value will surface in the UI (e.g., cost charts or the Usage header). For floating-point fields, prefer expect.closeTo() so that the assertion is intent-driven rather than platform-dependent:
| expect(summary.totals).toEqual({ | |
| input: 6, | |
| output: 8, | |
| cacheRead: 10, | |
| cacheWrite: 12, | |
| totalTokens: 40, | |
| totalCost: 4, | |
| inputCost: 0.7, | |
| outputCost: 1, | |
| cacheReadCost: 1.1, | |
| cacheWriteCost: 1.2000000000000002, | |
| missingCostEntries: 1, | |
| }); | |
| expect(summary.daily).toEqual([ | |
| { | |
| date: "2026-02-05", | |
| input: 6, | |
| output: 8, | |
| cacheRead: 10, | |
| cacheWrite: 12, | |
| totalTokens: 40, | |
| totalCost: 4, | |
| inputCost: 0.7, | |
| outputCost: 1, | |
| cacheReadCost: 1.1, | |
| cacheWriteCost: 1.2000000000000002, | |
| missingCostEntries: 1, | |
| }, | |
| ]); | |
| expect(summary.totals).toEqual( | |
| expect.objectContaining({ | |
| input: 6, | |
| output: 8, | |
| cacheRead: 10, | |
| cacheWrite: 12, | |
| totalTokens: 40, | |
| totalCost: 4, | |
| inputCost: expect.closeTo(0.7), | |
| outputCost: expect.closeTo(1.0), | |
| cacheReadCost: expect.closeTo(1.1), | |
| cacheWriteCost: expect.closeTo(1.2), | |
| missingCostEntries: 1, | |
| }), | |
| ); |
The same applies to the summary.daily assertion at line 267–282. Using expect.closeTo also makes it clear this is intentional rounding—not a bug to preserve.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/gateway/server-methods/usage.test.ts
Line: 254-282
Comment:
**Hardcoded floating-point precision artifact**
The test explicitly encodes the floating-point precision artefact `cacheWriteCost: 1.2000000000000002` (i.e., `0.4 + 0.8` in IEEE 754). While this documents current behaviour, it means the same imprecise value will surface in the UI (e.g., cost charts or the Usage header). For floating-point fields, prefer `expect.closeTo()` so that the assertion is intent-driven rather than platform-dependent:
```suggestion
expect(summary.totals).toEqual(
expect.objectContaining({
input: 6,
output: 8,
cacheRead: 10,
cacheWrite: 12,
totalTokens: 40,
totalCost: 4,
inputCost: expect.closeTo(0.7),
outputCost: expect.closeTo(1.0),
cacheReadCost: expect.closeTo(1.1),
cacheWriteCost: expect.closeTo(1.2),
missingCostEntries: 1,
}),
);
```
The same applies to the `summary.daily` assertion at line 267–282. Using `expect.closeTo` also makes it clear this is intentional rounding—not a bug to preserve.
How can I resolve this? If you propose a fix, please make it concise.| const agentIds = listAgentsForGateway(params.config) | ||
| .agents.map((agent) => agent.id) | ||
| .toSorted(); | ||
| const effectiveAgentIds = agentIds.length ? agentIds : ["main"]; |
There was a problem hiding this comment.
Pre-existing TTL test may now fail with empty config
The "loadCostUsageSummaryCached caches within TTL" test passes config = {} as OpenClawConfig. Before this change, loadCostUsageSummaryCached never called listAgentsForGateway; now it does on every invocation. If listAgentsForGateway({}) throws (e.g., if resolveDefaultAgentId or normalizeAgentId cannot handle an empty config), that test will start failing. Consider also mocking listAgentsForGateway in that existing test, or ensuring the TTL test is still exercised with a minimal valid config stub.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/gateway/server-methods/usage.ts
Line: 353-356
Comment:
**Pre-existing TTL test may now fail with empty config**
The `"loadCostUsageSummaryCached caches within TTL"` test passes `config = {} as OpenClawConfig`. Before this change, `loadCostUsageSummaryCached` never called `listAgentsForGateway`; now it does on every invocation. If `listAgentsForGateway({})` throws (e.g., if `resolveDefaultAgentId` or `normalizeAgentId` cannot handle an empty config), that test will start failing. Consider also mocking `listAgentsForGateway` in that existing test, or ensuring the TTL test is still exercised with a minimal valid config stub.
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: da3d7d3eb7
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| endMs: params.endMs, | ||
| config: params.config, | ||
| }) | ||
| const inFlight = Promise.all( |
There was a problem hiding this comment.
Isolate per-agent load failures in usage.cost aggregation
Using Promise.all here makes the entire usage.cost response fail when any single agent summary read rejects (for example, one agent has an unreadable/corrupt transcript file), even though other agents could still produce valid totals. Before this change only the main-agent read could fail; now one broken secondary agent can take down the aggregate endpoint for everyone querying it. Consider handling per-agent errors (e.g., settling each load and merging successful summaries) so partial data is still returned.
Useful? React with 👍 / 👎.
|
Closing this as duplicate or superseded after Codex automated review. PR #42063 is a focused fix for aggregating Best possible solution: Close #42063 as superseded and keep the remaining What I checked:
So I’m closing this here and keeping the remaining discussion on the canonical linked item. Codex Review notes: model gpt-5.5, reasoning high; reviewed against a57d681db9b2. |
Summary
usage.costacross all configured gateway agents instead of only the main agentTesting
./node_modules/.bin/vitest run src/gateway/server-methods/usage.test.ts src/gateway/server-methods/usage.sessions-usage.test.ts --config vitest.gateway.config.ts./node_modules/.bin/oxfmt --check src/gateway/server-methods/usage.ts src/gateway/server-methods/usage.test.tsnode --import tsx - <<'TS' ... __test.loadCostUsageSummaryCached(...) ... TS(local smoke check against configured agents)Fixes #41986.