[refactor] Semantic function clustering: duplicated hashing/env helpers and misplaced functions in internal/
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 48/100
調査の方向性
Start with D1 in internal/enclavegithub/capability.go and compare Claims.AgentID with util.HashForLog in internal/util; the report says their outputs are identical. Read the helper and existing tests, then add the suggested fixed-input equivalence test and run the relevant Go tests. Done means the implementation uses the shared helper and the test confirms unchanged output; the report lists several other independent refactors, so agree on one item before expanding scope.
索引モデルが issue の本文から書いたものです。
説明
Overview
Analyzed all 203 non-test Go files under internal/ (about 38k lines, 1,336 top-level function and method declarations). Function names were clustered by naming and purpose, and candidate duplicates were checked against their bodies and call sites. No code was changed.
The clearest wins are:
- D1: a hand-rolled copy of the
util.HashForLogtoken inenclavegithub, whose output can be produced identically by the shared helper. - D2: a duplicated gateway agent-ID env fallback (
MCP_GATEWAY_AGENT_IDvs deprecatedMCP_GATEWAY_API_KEY) inconfig/validation_env.go. - O1-O6: several files that mix unrelated features, notably
middleware/jqschema.go,proxy/handler.go,guard/wasm_lifecycle.go, andutil/format.go.
Full Report
1. Duplicate implementations
| # | Finding | Locations | Recommendation | Priority |
|---|---|---|---|---|
| D1 | Hand-rolled truncated SHA-256 token. util.HashForLog is documented as the "single source of truth" for this pattern. |
internal/enclavegithub/capability.go:42-43 (Claims.AgentID) |
return util.HashForLog(c.Run+"\x00"+c.Invocation, 32, "enclave:"). Output is identical because the composite string always contains \x00, so it is never empty. |
High |
| D2 | Agent-ID env fallback (MCP_GATEWAY_AGENT_ID vs deprecated MCP_GATEWAY_API_KEY) re-implemented with raw os.Getenv. |
internal/config/validation_env.go:184-199 (checkRequiredEnvVars) vs internal/config/gateway_env.go:76 (GetGatewayAgentIDFromEnv) |
Have checkRequiredEnvVars use GetGatewayAgentIDFromEnv() != "" for the agent-ID case so the deprecation rule lives in one place. |
High |
| D3 | Two names for the same log-safe session token. FormatSessionIDForLog is a pure alias of HashIdentifierForLog, which wraps HashForLog. |
internal/util/format.go:22-24, 33-35 |
Keep one exported name and migrate callers. HashIdentifierForLog has 60+ call sites. |
Medium |
| D4 | hashForAudit is a one-line wrapper that fixes (32, ""). |
internal/delegation/audit.go:19-25 |
Keep if the fixed width is a deliberate audit contract; otherwise inline. | Low |
| D5 | Non-empty string-array validation implemented twice, for []string and []interface{}, with different messages. |
internal/config/validation_rules.go:47 (validateNonEmptyStringSlice), internal/config/guard_policy_validation.go:386 (ValidateStringArrayField) |
Extract a shared element check; keep the message wrappers. | Medium |
| D6 | PEM CA loading implemented twice with different strictness. | internal/httputil/tls.go:120-121 (AppendCertsFromPEM) vs internal/mcp/connection.go:308 (loadServerCARoots, manual PEM block loop) |
Move PEM-to-pool loading into httputil and reuse it. Keep system-root behavior in the mcp caller. |
Medium |
| D7 | Three get-or-create primitives: syncutil.MapGetOrCreate (3 callers), Registry.GetOrCreate, TTLCache.GetOrCreate. |
internal/syncutil/cache.go:20, registry.go:44, ttl_cache.go:56 |
Migrate the three MapGetOrCreate callers to Registry/TTLCache, or document why a raw map is needed. |
Low |
2. Outlier functions (wrong file for their purpose)
| # | Function(s) | Current location | Suggested location | Reason |
|---|---|---|---|---|
| O1 | savePayload, auditObservedURLDomains, inferSchema |
internal/middleware/jqschema.go:729, 714, 801 |
Separate payload-storage, URL-audit, and schema files | jqschema.go (837 lines, 18 funcs) mixes jq compilation, filesystem payload storage, URL-domain audit, and schema inference. |
| O2 | containerRuntimeArgs |
internal/config/config_core.go:684 (sole caller at :574) |
internal/config/container_runtime.go |
Docker command-argument parsing. container_runtime.go already exists in the same package. |
| O3 | Cache: newCompilationCache, ConfigureGlobalCompilationCache, getGlobalCompilationCache, CloseGlobalCompilationCache. Discovery: GetWASMGuardsRootDir, FindServerWASMGuardFile, FindGuardFile, findGuardFile. |
internal/guard/wasm_lifecycle.go:40-175 |
Separate files for compilation cache and guard discovery. | wasm_lifecycle.go (24 funcs) mixes three concerns. |
| O4 | Response-shaping helpers: copyResponseHeaders, clientAPIURL, rewriteUpstreamAPIURL, rewriteLinkHeader, injectRetryAfterIfRateLimited, isArtifactDownload |
internal/proxy/handler.go:625-783 |
internal/proxy/response_headers.go |
handler.go (810 lines) mixes request dispatch with upstream response shaping. |
| O5 | Self-signed TLS generation: GenerateSelfSignedTLS, randomSerial, newCertTemplate, writePEM |
internal/proxy/tls.go |
Alongside TLS config in internal/httputil/tls.go |
TLS config is already split between httputil/tls.go and proxy/tls.go. |
| O6 | Time formatting (FormatFutureTime, FormatDuration) and tool-name parsing (ParseServerIDFromToolName) |
internal/util/format.go |
Time: a timeutil package. Tool name: near the MCP tool-name code. |
The file's own header says time formatting should move to timeutil, which does not exist yet. |
| O7 | RedactRequestValueForLog |
internal/mcp/enclave_session.go:40 |
Leave, or move beside sanitize.ShouldRedactPayload. |
Low priority. It bridges request context and sanitize, and the placement is defensible. |
3. Clustering summary
- Hashing/redaction (
util.HashForLog,HashIdentifierForLog,HashForLogIf,sanitize.KeyedDigest,RedactSecret,delegation.hashForAudit): the main duplication cluster.KeyedDigest(HMAC, per-process key) is intentionally different from the plain-SHA-256 helpers, and the code documents why. Do not merge them. - Env access (
envutil,config/gateway_env.go,config/validation_env.go): 35 rawos.Getenv/os.LookupEnvcalls exist outsideenvutil. Most are legitimate. D2 is the one with duplicated logic. - Validation (
config/validation_rules.go,config/guard_policy_validation.go,config/validation_*.go): parallel validators with overlapping shape (D5). - Rate-limit detection: already centralized in
githubhttpand used byserverandproxy. No action. - Logger lock and dispatch helpers (
withLock,withMutexLock,logWithLevel*): already factored. No action. - Context-based enclave helpers (
mcp.IsEnclaveSession,proxy.agentIDFromContext,guard.GetAgentIDFromContext,server.isEnclaveSession): distinct responsibilities, not duplicates.
4. Refactoring recommendations
Priority 1: small, mechanical, behavior-preserving
- D1: Replace the
Claims.AgentIDhash withutil.HashForLog. Add a test asserting the same output for a fixedRun/Invocationpair. - D2: Route
checkRequiredEnvVarsthroughGetGatewayAgentIDFromEnv. Cover bothMCP_GATEWAY_AGENT_IDandMCP_GATEWAY_API_KEYin tests. - O2: Move
containerRuntimeArgstocontainer_runtime.go. Pure move.
Priority 2: file reorganization (same package, no API change)
- O1, O3, O4, O5, O6: split files by feature.
- D3: Collapse the
FormatSessionIDForLog/HashIdentifierForLogalias.
Priority 3: consolidation that needs care
- D5, D6: Share validation and PEM-loading helpers.
- D7: Consolidate get-or-create primitives after confirming lock-ordering assumptions.
5. Implementation checklist
- Priority 1 items, with tests
- Priority 2 file moves (run
make lintafter each package) - Priority 3 consolidations
-
make agent-finishedpasses after each step
Method and limits
- Inventory:
grepof top-levelfuncdeclarations across 203 non-test files (1,336 declarations), with per-file review. - Duplicate candidates were checked by reading bodies and call sites. No code was executed and no files were changed.
- Counts come from grep and may miss usage through interfaces or generated code.
Analysis metadata
- Go files analyzed: 203 (non-test, under
internal/) - Lines: about 38,010
- Function and method declarations: 1,336
- Outlier groups: 7
- Duplicate candidates: 7
- Analysis date: 2026-11-17
References:
Generated by Semantic Function Refactoring · copilot · routed: haiku55 xhigh · ~40 AIC · ⊞ 7.4K · ◷
- 主要言語
- Go
- スター
- 188
- フォーク
- 60
- 平均マージ
- 6時間 50分
- マージ済み PR(30日)
- 177
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile または Docker Compose ファイルあり
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
github/gh-aw-mcpg のほかの issue
-
automated documentation maintenance
難易度 1/5 1〜3時間 初心者へのやさしさ 75/100
github/gh-aw-mcpg#16717 ·
メンテナーはふだん 1 日以内に返信
-
refactor rust-guard
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
github/gh-aw-mcpg#16697 ·
メンテナーはふだん 1 日以内に返信
-
automated-analysis code-quality
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
github/gh-aw-mcpg#16691 ·
メンテナーはふだん 1 日以内に返信
-
go-fan module-review
難易度 2/5 1〜3時間 初心者へのやさしさ 73/100
github/gh-aw-mcpg#16659 ·
メンテナーはふだん 1 日以内に返信
-
agentic-workflows
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
github/gh-aw-mcpg#16714 ·
メンテナーはふだん 1 日以内に返信
github/gh-aw-mcpg の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 77/100
メンテナーはふだん 1 日以内に返信
-
Docs: Recording rules pages don't say what happens to a recorded metric when its rule is deletedオープンinternal
難易度 1/5 1〜3時間 初心者へのやさしさ 84/100
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
jesseduffield/lazygit#6229 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
prometheus/prometheus#20482 ·
メンテナーはふだん 2 日以内に返信
-
Add iterate GmbH domains to a dedicated list and include it in company and developer categoriesオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
v2fly/domain-list-community#4307 ·
メンテナーはふだん 1 日以内に返信