Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 18, 2026

Overview

Eliminates 41 lines of duplicate code across 3 patterns identified in the January 2026 duplicate code analysis (issues #317, #318, #319).

Changes

Pattern 1: JSON Unmarshal Argument Parsing (internal/server/unified.go)

Before - 10 lines duplicated in 2 handlers:

var toolArgs map[string]interface{}
if req.Params.Arguments != nil {
    if err := json.Unmarshal(req.Params.Arguments, &toolArgs); err != nil {
        logger.LogError("client", "Failed to unmarshal tool arguments...")
        return &sdk.CallToolResult{IsError: true}, nil, fmt.Errorf("failed to parse arguments: %w", err)
    }
} else {
    toolArgs = make(map[string]interface{})
}

After - Single helper, 1-line call sites:

func parseToolArguments(req *sdk.CallToolRequest) (map[string]interface{}, error) { ... }

// Usage
toolArgs, err := parseToolArguments(req)

Pattern 2: Markdown Logger Wrappers (internal/logger/markdown_logger.go)

Replaced 4 nearly-identical 12-line functions (LogInfoMd, LogWarnMd, LogErrorMd, LogDebugMd) with a generic helper using function parameters:

func logWithMarkdown(level LogLevel, regularLogFunc func(string, string, ...interface{}), 
                     category, format string, args ...interface{}) { ... }

func LogInfoMd(category, format string, args ...interface{}) {
    logWithMarkdown(LogLevelInfo, LogInfo, category, format, args...)
}

Pattern 3: RPC Message Logging (internal/logger/rpc_logger.go)

Extracted common text/markdown/JSONL logging logic from LogRPCRequest and LogRPCResponse into logRPCMessageToAll(). Both functions now delegate to this single implementation.

Impact

  • -41 net lines (92 deletions, 51 insertions)
  • 3 files changed
  • Zero behavioral changes - all existing tests pass
Original prompt

This section details on the original issue you should resolve

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

Analysis of commit e36d56f

Summary

This analysis identified 3 significant duplication patterns in the Go codebase that impact maintainability and should be refactored. These patterns represent approximately 60+ lines of duplicated code across critical paths in the server and logging subsystems.

Detected Patterns

  1. JSON Unmarshal Argument Parsing - Severity: MEDIUM - See sub-issue [duplicate-code] Duplicate Code Pattern: JSON Unmarshal Argument Parsing #317

    • 3+ instances of identical 8-12 line blocks
    • Located in server/unified.go and testutil packages
    • Direct impact on tool execution paths
  2. Markdown Logger Wrapper Functions - Severity: MEDIUM - See sub-issue [duplicate-code] Duplicate Code Pattern: Markdown Logger Wrapper Functions #318

    • 4 nearly identical functions (12-15 lines each)
    • Located in logger/markdown_logger.go
    • 48+ lines of duplicated code
  3. RPC Message Logging Duplication - Severity: MEDIUM - See sub-issue [duplicate-code] Duplicate Code Pattern: RPC Message Logging Functions #319

    • 2 functions with identical structure (11-14 lines each)
    • Located in logger/rpc_logger.go
    • Affects all RPC communication logging

Overall Impact

  • Total Duplicated Lines: ~60-80 lines
  • Affected Files: 5 Go files (server, logger, testutil packages)
  • Maintainability Risk: MEDIUM - Changes require updates in multiple locations
  • Refactoring Priority: HIGH - Patterns are in frequently called code paths

Next Steps

  1. Review individual pattern sub-issues for detailed analysis
  2. Prioritize refactoring based on severity and impact (suggest starting with [duplicate-code] Duplicate Code Pattern: Markdown Logger Wrapper Functions #318)
  3. Create implementation plan for highest priority patterns
  4. Consider extracting common utilities to reduce duplication

Positive Findings

Good patterns already in place:

  • Global logger initialization/cleanup properly extracted (internal/logger/global_patterns.go)
  • Consistent error wrapping using idiomatic Go patterns
  • Appropriate use of mutex locking patterns

Analysis Metadata

  • Analyzed Files: 93 Go files (excluding test files)
  • Detection Method: Semantic code analysis
  • Commit: e36d56f
  • Analysis Date: 2026-01-17T10:09:06Z
  • Triggered by: @pelikhan</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.

Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor duplicate code in Go codebase Refactor duplicate code patterns in logger and server packages Jan 18, 2026
Copilot AI requested a review from lpcox January 18, 2026 16:22
@lpcox lpcox marked this pull request as ready for review January 18, 2026 17:04
@lpcox lpcox merged commit 5e41715 into main Jan 18, 2026
3 checks passed
@lpcox lpcox deleted the copilot/refactor-duplicate-code-patterns branch January 18, 2026 17:04
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 - January 2026

2 participants