Skip to content

Refactor: Reduce duplicate logging patterns in launcher helpers#941

Closed
Claude wants to merge 2 commits intomainfrom
claude/fix-duplicate-code-issues
Closed

Refactor: Reduce duplicate logging patterns in launcher helpers#941
Claude wants to merge 2 commits intomainfrom
claude/fix-duplicate-code-issues

Conversation

@Claude
Copy link
Contributor

@Claude Claude AI commented Feb 14, 2026

Addresses duplicate code issues #930 and #931 from the February 2026 analysis. Three launcher logging functions (logLaunchStart, logTimeoutError, logLaunchSuccess) contained ~45 lines of duplicated if/else branching and repetitive triple-logging calls (file, stdout, debug).

Changes

  • Extracted tripleLogInfo() helper: Encapsulates the pattern of logging to three destinations at INFO level, eliminating ~20 lines of duplicate code across session-aware logging functions

  • Refactored session-aware branching: Updated logLaunchStart() and logLaunchSuccess() to use the helper; extracted sessionSfx variable in logTimeoutError() for consistency

  • Fixed pre-existing test failures: Tests expected debug logger output (logLauncher.Printf) which only appears with DEBUG env var set, but test harness only captures standard log output. Updated assertions to check actual captured content.

Example

Before:

if sessionID != "" {
    logger.LogInfoWithServer(serverID, "backend", "msg for session: %s, %s", serverID, sessionID)
    log.Printf("[LAUNCHER] msg: %s (session: %s)", serverID, sessionID)
    logLauncher.Printf("msg: serverID=%s, sessionID=%s", serverID, sessionID)
} else {
    logger.LogInfoWithServer(serverID, "backend", "msg: %s", serverID)
    log.Printf("[LAUNCHER] msg: %s", serverID)
    logLauncher.Printf("msg: serverID=%s", serverID)
}

After:

if sessionID != "" {
    tripleLogInfo(
        serverID, sessionID, "backend",
        "msg for session: %s, %s", []interface{}{serverID, sessionID},
        "[LAUNCHER] msg: %s (session: %s)", []interface{}{serverID, sessionID},
        "msg: serverID=%s, sessionID=%s", []interface{}{serverID, sessionID},
    )
} else { /* similar */ }

Assessment of Issue #932

