model-usage: parse numeric-string costs and skip non-finite values#37877
model-usage: parse numeric-string costs and skip non-finite values#37877shuofengzhang wants to merge 1 commit into
Conversation
Greptile SummaryThis PR introduces a Key observations:
Confidence Score: 4/5
Last reviewed commit: 9316b09 |
| def test_aggregate_costs_accepts_numeric_strings_and_skips_non_finite_values(self): | ||
| entries = [ | ||
| { | ||
| "modelBreakdowns": [ | ||
| {"modelName": "gpt-5", "cost": "1.25"}, | ||
| {"modelName": "gpt-5", "cost": "nan"}, | ||
| {"modelName": "gpt-5", "cost": True}, | ||
| {"modelName": "gpt-5-mini", "cost": 0.75}, | ||
| ] | ||
| } | ||
| ] | ||
|
|
||
| totals = aggregate_costs(entries) | ||
|
|
||
| self.assertEqual(totals["gpt-5"], 1.25) | ||
| self.assertEqual(totals["gpt-5-mini"], 0.75) | ||
|
|
||
| def test_latest_day_cost_parses_numeric_string(self): | ||
| entries = [ | ||
| {"date": "2026-03-04", "modelBreakdowns": [{"modelName": "gpt-5", "cost": 0.5}]}, | ||
| {"date": "2026-03-05", "modelBreakdowns": [{"modelName": "gpt-5", "cost": "1.75"}]}, | ||
| ] | ||
|
|
||
| latest_date, latest_cost = latest_day_cost(entries, "gpt-5") | ||
|
|
||
| self.assertEqual(latest_date, "2026-03-05") | ||
| self.assertEqual(latest_cost, 1.75) | ||
|
|
There was a problem hiding this comment.
Missing coverage for pick_current_model with new cost types
pick_current_model was updated to use parse_cost, but there is no test exercising the new behaviour with string costs, boolean values, or non-finite values. The two new tests only cover aggregate_costs and latest_day_cost. Consider adding a test such as:
def test_pick_current_model_accepts_numeric_string_cost(self):
entries = [
{"date": "2026-03-05", "modelBreakdowns": [
{"modelName": "gpt-5", "cost": "2.00"},
{"modelName": "gpt-5-mini", "cost": True}, # should be ignored
]}
]
model, _ = pick_current_model(entries)
self.assertEqual(model, "gpt-5")Without this, a regression in pick_current_model's use of parse_cost would go undetected.
Prompt To Fix With AI
This is a comment left during a code review.
Path: skills/model-usage/scripts/test_model_usage.py
Line: 38-65
Comment:
Missing coverage for `pick_current_model` with new cost types
`pick_current_model` was updated to use `parse_cost`, but there is no test exercising the new behaviour with string costs, boolean values, or non-finite values. The two new tests only cover `aggregate_costs` and `latest_day_cost`. Consider adding a test such as:
```python
def test_pick_current_model_accepts_numeric_string_cost(self):
entries = [
{"date": "2026-03-05", "modelBreakdowns": [
{"modelName": "gpt-5", "cost": "2.00"},
{"modelName": "gpt-5-mini", "cost": True}, # should be ignored
]}
]
model, _ = pick_current_model(entries)
self.assertEqual(model, "gpt-5")
```
Without this, a regression in `pick_current_model`'s use of `parse_cost` would go undetected.
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: 9316b090e0
ℹ️ About Codex in GitHub
Your team has set up Codex to 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 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if isinstance(value, (int, float)): | ||
| parsed = float(value) |
There was a problem hiding this comment.
Handle overflow when parsing integer costs
parse_cost now does float(value) for any int without catching OverflowError, and this commit calls parse_cost before modelName validation in both aggregate_costs and pick_current_model. With a malformed row such as {"modelName": null, "cost": 10**1000}, the script now raises and exits instead of skipping the bad row, so a single corrupt payload entry can break the whole usage report.
Useful? React with 👍 / 👎.
|
Closing this PR because the author has more than 10 active PRs in this repo. Please reduce the active PR queue and reopen or resubmit once it is back under the limit. You can close your own PRs to get back under the limit. |
What changed
parse_cost()helper inskills/model-usage/scripts/model_usage.py.aggregate_costs(),pick_current_model(), andlatest_day_cost()now useparse_cost()so they:"1.75"),NaN,Infinity).skills/model-usage/scripts/test_model_usage.pywith coverage for:Why
int/float, so valid string costs were silently dropped.Testing
source .venv/bin/activate && pytest -q skills/model-usage/scripts/test_model_usage.pyscripts/clone_and_test.sh openclaw/openclaw