Skip to content

refactor: eliminate 4 cross-package duplicate-code patterns (server/proxy)#7209

Merged
lpcox merged 4 commits into
mainfrom
copilot/duplicate-code-cross-package-structural-duplicatio
Jun 8, 2026
Merged

refactor: eliminate 4 cross-package duplicate-code patterns (server/proxy)#7209
lpcox merged 4 commits into
mainfrom
copilot/duplicate-code-cross-package-structural-duplicatio

Conversation

Copilot AI commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Four structural duplication patterns across internal/server and internal/proxy were causing diverging implementations that silently drift as the codebase evolves.

Pattern 1 — newSDKServer helper (internal/server/http_server.go)

Extracted the repeated sdk.NewServer(&sdk.Implementation{...}, &sdk.ServerOptions{...}) block into a shared helper used by both routed.go and unified.go. Removed now-unused version imports from both files.

func newSDKServer(name string, log *logger.Logger) *sdk.Server {
    return sdk.NewServer(&sdk.Implementation{
        Name:    name,
        Version: version.Get(),
    }, &sdk.ServerOptions{
        Logger: logger.NewSlogLoggerWithHandler(log),
    })
}

Pattern 2 — Standardized DIFC parse-error warning (internal/proxy/proxy.go)

Replaced logProxy.Printf("WARNING: ...") with logger.LogWarn("startup", ...) to match the logging style already used in server/unified.go.

Pattern 3 — Shared health/reflect response helpers (internal/httputil/endpoint_helpers.go)

Added WriteSimpleHealthResponse(w) and WriteReflectResponse(w, difcComponents) to httputil — the package both server and proxy already import. The proxy's inline /health and /reflect branches now call these helpers; server/reflect.go does too. This ensures both packages route through identical code as difc.BuildReflectResponse evolves.

Pattern 4 — toResultOrWriteEmpty method (internal/proxy/handler.go)

Extracted the two identical Phase-5 ToResult failure blocks into a single method on proxyHandler:

func (h *proxyHandler) toResultOrWriteEmpty(w, resp, responseData, labeled) (interface{}, bool)

Returns (result, true) on success; logs, writes an empty-shaped response, and returns (nil, false) on error so the caller can return cleanly.

Copilot AI added 2 commits June 8, 2026 14:09
…kages

Pattern 1: Extract newSDKServer helper in internal/server/http_server.go,
replacing the identical sdk.NewServer construction in routed.go and unified.go.

Pattern 2: Standardize DIFC parse-error warning in proxy/proxy.go to use
logger.LogWarn (matching the style already used in unified.go).

Pattern 3: Add WriteSimpleHealthResponse and WriteReflectResponse helpers to
internal/httputil/endpoint_helpers.go; update server/reflect.go and the
inline health/reflect checks in proxy/handler.go to use them, ensuring both
packages share the same response construction path.

Pattern 4: Extract writeToResultOrEmpty method on proxyHandler in
proxy/handler.go, replacing the two identical ToResult-failure blocks.
- Rename writeToResultOrEmpty -> toResultOrWriteEmpty (the method returns
  the result on success; it only writes on error, so the name now reads
  in execution order: try ToResult, or write-empty on failure)
- Rename components -> difcComponents in httputil.WriteReflectResponse to
  match call-site naming conventions
Copilot AI changed the title [WIP] Fix duplicate code pattern in server and proxy packages refactor: eliminate 4 cross-package duplicate-code patterns (server/proxy) Jun 8, 2026
Copilot finished work on behalf of lpcox June 8, 2026 14:11
Copilot AI requested a review from lpcox June 8, 2026 14:11
@lpcox

lpcox commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

@copilot resolve the merge conflicts in this pull request

Copilot AI commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

@copilot resolve the merge conflicts in this pull request

Merge conflicts resolved in commit 8e584792. The only conflict was a single import line in internal/server/routed.go: our branch removed version (moved to http_server.go via newSDKServer) while main's #7170 refactor removed strutil (moved to session_util.go via a local truncateSessionID wrapper). The correct resolution is to drop both imports — the merged file uses neither directly. All tests pass.

Copilot finished work on behalf of lpcox June 8, 2026 14:45
@lpcox lpcox marked this pull request as ready for review June 8, 2026 15:45
Copilot AI review requested due to automatic review settings June 8, 2026 15:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 refactors repeated logic across internal/server and internal/proxy to reduce drift between “unified” vs “routed” server modes and proxy endpoints, by centralizing common server construction, endpoint responses, and DIFC result handling.

Changes:

  • Consolidates MCP SDK server creation into a shared newSDKServer helper used by both unified and routed server modes.
  • Standardizes proxy DIFC parse-error warnings via logger.LogWarn(...) to match existing startup logging patterns.
  • Extracts shared /health and /reflect response helpers into internal/httputil and reuses them from both server and proxy.
  • Removes duplicated Phase-5 ToResult error handling by introducing toResultOrWriteEmpty in the proxy handler.
Show a summary per file
File Description
internal/server/unified.go Switches MCP SDK server construction to the shared newSDKServer helper.
internal/server/routed.go Switches routed per-backend MCP SDK server construction to newSDKServer.
internal/server/reflect.go Reuses shared httputil.WriteReflectResponse for consistent reflect output.
internal/server/http_server.go Adds newSDKServer helper and centralizes version/logger wiring for MCP SDK server creation.
internal/proxy/proxy.go Uses logger.LogWarn for DIFC parse warnings to align with unified server logging style.
internal/proxy/handler.go Extracts duplicated ToResult failure handling; uses shared health/reflect response helpers.
internal/httputil/endpoint_helpers.go Adds shared health and reflect response helpers used by both server and proxy.

Copilot's findings

Tip

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

  • Files reviewed: 7/7 changed files
  • Comments generated: 0

@lpcox lpcox merged commit e97979c into main Jun 8, 2026
17 checks passed
@lpcox lpcox deleted the copilot/duplicate-code-cross-package-structural-duplicatio branch June 8, 2026 15:49
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 Pattern: Cross-Package Structural Duplication (SDK init, DIFC init, health endpoints)

3 participants