Consolidate duplicate sanitization and auth parsing logic #333
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Semantic analysis identified duplicate function implementations across authentication, sanitization, and logging packages—3 sanitization functions with 80-90% overlap and 2 auth parsers sharing 70% of logic.
Changes
Sanitization consolidation (
internal/logger/sanitize)TruncateSecret(string)andTruncateSecretMap(map[string]string)for prefix truncation (first 4 chars + "...")auth/header.goandlauncher/launcher.goAuth parsing consolidation (
internal/auth)ExtractAgentID(authHeader)convenience wrapper aroundParseAuthHeader()guard.ExtractAgentIDFromAuthHeader()to delegate to auth packageReduced rpc_logger.go complexity
rpc_logger.go(108 lines): coordination and public APIrpc_formatter.go(129 lines): text/markdown formattingrpc_helpers.go(82 lines): payload processing utilitiesDocumentation
auth,guard,sanitize, and logger RPC modulesglobal_patterns.go→global_helpers.goto reflect actual purposeExample
Before (duplicated across 3 files):
After (centralized):
All changes are internal refactoring with complete backward compatibility.
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>## Executive Summary
Analysis of 44 non-test Go files in
internal/identified 8 high-priority refactoring opportunities across authentication, sanitization, logging, and validation domains. The most significant findings include duplicated sanitization logic (3 implementations), auth header parsing duplication (2 packages), and complexity in the logger package (10 files, 65 unexported helpers).Estimated Total Effort: 4-6 hours of focused refactoring across 3 phases.
Files Analyzed (30 files)
🔴 Critical Findings: Duplicate Functions
1. Sanitization Logic Duplication (HIGH PRIORITY)
Problem: 3 different implementations of "first 4 chars + ..." sanitization across packages.
Duplicate Functions:
Similarity: 80-90% functional overlap (prefix truncation for logging safety)
Impact:
Recommendation:
internal/logger/sanitizepackageauth/header.go,launcher/launcher.go,rpc_logger.goEffort: 1 hour (30 min refactor + 30 min test updates)
2. Auth Header Parsing Duplication (MEDIUM PRIORITY)
Problem: Auth header parsing logic exists in 2 packages with 70% code overlap.
Duplicate Functions:
Code Overlap: 70% (both parse Bearer/Agent prefixes)
Differences:
ParseAuthHeader: Full error handling, logging, returns tupleExtractAgentIDFromAuthHeader: Silent fallback to "default", no errorsImpact:
server/auth.gouses neither (direct header comparison)Recommendation:
internal/auththe single source of truth// auth.ExtractAgentID(header string) string // Wraps ParseAuthHeader, no errorguard/context.goto callauth.ExtractAgentID()guard.ExtractAgentIDFromAuthHeaderwith commentEffort: 45 minutes (code + tests + validation)
3. Error Message Extraction Confusion (LOW PRIORITY)
Problem: File naming suggests duplication but files serve different purposes.
Files:
internal/logger/error_formatting.go(47 lines) - Single functionExtractErrorMessageinternal/logger/global_patterns.go(83 lines) - Global state helpers (6 functions)Analysis:
error_formatting.go: Log line cleanup (removes timestamps/levels)global_patterns.go: Mutex-wrapped global logger init/close helpersglobal_patterns.goshould be renamed toglobal_helpers.goorglobal_state.goRecommendation:
global_patterns.go→global_helpers.go(better reflects purpose)💡 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.