Skip to content

[Code Quality] Inject EngineRegistry dependency instead of global singleton #14522

Closed as not planned

Description

@github-actions

Description

GetGlobalEngineRegistry() is called 24 times across the codebase, creating tight coupling to a global singleton. This makes unit testing difficult (can't inject mock registry) and prevents parallel test execution due to shared global state.

Current State

File: pkg/workflow/agentic_engine.go:303 - Singleton getter definition
File: pkg/workflow/compiler_types.go:145 - Usage in NewCompiler()

func NewCompiler(opts ...CompilerOption) *Compiler {
    c := &Compiler{
        // ...
        engineRegistry: GetGlobalEngineRegistry(),  // Tight coupling
        // ...
    }
    return c
}

22+ additional call sites across the codebase create the same tight coupling.

Suggested Changes

Pass *EngineRegistry as constructor parameter using dependency injection via functional option:

// Add functional option for registry injection
func WithEngineRegistry(registry *EngineRegistry) CompilerOption {
    return func(c *Compiler) {
        c.engineRegistry = registry
    }
}

func NewCompiler(opts ...CompilerOption) *Compiler {
    c := &Compiler{
        engineRegistry: nil,  // Will be set by option or default
        // ...
    }
    
    for _, opt := range opts {
        opt(c)
    }
    
    // Default to global registry if not injected
    if c.engineRegistry == nil {
        c.engineRegistry = GetGlobalEngineRegistry()
    }
    
    return c
}

// Test usage with mock registry
mockRegistry := NewEngineRegistry()
mockRegistry.Register("test-engine", &MockEngine{})
compiler := NewCompiler(
    WithEngineRegistry(mockRegistry),
)

Files Affected

  • pkg/workflow/compiler_types.go - Add WithEngineRegistry option
  • Test files - Demonstrate mock registry injection
  • 22+ additional call sites (for future migration)

Success Criteria

  • Add WithEngineRegistry functional option to compiler_types.go
  • Update NewCompiler to use injected registry or default to global
  • Create test demonstrating mock registry injection
  • Run existing tests to ensure backward compatibility
  • Document migration pattern for other singleton usages

Source

Extracted from Sergo Report: Constructor Explosion & Magic Numbers Analysis discussion #13058

Priority

Medium - Improves testability and sets pattern for removing other singletons. Can be implemented incrementally with full backward compatibility.

AI generated by Discussion Task Miner - Code Quality Improvement Agent

  • expires on Feb 9, 2026, 1:17 PM UTC

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions