774 lines
30 KiB
Go
774 lines
30 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"reflect"
|
|
"sort"
|
|
"strings"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
)
|
|
|
|
type ResolveOptions struct {
|
|
CacheOverride *contracts.CacheMode
|
|
Only []string
|
|
LLMProfileOverride *string
|
|
ReferenceOverrides []ReferenceBinding
|
|
ReferenceUnbinds []ReferenceUnbind
|
|
}
|
|
|
|
type ReferenceBinding struct {
|
|
Stage ModuleStage `json:"stage,omitempty"`
|
|
LaneID string `json:"lane_id,omitempty"`
|
|
SlotName string `json:"slot_name"`
|
|
Source string `json:"source,omitempty"`
|
|
Artifact *ArtifactReference `json:"artifact,omitempty"`
|
|
BindingSource string `json:"binding_source,omitempty"`
|
|
}
|
|
|
|
type ReferenceUnbind struct {
|
|
Stage ModuleStage `json:"stage,omitempty"`
|
|
LaneID string `json:"lane_id,omitempty"`
|
|
SlotName string `json:"slot_name"`
|
|
}
|
|
|
|
type ResolvedReferenceTarget struct {
|
|
Stage ModuleStage `json:"stage"`
|
|
StepID string `json:"step_id,omitempty"`
|
|
LaneID string `json:"lane_id,omitempty"`
|
|
Module string `json:"module"`
|
|
Bindings []ReferenceBinding `json:"bindings,omitempty"`
|
|
ReferenceSet contracts.ReferenceSet `json:"-"`
|
|
}
|
|
|
|
type ResolvedArtifactLane struct {
|
|
ExtractControls contracts.ProducerControls
|
|
MergeControls contracts.ProducerControls
|
|
NormalizeControls contracts.ProducerControls
|
|
StepID string
|
|
ID string
|
|
ArtifactKind contracts.ArtifactKind `json:"artifact_kind,omitempty"`
|
|
ArtifactSchemaID string `json:"artifact_schema_id,omitempty"`
|
|
ArtifactSchemaName string `json:"artifact_schema_name,omitempty"`
|
|
ArtifactSchemaVersion string `json:"artifact_schema_version,omitempty"`
|
|
ArtifactSchemaDigest string `json:"artifact_schema_digest,omitempty"`
|
|
Extract ModuleBinding
|
|
ExtractExecutionClass contracts.ExecutionClass `json:"extract_execution_class"`
|
|
ExtractPrompts []string
|
|
Merge ModuleBinding
|
|
MergeExecutionClass contracts.ExecutionClass `json:"merge_execution_class"`
|
|
MergePrompts []string
|
|
Normalize ModuleBinding
|
|
NormalizeExecutionClass contracts.ExecutionClass `json:"normalize_execution_class"`
|
|
NormalizePrompts []string
|
|
Validators []ModuleBinding
|
|
ExtractReferences ResolvedReferenceTarget `json:"extract_references"`
|
|
MergeReferences ResolvedReferenceTarget `json:"merge_references"`
|
|
NormalizeReferences ResolvedReferenceTarget `json:"normalize_references"`
|
|
}
|
|
|
|
type ResolvedPipelineStep struct {
|
|
ID string `json:"id"`
|
|
ArtifactLanes []ResolvedArtifactLane `json:"artifact_lanes"`
|
|
}
|
|
|
|
type ResolvedValidatorChain struct {
|
|
Stage ModuleStage `json:"stage"`
|
|
LaneID string `json:"lane_id,omitempty"`
|
|
ModuleKey string `json:"module_key"`
|
|
Validators []ResolvedValidator `json:"validators"`
|
|
}
|
|
|
|
type ResolvedValidator struct {
|
|
Prompts []string `json:"prompts,omitempty"`
|
|
Binding ModuleBinding `json:"binding"`
|
|
ExecutionClass contracts.ExecutionClass `json:"execution_class"`
|
|
Target ValidatorTarget `json:"target,omitempty"`
|
|
ArtifactKind contracts.ArtifactKind `json:"artifact_kind,omitempty"`
|
|
}
|
|
|
|
type ResolvedPipeline struct {
|
|
ChunkControls contracts.ProducerControls
|
|
ID string
|
|
Digest string
|
|
Input ModuleBinding
|
|
InputExecutionClass contracts.ExecutionClass `json:"input_execution_class"`
|
|
Chunk ModuleBinding
|
|
ChunkExecutionClass contracts.ExecutionClass `json:"chunk_execution_class"`
|
|
ChunkPrompts []string
|
|
ChunkReferences ResolvedReferenceTarget `json:"chunk_references"`
|
|
Steps []ResolvedPipelineStep
|
|
ValidatorChains []ResolvedValidatorChain `json:"validator_chains"`
|
|
Output ModuleBinding
|
|
OutputExecutionClass contracts.ExecutionClass `json:"output_execution_class"`
|
|
}
|
|
|
|
// AllArtifactLanes returns lanes in deterministic step order for read-only
|
|
// discovery and identity construction.
|
|
func (resolved ResolvedPipeline) AllArtifactLanes() []ResolvedArtifactLane {
|
|
var lanes []ResolvedArtifactLane
|
|
for _, step := range resolved.Steps {
|
|
lanes = append(lanes, step.ArtifactLanes...)
|
|
}
|
|
return lanes
|
|
}
|
|
|
|
type ModuleCatalog struct {
|
|
Inputs *InputAdapterRegistry
|
|
Chunkers *ChunkerRegistry
|
|
ArtifactCodecs *ArtifactCodecRegistry
|
|
ArtifactEvidence *ArtifactEvidenceRegistry
|
|
Extractors *ExtractorRegistry
|
|
Mergers *MergerRegistry
|
|
Normalizers *NormalizerRegistry
|
|
Validators *ValidatorRegistry
|
|
ValidatorChains *ValidatorChainRegistry
|
|
Outputs *OutputEncoderRegistry
|
|
}
|
|
|
|
// ExecutionClass returns the registered execution class for a module selected
|
|
// by stage and key without constructing the module.
|
|
func (catalog ModuleCatalog) ExecutionClass(stage ModuleStage, key string) (contracts.ExecutionClass, bool) {
|
|
var executionClass contracts.ExecutionClass
|
|
var ok bool
|
|
|
|
switch stage {
|
|
case StageInput:
|
|
var spec ModuleSpec
|
|
spec, ok = catalog.Inputs.Spec(key)
|
|
executionClass = spec.ExecutionClass
|
|
case StageChunk:
|
|
var spec ModuleSpec
|
|
spec, ok = catalog.Chunkers.Spec(key)
|
|
executionClass = spec.ExecutionClass
|
|
case StageExtract:
|
|
var spec ModuleSpec
|
|
spec, ok = catalog.Extractors.Spec(key)
|
|
executionClass = spec.ExecutionClass
|
|
case StageMerge:
|
|
var spec ModuleSpec
|
|
spec, ok = catalog.Mergers.Spec(key)
|
|
executionClass = spec.ExecutionClass
|
|
case StageNormalize:
|
|
var spec ModuleSpec
|
|
spec, ok = catalog.Normalizers.Spec(key)
|
|
executionClass = spec.ExecutionClass
|
|
case StageValidate:
|
|
var spec ValidatorSpec
|
|
spec, ok = catalog.Validators.Spec(key)
|
|
executionClass = spec.ExecutionClass
|
|
case StageOutput:
|
|
var spec ModuleSpec
|
|
spec, ok = catalog.Outputs.Spec(key)
|
|
executionClass = spec.ExecutionClass
|
|
}
|
|
|
|
return executionClass, ok
|
|
}
|
|
|
|
func Binding(module string) ModuleBinding {
|
|
return ModuleBinding{Module: strings.TrimSpace(module)}
|
|
}
|
|
|
|
func ResolvePipeline(profile PipelineProfile, options ResolveOptions, catalog ModuleCatalog) (ResolvedPipeline, error) {
|
|
layout, err := LayoutPipeline(profile)
|
|
if err != nil {
|
|
return ResolvedPipeline{}, err
|
|
}
|
|
pipelineID := layout.ID
|
|
configuredLaneIDs := layout.ConfiguredLaneIDs
|
|
|
|
input, err := resolveBinding(profile.Input, "", fmt.Sprintf("pipeline %q input reference slot", pipelineID))
|
|
if err != nil {
|
|
return ResolvedPipeline{}, err
|
|
}
|
|
if input.Module == "" {
|
|
return ResolvedPipeline{}, fmt.Errorf("pipeline %q input module must not be empty", pipelineID)
|
|
}
|
|
inputModuleSpec, err := inputSpec(catalog, input.Module)
|
|
if err != nil {
|
|
return ResolvedPipeline{}, moduleLookupError(pipelineID, "", StageInput, input.Module, err)
|
|
}
|
|
|
|
capabilities := newCapabilitySet()
|
|
if missing, ok := capabilities.missing(inputModuleSpec.Requires); ok {
|
|
return ResolvedPipeline{}, capabilityError(pipelineID, "", StageInput, input.Module, missing)
|
|
}
|
|
capabilities.add(inputModuleSpec.Provides...)
|
|
|
|
targets, err := SelectPipelineTargets(layout, options.Only, catalog)
|
|
if err != nil {
|
|
return ResolvedPipeline{}, err
|
|
}
|
|
chunk, err := resolveBinding(targets.Chunk, DefaultChunkModule, fmt.Sprintf("pipeline %q chunk reference slot", pipelineID))
|
|
if err != nil {
|
|
return ResolvedPipeline{}, err
|
|
}
|
|
chunkSpec := targets.ChunkSpec
|
|
if missing, ok := capabilities.missing(chunkSpec.Requires); ok {
|
|
return ResolvedPipeline{}, capabilityError(pipelineID, "", StageChunk, chunk.Module, missing)
|
|
}
|
|
capabilities.add(chunkSpec.Provides...)
|
|
|
|
var allLanes []PipelineTargetLane
|
|
for _, step := range targets.Steps {
|
|
allLanes = append(allLanes, step.Lanes...)
|
|
}
|
|
if err := validatePipelineReferenceDefaults(pipelineID, profile.References, chunkSpec, allLanes); err != nil {
|
|
return ResolvedPipeline{}, err
|
|
}
|
|
for _, source := range profile.References {
|
|
if source.Artifact != nil {
|
|
return ResolvedPipeline{}, fmt.Errorf("pipeline %q pipeline-level references may not use generated artifact selectors", pipelineID)
|
|
}
|
|
}
|
|
|
|
chunkReferences, err := resolveReferenceTargetBindings(referenceResolutionTarget{
|
|
PipelineID: pipelineID,
|
|
Stage: StageChunk,
|
|
Module: chunk.Module,
|
|
Slots: chunkSpec.ReferenceSlots,
|
|
PipelineReferences: profile.References,
|
|
LocalReferences: chunk.References,
|
|
Options: options,
|
|
})
|
|
if err != nil {
|
|
return ResolvedPipeline{}, err
|
|
}
|
|
output, err := resolveBinding(profile.Output, DefaultOutputModule, fmt.Sprintf("pipeline %q output reference slot", pipelineID))
|
|
if err != nil {
|
|
return ResolvedPipeline{}, err
|
|
}
|
|
resolved := ResolvedPipeline{
|
|
ID: pipelineID,
|
|
Input: input,
|
|
InputExecutionClass: inputModuleSpec.ExecutionClass,
|
|
Chunk: chunk,
|
|
ChunkExecutionClass: chunkSpec.ExecutionClass,
|
|
ChunkPrompts: append([]string(nil), chunkSpec.Prompts...),
|
|
ChunkReferences: referenceTarget(StageChunk, "", chunk.Module, chunkReferences),
|
|
Output: output,
|
|
}
|
|
chunkValidatorChain, err := resolveValidatorChain(pipelineID, "", StageChunk, chunk.Module, chunk.Validators, "", nil, catalog)
|
|
if err != nil {
|
|
return ResolvedPipeline{}, err
|
|
}
|
|
resolved.ValidatorChains = append(resolved.ValidatorChains, chunkValidatorChain)
|
|
outputCapabilities := capabilities.clone()
|
|
for _, step := range targets.Steps {
|
|
stepID := step.ID
|
|
if referenceSourceMapsConflict(profile.References, step.References) {
|
|
return ResolvedPipeline{}, fmt.Errorf("pipeline %q step %q has a generated and external reference collision", pipelineID, stepID)
|
|
}
|
|
if err := validatePipelineReferenceDefaults(pipelineID, step.References, chunkSpec, step.Lanes); err != nil {
|
|
return ResolvedPipeline{}, err
|
|
}
|
|
stepReferences := mergeReferenceMaps(profile.References, step.References)
|
|
resolvedStep := ResolvedPipelineStep{ID: stepID}
|
|
for _, selected := range step.Lanes {
|
|
if !selected.Selected {
|
|
continue
|
|
}
|
|
lane, validatorChains, laneCapabilities, err := resolveArtifactLane(pipelineID, stepID, selected, stepReferences, options, capabilities, catalog)
|
|
if err != nil {
|
|
return ResolvedPipeline{}, err
|
|
}
|
|
resolvedStep.ArtifactLanes = append(resolvedStep.ArtifactLanes, lane)
|
|
resolved.ValidatorChains = append(resolved.ValidatorChains, validatorChains...)
|
|
outputCapabilities.addSet(laneCapabilities)
|
|
}
|
|
resolved.Steps = append(resolved.Steps, resolvedStep)
|
|
}
|
|
if err := validateGeneratedBindings(pipelineID, resolved, catalog); err != nil {
|
|
return ResolvedPipeline{}, err
|
|
}
|
|
|
|
outputSpec, err := outputSpec(catalog, resolved.Output.Module)
|
|
if err != nil {
|
|
return ResolvedPipeline{}, moduleLookupError(pipelineID, "", StageOutput, resolved.Output.Module, err)
|
|
}
|
|
if missing, ok := outputCapabilities.missing(outputSpec.Requires); ok {
|
|
return ResolvedPipeline{}, capabilityError(pipelineID, "", StageOutput, resolved.Output.Module, missing)
|
|
}
|
|
resolved.OutputExecutionClass = outputSpec.ExecutionClass
|
|
if err := applyEffectiveLLMProfiles(&resolved, profile.LLMProfile, options.LLMProfileOverride); err != nil {
|
|
return ResolvedPipeline{}, err
|
|
}
|
|
if err := applyEffectiveProducerControls(&resolved, profile, options.CacheOverride); err != nil {
|
|
return ResolvedPipeline{}, err
|
|
}
|
|
if err := validateResolvedOptions(resolved, catalog, configuredLaneIDs); err != nil {
|
|
return ResolvedPipeline{}, err
|
|
}
|
|
|
|
digest, err := resolvedPipelineDigest(resolved)
|
|
if err != nil {
|
|
return ResolvedPipeline{}, fmt.Errorf("pipeline %q digest: %w", pipelineID, err)
|
|
}
|
|
resolved.Digest = digest
|
|
return resolved, nil
|
|
}
|
|
|
|
func resolveArtifactLane(
|
|
pipelineID string,
|
|
stepID string,
|
|
selected PipelineTargetLane,
|
|
pipelineReferences map[string]ReferenceSource,
|
|
options ResolveOptions,
|
|
inherited capabilitySet,
|
|
catalog ModuleCatalog,
|
|
) (ResolvedArtifactLane, []ResolvedValidatorChain, capabilitySet, error) {
|
|
laneID, profile := selected.ID, selected.Profile
|
|
extract, err := resolveBinding(selected.Extract, "", fmt.Sprintf("pipeline %q step %q lane %q extract reference slot", pipelineID, stepID, laneID))
|
|
if err != nil {
|
|
return ResolvedArtifactLane{}, nil, nil, err
|
|
}
|
|
merge, err := resolveBinding(selected.Merge, DefaultMergeModule, fmt.Sprintf("pipeline %q step %q lane %q merge reference slot", pipelineID, stepID, laneID))
|
|
if err != nil {
|
|
return ResolvedArtifactLane{}, nil, nil, err
|
|
}
|
|
normalize, err := resolveBinding(selected.Normalize, DefaultNormalizeModule, fmt.Sprintf("pipeline %q step %q lane %q normalize reference slot", pipelineID, stepID, laneID))
|
|
if err != nil {
|
|
return ResolvedArtifactLane{}, nil, nil, err
|
|
}
|
|
validators, err := resolveBindings(profile.Validators, "", fmt.Sprintf("pipeline %q step %q lane %q validator reference slot", pipelineID, stepID, laneID))
|
|
if err != nil {
|
|
return ResolvedArtifactLane{}, nil, nil, err
|
|
}
|
|
lane := ResolvedArtifactLane{
|
|
StepID: strings.TrimSpace(stepID),
|
|
ID: laneID,
|
|
Extract: extract,
|
|
Merge: merge,
|
|
Normalize: normalize,
|
|
Validators: validators,
|
|
}
|
|
if lane.Extract.Module == "" {
|
|
return ResolvedArtifactLane{}, nil, nil, fmt.Errorf("pipeline %q lane %q extract module must not be empty", pipelineID, laneID)
|
|
}
|
|
|
|
capabilities := inherited.clone()
|
|
|
|
extractSpec := selected.ExtractSpec
|
|
if missing, ok := capabilities.missing(extractSpec.Requires); ok {
|
|
return ResolvedArtifactLane{}, nil, nil, capabilityError(pipelineID, laneID, StageExtract, lane.Extract.Module, missing)
|
|
}
|
|
artifactType, err := resolveArtifactIdentity(pipelineID, laneID, &lane, extractSpec, catalog)
|
|
if err != nil {
|
|
return ResolvedArtifactLane{}, nil, nil, err
|
|
}
|
|
if referenceSourceMapsConflict(profile.References, lane.Extract.References) {
|
|
return ResolvedArtifactLane{}, nil, nil, fmt.Errorf("pipeline %q step %q lane %q extract has a generated and external reference collision", pipelineID, stepID, laneID)
|
|
}
|
|
extractReferences := mergeReferenceMaps(profile.References, lane.Extract.References)
|
|
references, err := resolveReferenceTargetBindings(referenceResolutionTarget{
|
|
PipelineID: pipelineID,
|
|
StepID: strings.TrimSpace(stepID),
|
|
LaneID: laneID,
|
|
Stage: StageExtract,
|
|
Module: lane.Extract.Module,
|
|
Slots: extractSpec.ReferenceSlots,
|
|
PipelineReferences: pipelineReferences,
|
|
LocalReferences: extractReferences,
|
|
Options: options,
|
|
})
|
|
if err != nil {
|
|
return ResolvedArtifactLane{}, nil, nil, err
|
|
}
|
|
lane.ExtractReferences = referenceTarget(StageExtract, laneID, lane.Extract.Module, references)
|
|
lane.ExtractReferences.StepID = strings.TrimSpace(stepID)
|
|
lane.ExtractExecutionClass = extractSpec.ExecutionClass
|
|
lane.ExtractPrompts = append([]string(nil), extractSpec.Prompts...)
|
|
capabilities.add(extractSpec.Provides...)
|
|
|
|
mergeSpec := selected.MergeSpec
|
|
if selected.mergeType != artifactType {
|
|
return ResolvedArtifactLane{}, nil, nil, moduleLookupError(pipelineID, laneID, StageMerge, lane.Merge.Module, fmt.Errorf("artifact kind %q requires Go type %s, but merger %q variant uses %s", lane.ArtifactKind, typeName(artifactType), lane.Merge.Module, typeName(selected.mergeType)))
|
|
}
|
|
if missing, ok := capabilities.missing(mergeSpec.Requires); ok {
|
|
return ResolvedArtifactLane{}, nil, nil, capabilityError(pipelineID, laneID, StageMerge, lane.Merge.Module, missing)
|
|
}
|
|
mergeReferences, err := resolveReferenceTargetBindings(referenceResolutionTarget{
|
|
PipelineID: pipelineID,
|
|
StepID: strings.TrimSpace(stepID),
|
|
LaneID: laneID,
|
|
Stage: StageMerge,
|
|
Module: lane.Merge.Module,
|
|
Slots: mergeSpec.ReferenceSlots,
|
|
PipelineReferences: pipelineReferences,
|
|
LocalReferences: lane.Merge.References,
|
|
Options: options,
|
|
})
|
|
if err != nil {
|
|
return ResolvedArtifactLane{}, nil, nil, err
|
|
}
|
|
lane.MergeReferences = referenceTarget(StageMerge, laneID, lane.Merge.Module, mergeReferences)
|
|
lane.MergeReferences.StepID = strings.TrimSpace(stepID)
|
|
lane.MergeExecutionClass = mergeSpec.ExecutionClass
|
|
lane.MergePrompts = append([]string(nil), mergeSpec.Prompts...)
|
|
capabilities.add(mergeSpec.Provides...)
|
|
|
|
normalizeSpec := selected.NormalizeSpec
|
|
if selected.normalizeType != artifactType {
|
|
return ResolvedArtifactLane{}, nil, nil, moduleLookupError(pipelineID, laneID, StageNormalize, lane.Normalize.Module, fmt.Errorf("artifact kind %q requires Go type %s, but normalizer %q variant uses %s", lane.ArtifactKind, typeName(artifactType), lane.Normalize.Module, typeName(selected.normalizeType)))
|
|
}
|
|
if missing, ok := capabilities.missing(normalizeSpec.Requires); ok {
|
|
return ResolvedArtifactLane{}, nil, nil, capabilityError(pipelineID, laneID, StageNormalize, lane.Normalize.Module, missing)
|
|
}
|
|
normalizeReferences, err := resolveReferenceTargetBindings(referenceResolutionTarget{
|
|
PipelineID: pipelineID,
|
|
StepID: strings.TrimSpace(stepID),
|
|
LaneID: laneID,
|
|
Stage: StageNormalize,
|
|
Module: lane.Normalize.Module,
|
|
Slots: normalizeSpec.ReferenceSlots,
|
|
PipelineReferences: pipelineReferences,
|
|
LocalReferences: lane.Normalize.References,
|
|
Options: options,
|
|
})
|
|
if err != nil {
|
|
return ResolvedArtifactLane{}, nil, nil, err
|
|
}
|
|
lane.NormalizeReferences = referenceTarget(StageNormalize, laneID, lane.Normalize.Module, normalizeReferences)
|
|
lane.NormalizeReferences.StepID = strings.TrimSpace(stepID)
|
|
lane.NormalizeExecutionClass = normalizeSpec.ExecutionClass
|
|
lane.NormalizePrompts = append([]string(nil), normalizeSpec.Prompts...)
|
|
capabilities.add(normalizeSpec.Provides...)
|
|
|
|
if len(lane.Validators) > 0 {
|
|
return ResolvedArtifactLane{}, nil, nil, configuredValidatorsError(pipelineID, laneID)
|
|
}
|
|
|
|
extractValidatorChain, err := resolveValidatorChain(pipelineID, laneID, StageExtract, lane.Extract.Module, lane.Extract.Validators, lane.ArtifactKind, artifactType, catalog)
|
|
if err != nil {
|
|
return ResolvedArtifactLane{}, nil, nil, err
|
|
}
|
|
mergeValidatorChain, err := resolveValidatorChain(pipelineID, laneID, StageMerge, lane.Merge.Module, lane.Merge.Validators, lane.ArtifactKind, artifactType, catalog)
|
|
if err != nil {
|
|
return ResolvedArtifactLane{}, nil, nil, err
|
|
}
|
|
normalizeValidatorChain, err := resolveValidatorChain(pipelineID, laneID, StageNormalize, lane.Normalize.Module, lane.Normalize.Validators, lane.ArtifactKind, artifactType, catalog)
|
|
if err != nil {
|
|
return ResolvedArtifactLane{}, nil, nil, err
|
|
}
|
|
validatorChains := []ResolvedValidatorChain{extractValidatorChain, mergeValidatorChain, normalizeValidatorChain}
|
|
|
|
return lane, validatorChains, capabilities, nil
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func resolveArtifactIdentity(pipelineID, laneID string, lane *ResolvedArtifactLane, extractSpec ModuleSpec, catalog ModuleCatalog) (reflect.Type, error) {
|
|
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)
|
|
}
|
|
if catalog.Extractors == nil {
|
|
return nil, fmt.Errorf("pipeline %q lane %q extractor registry must not be nil", pipelineID, laneID)
|
|
}
|
|
extractor, ok := catalog.Extractors.typedEntry(lane.Extract.Module)
|
|
if !ok {
|
|
return nil, fmt.Errorf("pipeline %q lane %q extract module %q declares artifact kind %q without a typed registration", pipelineID, laneID, lane.Extract.Module, extractSpec.ArtifactKind)
|
|
}
|
|
if catalog.ArtifactCodecs == nil {
|
|
return nil, fmt.Errorf("pipeline %q lane %q artifact codec registry must not be nil for kind %q", pipelineID, laneID, extractSpec.ArtifactKind)
|
|
}
|
|
codecSpec, ok := catalog.ArtifactCodecs.Spec(extractSpec.ArtifactKind)
|
|
if !ok {
|
|
return nil, fmt.Errorf("pipeline %q lane %q artifact codec %q is not registered", pipelineID, laneID, extractSpec.ArtifactKind)
|
|
}
|
|
codecType, ok := catalog.ArtifactCodecs.valueType(extractSpec.ArtifactKind)
|
|
if !ok {
|
|
return nil, fmt.Errorf("pipeline %q lane %q artifact codec %q has no Go type", pipelineID, laneID, extractSpec.ArtifactKind)
|
|
}
|
|
if extractor.valueType != codecType {
|
|
return nil, artifactTypeMismatchError(pipelineID, laneID, StageExtract, lane.Extract.Module, extractSpec.ArtifactKind, codecType, extractor.valueType)
|
|
}
|
|
lane.ArtifactKind = codecSpec.Kind
|
|
lane.ArtifactSchemaID = codecSpec.Schema.ID
|
|
lane.ArtifactSchemaName = codecSpec.Schema.Name
|
|
lane.ArtifactSchemaVersion = codecSpec.Schema.Version
|
|
lane.ArtifactSchemaDigest = codecSpec.SchemaDigest
|
|
return codecType, nil
|
|
}
|
|
|
|
func validatorSpecForTarget(registry *ValidatorRegistry, stage ModuleStage, key string, kind contracts.ArtifactKind, expectedType reflect.Type) (ValidatorSpec, ValidatorTarget, error) {
|
|
key = strings.TrimSpace(key)
|
|
if stage == StageChunk {
|
|
if entry, ok := registry.chunkEntry(key); ok {
|
|
return entry.spec, ValidatorTargetChunk, nil
|
|
}
|
|
if entry, ok := registry.serializedEntry(key); ok && entry.spec.SupportsChunks {
|
|
return entry.spec.ValidatorSpec, ValidatorTargetSerialized, nil
|
|
}
|
|
if spec, ok := registry.Spec(key); ok {
|
|
return spec, "", nil
|
|
}
|
|
return ValidatorSpec{}, "", fmt.Errorf("references unknown validator %q for chunk target", key)
|
|
}
|
|
if kind == "" {
|
|
if spec, ok := registry.Spec(key); ok {
|
|
return spec, "", nil
|
|
}
|
|
return ValidatorSpec{}, "", fmt.Errorf("references unknown validator %q without an artifact kind", key)
|
|
}
|
|
if entry, ok := registry.typedEntry(key, kind); ok {
|
|
if entry.valueType != expectedType {
|
|
return ValidatorSpec{}, "", fmt.Errorf("artifact kind %q requires Go type %s, but validator %q variant uses %s", kind, typeName(expectedType), key, typeName(entry.valueType))
|
|
}
|
|
return entry.spec, ValidatorTargetTyped, nil
|
|
}
|
|
if entry, ok := registry.serializedEntry(key); ok && entry.spec.SupportsArtifacts {
|
|
return entry.spec.ValidatorSpec, ValidatorTargetSerialized, nil
|
|
}
|
|
if _, ok := registry.Spec(key); !ok {
|
|
return ValidatorSpec{}, "", fmt.Errorf("references unknown validator %q for artifact kind %q", key, kind)
|
|
}
|
|
return ValidatorSpec{}, "", missingArtifactVariantError("validator", key, kind, registry.registeredTypedKinds(key))
|
|
}
|
|
|
|
func missingArtifactVariantError(moduleType, key string, kind contracts.ArtifactKind, registered []contracts.ArtifactKind) error {
|
|
if len(registered) == 0 {
|
|
return fmt.Errorf("%s %q has no typed variant for artifact kind %q", moduleType, key, kind)
|
|
}
|
|
values := make([]string, len(registered))
|
|
for i, value := range registered {
|
|
values[i] = string(value)
|
|
}
|
|
return fmt.Errorf("%s %q has no typed variant for artifact kind %q; registered kinds: %s", moduleType, key, kind, strings.Join(values, ", "))
|
|
}
|
|
|
|
func artifactTypeMismatchError(pipelineID, laneID string, stage ModuleStage, module string, kind contracts.ArtifactKind, expected, actual reflect.Type) error {
|
|
return fmt.Errorf("pipeline %q lane %q %s module %q artifact kind %q requires Go type %s, got %s", pipelineID, laneID, stage, module, kind, typeName(expected), typeName(actual))
|
|
}
|
|
|
|
func typeName(value reflect.Type) string {
|
|
if value == nil {
|
|
return "<nil>"
|
|
}
|
|
return value.String()
|
|
}
|
|
|
|
func resolveValidatorChain(pipelineID string, laneID string, stage ModuleStage, module string, override ValidatorOverride, artifactKind contracts.ArtifactKind, artifactType reflect.Type, catalog ModuleCatalog) (ResolvedValidatorChain, error) {
|
|
chain := ResolvedValidatorChain{
|
|
Stage: stage,
|
|
LaneID: strings.TrimSpace(laneID),
|
|
ModuleKey: strings.TrimSpace(module),
|
|
}
|
|
if chain.ModuleKey == "" {
|
|
return ResolvedValidatorChain{}, fmt.Errorf("pipeline %q validator chain %q module key must not be empty", pipelineID, stage)
|
|
}
|
|
switch stage {
|
|
case StageChunk, StageExtract, StageMerge, StageNormalize:
|
|
default:
|
|
return ResolvedValidatorChain{}, fmt.Errorf("pipeline %q validator chain stage %q is not supported", pipelineID, stage)
|
|
}
|
|
|
|
var bindings []ModuleBinding
|
|
if override.Set {
|
|
bindings = cloneModuleBindings(override.Validators)
|
|
} else if catalog.ValidatorChains != nil {
|
|
bindings = catalog.ValidatorChains.Validators(stage, chain.ModuleKey)
|
|
}
|
|
if len(bindings) == 0 {
|
|
return chain, nil
|
|
}
|
|
if catalog.Validators == nil {
|
|
return ResolvedValidatorChain{}, fmt.Errorf("pipeline %q validator registry must not be nil for %s validator chain on module %q", pipelineID, stage, chain.ModuleKey)
|
|
}
|
|
chain.Validators = make([]ResolvedValidator, 0, len(bindings))
|
|
for _, validator := range bindings {
|
|
spec, target, err := validatorSpecForTarget(catalog.Validators, stage, validator.Module, artifactKind, artifactType)
|
|
if err != nil {
|
|
return ResolvedValidatorChain{}, fmt.Errorf("pipeline %q %s validator chain for module %q: %w", pipelineID, stage, chain.ModuleKey, err)
|
|
}
|
|
chain.Validators = append(chain.Validators, ResolvedValidator{
|
|
Binding: CloneModuleBinding(validator),
|
|
ExecutionClass: spec.ExecutionClass,
|
|
Prompts: append([]string(nil), spec.Prompts...),
|
|
Target: target,
|
|
ArtifactKind: artifactKind,
|
|
})
|
|
}
|
|
return chain, nil
|
|
}
|
|
|
|
func cloneResolvedValidatorChains(chains []ResolvedValidatorChain) []ResolvedValidatorChain {
|
|
if len(chains) == 0 {
|
|
return nil
|
|
}
|
|
out := make([]ResolvedValidatorChain, len(chains))
|
|
for i, chain := range chains {
|
|
out[i] = ResolvedValidatorChain{
|
|
Stage: chain.Stage,
|
|
LaneID: strings.TrimSpace(chain.LaneID),
|
|
ModuleKey: strings.TrimSpace(chain.ModuleKey),
|
|
Validators: cloneResolvedValidators(chain.Validators),
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func cloneResolvedValidators(validators []ResolvedValidator) []ResolvedValidator {
|
|
if len(validators) == 0 {
|
|
return nil
|
|
}
|
|
out := make([]ResolvedValidator, len(validators))
|
|
for i, validator := range validators {
|
|
out[i] = ResolvedValidator{
|
|
Binding: CloneModuleBinding(validator.Binding),
|
|
ExecutionClass: validator.ExecutionClass,
|
|
Prompts: append([]string(nil), validator.Prompts...),
|
|
Target: validator.Target,
|
|
ArtifactKind: validator.ArtifactKind,
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func selectedLayoutLaneIDs(pipelineID string, step PipelineLayoutStep, options ResolveOptions) ([]string, error) {
|
|
if len(options.Only) == 0 {
|
|
return step.LaneIDs, nil
|
|
}
|
|
|
|
selected := make(map[string]struct{}, len(options.Only))
|
|
for _, rawLaneID := range options.Only {
|
|
laneID := strings.TrimSpace(rawLaneID)
|
|
if laneID == "" {
|
|
return nil, fmt.Errorf("pipeline %q selected artifact lane id must not be empty", pipelineID)
|
|
}
|
|
if _, ok := step.Artifacts[laneID]; !ok {
|
|
return nil, fmt.Errorf("pipeline %q selected artifact lane %q is not declared", pipelineID, laneID)
|
|
}
|
|
selected[laneID] = struct{}{}
|
|
}
|
|
|
|
keys := make([]string, 0, len(selected))
|
|
for laneID := range selected {
|
|
keys = append(keys, laneID)
|
|
}
|
|
sort.Strings(keys)
|
|
return keys, nil
|
|
}
|
|
|
|
func resolvedPipelineDigest(resolved ResolvedPipeline) (string, error) {
|
|
withoutDigest := struct {
|
|
ChunkControls contracts.ProducerControls
|
|
ID string
|
|
Input ModuleBinding
|
|
InputExecutionClass contracts.ExecutionClass
|
|
Chunk ModuleBinding
|
|
ChunkExecutionClass contracts.ExecutionClass
|
|
ChunkPrompts []string
|
|
ChunkReferences ResolvedReferenceTarget
|
|
Steps []ResolvedPipelineStep
|
|
ValidatorChains []ResolvedValidatorChain
|
|
Output ModuleBinding
|
|
OutputExecutionClass contracts.ExecutionClass
|
|
}{
|
|
ID: resolved.ID,
|
|
ChunkControls: resolved.ChunkControls,
|
|
Input: resolved.Input,
|
|
InputExecutionClass: resolved.InputExecutionClass,
|
|
Chunk: resolved.Chunk,
|
|
ChunkExecutionClass: resolved.ChunkExecutionClass,
|
|
ChunkPrompts: append([]string(nil), resolved.ChunkPrompts...),
|
|
ChunkReferences: resolved.ChunkReferences,
|
|
Steps: resolved.Steps,
|
|
ValidatorChains: resolved.ValidatorChains,
|
|
Output: resolved.Output,
|
|
OutputExecutionClass: resolved.OutputExecutionClass,
|
|
}
|
|
encoded, err := json.Marshal(withoutDigest)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
sum := sha256.Sum256(encoded)
|
|
return "sha256:" + hex.EncodeToString(sum[:]), nil
|
|
}
|
|
|
|
func inputSpec(catalog ModuleCatalog, key string) (ModuleSpec, error) {
|
|
return registrySpec(catalog.Inputs, key)
|
|
}
|
|
|
|
func chunkerSpec(catalog ModuleCatalog, key string) (ModuleSpec, error) {
|
|
return registrySpec(catalog.Chunkers, key)
|
|
}
|
|
|
|
func extractorSpec(catalog ModuleCatalog, key string) (ModuleSpec, error) {
|
|
return registrySpec(catalog.Extractors, key)
|
|
}
|
|
|
|
func outputSpec(catalog ModuleCatalog, key string) (ModuleSpec, error) {
|
|
return registrySpec(catalog.Outputs, key)
|
|
}
|
|
|
|
type specRegistry interface {
|
|
Spec(key string) (ModuleSpec, bool)
|
|
}
|
|
|
|
func registrySpec(registry specRegistry, key string) (ModuleSpec, error) {
|
|
if registry == nil {
|
|
return ModuleSpec{}, fmt.Errorf("module %q is not registered", key)
|
|
}
|
|
spec, ok := registry.Spec(key)
|
|
if !ok {
|
|
return ModuleSpec{}, fmt.Errorf("module %q is not registered", key)
|
|
}
|
|
return spec, nil
|
|
}
|
|
|
|
func moduleLookupError(pipelineID, laneID string, stage ModuleStage, module string, err error) error {
|
|
if laneID != "" {
|
|
return fmt.Errorf("pipeline %q lane %q %s module %q: %w", pipelineID, laneID, stage, module, err)
|
|
}
|
|
return fmt.Errorf("pipeline %q %s module %q: %w", pipelineID, stage, module, err)
|
|
}
|
|
|
|
func capabilityError(pipelineID, laneID string, stage ModuleStage, module, capability string) error {
|
|
if laneID != "" {
|
|
return fmt.Errorf("pipeline %q lane %q %s module %q requires missing capability %q", pipelineID, laneID, stage, module, capability)
|
|
}
|
|
return fmt.Errorf("pipeline %q %s module %q requires missing capability %q", pipelineID, stage, module, capability)
|
|
}
|
|
|
|
type capabilitySet map[string]struct{}
|
|
|
|
func newCapabilitySet() capabilitySet {
|
|
return make(capabilitySet)
|
|
}
|
|
|
|
func (set capabilitySet) clone() capabilitySet {
|
|
copied := make(capabilitySet, len(set))
|
|
for capability := range set {
|
|
copied[capability] = struct{}{}
|
|
}
|
|
return copied
|
|
}
|
|
|
|
func (set capabilitySet) add(values ...string) {
|
|
for _, value := range values {
|
|
set[value] = struct{}{}
|
|
}
|
|
}
|
|
|
|
func (set capabilitySet) addSet(other capabilitySet) {
|
|
for value := range other {
|
|
set[value] = struct{}{}
|
|
}
|
|
}
|
|
|
|
func (set capabilitySet) missing(required []string) (string, bool) {
|
|
for _, capability := range required {
|
|
if _, ok := set[capability]; !ok {
|
|
return capability, true
|
|
}
|
|
}
|
|
return "", false
|
|
}
|