refactor: semantic function clustering improvements (Finding 1–4) by Copilot · Pull Request #8507 · github/gh-aw-mcpg · GitHub
Skip to content

refactor: semantic function clustering improvements (Finding 1–4)#8507

Merged
lpcox merged 4 commits into
mainfrom
copilot/refactor-semantic-function-clustering
Jul 2, 2026
Merged

refactor: semantic function clustering improvements (Finding 1–4)#8507
lpcox merged 4 commits into
mainfrom
copilot/refactor-semantic-function-clustering

Conversation

Copilot AI commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Addresses all 4 actionable findings from the automated semantic analysis of 146 non-test Go source files (#8499).

Finding 1 — PositiveInteger delegates to TimeoutMinimum

internal/config/validation_rules.go — eliminates duplicated < 1 guard; matches the pattern already used by TimeoutPositive. Error Message and Suggestion text preserved via override:

// Before: independent < 1 guard
if value < 1 { return &ValidationError{...} }

// After: delegates, then customises the returned error
if err := TimeoutMinimum(value, 1, fieldName, jsonPath); err != nil {
    err.Message    = fmt.Sprintf("%s must be a positive integer (>= 1), got %d", ...)
    err.Suggestion = fmt.Sprintf("Use a positive integer (>= 1) for %s", ...)
    return err
}

Finding 2 — internal/strutilinternal/util

Package 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 their loggerFactory var declarations. Tests updated to use factory.setup(...) / factory.onError(...).

Finding 4 — Route arg-key constants in proxy/router.go

Seven constants (argOwner, argRepo, argPullNumber, argIssueNumber, argMethod, argResourceID, argDiscussionNumber) replace ~20 scattered bare string literals across the route table and MatchRoute, providing a single source of truth and typo protection.

GitHub Advanced Security started work on behalf of lpcox July 2, 2026 22:33 View session
GitHub Advanced Security finished work on behalf of lpcox July 2, 2026 22:34
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
Copilot AI changed the title [WIP] Refactor semantic function clustering analysis for code organization refactor: semantic function clustering improvements (Finding 1–4) Jul 2, 2026
@lpcox lpcox marked this pull request as ready for review July 2, 2026 22:43
Copilot AI review requested due to automatic review settings July 2, 2026 22:43
Copilot finished work on behalf of lpcox July 2, 2026 22:43
Copilot AI requested a review from lpcox July 2, 2026 22:43

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

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 PositiveInteger to delegate to TimeoutMinimum while preserving its specific error message/suggestion wording.
  • Renames internal/strutil to internal/util and 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 strutilutil.
AGENTS.md Updates documentation references from strutilutil.

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

Comment on lines +11 to +15
@@ -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")
Comment on lines +25 to +29
@@ -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")
Comment on lines +38 to +42
@@ -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")
Comment on lines +74 to +78
@@ -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")
Comment on lines 370 to +374
// 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)
Comment thread internal/logger/observed_url_domains_logger_test.go Outdated
Comment thread AGENTS.md Outdated
- `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
Comment thread CONTRIBUTING.md Outdated
├── 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)
Comment thread CONTRIBUTING.md Outdated
- **`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)
GitHub Advanced Security started work on behalf of lpcox July 2, 2026 22:49 View session
GitHub Advanced Security finished work on behalf of lpcox July 2, 2026 22:50
@lpcox

lpcox commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

@copilot address review feedback

Copilot AI commented Jul 2, 2026

Copy link
Copy Markdown
Contributor Author

@copilot address review feedback

Done in the latest commit. Changes made:

  • Renamed the three test functions in fallback_handlers_test.go to TestFileLoggerFactory_OnError_UsesFallback, TestMarkdownLoggerFactory_OnError_UsesFallback, and TestToolsLoggerFactory_OnError_UsesFallback.
  • Updated the comment in markdown_logger_close_coverage_test.go to reference markdownLoggerFactory.onError.
  • Renamed TestSetupObservedURLDomainsLogger_WriteToFileFailsTestObservedURLDomainsLoggerFactory_Setup_WriteToFileFails and updated the assertion message to reference observedURLDomainsLoggerFactory.setup.
  • Updated AGENTS.md and both CONTRIBUTING.md entries for internal/util/ to accurately reflect that the package includes string/formatting, randomness, and JSON deep-clone utilities.

GitHub Advanced Security started work on behalf of lpcox July 2, 2026 22:53 View session
Copilot finished work on behalf of lpcox July 2, 2026 22:53
GitHub Advanced Security finished work on behalf of lpcox July 2, 2026 22:54
@lpcox

lpcox commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

@copilot resolve the merge conflicts in this pull request

…ntic-function-clustering

# Conflicts:
#	internal/config/validation_rules.go

Copilot AI commented Jul 2, 2026

Copy link
Copy Markdown
Contributor Author

GitHub Advanced Security started work on behalf of lpcox July 2, 2026 23:08 View session
Copilot finished work on behalf of lpcox July 2, 2026 23:08
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: Code Organization and Refactoring Opportunities

3 participants