Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 16, 2026

Analysis identified ~150 lines of duplicate code across the logger package, primarily in initialization and cleanup functions for FileLogger, JSONLLogger, and MarkdownLogger. Each logger type repeated identical mutex lock/unlock and close-then-assign patterns.

Changes

New file: internal/logger/global_patterns.go

  • Six helper functions encapsulating init/close patterns:
    • initGlobalFileLogger(), closeGlobalFileLogger()
    • initGlobalJSONLLogger(), closeGlobalJSONLLogger()
    • initGlobalMarkdownLogger(), closeGlobalMarkdownLogger()

Refactored logger files:

  • file_logger.go: InitFileLogger and CloseGlobalLogger now delegate to helpers
  • jsonl_logger.go: InitJSONLLogger and CloseJSONLLogger now delegate to helpers
  • markdown_logger.go: InitMarkdownLogger and CloseMarkdownLogger now delegate to helpers

Before/After

Before (repeated 3x):

func CloseGlobalLogger() error {
    globalLoggerMu.Lock()
    defer globalLoggerMu.Unlock()
    
    if globalFileLogger != nil {
        err := globalFileLogger.Close()
        globalFileLogger = nil
        return err
    }
    return nil
}

After:

func CloseGlobalLogger() error {
    return closeGlobalFileLogger()
}

Impact

  • Removes ~56 lines of duplicate mutex handling code
  • Centralizes init/close patterns for future modifications
  • Maintains type safety without complex generics
Original prompt

This section details on the original issue you should resolve

<issue_title>[duplicate-code] Duplicate Code Analysis Report</issue_title>
<issue_description># 🔍 Duplicate Code Analysis Report

Analysis of commit 7998e88

Summary

This analysis identified 3 significant duplication patterns in the Go codebase that impact maintainability and create opportunities for bugs. The patterns affect critical infrastructure code including logging systems, server initialization, and global state management.

Detected Patterns

This analysis found 3 significant duplication patterns:

  1. Logger Initialization Pattern - Severity: High - See sub-issue [duplicate-code] Duplicate Code Pattern: Logger Initialization and Cleanup #265
  2. Global Logger Cleanup Functions - Severity: Medium - See sub-issue [duplicate-code] Duplicate Code Pattern: Dual Logging Functions (Md suffix) #266
  3. Mutex Lock/Unlock Patterns - Severity: Medium - See sub-issue [duplicate-code] Duplicate Code Pattern: Mutex Lock/Defer Unlock Boilerplate #267

Overall Impact

  • Total Duplicated Lines: ~150+ lines across multiple files
  • Affected Files: 9 files with significant duplication (primarily in internal/logger/ package)
  • Maintainability Risk: High - Changes to logging infrastructure require updates in 4 separate logger implementations
  • Refactoring Priority: High - Logger consolidation should be prioritized to prevent bugs from inconsistent updates

Next Steps

  1. Review individual pattern sub-issues for detailed analysis
  2. Prioritize refactoring based on severity and impact
  3. Create implementation plan for highest priority patterns
  4. Consider extracting common patterns into shared utilities or base types

Analysis Metadata

  • Analyzed Files: 43 Go files (non-test)
  • Detection Method: Manual semantic code analysis
  • Commit: 7998e88
  • Analysis Date: 2026-01-15
  • Repository: githubnext/gh-aw-mcpg</issue_description>

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


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

- Created global_patterns.go with 6 helper functions
- Extracted mutex lock/unlock/check/close/nil patterns
- Reduced duplication in FileLogger, JSONLLogger, MarkdownLogger
- Init*Logger functions now call initGlobal*Logger helpers
- Close*Logger functions now call closeGlobal*Logger helpers
- Each helper encapsulates the mutex handling and close-then-assign pattern
- Maintains type safety with specific helpers for each logger type
- All existing tests pass

Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix duplicate code issues identified in analysis report Extract duplicate global logger init/close patterns into helpers Jan 16, 2026
Copilot AI requested a review from lpcox January 16, 2026 03:36
@lpcox lpcox marked this pull request as ready for review January 16, 2026 03:50
@lpcox lpcox merged commit 6d711cf into main Jan 16, 2026
3 checks passed
@lpcox lpcox deleted the copilot/fix-duplicate-code-patterns branch January 16, 2026 03:50
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 Analysis Report

2 participants