package workflow import ( "errors" "fmt" "maps" "sync" "github.com/samcharles93/archie-core/internal/domain/lifecycle" "github.com/samcharles93/archie-core/internal/domain/stableid" ) // StepType is one named workflow step type: the vocabulary word a YAML // workflow names, plus the factory that builds its stage. type StepType struct { Name string Factory StepFactory // Settings is a zero value of the step's settings type, from which the // control plane derives the settings schema editors build forms from. // Nil means the step takes no settings. Settings any } // StepTypeProvider is the typed extension point through which a plugin // contributes workflow step types. StepTypes is the capability-specific // operation — a provider exposing identity metadata alone would be a metadata // entry, not a step-type provider — and it is the only method the family calls. type StepTypeProvider interface { // Name identifies the provider in registration diagnostics. Name() string // StepTypes returns every step type this provider contributes. The // manager validates the whole contribution before applying any of it. StepTypes() []StepType } // Manager owns registration and resolution of workflow step types. Each // process builds one from the shared provider set and injects it. Register // every provider before the first resolution. type Manager struct { mu sync.Mutex registry StepRegistry // owners maps each step type to the provider that registered it. owners map[string]string settings map[string]any providers []string } // NewManager builds an empty family manager. It holds no vocabulary of its own: // the shipped stages are a provider like any other, registered by the process's // composition root, so the vocabulary every side resolves is the one that was // actually registered. func NewManager() *Manager { return &Manager{registry: StepRegistry{}, owners: map[string]string{}, settings: map[string]any{}} } // Register validates a provider's whole contribution and applies all of it or // none. It refuses nil or unnamed providers, bad identifiers, missing // factories, duplicates and types another provider already owns. func (m *Manager) Register(provider StepTypeProvider) error { if lifecycle.IsNil(provider) { return errors.New("workflow step type provider is nil") } name := provider.Name() if !stableid.Valid(name) { return fmt.Errorf("workflow step type provider name %q is not a stable identifier", name) } contributed := provider.StepTypes() pending := make(StepRegistry, len(contributed)) settings := make(map[string]any, len(contributed)) order := make([]string, 0, len(contributed)) for _, stepType := range contributed { switch { case !stableid.Valid(stepType.Name): return fmt.Errorf("workflow step type provider %q: step type %q is not a stable identifier", name, stepType.Name) case stepType.Factory == nil: return fmt.Errorf("workflow step type provider %q: step type %q has no factory", name, stepType.Name) } if _, declared := pending[stepType.Name]; declared { return fmt.Errorf("workflow step type provider %q declares step type %q twice", name, stepType.Name) } pending[stepType.Name] = stepType.Factory settings[stepType.Name] = stepType.Settings order = append(order, stepType.Name) } m.mu.Lock() defer m.mu.Unlock() for _, stepType := range order { if owner, claimed := m.owners[stepType]; claimed { return fmt.Errorf("workflow step type %q is already claimed by provider %q", stepType, owner) } } for _, stepType := range order { m.registry[stepType] = pending[stepType] m.settings[stepType] = settings[stepType] m.owners[stepType] = name } m.providers = append(m.providers, name) return nil } // Registry returns the assembled vocabulary for the parser and compiler entry // points. It is a copy: the closed set is a property of the manager, not // something a consumer can widen. func (m *Manager) Registry() StepRegistry { m.mu.Lock() defer m.mu.Unlock() registry := make(StepRegistry, len(m.registry)) maps.Copy(registry, m.registry) return registry } // Settings returns each registered step type's settings value (see // StepType.Settings), keyed by type name. func (m *Manager) Settings() map[string]any { m.mu.Lock() defer m.mu.Unlock() return maps.Clone(m.settings) }