Route merge and normalization through core with domain migrations pending
This commit is contained in:
@@ -24,15 +24,15 @@ func NewTyped[T any](combine CombineFunc[T]) (*TypedMerger[T], error) {
|
||||
|
||||
func (m *TypedMerger[T]) Key() string { return Key }
|
||||
|
||||
func (m *TypedMerger[T]) Merge(ctx context.Context, req contracts.TypedMergeRequest[T]) (contracts.TypedMergeResult[T], error) {
|
||||
func (m *TypedMerger[T]) PrepareMerge(ctx context.Context, req contracts.MergeInput[T]) (contracts.PreparedOperation[T], error) {
|
||||
if m == nil || m.combine == nil {
|
||||
return contracts.TypedMergeResult[T]{}, mergerErrorf("merger must not be nil")
|
||||
return nil, mergerErrorf("merger must not be nil")
|
||||
}
|
||||
if ctx == nil {
|
||||
return contracts.TypedMergeResult[T]{}, mergerErrorf("context must not be nil")
|
||||
return nil, mergerErrorf("context must not be nil")
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return contracts.TypedMergeResult[T]{}, mergerErrorf("context error before merge: %w", err)
|
||||
return nil, mergerErrorf("context error before merge: %w", err)
|
||||
}
|
||||
values := make([]T, len(req.ExtractOutputs))
|
||||
for i, output := range req.ExtractOutputs {
|
||||
@@ -40,9 +40,9 @@ func (m *TypedMerger[T]) Merge(ctx context.Context, req contracts.TypedMergeRequ
|
||||
}
|
||||
value, err := m.combine(values)
|
||||
if err != nil {
|
||||
return contracts.TypedMergeResult[T]{}, mergerErrorf("combine values: %w", err)
|
||||
return nil, mergerErrorf("combine values: %w", err)
|
||||
}
|
||||
return contracts.TypedMergeResult[T]{Value: value}, nil
|
||||
return contracts.DeterministicOperation[T]{Evaluation: contracts.CandidateEvaluation[T]{Result: contracts.EvaluationView[T]{Candidate: &value}}}, nil
|
||||
}
|
||||
|
||||
func TypedModuleSpec(kind contracts.ArtifactKind) pipeline.ModuleSpec {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
||||
)
|
||||
|
||||
func TestTypedMergerCombinesValuesInFrameworkOrder(t *testing.T) {
|
||||
@@ -19,7 +20,11 @@ func TestTypedMergerCombinesValuesInFrameworkOrder(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err := merger.Merge(context.Background(), contracts.TypedMergeRequest[string]{ExtractOutputs: []contracts.ExtractArtifact[string]{{ChunkIndex: 0, Value: "first"}, {ChunkIndex: 1, Value: "second"}}})
|
||||
result, err := pipeline.ExecuteOperation(context.Background(), pipeline.ExecutionContext[string]{Assess: func(context.Context, string) (contracts.CandidateAssessment, error) {
|
||||
return contracts.CandidateAssessment{}, nil
|
||||
}}, func(ctx context.Context) (contracts.PreparedOperation[string], error) {
|
||||
return merger.PrepareMerge(ctx, contracts.MergeInput[string]{ExtractOutputs: []contracts.ExtractArtifact[string]{{ChunkIndex: 0, Value: "first"}, {ChunkIndex: 1, Value: "second"}}})
|
||||
})
|
||||
if err != nil || result.Value != "firstsecond" {
|
||||
t.Fatalf("result=%#v err=%v", result, err)
|
||||
}
|
||||
@@ -35,16 +40,16 @@ func TestTypedMergerPropagatesApplicationErrorWithoutRetry(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err := merger.Merge(context.Background(), contracts.TypedMergeRequest[string]{
|
||||
Operation: contracts.ProducerOperation[string]{
|
||||
Controls: contracts.ProducerControls{Mode: contracts.ModePermissive, MaxAdditionalTurns: 3},
|
||||
Assess: func(context.Context, string) (contracts.CandidateAssessment, error) {
|
||||
t.Fatal("merger invoked assessment instead of returning its operation error")
|
||||
return contracts.CandidateAssessment{}, nil
|
||||
},
|
||||
result, err := pipeline.ExecuteOperation(context.Background(), pipeline.ExecutionContext[string]{
|
||||
Controls: contracts.ProducerControls{Mode: contracts.ModePermissive, MaxAdditionalTurns: 3},
|
||||
Assess: func(context.Context, string) (contracts.CandidateAssessment, error) {
|
||||
t.Fatal("merger invoked assessment instead of returning its operation error")
|
||||
return contracts.CandidateAssessment{}, nil
|
||||
},
|
||||
}, func(ctx context.Context) (contracts.PreparedOperation[string], error) {
|
||||
return merger.PrepareMerge(ctx, contracts.MergeInput[string]{})
|
||||
})
|
||||
if !errors.Is(err, failure) || calls != 1 || result.Outcome.Disposition != "" || result.Fallback != nil {
|
||||
if !errors.Is(err, failure) || calls != 1 || result.Outcome.Disposition != "" {
|
||||
t.Fatalf("Merge() = %#v, %v; combine calls = %d", result, err, calls)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,17 +14,18 @@ func NewTyped[T any]() *TypedNormalizer[T] { return &TypedNormalizer[T]{} }
|
||||
func (n *TypedNormalizer[T]) Key() string { return Key }
|
||||
func (n *TypedNormalizer[T]) ReferenceSlots() []contracts.ReferenceSlot { return nil }
|
||||
|
||||
func (n *TypedNormalizer[T]) Normalize(ctx context.Context, req contracts.TypedNormalizeRequest[T]) (contracts.TypedNormalizeResult[T], error) {
|
||||
func (n *TypedNormalizer[T]) PrepareNormalize(ctx context.Context, req contracts.NormalizeInput[T]) (contracts.PreparedOperation[T], error) {
|
||||
if n == nil {
|
||||
return contracts.TypedNormalizeResult[T]{}, normalizerErrorf("normalizer must not be nil")
|
||||
return nil, normalizerErrorf("normalizer must not be nil")
|
||||
}
|
||||
if ctx == nil {
|
||||
return contracts.TypedNormalizeResult[T]{}, normalizerErrorf("context must not be nil")
|
||||
return nil, normalizerErrorf("context must not be nil")
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return contracts.TypedNormalizeResult[T]{}, normalizerErrorf("context error before normalize: %w", err)
|
||||
return nil, normalizerErrorf("context error before normalize: %w", err)
|
||||
}
|
||||
return contracts.TypedNormalizeResult[T]{Value: req.MergeOutput.Value}, nil
|
||||
value := req.MergeOutput.Value
|
||||
return contracts.DeterministicOperation[T]{Evaluation: contracts.CandidateEvaluation[T]{Result: contracts.EvaluationView[T]{Candidate: &value}}}, nil
|
||||
}
|
||||
|
||||
func TypedModuleSpec(kind contracts.ArtifactKind) pipeline.ModuleSpec {
|
||||
|
||||
@@ -7,27 +7,27 @@ import (
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
||||
)
|
||||
|
||||
func TestTypedNormalizerReturnsIncomingValueForRunnerAssessment(t *testing.T) {
|
||||
value := []string{"second", "first", "first"}
|
||||
request := contracts.TypedNormalizeRequest[[]string]{
|
||||
request := contracts.NormalizeInput[[]string]{
|
||||
MergeOutput: contracts.MergeArtifact[[]string]{Value: value},
|
||||
Operation: contracts.ProducerOperation[[]string]{
|
||||
Controls: contracts.ProducerControls{Mode: contracts.ModePermissive, MaxAdditionalTurns: 3},
|
||||
Assess: func(context.Context, []string) (contracts.CandidateAssessment, error) {
|
||||
t.Fatal("deterministic normalizer invoked the runner's assessor")
|
||||
return contracts.CandidateAssessment{}, nil
|
||||
},
|
||||
},
|
||||
}
|
||||
result, err := NewTyped[[]string]().Normalize(context.Background(), request)
|
||||
if err != nil || !reflect.DeepEqual(result.Value, value) || result.Outcome.Disposition != "" || result.Fallback != nil {
|
||||
t.Fatalf("Normalize() = %#v, %v; want unchanged value for direct runner assessment", result, err)
|
||||
assessments := 0
|
||||
result, err := pipeline.ExecuteOperation(context.Background(), pipeline.ExecutionContext[[]string]{Assess: func(context.Context, []string) (contracts.CandidateAssessment, error) {
|
||||
assessments++
|
||||
return contracts.CandidateAssessment{}, nil
|
||||
}}, func(ctx context.Context) (contracts.PreparedOperation[[]string], error) {
|
||||
return NewTyped[[]string]().PrepareNormalize(ctx, request)
|
||||
})
|
||||
if err != nil || !reflect.DeepEqual(result.Value, value) || result.Outcome.Disposition != contracts.ProducerAccepted || assessments != 1 {
|
||||
t.Fatalf("Normalize() = %#v, %v; assessments=%d", result, err, assessments)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
if _, err := NewTyped[[]string]().Normalize(ctx, request); !errors.Is(err, context.Canceled) {
|
||||
if _, err := NewTyped[[]string]().PrepareNormalize(ctx, request); !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("Normalize() after cancellation = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user