Skip to content

[refactor] Semantic function clustering: dead helpers, hash-length drift, and minor duplicates #17205

In progress

Description

@github-actions

Overview

Analyzed 217 non-test Go files (about 38.8k lines, 1,383 function declarations) under internal/. No open [refactor] issues existed, so none were closed. Many earlier findings were already fixed (util.IsASCII and util.HashForLog are now shared; see Closes #17105 and #16716). The remaining issues are about 25 production functions with no non-test callers, a hash-length inconsistency in delegation audit logs, a few inline duplicates of util helpers, and three direct env reads that cannot move to envutil without an import cycle.

No code was changed. Dead-code claims come from a repo-wide name search that excludes comments and definitions. Check interface, reflection, and external use before deleting anything.

Full Report

1. Dead or test-only production functions (zero non-test references)

File Functions Note
internal/sys/docker.go CheckDockerAccessible, CheckContainerRuntimeAccessible Callers use the *AccessError variants
internal/sys/container.go extractContainerIDFromCgroup DetectContainerID calls extractContainerIDFromCgroupFiles directly
internal/config/validation_env.go ValidateExecutionEnvironment, ValidateContainerizedEnvironment cmd/root.go uses the ForRuntime variants
internal/config/gateway_env.go GetGatewayPortFromEnv, GetGatewayDomainFromEnv Unused
internal/server/middleware_auth.go authMiddleware applyAuthIfConfiguredWithDelegation is the live path
internal/server/hmac.go computeHMAC Verifier uses hmacMessage + hmacutil.Verify
internal/server/session.go getSessionKeys Debug helper with no caller
internal/proxy/graphql_rewrite.go InjectGuardFields, injectPRGuardFields Live path is injectGuardFields → injectPRGuardFieldsParsed
internal/delegation/store.go markReconciled, IsRecoveryIncomplete MarkReconciledAndSaveState inlines the same logic
internal/githubhttp/visibility.go VerifySinkVisibility Server uses EffectiveSinkVisibility plus its own lookup
internal/difc/reflect.go BuildReflectResponse No importers
internal/difc/evaluator.go NewEvaluator Unused
internal/logger/rpc_logger.go LogRPCMessage, LogRPCRequest, LogRPCResponse *ForSession variants are used
internal/logger/jsonl_logger.go LogRPCMessageJSONL Unused
internal/logger/slog_adapter.go NewSlogLogger NewSlogLoggerWithHandler is used
internal/server/unified_lifecycle.go GetPayloadSizeThreshold, IsDIFCEnabled Unused
internal/auth/header.go ValidateAgentID Unused outside comments
internal/auth/id.go GenerateRandomAgentID Unused, despite the startup-generation comment
internal/httputil/tls.go TLSTrustEnvKeys Test-only
internal/mcp/connection.go NewHTTPConnection Launcher calls NewHTTPConnectionWithCA
internal/server/unified_lifecycle.go RegisterTestTool, SetTestMode Test seams in production code

internal/guard/registry.go RegisterGuardType has no production caller. CreateGuard only resolves noop unless a guard is registered, so confirm the intended registration path before cleaning this up.

2. Duplicates and near-duplicates

  • Hash-length drift (behavior decision needed): internal/delegation/audit.go hashForAudit uses 32 hex characters. control_handler.go, proxy/delegation.go, and server/delegation.go call util.HashForLog(x, 16, ""). The same RunID therefore appears at two lengths across audit and debug logs. Pick one length before unifying, because this changes log output.
  • lowercaseASCII (internal/config/guard_policy_validation.go:268): Not interchangeable with util.NormalizeStringCI, which trims and uses full Unicode ToLower. Move it into util as an explicitly ASCII helper instead of collapsing it.
  • Inline lowercase and trim (internal/oidc/azure.go:62): Duplicates util.NormalizeStringCI. oidc can import util without a cycle.
  • Inline truncation (internal/sys/docker.go:101-102): detail[:N] + "..." duplicates util.TruncateWithSuffix.
  • Hand-rolled map copy (internal/mcp/schema.go copySchemaWithKey): Could use util.ShallowCloneMap plus one assignment.

3. Placement and layering

  • Direct env reads bypass envutil: internal/logger/logger.go:40,181 (DEBUG, DEBUG_COLORS) and internal/sanitize/payload_redaction.go:96 (raw payload opt-in). envutil imports logger and sanitize, so these files cannot import it without a cycle. Consolidating needs a lower-level env helper package first.
  • Test seams in production: RegisterTestTool and SetTestMode live in unified_lifecycle.go. Consider moving them to a test-only file.

Recommendations

  1. Low risk: Delete confirmed dead production wrappers and move test-only helpers into _test.go files. Re-run make agent-finished after each package.
  2. Decision needed: Choose a single hash length for HashForLog in delegation audit output.
  3. Low risk: Move lowercaseASCII to util. Replace the inline truncation in sys/docker.go and the inline normalization in oidc/azure.go with util helpers.
  4. Medium: Add a low-level env helper package to resolve the envutil cycle, then move the three direct env reads.

Analysis metadata

  • Non-test Go files: 217 (about 38.8k lines)
  • Function declarations: 1,383 (1,276 unique names)
  • Dead or test-only functions flagged: about 25
  • Duplicate groups flagged: 5
  • Method: Repo-wide name search, call-site inspection, and import-graph checks
  • Code changes: none

References: §45264742054

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions