Overview
Analysis of 114 non-test Go source files across internal/ (765 total functions) surfaced three concrete refactoring opportunities with meaningful impact on code organisation and discoverability. No duplicate function implementations were found — the existing split into focused packages (e.g. strutil, httputil, envutil) is working well. The issues below are about misplaced functions and thin wrappers, not logic duplication.
Finding 1 — Session helpers living in server/http_helpers.go instead of server/session.go
File: internal/server/http_helpers.go (371 lines)
Related file: internal/server/session.go (112 lines)
http_helpers.go has a clear mixed-concern problem. Four functions deal exclusively with session lifecycle but live in the HTTP-helpers file rather than the dedicated session file:
| Function |
What it does |
extractAndValidateSession |
Extracts & validates session ID from the Authorization header |
injectSessionContext |
Stores session ID / backend ID into the request context |
setupSessionCallback |
Combines the two above + logs new connection |
logHTTPRequestBody |
Logs the request body tagged with session/backend IDs |
The remaining eight functions in http_helpers.go are genuine HTTP/middleware concerns (writeErrorResponse, rejectRequest, withResponseLogging, WithOTELTracing, wrapWithMiddleware, WithSDKLogging, logRuntimeError, peekRequestBody) and belong where they are.
Recommendation: Move the four session-related functions to internal/server/session.go.
Estimated effort: ~30 min (mechanical move + verify no circular imports)
Impact: session.go becomes the single place to look for all session lifecycle logic; http_helpers.go shrinks from 371 → ~260 lines.
Finding 2 — buildCircuitBreakers, buildAllowedToolSets, and hasWildcard in server/unified.go instead of their dedicated files
File: internal/server/unified.go (855 lines)
Three constructor/helper functions live in unified.go that logically belong in the files named after the feature they initialise:
| Function |
Current location |
Natural home |
buildCircuitBreakers |
unified.go:332 |
circuit_breaker.go |
buildAllowedToolSets |
unified.go:365 |
tool_registry.go |
hasWildcard |
unified.go:389 |
tool_registry.go |
unified.go is already the largest file in the server package (855 lines). The dedicated files exist precisely to absorb feature-specific logic:
circuit_breaker.go (325 lines) — owns all circuit-breaker types and behaviour
tool_registry.go (450 lines) — owns all tool registration and filtering
Recommendation: Move buildCircuitBreakers to circuit_breaker.go and buildAllowedToolSets + hasWildcard to tool_registry.go.
Estimated effort: ~30 min (mechanical move + verify compile)
Impact: unified.go shrinks by ~60 lines; feature-specific construction logic lives next to feature-specific types.
Finding 3 — writeErrorResponse in server/http_helpers.go is a one-line passthrough wrapper
File: internal/server/http_helpers.go:61
func writeErrorResponse(w http.ResponseWriter, statusCode int, code, message string) {
httputil.WriteErrorResponse(w, statusCode, code, message)
}
This private function adds zero logic — it is a rename of httputil.WriteErrorResponse. All callers within server/ could call httputil.WriteErrorResponse directly (the package is already imported wherever this is used).
Recommendation: Delete writeErrorResponse and replace the ~5 call sites with httputil.WriteErrorResponse directly.
Estimated effort: ~15 min
Impact: Removes one layer of indirection; makes the dependency on httputil explicit at each call site.
Bonus observation — cmd/tracing_helpers.go and cmd/flags_tracing.go could be one file
These two files total only 75 lines and cover a single feature (tracing in the CLI layer):
tracing_helpers.go — 52 lines (registerTracingFlags, initTracingProviderWithFallback, shutdownTracingProviderWithTimeout)
flags_tracing.go — 23 lines (init() registering the flags)
Recommendation: Merge into a single cmd/tracing.go.
Estimated effort: ~10 min
Impact: Eliminates a file; all tracing CLI wiring lives in one place.
Analysis Metadata
| Metric |
Value |
| Go source files analysed |
114 (non-test, internal/) |
| Total functions cataloged |
765 |
| Exact/near duplicates found |
0 |
| Outlier functions found |
7 |
| Thin wrapper functions |
1 |
| Candidate file merges |
1 |
Implementation Checklist
References: §25211549765
Generated by Semantic Function Refactoring · ● 1.1M · ◷
Overview
Analysis of 114 non-test Go source files across
internal/(765 total functions) surfaced three concrete refactoring opportunities with meaningful impact on code organisation and discoverability. No duplicate function implementations were found — the existing split into focused packages (e.g.strutil,httputil,envutil) is working well. The issues below are about misplaced functions and thin wrappers, not logic duplication.Finding 1 — Session helpers living in
server/http_helpers.goinstead ofserver/session.goFile:
internal/server/http_helpers.go(371 lines)Related file:
internal/server/session.go(112 lines)http_helpers.gohas a clear mixed-concern problem. Four functions deal exclusively with session lifecycle but live in the HTTP-helpers file rather than the dedicated session file:extractAndValidateSessionAuthorizationheaderinjectSessionContextsetupSessionCallbacklogHTTPRequestBodyThe remaining eight functions in
http_helpers.goare genuine HTTP/middleware concerns (writeErrorResponse,rejectRequest,withResponseLogging,WithOTELTracing,wrapWithMiddleware,WithSDKLogging,logRuntimeError,peekRequestBody) and belong where they are.Recommendation: Move the four session-related functions to
internal/server/session.go.Estimated effort: ~30 min (mechanical move + verify no circular imports)
Impact:
session.gobecomes the single place to look for all session lifecycle logic;http_helpers.goshrinks from 371 → ~260 lines.Finding 2 —
buildCircuitBreakers,buildAllowedToolSets, andhasWildcardinserver/unified.goinstead of their dedicated filesFile:
internal/server/unified.go(855 lines)Three constructor/helper functions live in
unified.gothat logically belong in the files named after the feature they initialise:buildCircuitBreakersunified.go:332circuit_breaker.gobuildAllowedToolSetsunified.go:365tool_registry.gohasWildcardunified.go:389tool_registry.gounified.gois already the largest file in theserverpackage (855 lines). The dedicated files exist precisely to absorb feature-specific logic:circuit_breaker.go(325 lines) — owns all circuit-breaker types and behaviourtool_registry.go(450 lines) — owns all tool registration and filteringRecommendation: Move
buildCircuitBreakerstocircuit_breaker.goandbuildAllowedToolSets+hasWildcardtotool_registry.go.Estimated effort: ~30 min (mechanical move + verify compile)
Impact:
unified.goshrinks by ~60 lines; feature-specific construction logic lives next to feature-specific types.Finding 3 —
writeErrorResponseinserver/http_helpers.gois a one-line passthrough wrapperFile:
internal/server/http_helpers.go:61This private function adds zero logic — it is a rename of
httputil.WriteErrorResponse. All callers withinserver/could callhttputil.WriteErrorResponsedirectly (the package is already imported wherever this is used).Recommendation: Delete
writeErrorResponseand replace the ~5 call sites withhttputil.WriteErrorResponsedirectly.Estimated effort: ~15 min
Impact: Removes one layer of indirection; makes the dependency on
httputilexplicit at each call site.Bonus observation —
cmd/tracing_helpers.goandcmd/flags_tracing.gocould be one fileThese two files total only 75 lines and cover a single feature (tracing in the CLI layer):
tracing_helpers.go— 52 lines (registerTracingFlags,initTracingProviderWithFallback,shutdownTracingProviderWithTimeout)flags_tracing.go— 23 lines (init()registering the flags)Recommendation: Merge into a single
cmd/tracing.go.Estimated effort: ~10 min
Impact: Eliminates a file; all tracing CLI wiring lives in one place.
Analysis Metadata
internal/)Implementation Checklist
extractAndValidateSession,injectSessionContext,setupSessionCallback,logHTTPRequestBodyfromserver/http_helpers.go→server/session.gobuildCircuitBreakersfromserver/unified.go→server/circuit_breaker.gobuildAllowedToolSets+hasWildcardfromserver/unified.go→server/tool_registry.gowriteErrorResponsewrapper; replace call sites withhttputil.WriteErrorResponsecmd/tracing_helpers.go+cmd/flags_tracing.go→cmd/tracing.gomake agent-finishedafter each change to verify no regressionsReferences: §25211549765