Skip to content

[file-diet] refactor: split pkg/cli/token_usage.go (1141 lines) into focused modules #48378

Description

@github-actions

Overview

The file pkg/cli/token_usage.go has grown to 1,141 lines, making it difficult to maintain and test. This task involves refactoring it into smaller, focused files with improved test coverage.

Current State

  • File: pkg/cli/token_usage.go
  • Size: 1,141 lines
  • Test file: pkg/cli/token_usage_test.go (782 lines, ratio ~0.69)
  • Functions: 31 functions across 10 structs
  • Complexity: Mixed concerns — file discovery, parsing, analysis, steering events, sub-agent attribution

Proposed File Splits

  1. token_usage_types.go — all structs, constants, package-level vars (~120 LOC)
  2. token_usage_find.go — findTokenUsageFile, findAgentUsageFile, findUsageJSONLFiles, findAPIProxyEventsFile, findAgentStdioFile (~120 LOC)
  3. token_usage_parse.go — parseTokenUsageFile, parseAgentUsageFile, record extraction helpers (~280 LOC)
  4. token_usage_analyze.go — analyzeTokenUsage, analyzeTokenUsageAICOnly, extractAmbientContextMetrics, populateAIC, TokenUsageSummary methods (~350 LOC)
  5. token_usage_steering.go — countAPIProxySteeringEvents, scanSteeringEntries, isSteeringEvent, proxyEventsEntry (~130 LOC)
  6. token_usage_subagent.go — augmentSubagentModelAttribution, addTokenUsageWarning, extractSubagentModelRequests (~200 LOC)

Acceptance Criteria

  • Original file split into 6 focused files
  • Each new file is under 500 lines
  • All tests pass (make test-unit)
  • Test coverage ≥80% for new files
  • No breaking changes to public API
  • Code passes linting (make lint) and build (make build)

Priority: Medium | Effort: Medium | Expected Impact: Improved maintainability and testability

Generated by 🧹 Daily File Diet · sonnet46 · 67.5 AIC · ⌖ 9.41 AIC · ⊞ 6.9K ·

  • expires on Jul 29, 2026, 5:38 AM UTC-08:00

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions