Separate pipeline reference and binding resolution
This commit is contained in:
@@ -12,176 +12,6 @@ import (
|
||||
"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 {
|
||||
CacheOverride *contracts.CacheMode
|
||||
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)
|
||||
}
|
||||
|
||||
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) {
|
||||
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)
|
||||
@@ -877,434 +630,6 @@ func cloneResolvedValidators(validators []ResolvedValidator) []ResolvedValidator
|
||||
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) {
|
||||
if len(options.Only) == 0 {
|
||||
return step.LaneIDs, nil
|
||||
|
||||
Reference in New Issue
Block a user