Skip to content

refactor: centralize outlier functions and deduplicate shared scaffolding - #9654

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

refactor: centralize outlier functions and deduplicate shared scaffolding#9654
lpcox merged 4 commits into
mainfrom
copilot/refactor-semantic-function-analysis

Conversation

Copilot AI commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Addresses findings from semantic function clustering analysis: four functions sat in wrong files, and two logger types duplicated the same three-field state struct.

Changes

internal/auth/id.go (new)

  • Moves GenerateRandomAgentID out of header.go (which only does header parsing/validation) into a dedicated file
  • Moves logAPIKey logger with it; updates namespace test exception list from header.goid.go
  • Removes now-unused fmt and util imports from header.go

internal/logger/fileutil.gojsonFileSink base struct

  • ToolsLogger and ObservedURLDomainsLogger both carried identical logDir string, fileName string, useFallback bool fields
  • Consolidates them into an embedded jsonFileSink struct with a writeJSON(data, perm) helper:
// Before — duplicated in both logger types:
type ToolsLogger struct {
    lockable
    logDir      string
    fileName    string
    data        *ToolsData
    useFallback bool
}

// After — shared base:
type ToolsLogger struct {
    lockable
    jsonFileSink        // logDir, fileName, useFallback + writeJSON()
    data *ToolsData
}

internal/util/toolname.go (new) — ParseServerIDFromToolName

  • parseServerIDFromToolName was a generic string utility embedded in jqschema.go (jq filtering logic)
  • Exported as util.ParseServerIDFromToolName with a dedicated toolname_test.go; jqschema.go and its coverage test updated to call the canonical location

Session ID extraction (no change needed)

  • extractSessionIDFromRequest in server/session.go is already a minimal one-line wrapper over auth.ExtractSessionIDFromHeaders — no duplication to fix

…extract jsonFileSink, centralize ParseServerIDFromToolName
Copilot AI changed the title [WIP] Refactor semantic function clustering analysis based on findings refactor: centralize outlier functions and deduplicate shared scaffolding Jul 19, 2026
Copilot AI requested a review from lpcox July 19, 2026 22:22
Copilot finished work on behalf of lpcox July 19, 2026 22:22
@lpcox
lpcox marked this pull request as ready for review July 19, 2026 23:32
Copilot AI review requested due to automatic review settings July 19, 2026 23:32

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

Centralizes misplaced helpers and deduplicates JSON-backed logger state.

Changes:

  • Moves agent ID generation and tool-name parsing into focused modules.
  • Introduces shared jsonFileSink logger scaffolding.
  • Updates callers, tests, and logger namespace exceptions.
Show a summary per file
File Description
internal/auth/id.go Houses agent ID generation.
internal/auth/header.go Removes ID generation concerns.
internal/util/toolname.go Adds canonical server ID parser.
internal/util/toolname_test.go Tests tool-name parsing.
internal/middleware/jqschema.go Uses the canonical parser.
internal/middleware/jqschema_coverage_test.go Updates parser references.
internal/logger/fileutil.go Adds shared JSON sink state.
internal/logger/tools_logger.go Embeds the shared sink.
internal/logger/tools_logger_test.go Updates logger construction.
internal/logger/observed_url_domains_logger.go Embeds the shared sink.
internal/logger/observed_url_domains_logger_test.go Updates sink-related tests.
internal/logger/logger_namespace_test.go Moves the API-key namespace exception.

Review details

Tip

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

  • Files reviewed: 12/12 changed files
  • Comments generated: 2
  • Review effort level: Medium

Comment on lines +602 to +603
// TestParseServerIDFromToolName exercises all three branches of
// util.ParseServerIDFromToolName (formerly the unexported helper in jqschema.go):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 3ffc166 — removed the duplicate TestParseServerIDFromToolName block and the now-unused util import. The canonical table in internal/util/toolname_test.go covers these cases and TestAuditObservedURLDomains covers the middleware call site.

Comment thread internal/auth/id.go
// GenerateRandomAgentID generates a cryptographically random agent ID.
// Per spec §7.3, the gateway SHOULD generate a random agent ID on startup
// if none is provided. Returns a 32-byte hex-encoded string (64 chars).
func GenerateRandomAgentID() (string, error) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 3ffc166 — moved the ID-generation failure/recovery tests and the errorReader helper into a new internal/auth/id_test.go, and dropped the now-unused crypto/rand and errors imports from header_test.go.

lpcox and others added 2 commits July 19, 2026 16:35
Remove the redundant embedded field `jsonFileSink` from the selector; the
`useFallback` field is promoted through the embedded struct, so
`globalObservedURLDomainsLogger.useFallback` is equivalent and satisfies
staticcheck QF1008.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
- Move GenerateRandomAgentID failure/recovery tests and the errorReader
  helper out of internal/auth/header_test.go into a new id_test.go, so the
  header-focused test file no longer owns ID-generation test code. Drop the
  now-unused crypto/rand and errors imports from header_test.go.
- Remove the duplicate TestParseServerIDFromToolName block (and the unused
  util import) from internal/middleware/jqschema_coverage_test.go; the
  canonical table in internal/util/toolname_test.go already covers these
  cases, and TestAuditObservedURLDomains covers the middleware call site.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
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 — Outliers, Near-Duplicates, and Scattered Helpers

3 participants