Skip to content

Replace raw fmt.Fprintf verbose debug output with console.LogVerbose in token_usage.go#39247

Merged
pelikhan merged 2 commits into
mainfrom
copilot/replace-verbose-debug-output
Jun 14, 2026
Merged

Replace raw fmt.Fprintf verbose debug output with console.LogVerbose in token_usage.go#39247
pelikhan merged 2 commits into
mainfrom
copilot/replace-verbose-debug-output

Conversation

Copilot AI commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Verbose debug messages in pkg/cli/token_usage.go used fmt.Fprintf(os.Stderr, ...) directly instead of console.LogVerbose(), bypassing consistent styled output and redundantly re-implementing the verbose gate.

Changes

  • analyzeTokenUsage and analyzeTokenUsageAICOnly: replaced all 5 if verbose { fmt.Fprintf(os.Stderr, ...) } blocks with console.LogVerbose(verbose, ...), removing the redundant explicit verbose check
  • For blocks that called os.Stat solely to populate the log message, moved the stat call outside the (now-removed) if verbose wrapper
  • Used string concatenation instead of fmt.Sprintf for the single %s-only format (per perfsprint linter)
// Before
if verbose {
    fileInfo, _ := os.Stat(filePath)
    if fileInfo != nil {
        fmt.Fprintf(os.Stderr, "  Found token usage file: %s (%d bytes)\n", filepath.Base(filePath), fileInfo.Size())
    }
}

// After
fileInfo, _ := os.Stat(filePath)
if fileInfo != nil {
    console.LogVerbose(verbose, fmt.Sprintf("  Found token usage file: %s (%d bytes)", filepath.Base(filePath), fileInfo.Size()))
}

…oken_usage.go

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Replace fmt.Fprintf with console.LogVerbose in token_usage.go Replace raw fmt.Fprintf verbose debug output with console.LogVerbose in token_usage.go Jun 14, 2026
Copilot AI requested a review from pelikhan June 14, 2026 16:40
@pelikhan pelikhan marked this pull request as ready for review June 14, 2026 16:46
Copilot AI review requested due to automatic review settings June 14, 2026 16:46

Copilot AI 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

This PR standardizes verbose debug output in pkg/cli/token_usage.go by routing messages through console.LogVerbose() instead of writing directly to os.Stderr, so verbose output is consistently formatted and gated in one place.

Changes:

  • Replaced direct fmt.Fprintf(os.Stderr, ...) verbose messages with console.LogVerbose(verbose, ...) in analyzeTokenUsage and analyzeTokenUsageAICOnly.
  • Updated message construction to satisfy the perfsprint guidance by using string concatenation for the single %s-only case.
Show a summary per file
File Description
pkg/cli/token_usage.go Switches verbose token-usage discovery logs from raw stderr printing to console.LogVerbose() for consistent formatting and centralized gating.

Copilot's findings

Tip

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

  • Files reviewed: 1/1 changed files
  • Comments generated: 5

Comment thread pkg/cli/token_usage.go
Comment on lines +443 to 446
fileInfo, _ := os.Stat(filePath)
if fileInfo != nil {
console.LogVerbose(verbose, fmt.Sprintf(" Found token usage file: %s (%d bytes)", filepath.Base(filePath), fileInfo.Size()))
}
Comment thread pkg/cli/token_usage.go
Comment on lines +461 to 464
agentFileInfo, _ := os.Stat(agentUsagePath)
if agentFileInfo != nil {
console.LogVerbose(verbose, fmt.Sprintf(" Found agent usage file: %s (%d bytes)", filepath.Base(agentUsagePath), agentFileInfo.Size()))
}
Comment thread pkg/cli/token_usage.go
Comment on lines +639 to 642
fileInfo, _ := os.Stat(filePath)
if fileInfo != nil {
console.LogVerbose(verbose, fmt.Sprintf(" Found token usage file: %s (%d bytes)", filepath.Base(filePath), fileInfo.Size()))
}
Comment thread pkg/cli/token_usage.go
Comment on lines +683 to 686
agentFileInfo, _ := os.Stat(agentUsagePath)
if agentFileInfo != nil {
console.LogVerbose(verbose, fmt.Sprintf(" Found agent usage file: %s (%d bytes)", filepath.Base(agentUsagePath), agentFileInfo.Size()))
}
Comment thread pkg/cli/token_usage.go
if verbose {
fmt.Fprintf(os.Stderr, " Found usage JSONL files: %s\n", strings.Join(usageJSONLFiles, ", "))
}
console.LogVerbose(verbose, " Found usage JSONL files: "+strings.Join(usageJSONLFiles, ", "))
@github-actions

Copy link
Copy Markdown
Contributor

@copilot review all comments and address unresolved review feedback.

Generated by 👨‍🍳 PR Sous Chef · 155.9 AIC · ⌖ 1.05 AIC · ⊞ 17.4K ·

@github-actions

Copy link
Copy Markdown
Contributor

Please refresh the branch and rerun checks, then summarize any remaining blockers.

Generated by 👨‍🍳 PR Sous Chef · 155.9 AIC · ⌖ 1.05 AIC · ⊞ 17.4K ·

@github-actions

Copy link
Copy Markdown
Contributor

Hey @pelikhan 👋 — great clean-up replacing the raw fmt.Fprintf(os.Stderr, ...) verbose guards in token_usage.go with the consistent console.LogVerbose() abstraction! The before/after in the PR body makes the intent crystal clear.

