Skip to content

Refactor: Move logger helpers and flag completions to correct files#1196

Merged
lpcox merged 3 commits intomainfrom
claude/refactor-semantic-function-clustering
Feb 20, 2026
Merged

Refactor: Move logger helpers and flag completions to correct files#1196
lpcox merged 3 commits intomainfrom
claude/refactor-semantic-function-clustering

Conversation

@Claude
Copy link
Contributor

@Claude Claude AI commented Feb 20, 2026

Static analysis identified two organizational inconsistencies where helper functions were placed in the wrong files, breaking established package patterns.

Changes

Logger Package (internal/logger/)

  • Moved initGlobalToolsLogger() and closeGlobalToolsLogger() from tools_logger.go to global_helpers.go
  • Updated documentation in common.go to include ToolsLogger helpers in the canonical list

Previously, all logger init/close helpers (FileLogger, JSONLLogger, MarkdownLogger, ServerFileLogger) were in global_helpers.go, but ToolsLogger's helpers were outliers in tools_logger.go. This change restores consistency:

// Now all helpers follow the same pattern in global_helpers.go
func initGlobalFileLogger(logger *FileLogger) { ... }
func initGlobalJSONLLogger(logger *JSONLLogger) { ... }
func initGlobalMarkdownLogger(logger *MarkdownLogger) { ... }
func initGlobalServerFileLogger(logger *ServerFileLogger) { ... }
func initGlobalToolsLogger(logger *ToolsLogger) { ... }  // ← Previously in tools_logger.go

CMD Package (internal/cmd/)

  • Moved registerFlagCompletions() from root.go to flags.go

The flags.go file centralizes all flag-related logic (RegisterFlag, registerAllFlags). The completion registration function was misplaced in root.go.

Impact

