Skip to content

refactor: extract TokenCoreMetrics to unify token-usage quartet - #46059

Merged
pelikhan merged 5 commits into
mainfrom
copilot/deep-report-unify-token-usage-metrics
Jul 16, 2026
Merged

refactor: extract TokenCoreMetrics to unify token-usage quartet#46059
pelikhan merged 5 commits into
mainfrom
copilot/deep-report-unify-token-usage-metrics

Conversation

CopilotAI commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

The {InputTokens, OutputTokens, CacheReadTokens, CacheWriteTokens} quartet (plus ReasoningTokens/EffectiveTokens) was duplicated across four structs with a camelCase/snake_case json tag inconsistency between copilotUsageMetrics and the rest.

Changes

  • New TokenCoreMetrics struct — single source of truth for the token quartet with snake_case json tags and console display tags. ReasoningTokens and EffectiveTokens are omitempty.

  • Embed in TokenUsageEntry, ModelTokenUsage, ModelTokenUsageRow — removes 5–6 duplicated field declarations per struct, collapses the ModelTokenUsage/ModelTokenUsageRow near-dup.

  • ModelRows() — copies usage.TokenCoreMetrics directly rather than field-by-field, so future additions to the core struct propagate automatically.

  • copilotUsageMetrics — kept as-is (must use camelCase to match the external Copilot events.jsonl wire format); now has an explicit comment distinguishing it from TokenCoreMetrics.

// Before: fields duplicated in each structtypeModelTokenUsagestruct {
Providerstring`json:"provider"`InputTokensint`json:"input_tokens" console:"header:Input,format:number"`OutputTokensint`json:"output_tokens" console:"header:Output,format:number"`CacheReadTokensint`json:"cache_read_tokens" console:"header:Cache Read,format:number"`CacheWriteTokensint`json:"cache_write_tokens" console:"header:Cache Write,format:number"`ReasoningTokensint`json:"reasoning_tokens,omitempty"`EffectiveTokensint`json:"effective_tokens,omitempty"`...
}
// After: single embedded coretypeModelTokenUsagestruct {
Providerstring`json:"provider"`TokenCoreMetrics...
}

Extract a shared TokenCoreMetrics struct as the single source of truth
for the {InputTokens, OutputTokens, CacheReadTokens, CacheWriteTokens}
quartet (plus ReasoningTokens and EffectiveTokens).
Embed TokenCoreMetrics in TokenUsageEntry, ModelTokenUsage, and
ModelTokenUsageRow, collapsing the ModelTokenUsage/ModelTokenUsageRow
near-dup and eliminating the duplicated field declarations.
Update ModelRows() to copy the embedded struct directly instead of
field-by-field to automatically include future additions.
Add a comment to copilotUsageMetrics explaining it intentionally uses
camelCase json tags to match the external Copilot events.jsonl format,
distinguishing it from the snake_case convention used by TokenCoreMetrics.
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
CopilotAI changed the title [WIP] Unify token-usage metrics and fix json-tag inconsistencyrefactor: extract TokenCoreMetrics to unify token-usage quartetJul 16, 2026
CopilotAI requested a review from pelikhanJuly 16, 2026 18:14
@github-actions

Copy link
Copy Markdown
Contributor

🤖 PR Triage

FieldValue
Categoryrefactor
Risk🟢 Low
Score52/100 (impact:25, urgency:10, quality:17)
Actionbatch_review

Rationale: Small refactor (3 files, +69/-64) extracting TokenCoreMetrics to unify duplicated token structs and fix JSON tag inconsistency. Low risk, no review activity yet. Good description quality.

Run §29524501227

Generated by 🔧 PR Triage Agent · 138.5 AIC · ⌖ 6.98 AIC · ⊞ 5.6K ·

@pelikhan
pelikhan marked this pull request as ready for review July 16, 2026 19:04
CopilotAI review requested due to automatic review settings July 16, 2026 19:04

CopilotAI 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.

Pull request overview

Refactors token metrics into a shared structure while preserving Copilot’s external wire format.

Changes:

  • Adds and embeds TokenCoreMetrics.
  • Simplifies model-row construction.
  • Clarifies Copilot’s camelCase JSON requirement.
Show a summary per file
FileDescription
pkg/cli/token_usage.goConsolidates token metrics and row mapping.
pkg/cli/token_usage_test.goUpdates fixtures for embedding.
pkg/cli/copilot_events_jsonl.goDocuments wire-format differences.

Review details

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 3/3 changed files
  • Comments generated: 1
  • Review effort level: Medium

Comment threadpkg/cli/token_usage.go Outdated
AvgDuration string `json:"avg_duration" console:"header:Avg Duration"`
Model string `json:"model" console:"header:Model"`
Provider string `json:"provider" console:"header:Provider"`
TokenCoreMetrics

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Implemented in cb19855. ModelTokenUsageRow now explicitly carries only the token quartet (no embedded TokenCoreMetrics), and ModelRows() maps only those four fields. I also added a regression in TestTokenUsageSummaryMethods to assert row JSON/rendered output do not include reasoning_tokens or effective_tokens columns.

@pelikhan

Copy link
Copy Markdown
Collaborator

@copilot run pr-finisher skill

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>

CopilotAI commented Jul 16, 2026

Copy link
Copy Markdown
ContributorAuthor

@copilot run pr-finisher skill

Ran the pr-finisher workflow and pushed cb19855 with the requested review-thread fix.

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
@pelikhan
pelikhan merged commit 3f7be99 into mainJul 16, 2026
29 checks passed
@pelikhan
pelikhan deleted the copilot/deep-report-unify-token-usage-metrics branch July 16, 2026 22:02
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This pull request is included in a new release.

Release: v0.82.12

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[deep-report] [Code Quality] Unify token-usage metrics quartet and fix camelCase/snake_case json-tag inconsistency

3 participants

@pelikhan