Skip to content

fix: guard active_user_messages/active_output_tokens in _build_completed_summary (#992)#994

Merged
microsasa merged 1 commit intomainfrom
fix/992-guard-active-counters-dfc732ab4872ea6d
Apr 19, 2026
Merged

fix: guard active_user_messages/active_output_tokens in _build_completed_summary (#992)#994
microsasa merged 1 commit intomainfrom
fix/992-guard-active-counters-dfc732ab4872ea6d

Conversation

@microsasa
Copy link
Copy Markdown
Owner

Closes #992

Problem

In _build_completed_summary, active_model_calls has an explicit if resume.session_resumed else 0 guard, but active_user_messages and active_output_tokens rely on an implicit invariant — that _first_pass never increments these counters without also setting session_resumed=True. A future refactor could break this invariant, causing double-counted tokens, spurious "↳ Since last shutdown" rows, and incorrect has_active_period_stats() results.

Fix

Apply the same guard consistently to all three active-period counters:

active_user_messages=(
    resume.post_shutdown_user_messages if resume.session_resumed else 0
),
active_output_tokens=(
    resume.post_shutdown_output_tokens if resume.session_resumed else 0
),

This is a no-op for current code paths but makes the contract explicit and self-documenting.

Testing

Added test_completed_session_zeroes_active_counters_despite_post_shutdown_values to TestResumeDetectionDirect — constructs a synthetic _ResumeInfo with session_resumed=False but non-zero post-shutdown counters and verifies the built summary zeros them out. Without the fix, this test fails.

Generated by Issue Implementer · ● 6.1M ·

…ted_summary (#992)

Apply the same 'if resume.session_resumed else 0' guard to
active_user_messages and active_output_tokens that already protects
active_model_calls, making the contract explicit rather than relying
on an implicit invariant in _first_pass.

Add test that constructs a _ResumeInfo with session_resumed=False but
non-zero post-shutdown counters and verifies the built summary zeros
them out.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@microsasa microsasa added the aw Created by agentic workflow label Apr 19, 2026
@microsasa microsasa enabled auto-merge April 19, 2026 05:20
Copilot AI review requested due to automatic review settings April 19, 2026 05:20
@microsasa microsasa added the aw Created by agentic workflow label Apr 19, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR makes _build_completed_summary explicitly zero out all active-period counters (active_model_calls, active_user_messages, active_output_tokens) when resume.session_resumed is False, preventing future regressions from implicit invariants in _first_pass.

Changes:

  • Add explicit resume.session_resumed guards for active_user_messages and active_output_tokens in _build_completed_summary.
  • Add a direct regression test that constructs a synthetic _ResumeInfo with session_resumed=False but non-zero post-shutdown counters and verifies the summary zeros them out.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/copilot_usage/parser.py Ensures active-period counters are consistently gated on session_resumed for completed sessions.
tests/copilot_usage/test_parser.py Adds a regression test to enforce the contract that active counters must be zero when session_resumed=False.

@microsasa microsasa added the aw-quality-gate-approved Quality gate approved the PR label Apr 19, 2026
Copy link
Copy Markdown
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Low-impact defensive consistency fix with a targeted regression test. The production change adds if resume.session_resumed else 0 guards to active_user_messages and active_output_tokens — matching the existing guard on active_model_calls. No behavior change for current code paths. Test correctly exercises the invariant-violation scenario. All CI green. Auto-approving for merge.

@microsasa microsasa merged commit d4af834 into main Apr 19, 2026
8 checks passed
@microsasa microsasa deleted the fix/992-guard-active-counters-dfc732ab4872ea6d branch April 19, 2026 05:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

aw Created by agentic workflow aw-quality-gate-approved Quality gate approved the PR

Projects

None yet

2 participants