Skip to content

[aw][code health] Bug: render_cost_view double-counts model calls in Grand Total for resumed sessions #20

@github-actions

Description

@github-actions

Root Cause

render_cost_view in src/copilot_usage/report.py incorrectly inflates the Grand Total Model Calls column for any session that is active/resumed.

In build_session_summary (parser.py), two fields are built from separate loops:

model_calls=total_turn_starts,         # ALL ASSISTANT_TURN_START events in the session
active_model_calls=post_shutdown_turn_starts,  # subset: only events AFTER the last shutdown

active_model_calls is already a subset of model_calls (the implementation doc at src/copilot_usage/docs/implementation.md:187 confirms: "model_calls … are the total counts across the entire session").

In render_cost_view (lines 958–969) both are added to grand_model_calls:

grand_model_calls += s.model_calls        # line 958 – correct: all turns
if s.is_active:
    ...
    grand_model_calls += s.active_model_calls  # line 969 – bug: post-shutdown already in model_calls

For a resumed session with model_calls=10 (total) and active_model_calls=3 (post-shutdown), the Grand Total accumulates 13 instead of the correct 10.

The README copilot-usage cost example (lines 108–114) was written with this bug in place — it shows Session Gamma model_calls=2, active=1 and a Grand Total of 9 (= 3+3+3), whereas the correct total is 8 (= 3+3+2).

Note: the grand_output accumulation is correctactive_output_tokens are not present in model_metrics, so adding them separately is intentional.

Fix

Remove the duplicate increment in render_cost_view:

# src/copilot_usage/report.py  ~line 969
# DELETE this line:
grand_model_calls += s.active_model_calls

Update the README example Grand Total (Model Calls column) to reflect the corrected value.

Testing Requirement

Add a regression test in tests/copilot_usage/test_report.py inside TestRenderCostView that:

  1. Creates a resumed session with known model_calls and active_model_calls values (e.g., model_calls=10, active_model_calls=3).
  2. Captures the render_cost_view output and asserts the Grand Total row shows 10 for Model Calls — not 13.

This ensures the double-count does not regress.

Generated by Code Health Analysis ·

Metadata

Metadata

Assignees

No one assigned

    Labels

    awCreated by agentic workflowbugSomething isn't workingcode-healthCode cleanup and maintenance

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions