Separate pipeline reference and binding resolution
This commit is contained in:
@@ -40,7 +40,10 @@ The composition digest describes resolved composition, not cache identity.
|
|||||||
|
|
||||||
Resolution receives exact reference bindings/unbinds, not CLI selector grammar.
|
Resolution receives exact reference bindings/unbinds, not CLI selector grammar.
|
||||||
It preserves required slots, external/generated conflicts, prior-step
|
It preserves required slots, external/generated conflicts, prior-step
|
||||||
dependencies, and artifact compatibility. [Reference materialization](../../internal/framework/pipeline/references.go)
|
dependencies, and artifact compatibility. [Reference resolution](../../internal/framework/pipeline/reference_resolution.go)
|
||||||
|
normalizes sources separately from overlays and precedence; [generated binding
|
||||||
|
checks](../../internal/framework/pipeline/generated_bindings.go) validate each
|
||||||
|
producer through structured step/lane keys. [Reference materialization](../../internal/framework/pipeline/references.go)
|
||||||
reads selected external bytes with declaration/invocation origins, media/UTF-8
|
reads selected external bytes with declaration/invocation origins, media/UTF-8
|
||||||
and declared size checks. Generated selections remain declarations until their
|
and declared size checks. Generated selections remain declarations until their
|
||||||
producer step completes. Public selection semantics belong in
|
producer step completes. Public selection semantics belong in
|
||||||
|
|||||||
@@ -433,6 +433,12 @@ arbitrary reflection copier or merge this with source-metadata validation.
|
|||||||
|
|
||||||
### CAT-018 — Pipeline Resolution Mixes Unrelated Responsibilities
|
### CAT-018 — Pipeline Resolution Mixes Unrelated Responsibilities
|
||||||
|
|
||||||
|
**Status:** Resolved. Pipeline values/wire handling, shared copies, effective
|
||||||
|
binding selection, reference resolution, and generated dependencies have
|
||||||
|
cohesive owners. Per-binding generated checks use structured producer keys;
|
||||||
|
source normalization is separate from overlay precedence. Existing independent
|
||||||
|
composition, type, reference, and digest checks and full verification pass.
|
||||||
|
|
||||||
**Assessment:** Local decomposition opportunity; original medium.
|
**Assessment:** Local decomposition opportunity; original medium.
|
||||||
|
|
||||||
**Source:** [CPLX-002](03-complexity.md#cplx-002--pipeline-resolution-mixes-too-many-responsibilities-in-one-file).
|
**Source:** [CPLX-002](03-complexity.md#cplx-002--pipeline-resolution-mixes-too-many-responsibilities-in-one-file).
|
||||||
|
|||||||
@@ -736,6 +736,12 @@ Update links to shared owners in CLI/config/pipeline internals.
|
|||||||
|
|
||||||
## Stage 26 — Decompose Complete Resolution
|
## Stage 26 — Decompose Complete Resolution
|
||||||
|
|
||||||
|
**Completed.** Value/wire handling, effective bindings, reference normalization
|
||||||
|
and overlays, and generated-dependency checks have cohesive same-package owners.
|
||||||
|
Generated bindings are checked individually through structured producer keys;
|
||||||
|
the shared layout, selection, and copies remain the composition inputs. Focused
|
||||||
|
composition and reference tests, full tests, vet, and build pass.
|
||||||
|
|
||||||
**Coverage:** CAT-018 completion. **Prerequisites:** Stages 3, 22–25.
|
**Coverage:** CAT-018 completion. **Prerequisites:** Stages 3, 22–25.
|
||||||
|
|
||||||
Split cohesive value/wire, copy, reference, generated-dependency, and effective
|
Split cohesive value/wire, copy, reference, generated-dependency, and effective
|
||||||
|
|||||||
114
internal/framework/pipeline/binding_resolution.go
Normal file
114
internal/framework/pipeline/binding_resolution.go
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
package pipeline
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||||
|
)
|
||||||
|
|
||||||
|
func resolveBinding(binding ModuleBinding, defaultModule string, referenceSlotLabel string) (ModuleBinding, error) {
|
||||||
|
module := resolveModuleKey(binding.Module, defaultModule)
|
||||||
|
llmProfile := strings.TrimSpace(binding.LLMProfile)
|
||||||
|
validators := cloneValidatorOverride(binding.Validators)
|
||||||
|
for i := range validators.Validators {
|
||||||
|
normalizeBindingNames(&validators.Validators[i])
|
||||||
|
}
|
||||||
|
references, err := normalizeReferenceMap(binding.References, referenceSlotLabel)
|
||||||
|
if err != nil {
|
||||||
|
return ModuleBinding{}, err
|
||||||
|
}
|
||||||
|
return ModuleBinding{
|
||||||
|
Module: module,
|
||||||
|
Mode: cloneControl(binding.Mode),
|
||||||
|
MaxAdditionalTurns: cloneControl(binding.MaxAdditionalTurns),
|
||||||
|
Cache: cloneControl(binding.Cache),
|
||||||
|
LLMProfile: llmProfile,
|
||||||
|
Options: CloneModuleOptions(binding.Options),
|
||||||
|
References: references,
|
||||||
|
Validators: validators,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func resolveModuleKey(module string, defaultModule string) string {
|
||||||
|
module = strings.TrimSpace(module)
|
||||||
|
if module == "" {
|
||||||
|
return defaultModule
|
||||||
|
}
|
||||||
|
return module
|
||||||
|
}
|
||||||
|
|
||||||
|
func applyEffectiveLLMProfiles(resolved *ResolvedPipeline, pipelineProfile string, overrideProfile *string) error {
|
||||||
|
pipelineProfile = strings.TrimSpace(pipelineProfile)
|
||||||
|
|
||||||
|
apply := func(stage ModuleStage, laneID, module string, binding *ModuleBinding, executionClass contracts.ExecutionClass, kind string) error {
|
||||||
|
binding.LLMProfile = strings.TrimSpace(binding.LLMProfile)
|
||||||
|
if executionClass != contracts.ExecutionClassLLMBacked {
|
||||||
|
if binding.LLMProfile != "" {
|
||||||
|
if laneID == "" {
|
||||||
|
return fmt.Errorf("pipeline %q %s %q assigns llm_profile to deterministic %s %q", resolved.ID, stage, module, kind, binding.Module)
|
||||||
|
}
|
||||||
|
return fmt.Errorf("pipeline %q lane %q %s %q assigns llm_profile to deterministic %s %q", resolved.ID, laneID, stage, module, kind, binding.Module)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if overrideProfile != nil {
|
||||||
|
binding.LLMProfile = strings.TrimSpace(*overrideProfile)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if binding.LLMProfile == "" {
|
||||||
|
binding.LLMProfile = pipelineProfile
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := apply(StageInput, "", resolved.Input.Module, &resolved.Input, resolved.InputExecutionClass, "module"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := apply(StageChunk, "", resolved.Chunk.Module, &resolved.Chunk, resolved.ChunkExecutionClass, "module"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for stepIndex := range resolved.Steps {
|
||||||
|
for laneIndex := range resolved.Steps[stepIndex].ArtifactLanes {
|
||||||
|
lane := &resolved.Steps[stepIndex].ArtifactLanes[laneIndex]
|
||||||
|
if err := apply(StageExtract, lane.ID, lane.Extract.Module, &lane.Extract, lane.ExtractExecutionClass, "module"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := apply(StageMerge, lane.ID, lane.Merge.Module, &lane.Merge, lane.MergeExecutionClass, "module"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := apply(StageNormalize, lane.ID, lane.Normalize.Module, &lane.Normalize, lane.NormalizeExecutionClass, "module"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := apply(StageOutput, "", resolved.Output.Module, &resolved.Output, resolved.OutputExecutionClass, "module"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for chainIndex := range resolved.ValidatorChains {
|
||||||
|
chain := &resolved.ValidatorChains[chainIndex]
|
||||||
|
for validatorIndex := range chain.Validators {
|
||||||
|
validator := &chain.Validators[validatorIndex]
|
||||||
|
if err := apply(chain.Stage, chain.LaneID, chain.ModuleKey, &validator.Binding, validator.ExecutionClass, "validator"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func resolveBindings(bindings []ModuleBinding, defaultModule string, referenceSlotLabel string) ([]ModuleBinding, error) {
|
||||||
|
if len(bindings) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
resolved := make([]ModuleBinding, 0, len(bindings))
|
||||||
|
for index, binding := range bindings {
|
||||||
|
resolvedBinding, err := resolveBinding(binding, defaultModule, fmt.Sprintf("%s %d", referenceSlotLabel, index))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
resolved = append(resolved, resolvedBinding)
|
||||||
|
}
|
||||||
|
return resolved, nil
|
||||||
|
}
|
||||||
103
internal/framework/pipeline/generated_bindings.go
Normal file
103
internal/framework/pipeline/generated_bindings.go
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
package pipeline
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||||
|
)
|
||||||
|
|
||||||
|
func validateGeneratedBindings(pipelineID string, resolved ResolvedPipeline, catalog ModuleCatalog) error {
|
||||||
|
stepIndex := make(map[string]int, len(resolved.Steps))
|
||||||
|
laneIndex := make(map[generatedOutputKey]ResolvedArtifactLane)
|
||||||
|
for index, step := range resolved.Steps {
|
||||||
|
if _, exists := stepIndex[step.ID]; exists {
|
||||||
|
return fmt.Errorf("pipeline %q step id %q is ambiguous", pipelineID, step.ID)
|
||||||
|
}
|
||||||
|
stepIndex[step.ID] = index
|
||||||
|
for _, lane := range step.ArtifactLanes {
|
||||||
|
laneIndex[generatedOutputKeyFor(step.ID, lane.ID)] = lane
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for consumerIndex, step := range resolved.Steps {
|
||||||
|
for _, lane := range step.ArtifactLanes {
|
||||||
|
for _, target := range []ResolvedReferenceTarget{lane.ExtractReferences, lane.MergeReferences, lane.NormalizeReferences} {
|
||||||
|
slots, err := resolvedReferenceSlots(target, lane.ArtifactKind, catalog)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("pipeline %q step %q lane %q: %w", pipelineID, step.ID, lane.ID, err)
|
||||||
|
}
|
||||||
|
slotByName := make(map[string]contracts.ReferenceSlot, len(slots))
|
||||||
|
for _, slot := range slots {
|
||||||
|
slotByName[slot.Name] = slot
|
||||||
|
}
|
||||||
|
for _, binding := range target.Bindings {
|
||||||
|
if binding.Artifact == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
context := generatedBindingContext{pipelineID: pipelineID, stepID: step.ID, laneID: lane.ID, consumerIndex: consumerIndex, stage: target.Stage, steps: stepIndex, lanes: laneIndex, slots: slotByName, catalog: catalog}
|
||||||
|
if err := validateGeneratedBinding(context, binding); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type generatedBindingContext struct {
|
||||||
|
pipelineID, stepID, laneID string
|
||||||
|
consumerIndex int
|
||||||
|
stage ModuleStage
|
||||||
|
steps map[string]int
|
||||||
|
lanes map[generatedOutputKey]ResolvedArtifactLane
|
||||||
|
slots map[string]contracts.ReferenceSlot
|
||||||
|
catalog ModuleCatalog
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateGeneratedBinding(context generatedBindingContext, binding ReferenceBinding) error {
|
||||||
|
producerStep, ok := context.steps[strings.TrimSpace(binding.Artifact.Step)]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("pipeline %q step %q lane %q %s reference slot %q producer step %q is not declared", context.pipelineID, context.stepID, context.laneID, context.stage, binding.SlotName, binding.Artifact.Step)
|
||||||
|
}
|
||||||
|
if producerStep >= context.consumerIndex {
|
||||||
|
return fmt.Errorf("pipeline %q step %q lane %q reference slot %q producer %q must be an earlier step", context.pipelineID, context.stepID, context.laneID, binding.SlotName, binding.Artifact.Step)
|
||||||
|
}
|
||||||
|
producer, ok := context.lanes[generatedOutputKeyFor(strings.TrimSpace(binding.Artifact.Step), strings.TrimSpace(binding.Artifact.Lane))]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("pipeline %q step %q lane %q reference slot %q producer lane %q is not selected", context.pipelineID, context.stepID, context.laneID, binding.SlotName, binding.Artifact.Lane)
|
||||||
|
}
|
||||||
|
slot, ok := context.slots[strings.TrimSpace(binding.SlotName)]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("pipeline %q step %q lane %q %s reference slot %q is not declared", context.pipelineID, context.stepID, context.laneID, context.stage, binding.SlotName)
|
||||||
|
}
|
||||||
|
if len(slot.AcceptedArtifactKinds) == 0 {
|
||||||
|
return fmt.Errorf("pipeline %q step %q lane %q reference slot %q does not accept generated artifacts", context.pipelineID, context.stepID, context.laneID, binding.SlotName)
|
||||||
|
}
|
||||||
|
accepted := false
|
||||||
|
for _, kind := range slot.AcceptedArtifactKinds {
|
||||||
|
if kind == producer.ArtifactKind {
|
||||||
|
accepted = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !accepted {
|
||||||
|
return fmt.Errorf("pipeline %q step %q lane %q reference slot %q does not accept artifact kind %q", context.pipelineID, context.stepID, context.laneID, binding.SlotName, producer.ArtifactKind)
|
||||||
|
}
|
||||||
|
codecSpec, ok := context.catalog.ArtifactCodecs.Spec(producer.ArtifactKind)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("pipeline %q step %q lane %q producer %q artifact codec %q is not registered", context.pipelineID, context.stepID, context.laneID, producer.ID, producer.ArtifactKind)
|
||||||
|
}
|
||||||
|
if !referenceMediaTypeAccepted(codecSpec.MediaType, slot.AcceptedMediaTypes) {
|
||||||
|
return fmt.Errorf("pipeline %q step %q lane %q reference slot %q does not accept producer media type %q", context.pipelineID, context.stepID, context.laneID, binding.SlotName, codecSpec.MediaType)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func resolvedReferenceSlots(target ResolvedReferenceTarget, artifactKind contracts.ArtifactKind, catalog ModuleCatalog) ([]contracts.ReferenceSlot, error) {
|
||||||
|
spec, err := referenceTargetSpec(target, artifactKind, catalog)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return contracts.CloneReferenceSlots(spec.ReferenceSlots), nil
|
||||||
|
}
|
||||||
@@ -12,176 +12,6 @@ import (
|
|||||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
DefaultChunkModule = "generic"
|
|
||||||
DefaultMergeModule = "appendorder"
|
|
||||||
DefaultNormalizeModule = "noop"
|
|
||||||
DefaultOutputModule = "json"
|
|
||||||
DefaultLLMProfile = "default"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ModuleBinding struct {
|
|
||||||
Mode *contracts.ExecutionMode `json:"mode,omitempty"`
|
|
||||||
MaxAdditionalTurns *int `json:"max_additional_turns,omitempty"`
|
|
||||||
Cache *contracts.CacheMode `json:"cache,omitempty"`
|
|
||||||
Module string `json:"module"`
|
|
||||||
LLMProfile string `json:"llm_profile,omitempty"`
|
|
||||||
Options map[string]any `json:"options,omitempty"`
|
|
||||||
References map[string]ReferenceSource `json:"references,omitempty"`
|
|
||||||
Validators ValidatorOverride `json:"validators,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// ArtifactReference identifies a normalized artifact produced by an earlier
|
|
||||||
// ordered step. It is a selector only; it does not contain artifact bytes.
|
|
||||||
type ArtifactReference struct {
|
|
||||||
Step string `json:"step"`
|
|
||||||
Lane string `json:"lane"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReferenceSource is the discriminated source form used by configured
|
|
||||||
// reference maps. Exactly one of Path and Artifact is set after resolution.
|
|
||||||
type ReferenceSource struct {
|
|
||||||
Path string `json:"path,omitempty"`
|
|
||||||
Artifact *ArtifactReference `json:"artifact,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExternalReference(path string) ReferenceSource {
|
|
||||||
return ReferenceSource{Path: strings.TrimSpace(path)}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExternalReferenceMap(values map[string]string) map[string]ReferenceSource {
|
|
||||||
out := make(map[string]ReferenceSource, len(values))
|
|
||||||
for key, value := range values {
|
|
||||||
out[key] = ExternalReference(value)
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
func GeneratedReference(step, lane string) ReferenceSource {
|
|
||||||
return ReferenceSource{Artifact: &ArtifactReference{Step: strings.TrimSpace(step), Lane: strings.TrimSpace(lane)}}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (source ReferenceSource) IsGenerated() bool { return source.Artifact != nil }
|
|
||||||
|
|
||||||
func (source ReferenceSource) MarshalJSON() ([]byte, error) {
|
|
||||||
if source.Path != "" && source.Artifact != nil {
|
|
||||||
return nil, fmt.Errorf("reference source must not contain both an external path and an artifact selector")
|
|
||||||
}
|
|
||||||
if source.Artifact != nil {
|
|
||||||
return json.Marshal(struct {
|
|
||||||
Artifact *ArtifactReference `json:"artifact"`
|
|
||||||
}{Artifact: source.Artifact})
|
|
||||||
}
|
|
||||||
return json.Marshal(source.Path)
|
|
||||||
}
|
|
||||||
|
|
||||||
type ValidatorOverride struct {
|
|
||||||
Set bool `json:"set,omitempty"`
|
|
||||||
Validators []ModuleBinding `json:"validators,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (binding ModuleBinding) MarshalJSON() ([]byte, error) {
|
|
||||||
type moduleBindingJSON struct {
|
|
||||||
Mode *contracts.ExecutionMode `json:"mode,omitempty"`
|
|
||||||
MaxAdditionalTurns *int `json:"max_additional_turns,omitempty"`
|
|
||||||
Cache *contracts.CacheMode `json:"cache,omitempty"`
|
|
||||||
Module string `json:"module"`
|
|
||||||
LLMProfile string `json:"llm_profile,omitempty"`
|
|
||||||
Options map[string]any `json:"options,omitempty"`
|
|
||||||
References map[string]ReferenceSource `json:"references,omitempty"`
|
|
||||||
Validators *[]ModuleBinding `json:"validators,omitempty"`
|
|
||||||
}
|
|
||||||
out := moduleBindingJSON{
|
|
||||||
Module: binding.Module,
|
|
||||||
Mode: cloneControl(binding.Mode),
|
|
||||||
MaxAdditionalTurns: cloneControl(binding.MaxAdditionalTurns),
|
|
||||||
Cache: cloneControl(binding.Cache),
|
|
||||||
LLMProfile: binding.LLMProfile,
|
|
||||||
Options: binding.Options,
|
|
||||||
References: binding.References,
|
|
||||||
}
|
|
||||||
if binding.Validators.Set {
|
|
||||||
validators := cloneModuleBindings(binding.Validators.Validators)
|
|
||||||
out.Validators = &validators
|
|
||||||
}
|
|
||||||
return json.Marshal(out)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (binding *ModuleBinding) UnmarshalJSON(data []byte) error {
|
|
||||||
if binding == nil {
|
|
||||||
return fmt.Errorf("module binding must not be nil")
|
|
||||||
}
|
|
||||||
type moduleBindingJSON struct {
|
|
||||||
Mode *contracts.ExecutionMode `json:"mode,omitempty"`
|
|
||||||
MaxAdditionalTurns *int `json:"max_additional_turns,omitempty"`
|
|
||||||
Cache *contracts.CacheMode `json:"cache,omitempty"`
|
|
||||||
Module string `json:"module"`
|
|
||||||
LLMProfile string `json:"llm_profile,omitempty"`
|
|
||||||
Options map[string]any `json:"options,omitempty"`
|
|
||||||
References map[string]ReferenceSource `json:"references,omitempty"`
|
|
||||||
Validators json.RawMessage `json:"validators,omitempty"`
|
|
||||||
}
|
|
||||||
var decoded moduleBindingJSON
|
|
||||||
if err := json.Unmarshal(data, &decoded); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
out := ModuleBinding{
|
|
||||||
Module: decoded.Module,
|
|
||||||
Mode: cloneControl(decoded.Mode),
|
|
||||||
MaxAdditionalTurns: cloneControl(decoded.MaxAdditionalTurns),
|
|
||||||
Cache: cloneControl(decoded.Cache),
|
|
||||||
LLMProfile: decoded.LLMProfile,
|
|
||||||
Options: decoded.Options,
|
|
||||||
References: decoded.References,
|
|
||||||
}
|
|
||||||
if len(decoded.Validators) > 0 && string(decoded.Validators) != "null" {
|
|
||||||
var validators []ModuleBinding
|
|
||||||
if err := json.Unmarshal(decoded.Validators, &validators); err != nil {
|
|
||||||
return fmt.Errorf("decode module binding validators: %w", err)
|
|
||||||
}
|
|
||||||
out.Validators = ValidatorOverride{Set: true, Validators: validators}
|
|
||||||
}
|
|
||||||
*binding = CloneModuleBinding(out)
|
|
||||||
normalizeBindingNames(binding)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func normalizeBindingNames(binding *ModuleBinding) {
|
|
||||||
binding.Module = strings.TrimSpace(binding.Module)
|
|
||||||
binding.LLMProfile = strings.TrimSpace(binding.LLMProfile)
|
|
||||||
for i := range binding.Validators.Validators {
|
|
||||||
normalizeBindingNames(&binding.Validators.Validators[i])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type ArtifactLaneProfile struct {
|
|
||||||
Extract ModuleBinding `json:"extract"`
|
|
||||||
Merge ModuleBinding `json:"merge,omitempty"`
|
|
||||||
Normalize ModuleBinding `json:"normalize,omitempty"`
|
|
||||||
Validators []ModuleBinding `json:"validators,omitempty"`
|
|
||||||
References map[string]ReferenceSource `json:"references,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type PipelineStepProfile struct {
|
|
||||||
ID string `json:"id"`
|
|
||||||
Artifacts map[string]ArtifactLaneProfile `json:"artifacts"`
|
|
||||||
References map[string]ReferenceSource `json:"references,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type PipelineProfile struct {
|
|
||||||
Mode *contracts.ExecutionMode `json:"mode,omitempty"`
|
|
||||||
MaxAdditionalTurns *int `json:"max_additional_turns,omitempty"`
|
|
||||||
Cache *contracts.CacheMode `json:"cache,omitempty"`
|
|
||||||
ID string `json:"id"`
|
|
||||||
LLMProfile string `json:"llm_profile,omitempty"`
|
|
||||||
Input ModuleBinding `json:"input"`
|
|
||||||
Chunk ModuleBinding `json:"chunk,omitempty"`
|
|
||||||
Artifacts map[string]ArtifactLaneProfile `json:"artifacts"`
|
|
||||||
Steps []PipelineStepProfile `json:"steps,omitempty"`
|
|
||||||
Output ModuleBinding `json:"output,omitempty"`
|
|
||||||
References map[string]ReferenceSource `json:"references,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type ResolveOptions struct {
|
type ResolveOptions struct {
|
||||||
CacheOverride *contracts.CacheMode
|
CacheOverride *contracts.CacheMode
|
||||||
Only []string
|
Only []string
|
||||||
@@ -633,83 +463,6 @@ func configuredValidatorsError(pipelineID string, laneID string) error {
|
|||||||
return fmt.Errorf("pipeline %q lane %q validators are not supported at artifact lane level; use extract.validators, merge.validators, or normalize.validators", pipelineID, laneID)
|
return fmt.Errorf("pipeline %q lane %q validators are not supported at artifact lane level; use extract.validators, merge.validators, or normalize.validators", pipelineID, laneID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateGeneratedBindings(pipelineID string, resolved ResolvedPipeline, catalog ModuleCatalog) error {
|
|
||||||
stepIndex := make(map[string]int, len(resolved.Steps))
|
|
||||||
laneIndex := make(map[generatedOutputKey]ResolvedArtifactLane)
|
|
||||||
for index, step := range resolved.Steps {
|
|
||||||
if _, exists := stepIndex[step.ID]; exists {
|
|
||||||
return fmt.Errorf("pipeline %q step id %q is ambiguous", pipelineID, step.ID)
|
|
||||||
}
|
|
||||||
stepIndex[step.ID] = index
|
|
||||||
for _, lane := range step.ArtifactLanes {
|
|
||||||
laneIndex[generatedOutputKeyFor(step.ID, lane.ID)] = lane
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for consumerIndex, step := range resolved.Steps {
|
|
||||||
for _, lane := range step.ArtifactLanes {
|
|
||||||
for _, target := range []ResolvedReferenceTarget{lane.ExtractReferences, lane.MergeReferences, lane.NormalizeReferences} {
|
|
||||||
slots, err := resolvedReferenceSlots(target, lane.ArtifactKind, catalog)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("pipeline %q step %q lane %q: %w", pipelineID, step.ID, lane.ID, err)
|
|
||||||
}
|
|
||||||
slotByName := make(map[string]contracts.ReferenceSlot, len(slots))
|
|
||||||
for _, slot := range slots {
|
|
||||||
slotByName[slot.Name] = slot
|
|
||||||
}
|
|
||||||
for _, binding := range target.Bindings {
|
|
||||||
if binding.Artifact == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
producerStep, ok := stepIndex[strings.TrimSpace(binding.Artifact.Step)]
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("pipeline %q step %q lane %q %s reference slot %q producer step %q is not declared", pipelineID, step.ID, lane.ID, target.Stage, binding.SlotName, binding.Artifact.Step)
|
|
||||||
}
|
|
||||||
if producerStep >= consumerIndex {
|
|
||||||
return fmt.Errorf("pipeline %q step %q lane %q reference slot %q producer %q must be an earlier step", pipelineID, step.ID, lane.ID, binding.SlotName, binding.Artifact.Step)
|
|
||||||
}
|
|
||||||
producer, ok := laneIndex[generatedOutputKeyFor(strings.TrimSpace(binding.Artifact.Step), strings.TrimSpace(binding.Artifact.Lane))]
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("pipeline %q step %q lane %q reference slot %q producer lane %q is not selected", pipelineID, step.ID, lane.ID, binding.SlotName, binding.Artifact.Lane)
|
|
||||||
}
|
|
||||||
slot, ok := slotByName[strings.TrimSpace(binding.SlotName)]
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("pipeline %q step %q lane %q %s reference slot %q is not declared", pipelineID, step.ID, lane.ID, target.Stage, binding.SlotName)
|
|
||||||
}
|
|
||||||
if len(slot.AcceptedArtifactKinds) == 0 {
|
|
||||||
return fmt.Errorf("pipeline %q step %q lane %q reference slot %q does not accept generated artifacts", pipelineID, step.ID, lane.ID, binding.SlotName)
|
|
||||||
}
|
|
||||||
accepted := false
|
|
||||||
for _, kind := range slot.AcceptedArtifactKinds {
|
|
||||||
if kind == producer.ArtifactKind {
|
|
||||||
accepted = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !accepted {
|
|
||||||
return fmt.Errorf("pipeline %q step %q lane %q reference slot %q does not accept artifact kind %q", pipelineID, step.ID, lane.ID, binding.SlotName, producer.ArtifactKind)
|
|
||||||
}
|
|
||||||
codecSpec, ok := catalog.ArtifactCodecs.Spec(producer.ArtifactKind)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("pipeline %q step %q lane %q producer %q artifact codec %q is not registered", pipelineID, step.ID, lane.ID, producer.ID, producer.ArtifactKind)
|
|
||||||
}
|
|
||||||
if !referenceMediaTypeAccepted(codecSpec.MediaType, slot.AcceptedMediaTypes) {
|
|
||||||
return fmt.Errorf("pipeline %q step %q lane %q reference slot %q does not accept producer media type %q", pipelineID, step.ID, lane.ID, binding.SlotName, codecSpec.MediaType)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func resolvedReferenceSlots(target ResolvedReferenceTarget, artifactKind contracts.ArtifactKind, catalog ModuleCatalog) ([]contracts.ReferenceSlot, error) {
|
|
||||||
spec, err := referenceTargetSpec(target, artifactKind, catalog)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return contracts.CloneReferenceSlots(spec.ReferenceSlots), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func resolveArtifactIdentity(pipelineID, laneID string, lane *ResolvedArtifactLane, extractSpec ModuleSpec, catalog ModuleCatalog) (reflect.Type, error) {
|
func resolveArtifactIdentity(pipelineID, laneID string, lane *ResolvedArtifactLane, extractSpec ModuleSpec, catalog ModuleCatalog) (reflect.Type, error) {
|
||||||
if extractSpec.ArtifactKind == "" {
|
if extractSpec.ArtifactKind == "" {
|
||||||
return nil, fmt.Errorf("pipeline %q lane %q extract module %q does not declare an artifact kind", pipelineID, laneID, lane.Extract.Module)
|
return nil, fmt.Errorf("pipeline %q lane %q extract module %q does not declare an artifact kind", pipelineID, laneID, lane.Extract.Module)
|
||||||
@@ -877,434 +630,6 @@ func cloneResolvedValidators(validators []ResolvedValidator) []ResolvedValidator
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
func referenceTarget(stage ModuleStage, laneID string, module string, bindings []ReferenceBinding) ResolvedReferenceTarget {
|
|
||||||
return ResolvedReferenceTarget{
|
|
||||||
Stage: stage,
|
|
||||||
LaneID: strings.TrimSpace(laneID),
|
|
||||||
Module: strings.TrimSpace(module),
|
|
||||||
Bindings: append([]ReferenceBinding(nil), bindings...),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func mergeReferenceMaps(base map[string]ReferenceSource, override map[string]ReferenceSource) map[string]ReferenceSource {
|
|
||||||
if len(base) == 0 && len(override) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
out := make(map[string]ReferenceSource, len(base)+len(override))
|
|
||||||
for key, value := range base {
|
|
||||||
out[key] = cloneReferenceSource(value)
|
|
||||||
}
|
|
||||||
for key, value := range override {
|
|
||||||
out[key] = cloneReferenceSource(value)
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
func referenceSourceMapsConflict(base, override map[string]ReferenceSource) bool {
|
|
||||||
for rawKey, left := range base {
|
|
||||||
key := strings.TrimSpace(rawKey)
|
|
||||||
for rawOverrideKey, right := range override {
|
|
||||||
if key == strings.TrimSpace(rawOverrideKey) && left.IsGenerated() != right.IsGenerated() {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func validatePipelineReferenceDefaults(
|
|
||||||
pipelineID string,
|
|
||||||
pipelineReferences map[string]ReferenceSource,
|
|
||||||
chunkSpec ModuleSpec,
|
|
||||||
lanes []PipelineTargetLane,
|
|
||||||
) error {
|
|
||||||
normalizedPipelineReferences, err := normalizeReferenceMap(pipelineReferences, fmt.Sprintf("pipeline %q reference slot", pipelineID))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if len(normalizedPipelineReferences) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
declaredByAnyTarget := make(map[string]struct{}, len(normalizedPipelineReferences))
|
|
||||||
for _, slot := range chunkSpec.ReferenceSlots {
|
|
||||||
declaredByAnyTarget[slot.Name] = struct{}{}
|
|
||||||
}
|
|
||||||
for _, lane := range lanes {
|
|
||||||
if lane.mergeType != lane.extractType {
|
|
||||||
return moduleLookupError(pipelineID, lane.ID, StageMerge, lane.Merge.Module, fmt.Errorf("artifact kind %q requires Go type %s, but merger %q variant uses %s", lane.ArtifactKind, typeName(lane.extractType), lane.Merge.Module, typeName(lane.mergeType)))
|
|
||||||
}
|
|
||||||
if lane.normalizeType != lane.extractType {
|
|
||||||
return moduleLookupError(pipelineID, lane.ID, StageNormalize, lane.Normalize.Module, fmt.Errorf("artifact kind %q requires Go type %s, but normalizer %q variant uses %s", lane.ArtifactKind, typeName(lane.extractType), lane.Normalize.Module, typeName(lane.normalizeType)))
|
|
||||||
}
|
|
||||||
for _, spec := range []ModuleSpec{lane.ExtractSpec, lane.MergeSpec, lane.NormalizeSpec} {
|
|
||||||
for _, slot := range spec.ReferenceSlots {
|
|
||||||
declaredByAnyTarget[slot.Name] = struct{}{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, slotName := range sortedStringMapKeys(normalizedPipelineReferences) {
|
|
||||||
if _, ok := declaredByAnyTarget[slotName]; !ok {
|
|
||||||
return fmt.Errorf("pipeline %q reference slot %q is not declared by any eligible reference target", pipelineID, slotName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type referenceResolutionTarget struct {
|
|
||||||
PipelineID string
|
|
||||||
StepID string
|
|
||||||
LaneID string
|
|
||||||
Stage ModuleStage
|
|
||||||
Module string
|
|
||||||
Slots []contracts.ReferenceSlot
|
|
||||||
PipelineReferences map[string]ReferenceSource
|
|
||||||
LocalReferences map[string]ReferenceSource
|
|
||||||
Options ResolveOptions
|
|
||||||
}
|
|
||||||
|
|
||||||
func resolveReferenceTargetBindings(target referenceResolutionTarget) ([]ReferenceBinding, error) {
|
|
||||||
slotByName := make(map[string]contracts.ReferenceSlot, len(target.Slots))
|
|
||||||
for _, slot := range target.Slots {
|
|
||||||
slotByName[slot.Name] = slot
|
|
||||||
}
|
|
||||||
|
|
||||||
bindings := make(map[string]ReferenceBinding)
|
|
||||||
addBinding := func(slotName string, source ReferenceSource, bindingSource string) error {
|
|
||||||
slotName = strings.TrimSpace(slotName)
|
|
||||||
if slotName == "" {
|
|
||||||
return fmt.Errorf("%s reference slot name must not be empty", referenceTargetErrorContext(target))
|
|
||||||
}
|
|
||||||
if source.Artifact == nil {
|
|
||||||
source.Path = strings.TrimSpace(source.Path)
|
|
||||||
}
|
|
||||||
if source.Artifact == nil && source.Path == "" {
|
|
||||||
return fmt.Errorf("%s reference slot %q source must not be empty", referenceTargetErrorContext(target), slotName)
|
|
||||||
}
|
|
||||||
if source.Artifact != nil {
|
|
||||||
if strings.TrimSpace(source.Path) != "" {
|
|
||||||
return fmt.Errorf("%s reference slot %q must contain exactly one source form", referenceTargetErrorContext(target), slotName)
|
|
||||||
}
|
|
||||||
artifact := *source.Artifact
|
|
||||||
artifact.Step = strings.TrimSpace(artifact.Step)
|
|
||||||
artifact.Lane = strings.TrimSpace(artifact.Lane)
|
|
||||||
if artifact.Step == "" || artifact.Lane == "" {
|
|
||||||
return fmt.Errorf("%s reference slot %q artifact selector step and lane must not be empty", referenceTargetErrorContext(target), slotName)
|
|
||||||
}
|
|
||||||
source.Artifact = &artifact
|
|
||||||
}
|
|
||||||
if _, ok := slotByName[slotName]; !ok {
|
|
||||||
return fmt.Errorf("%s reference slot %q is not declared by %s module %q", referenceTargetErrorContext(target), slotName, target.Stage, target.Module)
|
|
||||||
}
|
|
||||||
if previous, exists := bindings[slotName]; exists && (previous.Artifact != nil || source.Artifact != nil) {
|
|
||||||
return fmt.Errorf("%s reference slot %q has conflicting generated and external bindings", referenceTargetErrorContext(target), slotName)
|
|
||||||
}
|
|
||||||
binding := ReferenceBinding{
|
|
||||||
LaneID: target.LaneID,
|
|
||||||
SlotName: slotName,
|
|
||||||
BindingSource: bindingSource,
|
|
||||||
}
|
|
||||||
if source.Artifact != nil {
|
|
||||||
binding.Artifact = source.Artifact
|
|
||||||
} else {
|
|
||||||
binding.Source = source.Path
|
|
||||||
}
|
|
||||||
bindings[slotName] = binding
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
normalizedPipelineReferences, err := normalizeReferenceMap(target.PipelineReferences, fmt.Sprintf("pipeline %q reference slot", target.PipelineID))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
for _, slotName := range sortedStringMapKeys(normalizedPipelineReferences) {
|
|
||||||
if _, ok := slotByName[slotName]; !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err := addBinding(slotName, normalizedPipelineReferences[slotName], contracts.ReferenceBindingSourceConfig); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
normalizedLocalReferences, err := normalizeReferenceMap(target.LocalReferences, referenceTargetSlotLabel(target))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
for _, slotName := range sortedStringMapKeys(normalizedLocalReferences) {
|
|
||||||
if err := addBinding(slotName, normalizedLocalReferences[slotName], contracts.ReferenceBindingSourceConfig); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, override := range target.Options.ReferenceOverrides {
|
|
||||||
match, err := referenceOverrideMatchesTarget(target, override)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if !match {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
source := override.BindingSource
|
|
||||||
if strings.TrimSpace(source) == "" {
|
|
||||||
source = contracts.ReferenceBindingSourceCLI
|
|
||||||
}
|
|
||||||
if err := addBinding(override.SlotName, ExternalReference(override.Source), strings.TrimSpace(source)); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, unbind := range target.Options.ReferenceUnbinds {
|
|
||||||
match, err := referenceUnbindMatchesTarget(target, unbind)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if !match {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
slotName := strings.TrimSpace(unbind.SlotName)
|
|
||||||
if slotName == "" {
|
|
||||||
return nil, fmt.Errorf("%s reference unbind slot name must not be empty", referenceTargetErrorContext(target))
|
|
||||||
}
|
|
||||||
if _, ok := slotByName[slotName]; !ok {
|
|
||||||
return nil, fmt.Errorf("%s reference slot %q is not declared", referenceTargetErrorContext(target), slotName)
|
|
||||||
}
|
|
||||||
if binding, ok := bindings[slotName]; ok && binding.Artifact == nil {
|
|
||||||
delete(bindings, slotName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, slot := range target.Slots {
|
|
||||||
if slot.Required {
|
|
||||||
if _, ok := bindings[slot.Name]; !ok {
|
|
||||||
return nil, fmt.Errorf("%s required reference slot %q is not bound", referenceTargetErrorContext(target), slot.Name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return sortedReferenceBindings(bindings), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func referenceOverrideMatchesTarget(target referenceResolutionTarget, override ReferenceBinding) (bool, error) {
|
|
||||||
stage, laneID, err := normalizeReferenceOptionTarget(target.PipelineID, "override", override.Stage, override.LaneID)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
return stage == target.Stage && laneID == target.LaneID, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func referenceUnbindMatchesTarget(target referenceResolutionTarget, unbind ReferenceUnbind) (bool, error) {
|
|
||||||
stage, laneID, err := normalizeReferenceOptionTarget(target.PipelineID, "unbind", unbind.Stage, unbind.LaneID)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
return stage == target.Stage && laneID == target.LaneID, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func normalizeReferenceOptionTarget(pipelineID string, operation string, stage ModuleStage, laneID string) (ModuleStage, string, error) {
|
|
||||||
stage = ModuleStage(strings.TrimSpace(string(stage)))
|
|
||||||
if stage == "" {
|
|
||||||
stage = StageExtract
|
|
||||||
}
|
|
||||||
laneID = strings.TrimSpace(laneID)
|
|
||||||
|
|
||||||
switch stage {
|
|
||||||
case StageChunk:
|
|
||||||
if laneID != "" {
|
|
||||||
return "", "", fmt.Errorf("pipeline %q reference %s for chunk must not include a lane id", pipelineID, operation)
|
|
||||||
}
|
|
||||||
case StageExtract, StageMerge, StageNormalize:
|
|
||||||
if laneID == "" {
|
|
||||||
return "", "", fmt.Errorf("pipeline %q reference %s lane id must not be empty", pipelineID, operation)
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return "", "", fmt.Errorf("pipeline %q reference %s stage %q is not supported", pipelineID, operation, stage)
|
|
||||||
}
|
|
||||||
return stage, laneID, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func sortedReferenceBindings(bindings map[string]ReferenceBinding) []ReferenceBinding {
|
|
||||||
keys := sortedReferenceBindingKeys(bindings)
|
|
||||||
resolved := make([]ReferenceBinding, 0, len(keys))
|
|
||||||
for _, slotName := range keys {
|
|
||||||
resolved = append(resolved, bindings[slotName])
|
|
||||||
}
|
|
||||||
return resolved
|
|
||||||
}
|
|
||||||
|
|
||||||
func referenceTargetErrorContext(target referenceResolutionTarget) string {
|
|
||||||
if target.LaneID != "" {
|
|
||||||
if target.StepID != "" {
|
|
||||||
return fmt.Sprintf("pipeline %q step %q lane %q %s module %q", target.PipelineID, target.StepID, target.LaneID, target.Stage, target.Module)
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("pipeline %q lane %q %s module %q", target.PipelineID, target.LaneID, target.Stage, target.Module)
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("pipeline %q %s module %q", target.PipelineID, target.Stage, target.Module)
|
|
||||||
}
|
|
||||||
|
|
||||||
func referenceTargetSlotLabel(target referenceResolutionTarget) string {
|
|
||||||
if target.LaneID != "" {
|
|
||||||
if target.StepID != "" {
|
|
||||||
return fmt.Sprintf("pipeline %q step %q lane %q %s reference slot", target.PipelineID, target.StepID, target.LaneID, target.Stage)
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("pipeline %q lane %q %s reference slot", target.PipelineID, target.LaneID, target.Stage)
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("pipeline %q %s reference slot", target.PipelineID, target.Stage)
|
|
||||||
}
|
|
||||||
|
|
||||||
func normalizeReferenceMap(values map[string]ReferenceSource, keyName string) (map[string]ReferenceSource, error) {
|
|
||||||
if len(values) == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
out := make(map[string]ReferenceSource, len(values))
|
|
||||||
rawSlotNames := sortedStringMapKeys(values)
|
|
||||||
for _, rawSlotName := range rawSlotNames {
|
|
||||||
rawSource := values[rawSlotName]
|
|
||||||
slotName := strings.TrimSpace(rawSlotName)
|
|
||||||
if slotName == "" {
|
|
||||||
return nil, fmt.Errorf("%s must not be empty", keyName)
|
|
||||||
}
|
|
||||||
if _, ok := out[slotName]; ok {
|
|
||||||
return nil, fmt.Errorf("%s %q is duplicated after trimming", keyName, slotName)
|
|
||||||
}
|
|
||||||
source := cloneReferenceSource(rawSource)
|
|
||||||
if source.Artifact == nil && strings.TrimSpace(source.Path) == "" {
|
|
||||||
return nil, fmt.Errorf("%s %q source must not be empty", keyName, slotName)
|
|
||||||
}
|
|
||||||
out[slotName] = source
|
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func sortedStringMapKeys(values map[string]ReferenceSource) []string {
|
|
||||||
if len(values) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
keys := make([]string, 0, len(values))
|
|
||||||
for key := range values {
|
|
||||||
keys = append(keys, key)
|
|
||||||
}
|
|
||||||
sort.Strings(keys)
|
|
||||||
return keys
|
|
||||||
}
|
|
||||||
|
|
||||||
func sortedReferenceBindingKeys(values map[string]ReferenceBinding) []string {
|
|
||||||
if len(values) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
keys := make([]string, 0, len(values))
|
|
||||||
for key := range values {
|
|
||||||
keys = append(keys, key)
|
|
||||||
}
|
|
||||||
sort.Strings(keys)
|
|
||||||
return keys
|
|
||||||
}
|
|
||||||
|
|
||||||
func resolveBinding(binding ModuleBinding, defaultModule string, referenceSlotLabel string) (ModuleBinding, error) {
|
|
||||||
module := resolveModuleKey(binding.Module, defaultModule)
|
|
||||||
llmProfile := strings.TrimSpace(binding.LLMProfile)
|
|
||||||
validators := cloneValidatorOverride(binding.Validators)
|
|
||||||
for i := range validators.Validators {
|
|
||||||
normalizeBindingNames(&validators.Validators[i])
|
|
||||||
}
|
|
||||||
references, err := normalizeReferenceMap(binding.References, referenceSlotLabel)
|
|
||||||
if err != nil {
|
|
||||||
return ModuleBinding{}, err
|
|
||||||
}
|
|
||||||
return ModuleBinding{
|
|
||||||
Module: module,
|
|
||||||
Mode: cloneControl(binding.Mode),
|
|
||||||
MaxAdditionalTurns: cloneControl(binding.MaxAdditionalTurns),
|
|
||||||
Cache: cloneControl(binding.Cache),
|
|
||||||
LLMProfile: llmProfile,
|
|
||||||
Options: CloneModuleOptions(binding.Options),
|
|
||||||
References: references,
|
|
||||||
Validators: validators,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func resolveModuleKey(module string, defaultModule string) string {
|
|
||||||
module = strings.TrimSpace(module)
|
|
||||||
if module == "" {
|
|
||||||
return defaultModule
|
|
||||||
}
|
|
||||||
return module
|
|
||||||
}
|
|
||||||
|
|
||||||
func applyEffectiveLLMProfiles(resolved *ResolvedPipeline, pipelineProfile string, overrideProfile *string) error {
|
|
||||||
pipelineProfile = strings.TrimSpace(pipelineProfile)
|
|
||||||
|
|
||||||
apply := func(stage ModuleStage, laneID, module string, binding *ModuleBinding, executionClass contracts.ExecutionClass, kind string) error {
|
|
||||||
binding.LLMProfile = strings.TrimSpace(binding.LLMProfile)
|
|
||||||
if executionClass != contracts.ExecutionClassLLMBacked {
|
|
||||||
if binding.LLMProfile != "" {
|
|
||||||
if laneID == "" {
|
|
||||||
return fmt.Errorf("pipeline %q %s %q assigns llm_profile to deterministic %s %q", resolved.ID, stage, module, kind, binding.Module)
|
|
||||||
}
|
|
||||||
return fmt.Errorf("pipeline %q lane %q %s %q assigns llm_profile to deterministic %s %q", resolved.ID, laneID, stage, module, kind, binding.Module)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if overrideProfile != nil {
|
|
||||||
binding.LLMProfile = strings.TrimSpace(*overrideProfile)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if binding.LLMProfile == "" {
|
|
||||||
binding.LLMProfile = pipelineProfile
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := apply(StageInput, "", resolved.Input.Module, &resolved.Input, resolved.InputExecutionClass, "module"); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := apply(StageChunk, "", resolved.Chunk.Module, &resolved.Chunk, resolved.ChunkExecutionClass, "module"); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for stepIndex := range resolved.Steps {
|
|
||||||
for laneIndex := range resolved.Steps[stepIndex].ArtifactLanes {
|
|
||||||
lane := &resolved.Steps[stepIndex].ArtifactLanes[laneIndex]
|
|
||||||
if err := apply(StageExtract, lane.ID, lane.Extract.Module, &lane.Extract, lane.ExtractExecutionClass, "module"); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := apply(StageMerge, lane.ID, lane.Merge.Module, &lane.Merge, lane.MergeExecutionClass, "module"); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := apply(StageNormalize, lane.ID, lane.Normalize.Module, &lane.Normalize, lane.NormalizeExecutionClass, "module"); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := apply(StageOutput, "", resolved.Output.Module, &resolved.Output, resolved.OutputExecutionClass, "module"); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for chainIndex := range resolved.ValidatorChains {
|
|
||||||
chain := &resolved.ValidatorChains[chainIndex]
|
|
||||||
for validatorIndex := range chain.Validators {
|
|
||||||
validator := &chain.Validators[validatorIndex]
|
|
||||||
if err := apply(chain.Stage, chain.LaneID, chain.ModuleKey, &validator.Binding, validator.ExecutionClass, "validator"); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func resolveBindings(bindings []ModuleBinding, defaultModule string, referenceSlotLabel string) ([]ModuleBinding, error) {
|
|
||||||
if len(bindings) == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
resolved := make([]ModuleBinding, 0, len(bindings))
|
|
||||||
for index, binding := range bindings {
|
|
||||||
resolvedBinding, err := resolveBinding(binding, defaultModule, fmt.Sprintf("%s %d", referenceSlotLabel, index))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
resolved = append(resolved, resolvedBinding)
|
|
||||||
}
|
|
||||||
return resolved, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func selectedLayoutLaneIDs(pipelineID string, step PipelineLayoutStep, options ResolveOptions) ([]string, error) {
|
func selectedLayoutLaneIDs(pipelineID string, step PipelineLayoutStep, options ResolveOptions) ([]string, error) {
|
||||||
if len(options.Only) == 0 {
|
if len(options.Only) == 0 {
|
||||||
return step.LaneIDs, nil
|
return step.LaneIDs, nil
|
||||||
|
|||||||
179
internal/framework/pipeline/profile_values.go
Normal file
179
internal/framework/pipeline/profile_values.go
Normal file
@@ -0,0 +1,179 @@
|
|||||||
|
package pipeline
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
DefaultChunkModule = "generic"
|
||||||
|
DefaultMergeModule = "appendorder"
|
||||||
|
DefaultNormalizeModule = "noop"
|
||||||
|
DefaultOutputModule = "json"
|
||||||
|
DefaultLLMProfile = "default"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ModuleBinding struct {
|
||||||
|
Mode *contracts.ExecutionMode `json:"mode,omitempty"`
|
||||||
|
MaxAdditionalTurns *int `json:"max_additional_turns,omitempty"`
|
||||||
|
Cache *contracts.CacheMode `json:"cache,omitempty"`
|
||||||
|
Module string `json:"module"`
|
||||||
|
LLMProfile string `json:"llm_profile,omitempty"`
|
||||||
|
Options map[string]any `json:"options,omitempty"`
|
||||||
|
References map[string]ReferenceSource `json:"references,omitempty"`
|
||||||
|
Validators ValidatorOverride `json:"validators,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ArtifactReference identifies a normalized artifact produced by an earlier
|
||||||
|
// ordered step. It is a selector only; it does not contain artifact bytes.
|
||||||
|
type ArtifactReference struct {
|
||||||
|
Step string `json:"step"`
|
||||||
|
Lane string `json:"lane"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReferenceSource is the discriminated source form used by configured
|
||||||
|
// reference maps. Exactly one of Path and Artifact is set after resolution.
|
||||||
|
type ReferenceSource struct {
|
||||||
|
Path string `json:"path,omitempty"`
|
||||||
|
Artifact *ArtifactReference `json:"artifact,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExternalReference(path string) ReferenceSource {
|
||||||
|
return ReferenceSource{Path: strings.TrimSpace(path)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExternalReferenceMap(values map[string]string) map[string]ReferenceSource {
|
||||||
|
out := make(map[string]ReferenceSource, len(values))
|
||||||
|
for key, value := range values {
|
||||||
|
out[key] = ExternalReference(value)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func GeneratedReference(step, lane string) ReferenceSource {
|
||||||
|
return ReferenceSource{Artifact: &ArtifactReference{Step: strings.TrimSpace(step), Lane: strings.TrimSpace(lane)}}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (source ReferenceSource) IsGenerated() bool { return source.Artifact != nil }
|
||||||
|
|
||||||
|
func (source ReferenceSource) MarshalJSON() ([]byte, error) {
|
||||||
|
if source.Path != "" && source.Artifact != nil {
|
||||||
|
return nil, fmt.Errorf("reference source must not contain both an external path and an artifact selector")
|
||||||
|
}
|
||||||
|
if source.Artifact != nil {
|
||||||
|
return json.Marshal(struct {
|
||||||
|
Artifact *ArtifactReference `json:"artifact"`
|
||||||
|
}{Artifact: source.Artifact})
|
||||||
|
}
|
||||||
|
return json.Marshal(source.Path)
|
||||||
|
}
|
||||||
|
|
||||||
|
type ValidatorOverride struct {
|
||||||
|
Set bool `json:"set,omitempty"`
|
||||||
|
Validators []ModuleBinding `json:"validators,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (binding ModuleBinding) MarshalJSON() ([]byte, error) {
|
||||||
|
type moduleBindingJSON struct {
|
||||||
|
Mode *contracts.ExecutionMode `json:"mode,omitempty"`
|
||||||
|
MaxAdditionalTurns *int `json:"max_additional_turns,omitempty"`
|
||||||
|
Cache *contracts.CacheMode `json:"cache,omitempty"`
|
||||||
|
Module string `json:"module"`
|
||||||
|
LLMProfile string `json:"llm_profile,omitempty"`
|
||||||
|
Options map[string]any `json:"options,omitempty"`
|
||||||
|
References map[string]ReferenceSource `json:"references,omitempty"`
|
||||||
|
Validators *[]ModuleBinding `json:"validators,omitempty"`
|
||||||
|
}
|
||||||
|
out := moduleBindingJSON{
|
||||||
|
Module: binding.Module,
|
||||||
|
Mode: cloneControl(binding.Mode),
|
||||||
|
MaxAdditionalTurns: cloneControl(binding.MaxAdditionalTurns),
|
||||||
|
Cache: cloneControl(binding.Cache),
|
||||||
|
LLMProfile: binding.LLMProfile,
|
||||||
|
Options: binding.Options,
|
||||||
|
References: binding.References,
|
||||||
|
}
|
||||||
|
if binding.Validators.Set {
|
||||||
|
validators := cloneModuleBindings(binding.Validators.Validators)
|
||||||
|
out.Validators = &validators
|
||||||
|
}
|
||||||
|
return json.Marshal(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (binding *ModuleBinding) UnmarshalJSON(data []byte) error {
|
||||||
|
if binding == nil {
|
||||||
|
return fmt.Errorf("module binding must not be nil")
|
||||||
|
}
|
||||||
|
type moduleBindingJSON struct {
|
||||||
|
Mode *contracts.ExecutionMode `json:"mode,omitempty"`
|
||||||
|
MaxAdditionalTurns *int `json:"max_additional_turns,omitempty"`
|
||||||
|
Cache *contracts.CacheMode `json:"cache,omitempty"`
|
||||||
|
Module string `json:"module"`
|
||||||
|
LLMProfile string `json:"llm_profile,omitempty"`
|
||||||
|
Options map[string]any `json:"options,omitempty"`
|
||||||
|
References map[string]ReferenceSource `json:"references,omitempty"`
|
||||||
|
Validators json.RawMessage `json:"validators,omitempty"`
|
||||||
|
}
|
||||||
|
var decoded moduleBindingJSON
|
||||||
|
if err := json.Unmarshal(data, &decoded); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
out := ModuleBinding{
|
||||||
|
Module: decoded.Module,
|
||||||
|
Mode: cloneControl(decoded.Mode),
|
||||||
|
MaxAdditionalTurns: cloneControl(decoded.MaxAdditionalTurns),
|
||||||
|
Cache: cloneControl(decoded.Cache),
|
||||||
|
LLMProfile: decoded.LLMProfile,
|
||||||
|
Options: decoded.Options,
|
||||||
|
References: decoded.References,
|
||||||
|
}
|
||||||
|
if len(decoded.Validators) > 0 && string(decoded.Validators) != "null" {
|
||||||
|
var validators []ModuleBinding
|
||||||
|
if err := json.Unmarshal(decoded.Validators, &validators); err != nil {
|
||||||
|
return fmt.Errorf("decode module binding validators: %w", err)
|
||||||
|
}
|
||||||
|
out.Validators = ValidatorOverride{Set: true, Validators: validators}
|
||||||
|
}
|
||||||
|
*binding = CloneModuleBinding(out)
|
||||||
|
normalizeBindingNames(binding)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeBindingNames(binding *ModuleBinding) {
|
||||||
|
binding.Module = strings.TrimSpace(binding.Module)
|
||||||
|
binding.LLMProfile = strings.TrimSpace(binding.LLMProfile)
|
||||||
|
for i := range binding.Validators.Validators {
|
||||||
|
normalizeBindingNames(&binding.Validators.Validators[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type ArtifactLaneProfile struct {
|
||||||
|
Extract ModuleBinding `json:"extract"`
|
||||||
|
Merge ModuleBinding `json:"merge,omitempty"`
|
||||||
|
Normalize ModuleBinding `json:"normalize,omitempty"`
|
||||||
|
Validators []ModuleBinding `json:"validators,omitempty"`
|
||||||
|
References map[string]ReferenceSource `json:"references,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type PipelineStepProfile struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Artifacts map[string]ArtifactLaneProfile `json:"artifacts"`
|
||||||
|
References map[string]ReferenceSource `json:"references,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type PipelineProfile struct {
|
||||||
|
Mode *contracts.ExecutionMode `json:"mode,omitempty"`
|
||||||
|
MaxAdditionalTurns *int `json:"max_additional_turns,omitempty"`
|
||||||
|
Cache *contracts.CacheMode `json:"cache,omitempty"`
|
||||||
|
ID string `json:"id"`
|
||||||
|
LLMProfile string `json:"llm_profile,omitempty"`
|
||||||
|
Input ModuleBinding `json:"input"`
|
||||||
|
Chunk ModuleBinding `json:"chunk,omitempty"`
|
||||||
|
Artifacts map[string]ArtifactLaneProfile `json:"artifacts"`
|
||||||
|
Steps []PipelineStepProfile `json:"steps,omitempty"`
|
||||||
|
Output ModuleBinding `json:"output,omitempty"`
|
||||||
|
References map[string]ReferenceSource `json:"references,omitempty"`
|
||||||
|
}
|
||||||
333
internal/framework/pipeline/reference_resolution.go
Normal file
333
internal/framework/pipeline/reference_resolution.go
Normal file
@@ -0,0 +1,333 @@
|
|||||||
|
package pipeline
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||||
|
)
|
||||||
|
|
||||||
|
func referenceTarget(stage ModuleStage, laneID string, module string, bindings []ReferenceBinding) ResolvedReferenceTarget {
|
||||||
|
return ResolvedReferenceTarget{
|
||||||
|
Stage: stage,
|
||||||
|
LaneID: strings.TrimSpace(laneID),
|
||||||
|
Module: strings.TrimSpace(module),
|
||||||
|
Bindings: append([]ReferenceBinding(nil), bindings...),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mergeReferenceMaps(base map[string]ReferenceSource, override map[string]ReferenceSource) map[string]ReferenceSource {
|
||||||
|
if len(base) == 0 && len(override) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
out := make(map[string]ReferenceSource, len(base)+len(override))
|
||||||
|
for key, value := range base {
|
||||||
|
out[key] = cloneReferenceSource(value)
|
||||||
|
}
|
||||||
|
for key, value := range override {
|
||||||
|
out[key] = cloneReferenceSource(value)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func referenceSourceMapsConflict(base, override map[string]ReferenceSource) bool {
|
||||||
|
for rawKey, left := range base {
|
||||||
|
key := strings.TrimSpace(rawKey)
|
||||||
|
for rawOverrideKey, right := range override {
|
||||||
|
if key == strings.TrimSpace(rawOverrideKey) && left.IsGenerated() != right.IsGenerated() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func validatePipelineReferenceDefaults(
|
||||||
|
pipelineID string,
|
||||||
|
pipelineReferences map[string]ReferenceSource,
|
||||||
|
chunkSpec ModuleSpec,
|
||||||
|
lanes []PipelineTargetLane,
|
||||||
|
) error {
|
||||||
|
normalizedPipelineReferences, err := normalizeReferenceMap(pipelineReferences, fmt.Sprintf("pipeline %q reference slot", pipelineID))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(normalizedPipelineReferences) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
declaredByAnyTarget := make(map[string]struct{}, len(normalizedPipelineReferences))
|
||||||
|
for _, slot := range chunkSpec.ReferenceSlots {
|
||||||
|
declaredByAnyTarget[slot.Name] = struct{}{}
|
||||||
|
}
|
||||||
|
for _, lane := range lanes {
|
||||||
|
if lane.mergeType != lane.extractType {
|
||||||
|
return moduleLookupError(pipelineID, lane.ID, StageMerge, lane.Merge.Module, fmt.Errorf("artifact kind %q requires Go type %s, but merger %q variant uses %s", lane.ArtifactKind, typeName(lane.extractType), lane.Merge.Module, typeName(lane.mergeType)))
|
||||||
|
}
|
||||||
|
if lane.normalizeType != lane.extractType {
|
||||||
|
return moduleLookupError(pipelineID, lane.ID, StageNormalize, lane.Normalize.Module, fmt.Errorf("artifact kind %q requires Go type %s, but normalizer %q variant uses %s", lane.ArtifactKind, typeName(lane.extractType), lane.Normalize.Module, typeName(lane.normalizeType)))
|
||||||
|
}
|
||||||
|
for _, spec := range []ModuleSpec{lane.ExtractSpec, lane.MergeSpec, lane.NormalizeSpec} {
|
||||||
|
for _, slot := range spec.ReferenceSlots {
|
||||||
|
declaredByAnyTarget[slot.Name] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, slotName := range sortedStringMapKeys(normalizedPipelineReferences) {
|
||||||
|
if _, ok := declaredByAnyTarget[slotName]; !ok {
|
||||||
|
return fmt.Errorf("pipeline %q reference slot %q is not declared by any eligible reference target", pipelineID, slotName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type referenceResolutionTarget struct {
|
||||||
|
PipelineID string
|
||||||
|
StepID string
|
||||||
|
LaneID string
|
||||||
|
Stage ModuleStage
|
||||||
|
Module string
|
||||||
|
Slots []contracts.ReferenceSlot
|
||||||
|
PipelineReferences map[string]ReferenceSource
|
||||||
|
LocalReferences map[string]ReferenceSource
|
||||||
|
Options ResolveOptions
|
||||||
|
}
|
||||||
|
|
||||||
|
func resolveReferenceTargetBindings(target referenceResolutionTarget) ([]ReferenceBinding, error) {
|
||||||
|
slotByName := make(map[string]contracts.ReferenceSlot, len(target.Slots))
|
||||||
|
for _, slot := range target.Slots {
|
||||||
|
slotByName[slot.Name] = slot
|
||||||
|
}
|
||||||
|
|
||||||
|
bindings := make(map[string]ReferenceBinding)
|
||||||
|
normalizedPipelineReferences, err := normalizeReferenceMap(target.PipelineReferences, fmt.Sprintf("pipeline %q reference slot", target.PipelineID))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, slotName := range sortedStringMapKeys(normalizedPipelineReferences) {
|
||||||
|
if _, ok := slotByName[slotName]; !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err := addReferenceBinding(target, slotByName, bindings, slotName, normalizedPipelineReferences[slotName], contracts.ReferenceBindingSourceConfig); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
normalizedLocalReferences, err := normalizeReferenceMap(target.LocalReferences, referenceTargetSlotLabel(target))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, slotName := range sortedStringMapKeys(normalizedLocalReferences) {
|
||||||
|
if err := addReferenceBinding(target, slotByName, bindings, slotName, normalizedLocalReferences[slotName], contracts.ReferenceBindingSourceConfig); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, override := range target.Options.ReferenceOverrides {
|
||||||
|
match, err := referenceOverrideMatchesTarget(target, override)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !match {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
source := override.BindingSource
|
||||||
|
if strings.TrimSpace(source) == "" {
|
||||||
|
source = contracts.ReferenceBindingSourceCLI
|
||||||
|
}
|
||||||
|
if err := addReferenceBinding(target, slotByName, bindings, override.SlotName, ExternalReference(override.Source), strings.TrimSpace(source)); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, unbind := range target.Options.ReferenceUnbinds {
|
||||||
|
match, err := referenceUnbindMatchesTarget(target, unbind)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !match {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
slotName := strings.TrimSpace(unbind.SlotName)
|
||||||
|
if slotName == "" {
|
||||||
|
return nil, fmt.Errorf("%s reference unbind slot name must not be empty", referenceTargetErrorContext(target))
|
||||||
|
}
|
||||||
|
if _, ok := slotByName[slotName]; !ok {
|
||||||
|
return nil, fmt.Errorf("%s reference slot %q is not declared", referenceTargetErrorContext(target), slotName)
|
||||||
|
}
|
||||||
|
if binding, ok := bindings[slotName]; ok && binding.Artifact == nil {
|
||||||
|
delete(bindings, slotName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, slot := range target.Slots {
|
||||||
|
if slot.Required {
|
||||||
|
if _, ok := bindings[slot.Name]; !ok {
|
||||||
|
return nil, fmt.Errorf("%s required reference slot %q is not bound", referenceTargetErrorContext(target), slot.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sortedReferenceBindings(bindings), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeTargetReferenceSource(target referenceResolutionTarget, slotName string, source ReferenceSource) (string, ReferenceSource, error) {
|
||||||
|
slotName = strings.TrimSpace(slotName)
|
||||||
|
if slotName == "" {
|
||||||
|
return "", ReferenceSource{}, fmt.Errorf("%s reference slot name must not be empty", referenceTargetErrorContext(target))
|
||||||
|
}
|
||||||
|
if source.Artifact == nil {
|
||||||
|
source.Path = strings.TrimSpace(source.Path)
|
||||||
|
if source.Path == "" {
|
||||||
|
return "", ReferenceSource{}, fmt.Errorf("%s reference slot %q source must not be empty", referenceTargetErrorContext(target), slotName)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if strings.TrimSpace(source.Path) != "" {
|
||||||
|
return "", ReferenceSource{}, fmt.Errorf("%s reference slot %q must contain exactly one source form", referenceTargetErrorContext(target), slotName)
|
||||||
|
}
|
||||||
|
artifact := *source.Artifact
|
||||||
|
artifact.Step, artifact.Lane = strings.TrimSpace(artifact.Step), strings.TrimSpace(artifact.Lane)
|
||||||
|
if artifact.Step == "" || artifact.Lane == "" {
|
||||||
|
return "", ReferenceSource{}, fmt.Errorf("%s reference slot %q artifact selector step and lane must not be empty", referenceTargetErrorContext(target), slotName)
|
||||||
|
}
|
||||||
|
source.Artifact = &artifact
|
||||||
|
}
|
||||||
|
return slotName, source, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func addReferenceBinding(target referenceResolutionTarget, slots map[string]contracts.ReferenceSlot, bindings map[string]ReferenceBinding, slotName string, source ReferenceSource, bindingSource string) error {
|
||||||
|
slotName, source, err := normalizeTargetReferenceSource(target, slotName, source)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, ok := slots[slotName]; !ok {
|
||||||
|
return fmt.Errorf("%s reference slot %q is not declared by %s module %q", referenceTargetErrorContext(target), slotName, target.Stage, target.Module)
|
||||||
|
}
|
||||||
|
if previous, exists := bindings[slotName]; exists && (previous.Artifact != nil || source.Artifact != nil) {
|
||||||
|
return fmt.Errorf("%s reference slot %q has conflicting generated and external bindings", referenceTargetErrorContext(target), slotName)
|
||||||
|
}
|
||||||
|
binding := ReferenceBinding{LaneID: target.LaneID, SlotName: slotName, BindingSource: bindingSource}
|
||||||
|
if source.Artifact != nil {
|
||||||
|
binding.Artifact = source.Artifact
|
||||||
|
} else {
|
||||||
|
binding.Source = source.Path
|
||||||
|
}
|
||||||
|
bindings[slotName] = binding
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func referenceOverrideMatchesTarget(target referenceResolutionTarget, override ReferenceBinding) (bool, error) {
|
||||||
|
stage, laneID, err := normalizeReferenceOptionTarget(target.PipelineID, "override", override.Stage, override.LaneID)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return stage == target.Stage && laneID == target.LaneID, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func referenceUnbindMatchesTarget(target referenceResolutionTarget, unbind ReferenceUnbind) (bool, error) {
|
||||||
|
stage, laneID, err := normalizeReferenceOptionTarget(target.PipelineID, "unbind", unbind.Stage, unbind.LaneID)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return stage == target.Stage && laneID == target.LaneID, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeReferenceOptionTarget(pipelineID string, operation string, stage ModuleStage, laneID string) (ModuleStage, string, error) {
|
||||||
|
stage = ModuleStage(strings.TrimSpace(string(stage)))
|
||||||
|
if stage == "" {
|
||||||
|
stage = StageExtract
|
||||||
|
}
|
||||||
|
laneID = strings.TrimSpace(laneID)
|
||||||
|
|
||||||
|
switch stage {
|
||||||
|
case StageChunk:
|
||||||
|
if laneID != "" {
|
||||||
|
return "", "", fmt.Errorf("pipeline %q reference %s for chunk must not include a lane id", pipelineID, operation)
|
||||||
|
}
|
||||||
|
case StageExtract, StageMerge, StageNormalize:
|
||||||
|
if laneID == "" {
|
||||||
|
return "", "", fmt.Errorf("pipeline %q reference %s lane id must not be empty", pipelineID, operation)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return "", "", fmt.Errorf("pipeline %q reference %s stage %q is not supported", pipelineID, operation, stage)
|
||||||
|
}
|
||||||
|
return stage, laneID, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func sortedReferenceBindings(bindings map[string]ReferenceBinding) []ReferenceBinding {
|
||||||
|
keys := sortedReferenceBindingKeys(bindings)
|
||||||
|
resolved := make([]ReferenceBinding, 0, len(keys))
|
||||||
|
for _, slotName := range keys {
|
||||||
|
resolved = append(resolved, bindings[slotName])
|
||||||
|
}
|
||||||
|
return resolved
|
||||||
|
}
|
||||||
|
|
||||||
|
func referenceTargetErrorContext(target referenceResolutionTarget) string {
|
||||||
|
if target.LaneID != "" {
|
||||||
|
if target.StepID != "" {
|
||||||
|
return fmt.Sprintf("pipeline %q step %q lane %q %s module %q", target.PipelineID, target.StepID, target.LaneID, target.Stage, target.Module)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("pipeline %q lane %q %s module %q", target.PipelineID, target.LaneID, target.Stage, target.Module)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("pipeline %q %s module %q", target.PipelineID, target.Stage, target.Module)
|
||||||
|
}
|
||||||
|
|
||||||
|
func referenceTargetSlotLabel(target referenceResolutionTarget) string {
|
||||||
|
if target.LaneID != "" {
|
||||||
|
if target.StepID != "" {
|
||||||
|
return fmt.Sprintf("pipeline %q step %q lane %q %s reference slot", target.PipelineID, target.StepID, target.LaneID, target.Stage)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("pipeline %q lane %q %s reference slot", target.PipelineID, target.LaneID, target.Stage)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("pipeline %q %s reference slot", target.PipelineID, target.Stage)
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeReferenceMap(values map[string]ReferenceSource, keyName string) (map[string]ReferenceSource, error) {
|
||||||
|
if len(values) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
out := make(map[string]ReferenceSource, len(values))
|
||||||
|
rawSlotNames := sortedStringMapKeys(values)
|
||||||
|
for _, rawSlotName := range rawSlotNames {
|
||||||
|
rawSource := values[rawSlotName]
|
||||||
|
slotName := strings.TrimSpace(rawSlotName)
|
||||||
|
if slotName == "" {
|
||||||
|
return nil, fmt.Errorf("%s must not be empty", keyName)
|
||||||
|
}
|
||||||
|
if _, ok := out[slotName]; ok {
|
||||||
|
return nil, fmt.Errorf("%s %q is duplicated after trimming", keyName, slotName)
|
||||||
|
}
|
||||||
|
source := cloneReferenceSource(rawSource)
|
||||||
|
if source.Artifact == nil && strings.TrimSpace(source.Path) == "" {
|
||||||
|
return nil, fmt.Errorf("%s %q source must not be empty", keyName, slotName)
|
||||||
|
}
|
||||||
|
out[slotName] = source
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func sortedStringMapKeys(values map[string]ReferenceSource) []string {
|
||||||
|
if len(values) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
keys := make([]string, 0, len(values))
|
||||||
|
for key := range values {
|
||||||
|
keys = append(keys, key)
|
||||||
|
}
|
||||||
|
sort.Strings(keys)
|
||||||
|
return keys
|
||||||
|
}
|
||||||
|
|
||||||
|
func sortedReferenceBindingKeys(values map[string]ReferenceBinding) []string {
|
||||||
|
if len(values) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
keys := make([]string, 0, len(values))
|
||||||
|
for key := range values {
|
||||||
|
keys = append(keys, key)
|
||||||
|
}
|
||||||
|
sort.Strings(keys)
|
||||||
|
return keys
|
||||||
|
}
|
||||||
24
internal/framework/pipeline/reference_resolution_test.go
Normal file
24
internal/framework/pipeline/reference_resolution_test.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package pipeline
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNormalizeTargetReferenceSourceOwnsAndValidatesSelector(t *testing.T) {
|
||||||
|
target := referenceResolutionTarget{PipelineID: "demo", StepID: "consumer", LaneID: "lane", Stage: StageExtract, Module: "extractor"}
|
||||||
|
input := ReferenceSource{Artifact: &ArtifactReference{Step: " producer ", Lane: " output "}}
|
||||||
|
slot, source, err := normalizeTargetReferenceSource(target, " rules ", input)
|
||||||
|
if err != nil || slot != "rules" || source.Artifact.Step != "producer" || source.Artifact.Lane != "output" {
|
||||||
|
t.Fatalf("normalized = %q %#v, %v", slot, source, err)
|
||||||
|
}
|
||||||
|
source.Artifact.Step = "changed"
|
||||||
|
if input.Artifact.Step != " producer " {
|
||||||
|
t.Fatal("normalization mutated input selector")
|
||||||
|
}
|
||||||
|
for _, invalid := range []ReferenceSource{{}, {Path: "file", Artifact: &ArtifactReference{Step: "one", Lane: "two"}}, {Artifact: &ArtifactReference{Step: " ", Lane: "two"}}} {
|
||||||
|
if _, _, err := normalizeTargetReferenceSource(target, "rules", invalid); err == nil || !strings.Contains(err.Error(), `step "consumer" lane "lane"`) {
|
||||||
|
t.Fatalf("invalid source error = %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user