Skip to content

fix: extract shared parse_token_int and add value-equality tests (#880)#883

Merged
microsasa merged 3 commits intomainfrom
fix/880-token-value-equality-8342e9917f56ad22
Apr 9, 2026
Merged

fix: extract shared parse_token_int and add value-equality tests (#880)#883
microsasa merged 3 commits intomainfrom
fix/880-token-value-equality-8342e9917f56ad22

Conversation

@microsasa
Copy link
Copy Markdown
Owner

Closes #880

Summary

The cross-consistency test for _extract_output_tokens vs AssistantMessageData._sanitize_non_numeric_tokens only verified binary agreement (contributes / does not contribute), not that the actual numeric token counts matched. This meant the two independent implementations could silently diverge on values while all tests pass.

Changes

Structural fix: shared parse_token_int helper (models.py)

Extracted a single parse_token_int(raw: object) -> int | None function that centralises the token-validation rules:

  • bool / strNone
  • non-whole floatNone
  • zero or negative int/floatNone
  • positive whole-number float → coerced to int
  • positive int → returned as-is

Both _sanitize_non_numeric_tokens (Pydantic validator) and _extract_output_tokens (parser fast path) now delegate to this single helper, eliminating the duplication entirely.

Value-equality tests (test_parser.py)

Added TestExtractOutputTokensValueEquality with a parametrized test_token_value_agreement test covering the minimum cases from the issue spec:

  • 1 (positive int)
  • 42 (positive int)
  • 1234.0 (whole float)
  • 1.0 (whole float)

Each case asserts that both paths produce the exact same integer count, not just that they agree on the binary verdict.

Regression

All 12 existing _EQUIVALENCE_CASES binary tests continue to pass unchanged.

Generated by Issue Implementer · ● 12.7M ·

Extract a shared parse_token_int() helper in models.py that centralises
the token-validation rules used by both _sanitize_non_numeric_tokens
(Pydantic boundary) and _extract_output_tokens (parser fast path).
This eliminates the duplicated logic and makes divergence structurally
impossible.

Add TestExtractOutputTokensValueEquality with test_token_value_agreement
parametrized test that verifies numeric counts match (not just the
binary contributes/does-not-contribute verdict) for contributing values.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@microsasa microsasa added the aw Created by agentic workflow label Apr 9, 2026
Copilot AI review requested due to automatic review settings April 9, 2026 10:21
@microsasa microsasa enabled auto-merge April 9, 2026 10:21
@microsasa microsasa requested a review from Copilot April 9, 2026 10:25
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 addresses token-validation drift risk by extracting a shared parse_token_int() helper in copilot_usage.models, then tightening tests to ensure the parser fast-path and Pydantic model-path agree on exact token counts (not just “contributes vs not”).

Changes:

  • Added shared parse_token_int(raw: object) -> int | None in src/copilot_usage/models.py and exported it via __all__.
  • Updated _extract_output_tokens() in src/copilot_usage/parser.py to delegate to parse_token_int() (removing duplicated logic).
  • Added parametrized value-equality tests in tests/copilot_usage/test_parser.py to assert numeric count equality for contributing cases.

Reviewed changes

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

File Description
tests/copilot_usage/test_parser.py Adds value-equality tests to prevent silent numeric divergences between extraction paths.
src/copilot_usage/parser.py Replaces inline token parsing with a shared helper to enforce consistent rules.
src/copilot_usage/models.py Introduces parse_token_int() and refactors the field validator to use it.

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

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

@microsasa microsasa added the aw-review-response-attempted Responder has attempted to address review comments label Apr 9, 2026
- Handle None (JSON null) in _sanitize_non_numeric_tokens by mapping it
  to 0, preventing ValidationError on model_validate({"outputTokens": None})
- Fix overclaiming docstring in _extract_output_tokens to clarify that
  numeric equality is guaranteed only for contributing values
- Add None to _EQUIVALENCE_CASES as regression test

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 9, 2026

Commit pushed: 743a575

Generated by Review Responder

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

@microsasa microsasa added aw-review-response-1 Review response round 1 and removed aw-review-response-attempted Responder has attempted to address review comments labels Apr 9, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 9, 2026

Commit pushed: 9b44b0e

Generated by Review Responder

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

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

@microsasa microsasa added the aw-quality-gate-approved Quality gate approved the PR label Apr 9, 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.

Quality Gate: APPROVED

Medium-impact DRY refactor — extracts duplicated token-validation logic into a shared parse_token_int helper, eliminating divergence risk between the Pydantic model path and the parser fast path. New value-equality tests verify numeric agreement for contributing values. All 12 existing equivalence cases continue to pass. CI green across all 9 checks. Auto-approving for merge.

@microsasa microsasa merged commit 6a4c564 into main Apr 9, 2026
9 checks passed
@microsasa microsasa deleted the fix/880-token-value-equality-8342e9917f56ad22 branch April 9, 2026 10:48
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 aw-review-response-1 Review response round 1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[aw][code health] Token validation cross-consistency test only checks binary agreement, not actual values

2 participants