One gap to address before this is merge-ready:

  • Add tests — the 5 refactored call sites in analyzeTokenUsage and analyzeTokenUsageAICOnly aren't covered by the diff. Unit tests confirming that the verbose paths emit output through console.LogVerbose (and are silent when verbose=false) would give reviewers confidence the behavior is preserved.

If you'd like the agent to take care of it:

Add unit tests for the verbose logging paths in pkg/cli/token_usage.go.
Cover these scenarios for both analyzeTokenUsage and analyzeTokenUsageAICOnly:
1. verbose=false: LogVerbose should produce no output.
2. verbose=true + token usage file found: log should contain the base filename and byte size.
3. verbose=true + agent usage file found: log should contain the base filename and byte size.
4. verbose=true + usage JSONL files found (AICOnly path): log should contain the joined file list.
Use the existing test helpers in pkg/cli/ and mock or stub os.Stat where needed.

Generated by ✅ Contribution Check · 503.3 AIC · ⌖ 13.6 AIC · ⊞ 24.7K ·

@github-actions

github-actions Bot commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

🧠 Matt Pocock Skills Reviewer has completed the skills-based review. ✅

@github-actions

github-actions Bot commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Design Decision Gate 🏗️ completed the design decision gate check.

No ADR enforcement needed: PR #39247 does not have the implementation label and has only 13 new lines of code in business logic directories (≤100 threshold).

@github-actions

github-actions Bot commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Test Quality Sentinel completed test quality analysis.

No test files were added or modified in this PR. The only changed file is pkg/cli/token_usage.go (production code). Test Quality Sentinel skipped.

@github-actions

github-actions Bot commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

PR Code Quality Reviewer completed the code quality review.

@github-actions github-actions Bot mentioned this pull request Jun 14, 2026

@github-actions github-actions Bot 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.

Skills-Based Review 🧠

Applied /zoom-out and /improve-codebase-architecture — one non-blocking observation on a subtle behavioral change.

📋 Key Themes & Highlights

Key Theme

  • Unconditional os.Stat syscalls: moving the stat calls outside the if verbose guard means they now run on every invocation, even when verbose=false. The file stat result is only consumed for the log message, so this adds work that was previously avoided.

Positive Highlights

  • ✅ Correct goal: console.LogVerbose is the right abstraction; raw fmt.Fprintf(os.Stderr, ...) with a manual verbose gate is exactly the duplication this helper was designed to eliminate.
  • ✅ Clean, consistent application across both analyzeTokenUsage and analyzeTokenUsageAICOnly.
  • ✅ String concatenation (vs. fmt.Sprintf) for the %s-only JSONL message satisfies the perfsprint linter cleanly.
  • ✅ Sensible variable rename fileInfoagentFileInfo to avoid shadowing at the broader scope.

Suggestion

A LogVerbosef(verbose bool, format string, args ...any) variant in pkg/console would let the verbose gate live inside the helper for all call sites — including ones that need to compute expensive arguments. That would be the long-term fix without reintroducing explicit if verbose wrappers.

🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer · 183.1 AIC · ⌖ 13.5 AIC · ⊞ 27.9K

Comment thread pkg/cli/token_usage.go
if fileInfo != nil {
fmt.Fprintf(os.Stderr, " Found token usage file: %s (%d bytes)\n", filepath.Base(filePath), fileInfo.Size())
}
fileInfo, _ := os.Stat(filePath)

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.

[/zoom-out] os.Stat is now called unconditionally even when verbose=false — this is a subtle behavioral change from the original code where the stat was guarded by if verbose.

💡 Details and suggested fix

Since fileInfo is only used to format the log message, the syscall was previously skipped when not in verbose mode. The same pattern repeats at lines 461, 641, and 683.

The cleanest fix is to keep a verbose guard around both the os.Stat and the log call:

if verbose {
    if fileInfo, _ := os.Stat(filePath); fileInfo != nil {
        console.LogVerbose(verbose, fmt.Sprintf("  Found token usage file: %s (%d bytes)", filepath.Base(filePath), fileInfo.Size()))
    }
}

Alternatively, consider adding a LogVerbosef(verbose bool, format string, args ...any) helper to the console package. This would internalize the verbose gate so call sites never need the explicit if verbose wrapper — and it would allow lazy argument evaluation without adding syscalls in non-verbose paths.

@github-actions github-actions Bot 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.

Five instances of the same regression: non-verbose paths now do more work than before this PR.

The PR goal was to standardize verbose output through console.LogVerbose, but the implementation inverts the performance contract on non-verbose paths. See the five existing inline comments for per-site details.

Root cause: console.LogVerbose takes a pre-formatted string, so callers must eagerly evaluate all arguments before the verbose guard runs. The old if verbose { ... } wrappers correctly deferred both syscalls and string construction. Restore those wrappers while keeping console.LogVerbose for the actual write:

if verbose {
    fileInfo, _ := os.Stat(filePath)
    if fileInfo != nil {
        console.LogVerbose(verbose, fmt.Sprintf("  Found token usage file: %s (%d bytes)", filepath.Base(filePath), fileInfo.Size()))
    }
}

🔎 Code quality review by PR Code Quality Reviewer · 322.3 AIC · ⌖ 13.1 AIC · ⊞ 16.7K

@pelikhan pelikhan merged commit 8aa0aaa into main Jun 14, 2026
79 of 90 checks passed
@pelikhan pelikhan deleted the copilot/replace-verbose-debug-output branch June 14, 2026 17:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants