Skip to content

Refactor DIFC tool classification placement and extract middleware content-rewrite helpers#7992

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

Refactor DIFC tool classification placement and extract middleware content-rewrite helpers#7992
lpcox merged 3 commits into
mainfrom
copilot/refactor-semantic-function-clustering

Conversation

Copilot AI commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Semantic clustering flagged two structural outliers: isSingularReadTool lived in tool registration code despite being DIFC-only, and jqschema.go still contained a cohesive content-rewrite helper block better separated from jq/schema logic. This PR performs those refactors without changing behavior.

  • DIFC helper co-location

    • Moved isSingularReadTool(toolName string) bool from internal/server/tool_registry.go to internal/server/difc_log.go.
    • Keeps DIFC response/error shaping helpers (buildDIFCSingleItemFilteredError, notices, logging) in one place and removes DIFC-specific logic from tool registration.
  • Middleware rewrite-cluster extraction

    • Created internal/middleware/content_rewrite.go.
    • Moved the envelope/text rewrite helpers out of internal/middleware/jqschema.go:
      • rewriteFilteredTextPayload
      • rewriteEnvelopeTextPayload
      • rewriteFirstContentItem
      • rewriteContentItemText
    • Leaves jqschema.go focused on jq filter compilation/execution and middleware flow.
  • Behavior-preserving refactor

    • Function signatures and call sites remain unchanged; only file placement/organization changed.
// now in internal/server/difc_log.go
func isSingularReadTool(toolName string) bool {
	singular := !strings.HasPrefix(toolName, "list_") && !strings.HasPrefix(toolName, "search_")
	logDifcLog.Printf("isSingularReadTool: toolName=%s, singular=%v", toolName, singular)
	return singular
}

GitHub Advanced Security started work on behalf of lpcox June 23, 2026 18:33 View session
GitHub Advanced Security finished work on behalf of lpcox June 23, 2026 18:34
Copilot AI changed the title [WIP] Refactor semantic function clustering analysis Refactor DIFC tool classification placement and extract middleware content-rewrite helpers Jun 23, 2026
Copilot AI requested a review from lpcox June 23, 2026 18:40
Copilot finished work on behalf of lpcox June 23, 2026 18:40
GitHub Advanced Security started work on behalf of lpcox June 23, 2026 18:45 View session
GitHub Advanced Security finished work on behalf of lpcox June 23, 2026 18:45
@lpcox lpcox marked this pull request as ready for review June 23, 2026 21:47
Copilot AI review requested due to automatic review settings June 23, 2026 21:47

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 DIFC- and middleware-related helpers into more cohesive locations to keep tool registration and jq/schema middleware logic focused, without intended behavior changes.

Changes:

  • Moved isSingularReadTool from internal/server/tool_registry.go into internal/server/difc_log.go alongside other DIFC response/error shaping helpers.
  • Extracted content-rewrite helpers (rewriteFilteredTextPayload, rewriteEnvelopeTextPayload, rewriteFirstContentItem, rewriteContentItemText) out of internal/middleware/jqschema.go into a new internal/middleware/content_rewrite.go.
  • Kept existing signatures and call sites intact to preserve behavior.
Show a summary per file
File Description
internal/server/tool_registry.go Removes DIFC-specific helper to keep tool registration focused.
internal/server/difc_log.go Co-locates DIFC helper (isSingularReadTool) with DIFC logging/error shaping logic.
internal/middleware/jqschema.go Removes extracted content-rewrite helper block to narrow file focus to jq/schema middleware flow.
internal/middleware/content_rewrite.go New file containing the extracted content-rewrite helper functions.

Copilot's findings

Tip

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

  • Files reviewed: 4/4 changed files
  • Comments generated: 1

Comment on lines +1 to +12
package middleware

import (
"encoding/json"

"github.com/github/gh-aw-mcpg/internal/logger"
sdk "github.com/modelcontextprotocol/go-sdk/mcp"
)

func rewriteFilteredTextPayload(result *sdk.CallToolResult, data any, filteredText string) (*sdk.CallToolResult, any) {
logMiddleware.Printf("rewriteFilteredTextPayload: rewriting first text content item, filteredLen=%d", len(filteredText))
rewrittenContent := make([]sdk.Content, 0, len(result.Content))
Add a small delay after initializeDone before closing the SSE stream.
Under -race, goroutine scheduling jitter can cause the SDK to observe
the SSE stream close and propagate the 'exceeded 0 retries' error back
to Connect before the initialize response is fully processed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
GitHub Advanced Security started work on behalf of lpcox June 23, 2026 22:14 View session
GitHub Advanced Security finished work on behalf of lpcox June 23, 2026 22:15
@lpcox lpcox merged commit 5e7581b into main Jun 23, 2026
27 checks passed
@lpcox lpcox deleted the copilot/refactor-semantic-function-clustering branch June 23, 2026 22:17
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.

3 participants