Skip to content

[code-simplifier] refactor: simplify token usage parsing in parse_mcp_gateway_log.cjs#24032

Merged
pelikhan merged 1 commit intomainfrom
code-simplifier/token-usage-cleanup-20260402-55856fc034540111
Apr 2, 2026
Merged

[code-simplifier] refactor: simplify token usage parsing in parse_mcp_gateway_log.cjs#24032
pelikhan merged 1 commit intomainfrom
code-simplifier/token-usage-cleanup-20260402-55856fc034540111

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

@github-actions github-actions bot commented Apr 2, 2026

Targeted simplification of the token usage parsing code added in #24029, improving clarity while preserving identical behavior.

Files Simplified

  • actions/setup/js/parse_mcp_gateway_log.cjs — cleaned up newly added token usage functions

Improvements Made

1. Nullish assignment for model initialization (parseTokenUsageJsonl)

Replace verbose if (!x) x = {...} guard with ??= (logical nullish assignment):

// Before
if (!summary.byModel[model]) {
  summary.byModel[model] = { provider: ..., inputTokens: 0, ... };
}

// After
summary.byModel[model] ??= { provider: ..., inputTokens: 0, ... };

2. Removed redundant ternary (writeStepSummaryWithTokenUsage)

generateTokenUsageSummary() already handles null input (returns ""), so the ternary guard is unnecessary:

// Before
const summary = parseTokenUsageJsonl(content);
const markdown = summary ? generateTokenUsageSummary(summary) : "";

// After
const markdown = generateTokenUsageSummary(parseTokenUsageJsonl(content));

3. Optional chaining (writeStepSummaryWithTokenUsage)

// Before
if (content && content.trim()) {

// After
if (content?.trim()) {

Changes Based On

Recent changes from:

Testing

  • ✅ Inline validation tests pass (node runtime)
  • ✅ Linting passes (make lint-cjs)
  • ✅ No functional changes — behavior is identical

Review Focus

Please verify:

  • Functionality is preserved
  • ??= operator is available in Node.js 20+ (GitHub Actions runtime) ✅
  • generateTokenUsageSummary(null) returns "" as expected ✅

Automated by Code Simplifier Agent — analyzing code from the last 24 hours

References:

Generated by Code Simplifier ·

  • expires on Apr 3, 2026, 6:11 AM UTC

- Use ??= (nullish assignment) instead of if-guard for model initialization
- Remove redundant ternary: generateTokenUsageSummary() already handles null
- Use optional chaining (content?.trim()) instead of double-check

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions bot mentioned this pull request Apr 2, 2026
@pelikhan pelikhan marked this pull request as ready for review April 2, 2026 10:00
Copilot AI review requested due to automatic review settings April 2, 2026 10:00
Copy link
Copy Markdown
Contributor

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

Refactors the token usage parsing/rendering logic in parse_mcp_gateway_log.cjs to reduce verbosity while preserving existing behavior introduced in #24029.

Changes:

  • Uses ??= to initialize per-model token usage buckets more concisely in parseTokenUsageJsonl.
  • Simplifies token usage markdown generation by removing an unnecessary ternary guard.
  • Replaces a verbose content check with content?.trim().

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

@pelikhan pelikhan merged commit 2a608ee into main Apr 2, 2026
64 of 67 checks passed
@pelikhan pelikhan deleted the code-simplifier/token-usage-cleanup-20260402-55856fc034540111 branch April 2, 2026 10:07
Sign up for free to 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.

2 participants