Skip to content

SWF(bug): count model aliases once per receipt in Stats and Insights - #45

Merged
kodji7202-code merged 9 commits into
mainfrom
devin/1795210113-model-alias-run-count
Nov 26, 2026
Merged

kodji7202-code merged 9 commits into
mainfrom
devin/1795210113-model-alias-run-count

Conversation

@kodji7202-code

@kodji7202-code kodji7202-code commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Summary

A run that used two IDs for the same model (e.g. claude-sonnet-4-5-20250929 and claude-sonnet-4-5) was counted as 2 runs for that model in Stats and Insights, so Insights showed cost per run at half the real figure ($0.45 instead of $0.90).

Insights models table, before (main) left, after (this PR) right

Root cause: Database.stats (server/db.py) and _models_block (server/insights.py) map each raw model bucket to its display label with friendly_model inside the bucket loop and bump runs / priced_runs / quality samples once per bucket. Two aliases in one receipt collapse to one label, so that label's counters are bumped twice. Tokens and dollars add up correctly; per-receipt counts do not.

Fix: one shared helper, receipt_models(models, partial) in server/model_rollup.py, merges same-label buckets per receipt (summing tokens and cost) before either rollup counts anything. Both consumers now iterate its output, so each receipt counts once per label.

  • Lower-bound semantics are kept: if one alias is priced and the other unpriced, the merged label is partial (cost is a lower bound, ≥ on the dashboard). A label where every alias is unpriced stays unpriced, as before.
  • Other consumers checked: dashboard renderModels only displays API values; compliance exports call insights.compute and get the fix; CSV export and CLI stats key by raw model ID, so they are not affected.

Evidence

Live local team server, one receipt POSTed (201) with buckets Sonnet 4.5 dated (1,000 tok, $0.60) + undated alias (500 tok, $0.30), total $0.90:

Endpoint main this PR
/api/stats?days=30 Sonnet 4.5 runs: 2, priced_runs: 2, cost_usd: 0.9 runs: 1, priced_runs: 1, cost_usd: 0.9
/api/insights?days=30 Sonnet 4.5 runs: 2, priced_runs: 2, cost_per_run: 0.45 runs: 1, priced_runs: 1, cost_per_run: 0.9

New tests in tests/test_server.py (both APIs, fully priced and partly priced aliases, quality weighting across receipts, empty and malformed buckets) fail on main:

>       assert row["runs"] == 1
E       assert 2 == 1
FAILED tests/test_server.py::test_model_aliases_count_once_per_receipt[0.3-stats-by_model]
FAILED tests/test_server.py::test_model_aliases_count_once_per_receipt[0.3-insights-models]
FAILED tests/test_server.py::test_model_aliases_count_once_per_receipt[None-stats-by_model]
FAILED tests/test_server.py::test_model_aliases_count_once_per_receipt[None-insights-models]
4 failed, 89 deselected in 2.53s

and pass on this branch:

6 passed, 89 deselected in 2.47s

Full suite: main 4137 passed, 41 skipped; this branch 4143 passed, 41 skipped (the 6 new tests).

Try it:

. .venv/bin/activate && python -m pytest -q tests/test_server.py -k 'model_aliases_count_once_per_receipt or receipt_models_empty'

Not tested: the 41 skipped tests (platform-specific); browser check was Chromium on Linux only.


Software Factory opened this PR for Officialkairomgmt
Implemented in session Count model aliases once per receipt in Insights
Review with Devin · Open in Devin Desktop


officialkairomgmt and others added 2 commits November 20, 2026 21:38
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-20T21:43:58.395025Z 4d20d0b PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

officialkairomgmt and others added 5 commits November 20, 2026 22:38
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
… carry main's future-dated-runs entry into it)
@capy-ai

capy-ai Bot commented Nov 26, 2026

Copy link
Copy Markdown

Capy couldn't review this pull request because Cojo's workspace is out of credits, add credits or enable auto-reload to resume automatic reviews.

The code reviews included with your plan this billing period are used up, so reviews are paid from your credits until the plan resets. A Low or Medium effort review counts as 1 included review, and a High effort review counts as 2.

Open in Capy

…l-alias-run-count

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

# Conflicts:
#	CHANGELOG.md
@kodji7202-code
kodji7202-code merged commit 3dc0bb6 into main Nov 26, 2026
25 checks passed
@devin-ai-integration
devin-ai-integration Bot deleted the devin/1795210113-model-alias-run-count branch November 26, 2026 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant