Skip to content

[Code Quality] Refactor ParseWorkflowFile to reduce complexity #11264

@github-actions

Description

@github-actions

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 #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

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions