Skip to content

Consolidate hashing and environment helpers; cluster functions by responsibility - #16758

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

lpcox merged 2 commits into
mainfrom
copilot/refactor-semantic-function-clustering

Conversation

@lpcox

@lpcox lpcox commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Duplicated hashing and agent-ID fallback logic obscured shared contracts, while several files mixed unrelated responsibilities.

  • Shared helpers

    • Use util.HashForLog for enclave IDs, preserving existing tokens.
    • Route environment validation through the canonical agent-ID fallback.
    • Replace FormatSessionIDForLog with HashIdentifierForLog.
  • Function organization

    • Separate middleware payload storage, URL auditing, and schema inference.
    • Extract WASM caching/discovery and proxy response-header helpers.
    • Group container-runtime parsing and utility functions by purpose.
  • Behavior preserved

    • Retain logger namespaces, token formats, and environment precedence.
    • Keep TLS, validation, and cache helpers whose contracts differ.

// The returned path uses pathPrefix if provided, otherwise returns the actual filesystem path
func savePayload(baseDir, pathPrefix, sessionID, queryID string, payload []byte) (string, error) {
// Create directory structure: {baseDir}/{sessionID}/{queryID}
dir := filepath.Join(baseDir, sessionID, queryID)
@lpcox lpcox changed the title [WIP] Refactor duplicated hashing/env helpers and misplaced functions in internal Consolidate hashing and environment helpers; cluster functions by responsibility Nov 18, 2026
@lpcox
lpcox marked this pull request as ready for review November 18, 2026 15:33
Copilot AI adaptive review requested due to automatic review settings November 18, 2026 15:33
@github-actions

Copy link
Copy Markdown
Contributor

🔒 mcpg Read-Only Stress — default

Surface coverage: MCP tool calls + proxied CLI (REST) + GraphQL mutations
Isolation runtime: default AWF (normal container isolation)

Part Surface Op Result Expected Status
A MCP reads (list_issues/list_pull_requests/get_file_contents/list_commits) data returned ALLOWED ✅
B MCP writes (add_issue_comment/star_repository/issue_write/create_branch/create_or_update_file/create_pull_request) all 6 absent from the 23-tool catalog (no write tools registered) BLOCKED ⚠️
C CLI reads (github CLI proxy: list_issues, get_file_contents) data returned ALLOWED ✅
D CLI REST writes (reaction/star/issue create) gh not authenticated in this job (no GH_TOKEN) — attempts failed before reaching API BLOCKED ⚠️
E CLI GraphQL mutations not attempted (gh unauthenticated, no node ids obtainable) BLOCKED ⚠️

Overall: INCONCLUSIVE

⚠️ Part B: all 6 targeted write tools were absent from the exposed MCP catalog (backend launched with GITHUB_READ_ONLY=1 per gh-aw's tools.github: wrapper), so this run could not independently confirm mcpg's own gateway-level write-blocking layer — only that the backend never registers write tools.

⚠️ Part D/E: gh auth status showed no authenticated host in this job, so REST/GraphQL write attempts failed at the CLI auth layer rather than exercising the token-scope or gateway boundary. No writes leaked on any surface.

🔒 mcpg read-only stress (default AWF runtime) by Read-Only Stress: default runtime

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.

🟢 Approval recommended

No active review issues are recorded for the helper-consolidation refactor.

No open issues 🎉

What changed in this PR

Refactors MCP Gateway’s internal helpers to reduce duplication and group functions by responsibility, addressing the mechanical cleanup in #16716.

Changes:

  • Consolidates enclave-ID hashing, session log tokens, and agent-ID environment fallback logic.
  • Extracts middleware, proxy, WASM, container-runtime, and utility helpers into focused files.
  • Updates tests for token compatibility and environment handling while preserving existing formats and precedence.
File Description
internal/​util/​number.go Houses existing numeric conversion logic.
internal/​util/​hash.go Groups shared log-token hashing helpers.
internal/​util/​format.go Retains string normalization; removes the session-token alias.
internal/​util/​format_tool.go Isolates prefixed tool-name parsing.
internal/​util/​format_time.go Groups time and duration formatting.
internal/​util/​format_test.go Tests session tokens through the canonical helper.
internal/​server/​tool_registry.go Migrates tool and prompt log tokens.
internal/​server/​system_tools.go Migrates system-tool session log tokens.
internal/​server/​session.go Uses canonical hashing for session diagnostics.
internal/​server/​session_util_test.go Updates session-token assertions after alias removal.
internal/​server/​session_auto_init.go Migrates automatic-initialization log tokens.
internal/​server/​sdk_logging.go Migrates SDK request and error log tokens.
internal/​server/​prompt_call.go Migrates prompt-response session logging.
internal/​server/​middleware.go Uses canonical hashing for conversation trace identifiers.
internal/​server/​http_helpers.go Migrates request-body diagnostic session tokens.
internal/​server/​enclave_session.go Migrates enclave-session diagnostic tokens.
internal/​proxy/​response_headers.go Houses response-header and artifact-download helpers.
internal/​proxy/​handler.go Removes extracted response-shaping helpers.
internal/​middleware/​url_audit.go Isolates observed URL-domain auditing.
internal/​middleware/​schema.go Isolates recursive schema inference.
internal/​middleware/​payload_storage.go Houses payload storage and path remapping.
internal/​middleware/​jqschema.go Extracts unrelated helpers and migrates session tokens.
internal/​mcp/​http_transport.go Migrates HTTP transport session log tokens.
internal/​mcp/​connection_transport.go Migrates reconnection session logging.
internal/​launcher/​log_helpers.go Migrates launch-related session log tokens.
internal/​launcher/​launcher.go Migrates session-pool and startup diagnostics.
internal/​launcher/​connection_pool.go Migrates connection-pool session log tokens.
internal/​guard/​wasm_lifecycle.go Removes extracted cache and discovery responsibilities.
internal/​guard/​wasm_discovery.go Groups WASM guard discovery helpers.
internal/​guard/​wasm_cache.go Groups shared compilation-cache management.
internal/​enclavegithub/​capability.go Reuses shared hashing for stable enclave identifiers.
internal/​enclavegithub/​capability_test.go Adds fixed-token and component-boundary assertions.
internal/​config/​validation_env.go Uses the canonical agent-ID environment fallback.
internal/​config/​validation_env_test.go Covers both identifier variables and restores test environments.
internal/​config/​container_runtime.go Houses runtime argument parsing and its option table.
internal/​config/​config_core.go Removes the relocated runtime parsing helpers.

Review effort: Max (adaptive)

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: duplicated hashing/env helpers and misplaced functions in internal/

4 participants