[refactor] Semantic function clustering: duplicate isASCII helper and test-only wrapper in production code

オープン 初心者向け
#17,105 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
72/100
issue の種類
リファクタリング
明瞭さ
おおむね明確
活発さ
活発
技術スタック
go
領域
backend

調査の方向性

まず internal/reposelector/reposelector.go(90行目付近)と internal/sanitize/sanitize.go(136行目付近)にある、非公開の isASCII の2つのコピーから始め、次に internal/util の共有パッケージを確認します。internal/util にエクスポートされた IsASCII を1つ追加し、両方の呼び出し箇所をそれを使うように切り替え、非公開のコピーを削除してください。go test ./internal/reposelector/ ./internal/sanitize/ ./internal/util/ が動作を変えずに通れば完了です。

索引モデルが issue の本文から書いたものです。

説明

automated-analysis code-quality refactoring

Overview

Analyzed 215 non-test Go files under internal/ (about 38.7k lines, 1,378 function and method declarations). The prompt's estimate of about 39 files is out of date. No open [refactor] issues existed, so nothing was closed. The package layout is largely sound: random-token helpers already delegate to internal/util, and the agent-filtered server builders share registerFilteredTools.

Two actionable items were confirmed: an exact duplicate isASCII helper, and a production-file wrapper (applyIfConfigured) that only tests call. A JSON Pointer cohesion issue is a minor third item. Several look-alike pairs have different semantics and should be left alone (see below).

Full Report
Method
  • Listed every func declaration in non-test .go files and grouped them by package and file.
  • Scanned for repeated unexported free-function names across files. Repeated method names on different receivers were treated as expected and excluded.
  • Read the bodies of candidate pairs and checked their call sites, including test references.
  • Checked the import graph to confirm where a shared helper could live without an import cycle.

No code was modified. Detection is name- and body-based (grep and direct reads), not AST-based.

Priority 1: Actionable
1. Exact duplicate isASCII
Location Check
internal/reposelector/reposelector.go:90 if s[i] > 0x7f { return false }
internal/sanitize/sanitize.go:136 if s[i] >= 0x80 { return false }

These are semantically identical. Both are used for ASCII-only validation: reposelector in IsCanonicalOwner and IsCanonicalRepositorySelector, and sanitize in SanitizeString.

Recommendation: export one IsASCII from internal/util, which has no internal imports, so neither package creates a cycle. sanitize already imports util. Update both call sites and keep the existing behavior.

2. Test-only wrapper in production code
  • internal/server/middleware_auth.go:17: applyIfConfigured(key, handler, middleware) has no production caller. It is referenced only from middleware_auth_test.go and shutdown_auth_helpers_test.go.
  • It is a thin wrapper over applyIfConfiguredWithLog, which the production applyHMACIfConfigured calls directly.

Recommendation: move applyIfConfigured into a _test.go helper in package server, or have the tests call applyIfConfiguredWithLog directly. This removes dead production surface.

Priority 2: Cohesion
3. JSON Pointer escape logic split across files (internal/difc)
  • escapeJSONPointerToken is in path_collections.go:142.
  • The matching unescape (~1 to /, then ~0 to ~) is inline in splitJSONPointer in path_labels.go:329-330.

Recommendation: move both into one path_pointer.go so the encode and decode rules sit side by side under the one-file-per-feature rule.

Looked similar, but do not consolidate
Pair Why it stays separate
logger.redactTags (logger/jsonl_logger.go:211) vs guard.logSafeTags (guard/pipeline.go:263) Both redact tags for logs, but one uses RedactPrivateSelectors and the other a keyed hash (util.HashForLog). Different policies.
util.NormalizeStringCI (util/format.go:7, ToLower+TrimSpace) vs config.lowercaseASCII (config/guard_policy_validation.go:287) Merging would change behavior for non-ASCII scope strings, because lowercaseASCII leaves them unchanged and does not trim.
Inline io.ReadAll in proxy/handler.go:627 vs httputil.readResponseBody The proxy path reports errors through rejectProxyRequest with a span. readResponseBody has only 3 callers.
8 owner/repo split sites They use Cut, SplitN, and Split with different validation. Unify only with a behavior review.
generateRandomSpanID, generateOpaqueToken, randomSerial Already delegate to util.RandomBytes/RandomHex/RandomBigInt. Healthy.
createAgentFilteredServer, createAgentFilteredUnifiedServer, createDelegationFilteredServer Already share registerFilteredTools. Healthy.
Package Inventory (largest by function count)
Package Files Funcs
internal/server 29 177
internal/config 21 177
internal/difc 13 135
internal/logger 16 131
internal/proxy 12 110
internal/guard 14 95
internal/mcp 12 80

Several packages have no free functions beyond types and constants (for example hmacutil, jqutil, mcpresult, reposelector, version).

Implementation Checklist
  • Add util.IsASCII; switch reposelector and sanitize to it; delete both private copies
  • Move applyIfConfigured out of production code, or have tests call applyIfConfiguredWithLog
  • Consolidate JSON Pointer escape/unescape into internal/difc/path_pointer.go
  • Run go test ./internal/reposelector/ ./internal/sanitize/ ./internal/util/ ./internal/server/ ./internal/difc/
Analysis Metadata
  • Total Go files analyzed: 215 (non-test, under internal/)
  • Total functions cataloged: 1,378
  • Outliers found: 1 (test-only wrapper); 1 cohesion issue
  • Duplicates detected: 1 exact (isASCII); 2 near-duplicates rejected on semantics
  • Detection method: declaration inventory, repeated-name scan, body comparison, call-site and import-graph checks

References: §45069342734

Generated by Semantic Function Refactoring · copilot · routed: haiku55 xhigh · ~12 AIC · ⊞ 7.4K · ◷

主要言語
Go
スター
190
フォーク
61
平均マージ
7時間 35分
マージ済み PR(30日)
215

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

github/gh-aw-mcpg のほかの issue

github/gh-aw-mcpg の issue をすべて見る

似ている issue

Go の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。