Skip to content

[duplicate-code] Duplicate Code: JSON/JSONL Parsing Fallback in Log Parsers #4571

@github-actions

Description

@github-actions

🔍 Duplicate Code Detected: Log Parser JSON Fallback

Analysis of commit 102022d

Assignee: @copilot

Summary

Both Copilot and Claude log parsers reimplement the same JSON/JSONL parsing fallback logic, even down to identical trimming, array-handling, and line-by-line parsing. This block is ~50 lines long in each file and differs only by parser name, making it easy for the parsers to drift out of sync when formats change.

Duplication Details

Pattern: JSON/JSONL parsing fallback

  • Severity: Medium
  • Occurrences: 2
  • Locations:
    • pkg/workflow/js/parse_claude_log.cjs (lines 31-89)
    • pkg/workflow/js/parse_copilot_log.cjs (lines 60-118)
  • Code Sample:
    try {
      logEntries = JSON.parse(logContent);
      if (!Array.isArray(logEntries)) {
        throw new Error("Not a JSON array");
      }
    } catch (jsonArrayError) {
      logEntries = [];
      const lines = logContent.split("\n");
      for (const line of lines) {
        const trimmedLine = line.trim();
        if (trimmedLine === "") continue;
        if (trimmedLine.startsWith("[{")) {
          // try to parse array
        }
        if (!trimmedLine.startsWith("{")) continue;
        try {
          const jsonEntry = JSON.parse(trimmedLine);
          logEntries.push(jsonEntry);
        } catch (jsonLineError) {
          continue;
        }
      }
    }

Impact Analysis

  • Maintainability: Updating parsing behavior (e.g., new log format or stricter validation) requires editing both files, inviting divergence.
  • Bug Risk: Fixes applied to one parser (Copilot or Claude) may be missed in the other, leading to inconsistent user-facing summaries for the same log format.
  • Code Bloat: ~100 duplicated lines increase file size and noise.

Refactoring Recommendations

  1. Extract shared parser helper
    • Create a utility in log_parser_shared.cjs (e.g., parseLogEntries(logContent, options)) that encapsulates the JSON array → JSONL fallback.
    • Estimated effort: 2-3 hours. Benefits: single source of truth, easier future format tweaks.
  2. Inject parser-specific hooks
    • Allow callers to supply parser-specific behaviors (e.g., Copilot’s debug log handling) through options callbacks instead of duplicating the base logic.

Implementation Checklist

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

Analysis Metadata

  • Analyzed Files: 4
  • Detection Method: Serena semantic code analysis
  • Commit: 102022d
  • Analysis Date: 2024-06-05

AI generated by Duplicate Code Detector

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions