Skip to content

Refactor ParseWorkflowFile: extract three helper functions to reduce complexity#11592

Merged
pelikhan merged 5 commits intomainfrom
copilot/refactor-parse-workflow-file
Jan 24, 2026
Merged

Refactor ParseWorkflowFile: extract three helper functions to reduce complexity#11592
pelikhan merged 5 commits intomainfrom
copilot/refactor-parse-workflow-file

Conversation

Copy link
Contributor

Copilot AI commented Jan 24, 2026

ParseWorkflowFile was 807 lines with ~130 conditional branches, making it difficult to reason about and test. This refactors it into smaller, focused functions.

Changes

Extracted three helpers:

  • parseFrontmatterSection() - File I/O, YAML parsing, workflow type detection (127 lines)
  • setupEngineAndImports() - Engine configuration, imports processing, network/firewall setup (215 lines)
  • processToolsAndMarkdown() - Tools merging, MCP servers, runtimes, markdown expansion (254 lines)

Added typed result structs for data passing:

  • frontmatterParseResult
  • engineSetupResult
  • toolsProcessingResult

Main function reduced from 807 → 383 lines (52.5% reduction)

Example

Before:

func (c *Compiler) ParseWorkflowFile(markdownPath string) (*WorkflowData, error) {
    // 807 lines of mixed concerns:
    // - file reading
    // - frontmatter parsing
    // - engine setup
    // - imports processing
    // - tools merging
    // - markdown expansion
    // - workflow data building
}

After:

func (c *Compiler) ParseWorkflowFile(markdownPath string) (*WorkflowData, error) {
    parseResult, err := c.parseFrontmatterSection(markdownPath)
    if err != nil || parseResult.isSharedWorkflow {
        // handle early returns
    }
    
    engineSetup, err := c.setupEngineAndImports(result, cleanPath, content, markdownDir)
    toolsResult, err := c.processToolsAndMarkdown(result, cleanPath, markdownDir, 
        engineSetup.agenticEngine, engineSetup.engineSetting, engineSetup.importsResult)
    
    // Build WorkflowData from results
}

Each helper is independently testable and has a single responsibility.

Original prompt

This section details on the original issue you should resolve

<issue_title>[Code Quality] Refactor ParseWorkflowFile to reduce complexity</issue_title>
<issue_description>## Description

The ParseWorkflowFile function in pkg/workflow/compiler_orchestrator.go is ~800 lines long with 108 if statements, significantly exceeding ideal complexity thresholds. This makes the code difficult to understand, test, and maintain.

Background

Identified during the Daily Compiler Code Quality Report analysis on 2026-01-22:

  • File size: 864 lines (43% over ideal maximum)
  • Function size: ~800 lines (line 19 to ~816)
  • Cyclomatic complexity: Very high (~108 if statements)
  • Quality score: 76/100 (borderline "Good" rating)

Root cause: Single function mixes multiple responsibilities:

  • File I/O and frontmatter parsing
  • Workflow type detection and validation
  • Engine configuration and setup
  • Default application and finalization

Suggested Changes

Extract ParseWorkflowFile into smaller, focused functions:

Proposed Structure

ParseWorkflowFile (main orchestrator - 100-150 lines)
├── parseFrontmatterSection (50-100 lines)
│   └── Read file, parse YAML, extract frontmatter
├── validateWorkflowType (50-100 lines)
│   └── Detect workflow type, validate schema
├── setupEngineConfiguration (100-150 lines)
│   └── Engine setup, tool configuration, MCP servers
├── applyWorkflowDefaults (50-100 lines)
│   └── Default application, field normalization
└── validateAndFinalize (50-100 lines)
    └── Cross-field validation, final checks

Refactoring Approach

Phase 1: Extract logical phases (1 day)

  1. Extract file reading and frontmatter parsing
  2. Extract workflow type detection/validation
  3. Maintain existing test coverage during extraction

Phase 2: Extract engine setup (1 day)
4. Extract engine configuration logic
5. Extract default application logic
6. Add unit tests for each extracted helper

Phase 3: Cleanup and optimize (0.5 days)
7. Remove code duplication
8. Improve error messages
9. Update documentation

Files Affected

  • pkg/workflow/compiler_orchestrator.go (refactored)
  • pkg/workflow/compiler_orchestrator_test.go (enhanced tests)

Success Criteria

  • ParseWorkflowFile reduced to ≤200 lines
  • Each extracted function ≤150 lines
  • Cyclomatic complexity ≤15 per function
  • All existing tests pass
  • Test coverage maintained or improved
  • Quality score improves from 76/100 to ≥85/100

Priority

HIGH - Improves maintainability and testability

Estimated effort: 2-3 days

Source

Extracted from Daily Compiler Code Quality Report discussion githubnext/gh-aw#11176

Analysis quote:

"File Size - HIGH PRIORITY

  • Current: 864 lines (43% over ideal maximum of 600 lines)
  • Impact: Difficult to navigate, understand, and maintain
  • Root Cause: Single function ParseWorkflowFile appears to be extremely long (~800 lines)
  • Function starts at line 19, main logic ends around line 816
  • Contains ~108 if statements (very high cyclomatic complexity)"

AI generated by Discussion Task Miner - Code Quality Improvement Agent

  • expires on Feb 5, 2026, 12:26 PM UTC

Comments on the Issue (you are @copilot in this section)


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

Copilot AI and others added 4 commits January 24, 2026 05:06
- Extract file reading and frontmatter parsing into parseFrontmatterSection()
- Reduces ParseWorkflowFile from ~807 to 737 lines (70 line reduction)
- Creates frontmatterParseResult struct for intermediate data
- All existing tests pass

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
- Extract engine configuration, imports processing, and validation into setupEngineAndImports()
- Reduces ParseWorkflowFile from 737 to 553 lines (184 line reduction, 254 total)
- Creates engineSetupResult struct for intermediate data
- All existing tests pass

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
- Extract tools, runtimes, and markdown processing into processToolsAndMarkdown()
- Reduces ParseWorkflowFile from 553 to 383 lines (170 line reduction, 424 total)
- Creates toolsProcessingResult struct for intermediate data
- All existing tests pass
- Total reduction: 52.5% from original (807 → 383 lines)

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor ParseWorkflowFile to reduce complexity Refactor ParseWorkflowFile: extract three helper functions to reduce complexity Jan 24, 2026
Copilot AI requested a review from pelikhan January 24, 2026 05:21
@pelikhan pelikhan marked this pull request as ready for review January 24, 2026 05:31
@pelikhan pelikhan merged commit 940c830 into main Jan 24, 2026
152 checks passed
@pelikhan pelikhan deleted the copilot/refactor-parse-workflow-file branch January 24, 2026 05: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.

[Code Quality] Refactor ParseWorkflowFile to reduce complexity

2 participants