Skip to content

Refactor internal helper placement and naming to resolve semantic clustering outliers#7170

Merged
lpcox merged 3 commits into
mainfrom
copilot/refactor-semantic-function-clustering
Jun 8, 2026
Merged

Refactor internal helper placement and naming to resolve semantic clustering outliers#7170
lpcox merged 3 commits into
mainfrom
copilot/refactor-semantic-function-clustering

Conversation

Copilot AI commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

This PR resolves the semantic clustering findings across internal/ by relocating outlier functions/types to their owning domains and removing duplicate validation logic between config and guard. It also resolves an ambiguous transport-vs-server session method name collision.

  • Config expansion logic consolidation

    • Moved expandMapInPlace and expandTracingVariables into internal/config/expand.go so ${VAR} expansion behavior lives in one place.
    • Removed duplicated definitions from config_stdin.go and config_tracing.go.
  • Guard/config validator consolidation

    • Added canonical reusable validators in internal/config/guard_policy_validation.go:
      • ValidateStringArrayField
      • IsValidAllowOnlyReposValue
    • Updated internal/guard/wasm_validate.go to delegate to config validators instead of reimplementing equivalent checks.
  • WASM guard discovery ownership moved to guard

    • Moved filesystem discovery helpers into internal/guard/wasm.go:
      • GetWASMGuardsRootDir
      • FindServerWASMGuardFile
    • Updated internal/server/guard_init.go and related tests to call guard-owned helpers.
  • DIFC filtered-item map helper outliers removed

    • Inlined author/number extraction directly in buildFilteredItemLogEntry (internal/server/difc_log.go) per issue recommendation.
    • Removed standalone helper test file and added focused edge-case coverage in difc_log_test.go.
  • Logger cleanup API scope

    • Unexported ExtractErrorMessage to extractErrorMessage (internal/logger/log_cleanup.go) since usage is internal/test-only.
  • MCP tag snapshot type placement

    • Moved AgentTagsSnapshot and GetAgentTagsSnapshotFromContext from connection.go to connection_logging.go to colocate declarations with actual logging consumers.
  • Label-agent helper placement

    • Moved emptyAgentLabelsResult and ApplyLabelAgentResult from internal/guard/guard.go to internal/guard/label_agent.go alongside RunLabelAgent.
  • Session ID truncation ownership

    • Moved session-specific truncation behavior from internal/strutil into internal/server/session_util.go as truncateSessionID.
    • Updated server call sites; removed TruncateSessionID from strutil and moved tests to server package.
  • Naming collision removal

    • Renamed (*mcp.Connection).requireSession() to requireSDKSession() to distinguish SDK transport session validation from server.requireSession(ctx) lifecycle behavior.
// before
if err := c.requireSession(); err != nil { ... }

// after
if err := c.requireSDKSession(); err != nil { ... }

Copilot AI changed the title [WIP] Refactor semantic function clustering to fix outliers and duplicates Refactor internal helper placement and naming to resolve semantic clustering outliers Jun 7, 2026
Copilot finished work on behalf of lpcox June 7, 2026 22:46
Copilot AI requested a review from lpcox June 7, 2026 22:46
@lpcox lpcox marked this pull request as ready for review June 8, 2026 13:57
Copilot AI review requested due to automatic review settings June 8, 2026 13:57

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 several internal helpers to better align them with their owning domains (server/guard/config/logger/mcp), reduces duplicated validation logic, and resolves a session-method naming collision in the MCP connection layer.

Changes:

  • Moved session-ID truncation logic from internal/strutil into internal/server and updated all server logging call sites and tests.
  • Moved WASM guard discovery helpers into internal/guard and updated server initialization/tests to use guard-owned discovery.
  • Consolidated duplicated validation and expansion helpers (config/guard), simplified DIFC filtered-item metadata extraction, and renamed MCP SDK session validation helper to requireSDKSession.
Show a summary per file
File Description
internal/strutil/truncate.go Removes server-specific TruncateSessionID from shared string utilities.
internal/strutil/truncate_test.go Removes TruncateSessionID tests now that behavior is owned by server.
internal/server/unified_wasm_guards_dir_test.go Updates WASM guard discovery tests to exercise guard.FindServerWASMGuardFile.
internal/server/session.go Switches session logging to use truncateSessionID in server package.
internal/server/session_util.go Adds server-owned truncateSessionID helper for safe session logging.
internal/server/session_util_test.go Adds focused unit tests for server-owned session truncation.
internal/server/session_auto_init.go Updates auto-init logging to use server-owned session truncation.
internal/server/routed.go Updates cache/log output to use server-owned session truncation.
internal/server/middleware.go Updates OTEL/logging code paths to use server-owned session truncation.
internal/server/http_helpers.go Updates request-body logging to use server-owned session truncation.
internal/server/guard_init.go Delegates per-server WASM guard discovery/root-dir lookup to internal/guard.
internal/server/difc_log.go Inlines author/number extraction into buildFilteredItemLogEntry and removes helpers.
internal/server/difc_log_test.go Adds edge-case coverage for author-login and number extraction in the inlined logic.
internal/server/difc_log_helpers_test.go Removes tests for helper functions that were deleted.
internal/mcp/sdk_method_dispatch_test.go Updates comments to reflect requireSDKSession rename.
internal/mcp/pagination.go Renames SDK session validation calls to requireSDKSession.
internal/mcp/pagination_test.go Updates test comments to match requireSDKSession rename.
internal/mcp/connection.go Renames requireSession to requireSDKSession and moves tag snapshot type out to logging file.
internal/mcp/connection_test.go Updates tests to call requireSDKSession.
internal/mcp/connection_logging.go Relocates AgentTagsSnapshot + context extraction helper alongside logging consumers.
internal/logger/rpc_helpers_test.go Updates tests/benchmarks to call unexported extractErrorMessage.
internal/logger/log_cleanup.go Unexports ExtractErrorMessage to extractErrorMessage.
internal/guard/wasm.go Adds guard-owned WASM discovery helpers (GetWASMGuardsRootDir, FindServerWASMGuardFile).
internal/guard/wasm_validate.go Delegates string-array and allow-only repo validation to canonical config validators.
internal/guard/label_agent.go Moves label-agent helper functions into label-agent file alongside RunLabelAgent.
internal/guard/guard.go Removes label-agent helpers now relocated to label_agent.go.
internal/config/guard_policy_validation.go Adds canonical reusable validators (ValidateStringArrayField, IsValidAllowOnlyReposValue).
internal/config/expand.go Centralizes expandMapInPlace and expandTracingVariables into a shared expansion module.
internal/config/config_tracing.go Removes duplicated tracing-variable expansion helper.
internal/config/config_stdin.go Removes duplicated map expansion helper; continues calling shared helper.

Copilot's findings

Tip

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

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

@lpcox lpcox merged commit 0c77ba4 into main Jun 8, 2026
29 checks passed
@lpcox lpcox deleted the copilot/refactor-semantic-function-clustering branch June 8, 2026 14:34
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: 9 outliers and 2 duplicate clusters in internal/

3 participants