refactor: semantic function clustering improvements (Finding 1–4) - #8507
Conversation
Finding 1: Refactor PositiveInteger to delegate to TimeoutMinimum, preserving message/suggestion text by overriding after delegation. Finding 2: Rename internal/strutil package to internal/util to better reflect its general-purpose nature (not purely string utilities). Update all ~26 import sites across production code, tests, and documentation. Finding 3: Inline setup*/handle*Error named functions into logger factory var declarations for file, markdown, jsonl, tools, and observed-url-domains loggers. Update tests to call factory.setup/factory.onError instead. Finding 4: Add string constants (argOwner, argRepo, argPullNumber, etc.) in internal/proxy/router.go, replacing ~20 repeated bare string literals. Closes #8499
There was a problem hiding this comment.
Pull request overview
Refactors several small areas identified by semantic analysis to reduce duplication, improve naming/discoverability, and tighten up routing/logging internals without changing overall gateway behavior.
Changes:
- Refactors
PositiveIntegerto delegate toTimeoutMinimumwhile preserving its specific error message/suggestion wording. - Renames
internal/strutiltointernal/utiland updates imports/usages across the codebase. - Inlines single-use logger factory helpers and centralizes proxy router arg keys as constants.
Show a summary per file
| File | Description |
|---|---|
| internal/config/validation_rules.go | Refactors PositiveInteger to reuse TimeoutMinimum and then override message/suggestion text. |
| internal/config/guard_policy_validation.go | Updates imports/usages for the internal/util rename. |
| internal/util/util.go | Renames package declaration to util. |
| internal/util/util_test.go | Renames package declaration to util in tests. |
| internal/util/truncate.go | Renames package declaration to util. |
| internal/util/truncate_test.go | Renames package declaration to util in tests. |
| internal/util/random.go | Renames package declaration to util. |
| internal/util/random_test.go | Renames package declaration to util in tests. |
| internal/util/format_duration.go | Renames package + updates comment wording to match util. |
| internal/util/format_duration_test.go | Renames package declaration to util in tests. |
| internal/util/collections.go | Renames package declaration to util. |
| internal/util/collections_test.go | Renames package declaration to util in tests. |
| internal/urlutil/domains.go | Updates SortedSetKeys import/usage for internal/util. |
| internal/tracing/provider.go | Updates randomness helper import/usage for internal/util. |
| internal/server/session.go | Updates truncation helper import/usage for internal/util. |
| internal/server/guard_init.go | Updates CopyTrimmedStringIntMap import/usage for internal/util. |
| internal/server/difc_log.go | Updates map extraction helpers import/usage for internal/util. |
| internal/server/copy_tool_call_limits_test.go | Updates CopyTrimmedStringIntMap test usage to internal/util. |
| internal/server/circuit_breaker.go | Updates duration formatting helper import/usage for internal/util. |
| internal/sanitize/sanitize.go | Updates truncation helper import/usage for internal/util. |
| internal/proxy/router.go | Centralizes common arg keys as constants and updates route arg maps; updates helper imports to internal/util. |
| internal/proxy/response_transform.go | Updates DeepCloneJSON import/usage for internal/util. |
| internal/proxy/response_transform_test.go | Updates DeepCloneJSON test usage for internal/util. |
| internal/proxy/response_transform_coverage_test.go | Updates DeepCloneJSON coverage test usage for internal/util. |
| internal/proxy/proxy_test.go | Updates DeepCloneJSON test usage for internal/util. |
| internal/proxy/handler.go | Updates truncation helper import/usage for internal/util. |
| internal/proxy/graphql.go | Updates map extraction + truncation helper imports/usages for internal/util. |
| internal/middleware/jqschema.go | Updates random query ID helper import/usage for internal/util. |
| internal/mcp/http_transport.go | Updates truncation helper import/usage for internal/util. |
| internal/logger/tools_logger.go | Inlines factory setup/onError functions in the logger factory declaration. |
| internal/logger/rpc_format.go | Updates truncation helper import/usage for internal/util. |
| internal/logger/observed_url_domains_logger.go | Inlines factory setup/onError functions; updates sorting helper import/usage for internal/util. |
| internal/logger/observed_url_domains_logger_test.go | Updates tests to call observedURLDomainsLoggerFactory.setup(...) directly. |
| internal/logger/markdown_logger.go | Inlines factory setup/onError functions in the logger factory declaration. |
| internal/logger/markdown_logger_close_coverage_test.go | Updates tests to call markdownLoggerFactory.setup/onError(...) directly. |
| internal/logger/logger.go | Updates duration formatting helper import/usage for internal/util. |
| internal/logger/log_cleanup_test.go | Updates truncation helper import/usage for internal/util. |
| internal/logger/jsonl_logger.go | Inlines factory setup/onError functions in the logger factory declaration. |
| internal/logger/file_logger.go | Inlines factory setup/onError functions in the logger factory declaration. |
| internal/logger/fallback_handlers_test.go | Updates tests to use *.onError(...) factory functions. |
| internal/guard/wasm_payload.go | Updates slice conversion helper import/usage for internal/util. |
| internal/githubhttp/collaborator.go | Updates map extraction helper import/usage for internal/util. |
| internal/difc/sink_server_ids.go | Updates deduplication helper import/usage for internal/util. |
| internal/auth/header.go | Updates random agent ID helper import/usage for internal/util. |
| CONTRIBUTING.md | Updates documentation references from strutil → util. |
| AGENTS.md | Updates documentation references from strutil → util. |
Review details
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 46/46 changed files
- Comments generated: 9
- Review effort level: Low
| @@ -12,7 +12,7 @@ func TestHandleFileLoggerError_UsesFallback(t *testing.T) { | |||
| var logger *FileLogger | |||
| var err error | |||
| _ = captureStdLog(t, func() { | |||
| logger, err = handleFileLoggerError(assert.AnError, "/tmp/logs", "test.log") | |||
| logger, err = fileLoggerFactory.onError(assert.AnError, "/tmp/logs", "test.log") | |||
| @@ -26,7 +26,7 @@ func TestHandleMarkdownLoggerError_UsesFallback(t *testing.T) { | |||
| var logger *MarkdownLogger | |||
| var err error | |||
| _ = captureStdLog(t, func() { | |||
| logger, err = handleMarkdownLoggerError(assert.AnError, "/tmp/logs", "test.md") | |||
| logger, err = markdownLoggerFactory.onError(assert.AnError, "/tmp/logs", "test.md") | |||
| @@ -39,7 +39,7 @@ func TestHandleToolsLoggerError_UsesFallback(t *testing.T) { | |||
| var logger *ToolsLogger | |||
| var err error | |||
| _ = captureStdLog(t, func() { | |||
| logger, err = handleToolsLoggerError(assert.AnError, "/tmp/logs", "tools.json") | |||
| logger, err = toolsLoggerFactory.onError(assert.AnError, "/tmp/logs", "tools.json") | |||
| @@ -75,7 +75,7 @@ func TestMarkdownLogger_Close_FooterWriteError(t *testing.T) { | |||
| // Close() should return nil. | |||
| func TestMarkdownLogger_Close_NilLogFile(t *testing.T) { | |||
| tmpDir := t.TempDir() | |||
| ml, err := handleMarkdownLoggerError(os.ErrNotExist, tmpDir, "test.md") | |||
| ml, err := markdownLoggerFactory.onError(os.ErrNotExist, tmpDir, "test.md") | |||
| // Create a directory at the target file path so atomicWriteFile's Rename fails. | ||
| targetDir := filepath.Join(tmpDir, observedURLDomainsFileName) | ||
| require.NoError(t, os.MkdirAll(targetDir, 0755)) | ||
|
|
||
| l, err := setupObservedURLDomainsLogger(nil, tmpDir, observedURLDomainsFileName) | ||
| l, err := observedURLDomainsLoggerFactory.setup(nil, tmpDir, observedURLDomainsFileName) |
| - `internal/sanitize/` - Sensitive data redaction utilities (`SanitizeString`, `SanitizeJSON`, `TruncateSecret`) for safe log output | ||
| - `internal/server/` - HTTP server (routed/unified modes) | ||
| - `internal/strutil/` - String and formatting utilities | ||
| - `internal/util/` - String and formatting utilities |
| ├── sanitize/ # Sensitive data redaction utilities for logging | ||
| ├── server/ # HTTP server (routed/unified modes) | ||
| ├── strutil/ # String and formatting utility helpers (deduplication, trimming, duration formatting, JSON deep-clone) | ||
| ├── util/ # String and formatting utility helpers (deduplication, trimming, duration formatting, JSON deep-clone) |
| - **`internal/sanitize/`** - Sensitive data redaction utilities (`SanitizeString`, `SanitizeJSON`, `TruncateSecret`) for safe log output | ||
| - **`internal/server/`** - HTTP server with routed and unified modes | ||
| - **`internal/strutil/`** - String and formatting utility helpers (deduplication, trimming, duration formatting, JSON deep-clone) | ||
| - **`internal/util/`** - String and formatting utility helpers (deduplication, trimming, duration formatting, JSON deep-clone) |
|
@copilot address review feedback |
Done in the latest commit. Changes made:
|
|
@copilot resolve the merge conflicts in this pull request |
…ntic-function-clustering # Conflicts: # internal/config/validation_rules.go
Merged |
Addresses all 4 actionable findings from the automated semantic analysis of 146 non-test Go source files (#8499).
Finding 1 —
PositiveIntegerdelegates toTimeoutMinimuminternal/config/validation_rules.go— eliminates duplicated< 1guard; matches the pattern already used byTimeoutPositive. ErrorMessageandSuggestiontext preserved via override:Finding 2 —
internal/strutil→internal/utilPackage renamed to
util: the package contains duration formatting, cryptographic randomness, JSON deep-clone, and collection utilities — none of which are string-specific. All ~26 import sites updated (production, tests, AGENTS.md, CONTRIBUTING.md).Finding 3 — Logger factory functions inlined
file_logger.go,markdown_logger.go,jsonl_logger.go,tools_logger.go,observed_url_domains_logger.go— 10 named functions (setupX/handleXError, 2 per logger × 5 loggers) that were each referenced exactly once as struct field values are inlined as anonymous functions directly in theirloggerFactoryvar declarations. Tests updated to usefactory.setup(...)/factory.onError(...).Finding 4 — Route arg-key constants in
proxy/router.goSeven constants (
argOwner,argRepo,argPullNumber,argIssueNumber,argMethod,argResourceID,argDiscussionNumber) replace ~20 scattered bare string literals across the route table andMatchRoute, providing a single source of truth and typo protection.