Skip to content

[file-diet] File Diet: refactor pkg/workflow/workflow_builder.go (1084 lines) #54540

Description

@github-actions

Overview

The file pkg/workflow/workflow_builder.go has grown to 1084 lines, exceeding the healthy threshold of 800 lines. This task involves refactoring it into smaller, focused files with improved test coverage.

Current State

  • File: pkg/workflow/workflow_builder.go
  • Size: 1084 lines
  • Test Coverage: No dedicated workflow_builder_test.go file exists. Some of its 32 functions are exercised indirectly by other test files (e.g. github_app_owner_derivation_test.go and others), but there is no focused unit-test file for this module.
  • Complexity: Mixed responsibilities — the file combines initial workflow-data construction, YAML section extraction, model cost/policy overlay merging, concurrency/dispatch extraction, and pre/post step merging in a single file.
Full File Analysis

Detailed Breakdown

32 top-level functions identified, grouped by responsibility:

1. Workflow data initialization

  • buildInitialWorkflowData (line 19) — large orchestrator method on *Compiler

2. Config extraction helpers

  • extractEnclavesConfig (220)
  • extractLSPConfig (236)
  • extractFrontmatterSkills (264)
  • extractFrontmatterPlugins (283)
  • mergeFrontmatterPlugins (301)
  • extractFrontmatterSkillReferences (306)

3. Model cost/policy overlay merging (largest cohesive cluster, lines ~322-620)

  • extractMainModelCostsOverlay (322)
  • mergeModelCostOverlays (353)
  • mergeModelCostOverlayPair (374)
  • extractMainModelPolicyOverlay (426)
  • toFloat64 (452)
  • resolveDefaultAiCreditsPricing (477)
  • extractDefaultAiCreditsPricingFromModels (486)
  • extractDefaultAiCreditsPricingFromModelsMap (494)
  • extractDefaultAiCreditsPricingFromObject (509)
  • mergeModelPolicyOverlays (536)
  • filterAllowedModelConflictsWithSet (575)
  • modelConflictsWithDisallowedPolicy (589)
  • modelPolicyPatternMatches (606)

4. Import/env merging utilities

  • resolveInlinedImports (623)
  • mergeExcludedEnvVarNames (630)

5. YAML section / concurrency / dispatch extraction

  • extractYAMLSections (656) — method on *Compiler
  • extractConcurrencyJobDiscriminator (691)
  • extractConcurrencySection (714) — method on *Compiler
  • extractDispatchItemNumber (754)

6. Step merging (pre/post/pre-agent) (largest LOC cluster, lines 784-1069)

  • processAndMergeSteps (784)
  • processAndMergePreSteps (883)
  • processAndMergePreAgentSteps (945)
  • processAndMergePostSteps (1004)
  • frontmatterHasTrigger (1069)

This gives 5 clear functional domains that map naturally onto separate files, following the repository's existing "domain file" pattern used elsewhere in pkg/workflow/.

Refactoring Strategy

Proposed File Splits

Based on the semantic grouping above, split the file into the following modules:

  1. workflow_builder_model_overlays.go

    • Functions: extractMainModelCostsOverlay, mergeModelCostOverlays, mergeModelCostOverlayPair, extractMainModelPolicyOverlay, toFloat64, resolveDefaultAiCreditsPricing, extractDefaultAiCreditsPricingFromModels, extractDefaultAiCreditsPricingFromModelsMap, extractDefaultAiCreditsPricingFromObject, mergeModelPolicyOverlays, filterAllowedModelConflictsWithSet, modelConflictsWithDisallowedPolicy, modelPolicyPatternMatches
    • Responsibility: Model cost and policy overlay extraction/merging (AI credits pricing, allowed/disallowed model policy resolution)
    • Estimated LOC: ~300
  2. workflow_builder_steps.go

    • Functions: processAndMergeSteps, processAndMergePreSteps, processAndMergePreAgentSteps, processAndMergePostSteps, frontmatterHasTrigger
    • Responsibility: Merging pre/pre-agent/post step lists from frontmatter and imports
    • Estimated LOC: ~290
  3. workflow_builder_frontmatter_extract.go

    • Functions: extractEnclavesConfig, extractLSPConfig, extractFrontmatterSkills, extractFrontmatterPlugins, mergeFrontmatterPlugins, extractFrontmatterSkillReferences, extractYAMLSections, extractConcurrencyJobDiscriminator, extractConcurrencySection, extractDispatchItemNumber, resolveInlinedImports, mergeExcludedEnvVarNames
    • Responsibility: Small, focused frontmatter field extraction helpers (enclaves, LSP, skills, plugins, concurrency, dispatch, imports)
    • Estimated LOC: ~330
  4. workflow_builder.go (remaining)

    • Functions: buildInitialWorkflowData (core orchestrator)
    • Responsibility: Top-level workflow-data construction, calling into the extracted helper files
    • Estimated LOC: ~210

Shared Utilities

No additional cross-cutting utility file is required; toFloat64 stays with the model overlay group since it is only used there.

Interface Abstractions

Not needed — these are stateless helper functions and methods on *Compiler; no new interfaces are warranted.

Test Coverage Plan

Add comprehensive tests for each new file:

  1. workflow_builder_model_overlays_test.go

    • Test cases: cost overlay merge precedence (main overrides imports), policy overlay allow/disallow conflict resolution, AI credits pricing extraction from various frontmatter shapes, toFloat64 edge cases (int, float, string, invalid)
    • Target coverage: >80%
  2. workflow_builder_steps_test.go

    • Test cases: merging pre/post/pre-agent steps from multiple imports, ordering guarantees, frontmatterHasTrigger detection for string/array/map trigger shapes
    • Target coverage: >80%
  3. workflow_builder_frontmatter_extract_test.go

    • Test cases: enclaves config extraction defaults, LSP server config parsing, skill/plugin extraction and merging, concurrency discriminator/section extraction, dispatch item-number flag detection, inlined-imports resolution, excluded env var merging
    • Target coverage: >80%
  4. workflow_builder_test.go (new, currently missing)

    • Test cases: buildInitialWorkflowData end-to-end construction with representative frontmatter fixtures
    • 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. Add Tests First: Write tests for each new file before refactoring
  4. Incremental Changes: Split one module at a time
  5. Run Tests Frequently: Verify make test-unit passes after each split
  6. Update Imports: Ensure all import paths are correct
  7. Document Changes: Add comments explaining module boundaries

Acceptance Criteria

  • Original file is split into 4 focused files
  • Each new file is under 500 lines
  • All tests pass (make test-unit)
  • Test coverage is ≥80% for new files
  • No breaking changes to public API
  • Code passes linting (make lint)
  • Build succeeds (make build)
Additional Context
  • Repository Guidelines: Follow patterns in .github/agents/developer.instructions.agent.md
  • Code Organization: Prefer many small files grouped by functionality
  • Testing: Match existing test patterns in pkg/workflow/*_test.go

Priority: Medium
Effort: Medium — 32 functions across 5 domains, no existing dedicated test file to preserve/migrate
Expected Impact: Improved maintainability, easier testing, reduced complexity

Generated by 🧹 Daily File Diet · auto · 59.4 AIC · ⌖ 6.29 AIC · ⊞ 10.1K ·

  • expires on Aug 23, 2026, 5:01 AM UTC-08:00

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions