Registry Mirror
Skip to content

Conversation

Copilot AI commented Jan 17, 2026 •

Copy link
Copy Markdown
Contributor

Addresses Priority 1 refactoring from automated semantic function clustering analysis: two nearly identical integer parsing functions with 30+ lines of duplicated type-switching logic.

Changes

  • Enhanced parseIntValue with uint64 overflow checking (previously missing)
  • Refactored ParseIntFromConfig to delegate type conversion to parseIntValue
  • Net impact: -18 lines, single source of truth for numeric type handling

Before/After

Before:

// parseIntValue - handles int, int64, uint64 (no overflow check), float64
func parseIntValue(value any) (int, bool) { /* 20 lines */ }

// ParseIntFromConfig - duplicates same switch logic with overflow check
func ParseIntFromConfig(m map[string]any, key string, log *logger.Logger) int {
    switch v := value.(type) {
    case int: return v
    case int64: return int(v)
    case uint64: /* overflow check */ return int(v)
    case float64: return int(v)
    }
}

After:

// parseIntValue - now with consistent overflow checking
func parseIntValue(value any) (int, bool) { /* handles all types */ }

// ParseIntFromConfig - delegates to parseIntValue
func ParseIntFromConfig(m map[string]any, key string, log *logger.Logger) int {
    if result, ok := parseIntValue(value); ok {
        return result
    }
    return 0
}

All existing tests pass without modification.

Original prompt

This section details on the original issue you should resolve

<issue_title>[refactor] Semantic Function Clustering: Code Organization and Duplicate Detection Analysis</issue_title>
<issue_description># 🔧 Semantic Function Clustering Analysis

Automated analysis of repository: githubnext/gh-aw

Overview

This automated analysis examined 430 non-test Go source files across the repository to identify refactoring opportunities through semantic function clustering, outlier detection, and duplicate code identification. The analysis focuses on improving code organization, reducing duplication, and enhancing maintainability.

Key Findings

  • Package Distribution: 60% of code is in pkg/workflow (227 files), 28% in pkg/cli (136 files)
  • Major Clusters: 21 compiler files, 19 safe-output files, 30+ validation files, 13 AI engine files
  • Duplicates Detected: 15+ wrapper functions, 8 overlapping sanitization functions, duplicate parsing logic
  • Outliers Found: API client code in validation files, XML processing in workflow package, scattered utilities
  • Impact Potential: Eliminate 500+ lines of duplicate code, improve consistency across 30+ files
Full Report

1. Package Organization Analysis

File Distribution by Package

Package Non-Test Files % of Codebase Primary Focus
pkg/workflow 227 60% Workflow compilation, validation, engines
pkg/cli 136 28% CLI commands and orchestration
pkg/parser 26 5% Parsing frontmatter, schemas, imports
pkg/console 10 2% UI components (progress, spinners)
pkg/campaign 13 3% Campaign management
Other packages 18 4% Utilities (string, math, time, etc.)

Major File Clusters Identified

pkg/workflow Clusters

Cluster File Count Lines Organization Quality
compiler_* 21 18,331 ⚠️ Too many files, needs subdirectory
safe_outputs* 19 17,612+ ⚠️ Naming inconsistency, overlap
*validation* 30+ Varied ⚠️ Scattered, no clear grouping
*_engine* 13 10,140+ ✅ Good organization by engine type
expression_* 5 Varied ✅ Clear semantic grouping
runtime_* 6 Varied ✅ Well-organized
frontmatter_* 5 Varied ✅ Clear purpose

pkg/cli Clusters

Cluster File Count Lines Organization Quality
compile_* 14 7,882 ⚠️ Could benefit from subdirectory
logs_* 14 14,623 ⚠️ Large cluster, needs organization
mcp_* 17 14,623 ⚠️ Many files for one feature
update_* 9 Varied ✅ Reasonable organization
run_* 6 Varied ✅ Good grouping

2. Duplicate and Near-Duplicate Functions

2.1 Integer Parsing Duplication

Severity: 🔴 High
Files Affected: 2
Lines Duplicated: ~30

Two nearly identical functions for parsing integers from interface{} types:

Location 1: pkg/workflow/map_helpers.go:35

func parseIntValue(value any) (int, bool) {
    switch v := value.(type) {
    case int: return v, true
    case int64: return int(v), true
    case float64: 
        intVal := int(v)
        if v != float64(intVal) {
            mapHelpersLog.Printf("Float value %.2f truncated to integer %d", v, intVal)
        }
        return intVal, true
    default: return 0, false
    }
}

Location 2: pkg/workflow/config_helpers.go:272

func ParseIntFromConfig(m map[string]any, key string, log *logger.Logger) int {
    if value, exists := m[key]; exists {
        switch v := value.(type) {
        case int: return v
        case int64: return int(v)
        case float64: return int(v)
        case uint64:
            const maxInt = int(^uint(0) >> 1)
            if v > uint64(maxInt) { return 0 }
            return int(v)
        }
    }
    return 0
}

Recommendation: Merge into a single parseIntValueWithOptions function with optional overflow checking and truncation warnings.

2.2 Sanitization Function Proliferation

Severity: 🟡 Medium
Files Affected: 8
Lines Duplicated: ~200+

Eight sanitization functions across three packages with overlapping concerns:

Function Location Purpose
SanitizeName pkg/workflow/strings.go:142 Generic name sanitization
SanitizeWorkflowName pkg/workflow/strings.go:245 Workflow names
SanitizeIdentifier pkg/workflow/workflow_name.go:35 Clean identifiers
SanitizeErrorMessage pkg/stringutil/sanitize.go:52 Error message redaction
SanitizeParameterName pkg/stringutil/sanitize.go:96 Parameter names
SanitizePythonVariableName pkg/stringutil/sanitize.go:130 Python variables
SanitizeToolID pkg/stringutil/sanitize.go:162 Tool ide...

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

- Add uint64 overflow checking to parseIntValue for consistency
- Refactor ParseIntFromConfig to use parseIntValue internally
- Eliminates 24 lines of duplicated type-switching code
- All existing tests pass without modification

Co-authored-by: mnkiefer <8320933+mnkiefer@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor code organization and reduce duplicate functions Consolidate integer parsing logic to eliminate duplication Jan 17, 2026
Copilot AI requested a review from mnkiefer January 17, 2026 17:14
@pelikhan
pelikhan marked this pull request as ready for review January 17, 2026 17:34
@pelikhan
pelikhan merged commit e5a21a2 into main Jan 17, 2026
@pelikhan
pelikhan deleted the copilot/refactor-semantic-function-clustering-one-more-time branch January 17, 2026 17:34
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: Code Organization and Duplicate Detection Analysis

3 participants