Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 14, 2025

Both Claude and Copilot log parsers contained identical implementations of formatting utilities (formatDuration, formatBashCommand, truncateString, estimateTokens), totaling 138 lines of duplicate code across the two files.

Changes

  • New shared module log_parser_shared.cjs containing 4 utility functions with test coverage
  • Updated parsers to import from shared module instead of local implementations
  • Go embedding updated to include shared module in JavaScript sources map

Example

Before:

// parse_claude_log.cjs
function formatDuration(ms) { /* 15 lines */ }
function formatBashCommand(command) { /* 22 lines */ }
// ... duplicated in parse_copilot_log.cjs

After:

// parse_claude_log.cjs & parse_copilot_log.cjs
const { formatDuration, formatBashCommand, truncateString, estimateTokens } = 
  require("./log_parser_shared.cjs");

Note

formatInitializationSummary was not extracted - Claude returns {markdown, mcpFailures} while Copilot returns string with premium model info.

Original prompt

This section details on the original issue you should resolve

<issue_title>[duplicate-code] 🔍 Duplicate Code Detected: Log Parser Helper Functions</issue_title>
<issue_description>Analysis of commit 6417277

Assignee: @copilot

Summary

Both Copilot and Claude log parsers reimplement the same helper utilities (duration formatting, command normalization, initialization summaries). The duplication spans dozens of lines and makes the parsers diverge despite identical responsibilities.

Duplication Details

Pattern: Shared helper functions copied across log parsers

  • Severity: Medium
  • Occurrences: 4 helper functions mirrored in two files (8 instances total)
  • Locations:
    • pkg/workflow/js/parse_claude_log.cjs:242
    • pkg/workflow/js/parse_copilot_log.cjs:800
    • pkg/workflow/js/parse_claude_log.cjs:348
    • pkg/workflow/js/parse_copilot_log.cjs:923
    • pkg/workflow/js/parse_claude_log.cjs:520
    • pkg/workflow/js/parse_copilot_log.cjs:1115
    • pkg/workflow/js/parse_claude_log.cjs:555
    • pkg/workflow/js/parse_copilot_log.cjs:1152
  • Code Sample:
    function formatDuration(ms) {
      if (!ms || ms <= 0) return "";
      const seconds = Math.round(ms / 1000);
      if (seconds < 60) {
        return `${seconds}s`;
      }
      const minutes = Math.floor(seconds / 60);
      const remainingSeconds = seconds % 60;
      if (remainingSeconds === 0) {
        return `${minutes}m`;
      }
      return `${minutes}m ${remainingSeconds}s`;
    }

Impact Analysis

  • Maintainability: Changes to presentation (e.g., new summary fields or formatting tweaks) require editing two large files, risking inconsistency.
  • Bug Risk: Fixes applied to one parser may be missed in the other, causing divergent user experiences for different engines.
  • Code Bloat: ~60 duplicated lines inflate the scripts and obscure the core parsing logic.

Refactoring Recommendations

  1. Extract shared helpers into a common module

    • Move formatting utilities (formatDuration, formatBashCommand, truncateString, initialization summary) into pkg/workflow/js/log_parser_shared.cjs.
    • Estimated effort: Medium; requires updating both parsers to import the helpers and adding shared tests.
    • Benefits: Single source of truth for formatting, easier to evolve.
  2. Consider a base parser abstraction

    • Define a common pipeline that handles JSON/JSONL parsing and command summarization, letting engine-specific hooks supply custom behavior.
    • Estimated effort: Medium-high; improves reuse and clarity for future engines.

Implementation Checklist

  • Review duplication findings
  • Prioritize refactoring tasks
  • Create refactoring plan
  • Implement changes
  • Update tests
  • Verify no functionality broken

Analysis Metadata

  • Analyzed Files: 5
  • Detection Method: Serena semantic code analysis
  • Commit: 6417277
  • Analysis Date: 2025-11-14

AI generated by Duplicate Code Detector</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
@pelikhan pelikhan marked this pull request as ready for review November 14, 2025 22:45
Copilot AI review requested due to automatic review settings November 14, 2025 22:45
Copilot finished reviewing on behalf of pelikhan November 14, 2025 22:46
Copy link
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

This PR successfully refactors duplicate code from the Claude and Copilot log parsers by extracting 4 common utility functions into a shared module (log_parser_shared.cjs). The refactoring eliminates approximately 138 lines of duplicate code while maintaining backward compatibility and adding comprehensive test coverage.

Key Changes

  • Created a new shared utility module with 4 formatting functions
  • Added 19 comprehensive tests covering all edge cases
  • Updated both log parsers to use the shared functions via require()
  • Properly configured Go embedding for the new module

Reviewed Changes

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

Show a summary per file
File Description
pkg/workflow/js/log_parser_shared.cjs New shared module with 4 utility functions (formatDuration, formatBashCommand, truncateString, estimateTokens)
pkg/workflow/js/log_parser_shared.test.cjs Comprehensive test suite with 19 tests covering all edge cases
pkg/workflow/js/parse_claude_log.cjs Removed duplicate functions, added import from shared module
pkg/workflow/js/parse_copilot_log.cjs Removed duplicate functions, added import from shared module
pkg/workflow/js/parse_claude_log.test.cjs Added mock for shared module to support testing
pkg/workflow/js/parse_copilot_log.test.cjs Added mock for shared module to support testing
pkg/workflow/js.go Added embedding configuration for the new shared module
.github/workflows/*.lock.yml Regenerated lock files showing proper bundling of shared functions

The refactoring follows established project patterns, maintains identical functionality, and improves code maintainability by establishing a single source of truth for these formatting utilities. All tests pass and the code is properly formatted and linted.


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

Copilot AI changed the title [WIP] Fix duplicate code in log parser helper functions Extract shared utilities from log parsers to eliminate duplication Nov 14, 2025
Copilot AI requested a review from pelikhan November 14, 2025 22:48
Copilot finished work on behalf of pelikhan November 14, 2025 22:48
@pelikhan pelikhan merged commit 3216b82 into main Nov 14, 2025
132 of 138 checks passed
@pelikhan pelikhan deleted the copilot/remove-duplicate-log-helper-functions branch November 14, 2025 22:51
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.

[duplicate-code] 🔍 Duplicate Code Detected: Log Parser Helper Functions

2 participants