Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 31, 2025

Refactoring Plan: Split copilot_engine.go (1235 lines) into 6 focused files

Overview

Refactor the large copilot_engine.go file into smaller, focused files while maintaining 100% test coverage and preserving all existing behavior.

Proposed File Structure

  • Phase 1: Core & Lifecycle (copilot_engine_core.go)

    • Move NewCopilotEngine(), GetDefaultDetectionModel(), GetDeclaredOutputFiles(), GetCleanupStep()
    • Create corresponding copilot_engine_core_test.go
    • Run tests to verify
  • Phase 2: Error Patterns (copilot_engine_errors.go)

    • Move GetErrorPatterns() function
    • Create corresponding copilot_engine_errors_test.go
    • Run tests to verify
  • Phase 3: Installation (copilot_engine_installation.go)

    • Move GetInstallationSteps(), generateAWFInstallationStep()
    • Create corresponding copilot_engine_installation_test.go
    • Run tests to verify
  • Phase 4: MCP Configuration (copilot_engine_mcp.go)

    • Move RenderMCPConfig(), renderCopilotMCPConfig()
    • Create corresponding copilot_engine_mcp_test.go
    • Run tests to verify
  • Phase 5: Log Parsing (copilot_engine_logs.go)

    • Move ParseLogMetrics(), parseCopilotToolCallsWithSequence(), GetLogParserScriptId(), GetLogFileForParsing(), GetFirewallLogsCollectionStep(), GetSquidLogsSteps(), and helper functions
    • Create corresponding copilot_engine_logs_test.go
    • Run tests to verify
  • Phase 6: Execution (copilot_engine_execution.go)

    • Move GetExecutionSteps(), extractAddDirPaths(), computeCopilotToolArguments(), generateCopilotToolArgumentsComment()
    • Create corresponding copilot_engine_execution_test.go
    • Run tests to verify
  • Phase 7: Final Validation

    • Delete original copilot_engine.go and copilot_engine_test.go
    • Run make test-unit to ensure all tests pass
    • Run make fmt and make lint
    • Run make build
    • Run make recompile

Implementation Strategy

  • Work incrementally, one file at a time
  • Maintain the shared copilotLog logger across all files
  • Preserve all existing function signatures and behavior
  • Keep test coverage at ≥80% for each new file
  • Use make test-unit for fast feedback between changes
Original prompt

This section details on the original issue you should resolve

<issue_title>[file-diet] Refactor Large Go File: pkg/workflow/copilot_engine.go</issue_title>
<issue_description>The file pkg/workflow/copilot_engine.go has grown to 1,172 lines, making it difficult to maintain despite having excellent test coverage. This task involves refactoring it into smaller, focused files while preserving the strong test suite.

Current State

  • File: pkg/workflow/copilot_engine.go
  • Size: 1,172 lines
  • Test Coverage: 1,148 lines (98% test-to-source ratio - excellent!)
  • Complexity: Contains 18 functions handling engine initialization, installation, execution, MCP configuration, log parsing, and cleanup operations
  • Codebase Context: 17 files in the repository currently exceed the 800-line threshold

Refactoring Strategy

Proposed File Splits

Based on semantic analysis of function groupings and responsibilities:

1. copilot_engine_core.go

  • Functions:
    • NewCopilotEngine()
    • GetDefaultDetectionModel()
    • GetDeclaredOutputFiles()
    • GetCleanupStep()
  • Responsibility: Core engine initialization and lifecycle management
  • Estimated LOC: ~100 lines

2. copilot_engine_installation.go

  • Functions:
    • GetInstallationSteps()
    • generateAWFInstallationStep()
  • Responsibility: Installation step generation for Copilot CLI, Node.js, sandbox runtimes (SRT/AWF)
  • Estimated LOC: ~250 lines

3. copilot_engine_execution.go

  • Functions:
    • GetExecutionSteps()
    • extractAddDirPaths()
    • computeCopilotToolArguments()
    • generateCopilotToolArgumentsComment()
  • Responsibility: Execution step generation, CLI argument construction, tool permission management
  • Estimated LOC: ~450 lines

4. copilot_engine_mcp.go

  • Functions:
    • RenderMCPConfig()
    • renderCopilotMCPConfig()
  • Responsibility: MCP (Model Context Protocol) configuration rendering for various tools
  • Estimated LOC: ~200 lines

5. copilot_engine_logs.go

  • Functions:
    • ParseLogMetrics()
    • parseCopilotToolCallsWithSequence()
    • GetLogParserScriptId()
    • GetLogFileForParsing()
    • GetFirewallLogsCollectionStep()
    • GetSquidLogsSteps()
  • Responsibility: Log parsing, metrics extraction, firewall log handling
  • Estimated LOC: ~300 lines

6. copilot_engine_errors.go

  • Functions:
    • GetErrorPatterns()
  • Responsibility: Error pattern definitions for log analysis
  • Estimated LOC: ~100 lines

Shared Utilities

No new utility files needed - existing engine_helpers.go (424 lines) already provides shared functionality across engines.

Interface Abstractions

The current Engine interface (defined in engine.go) is well-designed and doesn't require changes. All refactored files will maintain the same public API.

Test Coverage Plan

The existing test file copilot_engine_test.go (1,148 lines) is comprehensive and should be similarly refactored:

1. copilot_engine_core_test.go

  • Test cases: Engine initialization, default model, lifecycle
  • Target coverage: >80%

2. copilot_engine_installation_test.go

  • Test cases: Installation steps for various configurations (global/local, SRT/AWF/standard)
  • Target coverage: >80%

3. copilot_engine_execution_test.go

  • Test cases: Execution step generation, argument construction, tool permissions, custom agents
  • Target coverage: >80%

4. copilot_engine_mcp_test.go

  • Test cases: MCP config rendering for all supported tools (GitHub, Playwright, Serena, etc.)
  • Target coverage: >80%

5. copilot_engine_logs_test.go

  • Test cases: Log parsing, metrics extraction, firewall logs, tool call sequence parsing
  • Target coverage: >80%

6. copilot_engine_errors_test.go

  • Test cases: Error pattern matching and validation
  • Target coverage: >80%

Implementation Guidelines

  1. Preserve Behavior: Ensure all existing functionality works identically
  2. Maintain Exports: Keep public API unchanged (exported functions/types)
  3. Move Tests in Parallel: Refactor test files alongside implementation files
  4. Incremental Changes: Split one module at a time (suggested order: core → errors → logs → mcp → installation → execution)
  5. Run Tests Frequently: Verify make test-unit passes after each split
  6. Update Imports: Ensure all import paths are correct and no circular dependencies
  7. Preserve Logger: Keep the shared copilotLog logger accessible across all files
  8. Document Changes: Add brief comments explaining module boundaries

Acceptance Criteria

  • Original file is split into 6 focused files
  • Each new file is under 500 lines (target: 200-300 lines)
  • All tests pass (make test-unit)
  • Test coverage remains ≥80% for all new files
  • ...

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

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.

[file-diet] Refactor Large Go File: pkg/workflow/copilot_engine.go

3 participants