Pure code organization refactoring with zero behavioral changes. All tests pass.

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-build3536313155/b275/launcher.test /tmp/go-build3536313155/b275/launcher.test -test.testlogfile=/tmp/go-build3536313155/b275/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true -c=4 -nolocalimports -importcfg /tmp/go-build3536313155/b256/importcfg -pack /home/REDACTED/go/pkg/mod/github.com/itchyny/gojq@v0.12.18/builtin.go /home/REDACTED/go/pkg/mod/github.com/itchyny/gojq@v0.12.18/code.go rev-�� go HEAD x_amd64/compile (dns block)
    • Triggering command: /tmp/go-build32475415/b279/launcher.test /tmp/go-build32475415/b279/launcher.test -test.testlogfile=/tmp/go-build32475415/b279/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true -lang=go1.25 01.o .cfg 64/src/net --gdwarf-5 64/pkg/tool/linu/tmp/go-build3223519424/b241/vet.cfg sh -c git status --porcelain --ignore-submodules | head -n 10 64/pkg/tool/linuHEAD ache/go/1.25.6/x64/pkg/tool/linux_amd64/compile -unreachable=fal/usr/bin/runc.original 6313155/b165/ cal/bin/as ache/go/1.25.6/x64/pkg/tool/linux_amd64/compile (dns block)
  • invalid-host-that-does-not-exist-12345.com
    • Triggering command: /tmp/go-build3536313155/b260/config.test /tmp/go-build3536313155/b260/config.test -test.testlogfile=/tmp/go-build3536313155/b260/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true go ternal/randutil/--gdwarf2 64/pkg/tool/linu--64 (dns block)
    • Triggering command: /tmp/go-build32475415/b264/config.test /tmp/go-build32475415/b264/config.test -test.testlogfile=/tmp/go-build32475415/b264/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true --abbrev-ref HEAD ache/Python/3.12.12/x64/git lcache/go/1.25.6/opt/hostedtoolcache/go/1.25.6/x64/pkg/tool/linux_amd64/compile .cfg 64/pkg/tool/linu/tmp/go-build3223519424/b260/_pkg_.a base64 -d -lang=go1.25 64/pkg/tool/linugithub.com/github/gh-aw-mcpg/internal/testutil/mcptest /usr/local/.ghcu-lang=go1.25 v0.12.18/builtin/opt/hostedtoolcache/go/1.25.6/x64/pkg/tool/linux_amd64/vet v0.12.18/code.go-unsafeptr=false 64/pkg/tool/linu/tmp/go-build3223519424/b186/vet.cfg git (dns block)
  • nonexistent.local
    • Triggering command: /tmp/go-build3536313155/b275/launcher.test /tmp/go-build3536313155/b275/launcher.test -test.testlogfile=/tmp/go-build3536313155/b275/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true -c=4 -nolocalimports -importcfg /tmp/go-build3536313155/b256/importcfg -pack /home/REDACTED/go/pkg/mod/github.com/itchyny/gojq@v0.12.18/builtin.go /home/REDACTED/go/pkg/mod/github.com/itchyny/gojq@v0.12.18/code.go rev-�� go HEAD x_amd64/compile (dns block)
    • Triggering command: /tmp/go-build32475415/b279/launcher.test /tmp/go-build32475415/b279/launcher.test -test.testlogfile=/tmp/go-build32475415/b279/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true -lang=go1.25 01.o .cfg 64/src/net --gdwarf-5 64/pkg/tool/linu/tmp/go-build3223519424/b241/vet.cfg sh -c git status --porcelain --ignore-submodules | head -n 10 64/pkg/tool/linuHEAD ache/go/1.25.6/x64/pkg/tool/linux_amd64/compile -unreachable=fal/usr/bin/runc.original 6313155/b165/ cal/bin/as ache/go/1.25.6/x64/pkg/tool/linux_amd64/compile (dns block)
  • slow.example.com
    • Triggering command: /tmp/go-build3536313155/b275/launcher.test /tmp/go-build3536313155/b275/launcher.test -test.testlogfile=/tmp/go-build3536313155/b275/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true -c=4 -nolocalimports -importcfg /tmp/go-build3536313155/b256/importcfg -pack /home/REDACTED/go/pkg/mod/github.com/itchyny/gojq@v0.12.18/builtin.go /home/REDACTED/go/pkg/mod/github.com/itchyny/gojq@v0.12.18/code.go rev-�� go HEAD x_amd64/compile (dns block)
    • Triggering command: /tmp/go-build32475415/b279/launcher.test /tmp/go-build32475415/b279/launcher.test -test.testlogfile=/tmp/go-build32475415/b279/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true -lang=go1.25 01.o .cfg 64/src/net --gdwarf-5 64/pkg/tool/linu/tmp/go-build3223519424/b241/vet.cfg sh -c git status --porcelain --ignore-submodules | head -n 10 64/pkg/tool/linuHEAD ache/go/1.25.6/x64/pkg/tool/linux_amd64/compile -unreachable=fal/usr/bin/runc.original 6313155/b165/ cal/bin/as ache/go/1.25.6/x64/pkg/tool/linux_amd64/compile (dns block)
  • this-host-does-not-exist-12345.com
    • Triggering command: /tmp/go-build3536313155/b284/mcp.test /tmp/go-build3536313155/b284/mcp.test -test.testlogfile=/tmp/go-build3536313155/b284/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true go ternal/fips140/t--gdwarf2 64/pkg/tool/linu--64 (dns block)
    • Triggering command: `/tmp/go-build32475415/b288/mcp.test /tmp/go-build32475415/b288/mcp.test -test.testlogfile=/tmp/go-build32475415/b288/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true -m Move ToolsLogger helpers to global_helpers.go

Co-authored-by: lgithub.com/github/gh-aw-mcpg/int/usr/bin/runc /home/REDACTED/work/temp/ghcca-node/node/bin/git g.a 6313155/b165/ /opt/hostedtoolcache/uv/0.10.4/x-x git rev-�� --abbrev-ref HEAD x_amd64/vet #^vebash -I tnet/tools/as x_amd64/vet` (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>[refactor] Semantic Function Clustering Analysis — Refactoring Opportunities</issue_title>
<issue_description>## Overview

Static analysis of all 65 non-test Go files in internal/ identified four actionable refactoring opportunities. Two large files exceed 1000 lines and mix distinct concerns (mcp/connection.go, server/unified.go). One package-wide inconsistency exists in the logger/ package where global helpers for ToolsLogger are misplaced. One utility function (registerFlagCompletions) is in the wrong file in cmd/.

Overall code organization is good — packages are purposefully structured, test utilities are separated, and the config, difc, guard, and auth packages are well-organized. The issues below are high-signal, actionable improvements.


Analysis Metadata

Field Value
Total Go files analyzed 65 (non-test, internal/ + root)
Total functions cataloged ~280
Function clusters identified 12
Outliers found 3
Duplicates/inconsistencies detected 2
Analysis date 2026-02-20

Identified Issues

1. 🔴 Outlier: initGlobalToolsLogger / closeGlobalToolsLogger in tools_logger.go

Priority: High — This breaks an established, documented pattern.

Issue: Every other global logger init/close helper pair lives in global_helpers.go:

  • initGlobalFileLogger / closeGlobalFileLoggerglobal_helpers.go
  • initGlobalJSONLLogger / closeGlobalJSONLLoggerglobal_helpers.go
  • initGlobalMarkdownLogger / closeGlobalMarkdownLoggerglobal_helpers.go
  • initGlobalServerFileLogger / closeGlobalServerFileLoggerglobal_helpers.go
  • initGlobalToolsLogger / closeGlobalToolsLoggertools_logger.go ← outlier

The file common.go even explicitly documents this pattern and lists all helpers expected to be in global_helpers.go (lines 164–167), but ToolsLogger's helpers are missing from that list.

Recommendation: Move initGlobalToolsLogger and closeGlobalToolsLogger from tools_logger.go to global_helpers.go, and add them to the documentation list in common.go. Estimated effort: 15 minutes.


2. 🔴 Monolith: mcp/connection.go (1010 lines, 5 distinct concerns)

Priority: High — A single file handles five distinct responsibilities.

Responsibilities currently mixed in connection.go:

Responsibility Functions
SSE/HTTP response parsing parseSSEResponse, parseJSONRPCResponseWithSSE, isHTTPConnectionError
Connection factory / transport negotiation NewConnection, NewHTTPConnection, newMCPClient, newHTTPConnection, trySDKTransport, tryStreamableHTTPTransport, trySSETransport, tryPlainJSONTransport
HTTP request building & execution createJSONRPCRequest, ensureToolCallArguments, setupHTTPRequest, executeHTTPRequest, sendHTTPRequest, initializeHTTPSession
MCP protocol dispatch listTools, callTool, listResources, readResource, listPrompts, getPrompt
Docker env utilities expandDockerEnvArgs, containsEqual

Specific outlier: expandDockerEnvArgs and containsEqual process Docker --env argument formatting. They are conceptually Docker launch utilities, not connection logic. They would fit better in the launcher/ package or in a dedicated internal/mcp/docker.go file.

Recommendation:

  • Extract Docker env utilities to a new internal/mcp/docker_env.go (or move to launcher/)
  • Consider splitting HTTP request infrastructure into internal/mcp/http_request.go
  • Estimated effort: 2–3 hours

3. 🟡 Monolith: server/unified.go (1037 lines, 4 distinct concerns)

Priority: Medium — A single file handles session management, tool registration, tool calling, and server lifecycle.

Responsibilities currently mixed in unified.go:

Responsibility Functions
Session management NewSession, getSessionID, requireSession, getSessionKeys, ensureSessionDirectory
Tool registration registerAllTools, registerAllToolsSequential, registerAllToolsParallel, registerToolsFromBackend, registerSysTools, registerGuard
Tool call dispatch callBackendTool, convertToCallToolResult, newErrorCallToolResult, parseToolArguments
Server lifecycle & status NewUnified, Run, Close, IsShutdown, InitiateShutdown, GetServerIDs, GetServerStatus, GetToolsForBackend, GetToolHandler, GetPayloadSizeThreshold, IsDIFCEnabled
Test helpers RegisterTestTool, SetTestMode, ShouldExit

Recommendation:

  • Extract session management to internal/server/session.go (the Session struct and its lifecycle functions already form a natural unit)
  • Extract tool registration to internal/server/tool_registry.go
  • Estimated effort: 3–4 hours

4. 🟡 Outlier: registerFlagCompletions in cmd/root.go

Priority: Low...

Claude AI and others added 2 commits February 20, 2026 22:20
Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
@Claude Claude AI changed the title [WIP] Refactor semantic function clustering for code organization Refactor: Move logger helpers and flag completions to correct files Feb 20, 2026
@lpcox lpcox marked this pull request as ready for review February 20, 2026 22:25
Copilot AI review requested due to automatic review settings February 20, 2026 22:25
@lpcox lpcox merged commit 9798b8a into main Feb 20, 2026
12 checks passed
@lpcox lpcox deleted the claude/refactor-semantic-function-clustering branch February 20, 2026 22:26
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 addresses organizational inconsistencies in the codebase by moving helper functions to their correct locations according to established package patterns. The refactoring was identified through static analysis and restores consistency without any behavioral changes.

Changes:

  • Moved ToolsLogger init/close helpers from tools_logger.go to global_helpers.go to match all other logger types
  • Updated common.go documentation to include ToolsLogger helpers in the canonical list
  • Moved registerFlagCompletions from root.go to flags.go to centralize flag-related logic

Reviewed changes

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

Show a summary per file
File Description
internal/logger/tools_logger.go Removed initGlobalToolsLogger and closeGlobalToolsLogger helper functions (moved to global_helpers.go)
internal/logger/global_helpers.go Added initGlobalToolsLogger and closeGlobalToolsLogger helper functions to match pattern of all other logger types
internal/logger/common.go Updated documentation to include initGlobalToolsLogger() in the canonical list of global helper functions
internal/cmd/root.go Removed registerFlagCompletions function (moved to flags.go)
internal/cmd/flags.go Added registerFlagCompletions function to centralize all flag-related logic

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

lpcox added a commit that referenced this pull request Feb 20, 2026
- [x] Fix Serena tools configuration in smoke-copilot.md (change nested
languages structure to array format)
- [x] Recompile smoke-copilot workflow to regenerate lock file
- [x] Verify the fix matches working configurations in other workflows
- [x] Store memory about correct Serena tool configuration format

<!-- START COPILOT ORIGINAL PROMPT -->



<details>

<summary>Original prompt</summary>

> 
> ----
> 
> *This section details on the original issue you should resolve*
> 
> <issue_title>Smoke Test: Copilot - 22243401416</issue_title>
> <issue_description>## Smoke Test Results
> 
> | Test | Status |
> |------|--------|
> | GitHub MCP - Last 2 merged PRs | ✅ PR #1195 "Fix
Serena MCP test tool discovery in smoke test workflow", PR
#1186 "Eliminate duplicate code in server HTTP endpoint
registration" |
> | Safe Inputs GH CLI - 2 PRs | ✅ PR #1196 "[WIP]
Refactor semantic function clustering for code organization" |
> | Serena MCP - activate_project + find_symbol | ❌ Tools not available
in this agent context |
> | Make Build | ✅ `make build` succeeded, `awmg` binary produced |
> | Playwright - github.com title | ✅ Title contains "GitHub" |
> | File Writing | ✅
`/tmp/gh-aw/agent/smoke-test-copilot-22243401416.txt` created |
> | Bash Tool | ✅ File read back successfully |
> | Discussion Interaction | ✅ Comment added to discussion
#45 |
> | Agentic Workflows MCP Status | ✅ smoke-copilot: compiled=Yes,
triggers=PR/reaction/schedule |
> 
> ## Overall Status: ⚠️ PARTIAL PASS (8/9 tests passed, Serena tools
unavailable)
> 
> **Run URL**:
https://github.com/github/gh-aw-mcpg/actions/runs/22243401416
> **Timestamp**: 2026-02-20T22:21 UTC
> 
> 
> 
> 
> > 📰 *BREAKING: Report filed by [Smoke
Copilot](https://github.com/github/gh-aw-mcpg/actions/runs/22243401416)*
> > - [x] expires <!-- gh-aw-expires: 2026-02-21T00:23:33.836Z --> on
Feb 21, 2026, 12:23 AM UTC
> 
> <!-- gh-aw-agentic-workflow: Smoke Copilot, engine: copilot, run:
https://github.com/github/gh-aw-mcpg/actions/runs/22243401416 -->
> 
> <!-- gh-aw-workflow-id: smoke-copilot --></issue_description>
> 
> <agent_instructions>Figure out why serena is not
passing</agent_instructions>
> 
> ## Comments on the Issue (you are @claude[agent] in this section)
> 
> <comments>
> </comments>
> 


</details>



<!-- START COPILOT CODING AGENT SUFFIX -->

- Fixes #1198
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.

[refactor] Semantic Function Clustering Analysis — Refactoring Opportunities

3 participants