Global logger initialization wrappers (issue #932) already use generic helpers (initGlobalLogger, closeGlobalLogger) that abstract complex logic. The type-specific wrappers provide stable, type-safe API and are acceptable as-is.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • example.com
    • Triggering command: /tmp/go-build2432024851/b279/launcher.test /tmp/go-build2432024851/b279/launcher.test -test.testlogfile=/tmp/go-build2432024851/b279/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true -l .; exit 1) x_amd64/compile /usr/bin/dirname-lang=go1.25 g_.a gKxUsK41W /home/REDACTED/.ca/tmp/go-build2110249177/b094/vet.cfg dirname /hom�� ache/Python/3.12-c=4 git ache/go/1.25.7/x-importcfg 64/src/runtime/c/opt/hostedtoolcache/go/1.25.7/x64/pkg/tool/linux_amd64/vet L9DlBEyy1 x_amd64/compile base64 (dns block)
  • invalid-host-that-does-not-exist-12345.com
    • Triggering command: /tmp/go-build2432024851/b264/config.test /tmp/go-build2432024851/b264/config.test -test.testlogfile=/tmp/go-build2432024851/b264/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true 86_64/git 64/pkg/tool/linu/tmp/go-build1419250802/b107/vet.cfg /opt/hostedtoolcache/Python/3.12.12/x64/bin/git go gS_UDtWia /opt/hostedtoolc/tmp/go-build2110249177/b054/vet.cfg git rev-�� --abbrev-ref HEAD ndor/bin/git 64/src/runtime/c/opt/hostedtoolcache/go/1.25.7/x64/pkg/tool/linux_amd64/vet p6T44YG/Vlapd5iY/tmp/go-build2110249177/b205/vet.cfg .12/x64/bin/as base64 (dns block)
  • nonexistent.local
    • Triggering command: /tmp/go-build2432024851/b279/launcher.test /tmp/go-build2432024851/b279/launcher.test -test.testlogfile=/tmp/go-build2432024851/b279/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true -l .; exit 1) x_amd64/compile /usr/bin/dirname-lang=go1.25 g_.a gKxUsK41W /home/REDACTED/.ca/tmp/go-build2110249177/b094/vet.cfg dirname /hom�� ache/Python/3.12-c=4 git ache/go/1.25.7/x-importcfg 64/src/runtime/c/opt/hostedtoolcache/go/1.25.7/x64/pkg/tool/linux_amd64/vet L9DlBEyy1 x_amd64/compile base64 (dns block)
  • slow.example.com
    • Triggering command: /tmp/go-build2432024851/b279/launcher.test /tmp/go-build2432024851/b279/launcher.test -test.testlogfile=/tmp/go-build2432024851/b279/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true -l .; exit 1) x_amd64/compile /usr/bin/dirname-lang=go1.25 g_.a gKxUsK41W /home/REDACTED/.ca/tmp/go-build2110249177/b094/vet.cfg dirname /hom�� ache/Python/3.12-c=4 git ache/go/1.25.7/x-importcfg 64/src/runtime/c/opt/hostedtoolcache/go/1.25.7/x64/pkg/tool/linux_amd64/vet L9DlBEyy1 x_amd64/compile base64 (dns block)
  • this-host-does-not-exist-12345.com
    • Triggering command: /tmp/go-build2432024851/b288/mcp.test /tmp/go-build2432024851/b288/mcp.test -test.testlogfile=/tmp/go-build2432024851/b288/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true nner/.nvm git cal/bin/git _DIR) == 1 { /opt/hostedtoolcache/go/1.25.7/x64/pkg/tool/linux_amd64/vet R9l7N8J2z /usr/local/sbin//tmp/go-build2110249177/b163/vet.cfg head -n 1 git ache/Python/3.12.12/x64/git 64/src/runtime/c/opt/hostedtoolcache/go/1.25.7/x64/pkg/tool/linux_amd64/vet 64/src/encoding//tmp/go-build2110249177/b249/vet.cfg ache/go/1.25.7/x64/pkg/tool/linu-lang=go1.25 base64 (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

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

Analysis of commit de05a2c

Summary

This analysis identified 3 significant duplication patterns across the logger and launcher packages, totaling approximately 85+ lines of duplicated or near-identical code. The duplication affects maintainability and increases the risk of inconsistent fixes.

Detected Patterns

This analysis found 3 significant duplication patterns:

  1. Session-Aware Logging Branching - Severity: High - See sub-issue [duplicate-code] Session-Aware Logging Branching Duplication #930

    • 3 functions with identical if/else branching for sessionID
    • ~45 lines of duplicated conditional logic
  2. Multi-Level Logging Calls - Severity: Medium - See sub-issue [duplicate-code] Multi-Level Logging Calls Duplication #931

    • Triple logging pattern repeated 6+ times
    • ~30+ lines of repetitive logging calls
  3. Global Logger Initialization Wrappers - Severity: Low - See sub-issue [duplicate-code] Global Logger Initialization Wrapper Boilerplate #932

    • 10 nearly identical wrapper functions
    • ~20 lines of boilerplate code

Overall Impact

  • Total Duplicated Lines: ~85-95 lines
  • Affected Files: 3 Go files (launcher/log_helpers.go, logger/global_helpers.go, logger/tools_logger.go)
  • Maintainability Risk: Medium-High (conditional logic duplication increases bug risk)
  • Refactoring Priority: High for Pattern 1, Medium for Pattern 2, Low for Pattern 3

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 logging utilities

Analysis Metadata

  • Analyzed Files: 10 Go files (excluding tests)
  • Detection Method: Serena semantic code analysis + pattern matching
  • Commit: de05a2c
  • Analysis Date: 2026-02-14
  • Files Analyzed: internal/launcher/log_helpers.go, internal/logger/global_helpers.go, internal/logger/tools_logger.go, internal/logger/sanitize/sanitize.go, internal/mcp/connection.go, internal/middleware/jqschema.go, internal/server/unified.go, internal/cmd/root.go, internal/cmd/flags_core.go, main.go</issue_description>

Comments on the Issue (you are @claude[agent] in this section)

…g duplication

- Add tripleLogInfo() helper function that encapsulates the pattern of logging to three destinations (file, stdout, debug) at INFO level
- Refactor logLaunchStart() and logLaunchSuccess() to use the new helper, reducing code duplication from the triple-logging pattern
- Extract sessionSfx variable in logTimeoutError() for consistency
- Fix pre-existing test failures: tests were expecting debug logger output (logLauncher.Printf) which only appears with DEBUG env var, but test harness only captures standard log output
- Update test assertions to check for content that actually appears in captured output
- Addresses duplicate code issues #930 and #931 (partial)

Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
@Claude Claude AI changed the title [WIP] Fix duplicate code patterns in logger and launcher packages Refactor: Reduce duplicate logging patterns in launcher helpers Feb 14, 2026
@Claude Claude AI requested a review from lpcox February 14, 2026 13:21
@lpcox lpcox closed this Feb 14, 2026
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 - February 2026

2 participants