125 lines
5.0 KiB
Go
125 lines
5.0 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
"go.maximumdirect.net/promptkit"
|
|
)
|
|
|
|
// ExecutionContext binds framework-owned collaborators for one operation. It
|
|
// is supplied by the runner or a direct caller, never by domain preparation.
|
|
type ExecutionContext[T any] struct {
|
|
Engine *promptkit.Engine
|
|
Controls contracts.ProducerControls
|
|
Assess contracts.CandidateAssessor[T]
|
|
Execution contracts.ExecutionOptions
|
|
ReuseEligible bool
|
|
Cache *contracts.ProducerCache[T]
|
|
}
|
|
|
|
// OperationResult contains core's final value and its associated assessment
|
|
// and domain evaluation, if any. A fallback is never eligible for reuse.
|
|
type OperationResult[T any] struct {
|
|
Value T
|
|
Evaluation contracts.Evaluation[T]
|
|
Assessment contracts.CandidateAssessment
|
|
Outcome contracts.ProducerOutcome
|
|
Diagnostics []contracts.ProducerDiagnostic
|
|
}
|
|
|
|
// ExecuteOperation invokes preparation once and executes its selected form.
|
|
func ExecuteOperation[T any](ctx context.Context, execution ExecutionContext[T], prepare contracts.Preparation[T]) (OperationResult[T], error) {
|
|
var empty OperationResult[T]
|
|
if ctx == nil {
|
|
return empty, errors.New("operation context must not be nil")
|
|
}
|
|
if err := ctx.Err(); err != nil {
|
|
return empty, err
|
|
}
|
|
if prepare == nil {
|
|
return empty, errors.New("operation preparation must be supplied")
|
|
}
|
|
operation, err := prepare(ctx)
|
|
if canceled := ctx.Err(); canceled != nil {
|
|
return empty, canceled
|
|
}
|
|
if err != nil {
|
|
return empty, err
|
|
}
|
|
switch prepared := operation.(type) {
|
|
case contracts.DeterministicOperation[T]:
|
|
return executeDeterministic(ctx, execution, prepared)
|
|
case *contracts.DeterministicOperation[T]:
|
|
if prepared == nil {
|
|
return empty, errors.New("deterministic operation must not be nil")
|
|
}
|
|
return executeDeterministic(ctx, execution, *prepared)
|
|
case contracts.PreparationLimit[T]:
|
|
return finishOperation(ctx, execution.Controls.Mode, prepared.Cause, nil, contracts.CandidateAssessment{}, prepared.Fallback, prepared.Diagnostics)
|
|
case *contracts.PreparationLimit[T]:
|
|
if prepared == nil {
|
|
return empty, errors.New("preparation limit must not be nil")
|
|
}
|
|
return finishOperation(ctx, execution.Controls.Mode, prepared.Cause, nil, contracts.CandidateAssessment{}, prepared.Fallback, prepared.Diagnostics)
|
|
case contracts.NativeOperation[T]:
|
|
return empty, errors.New("native operation execution is not available")
|
|
case *contracts.NativeOperation[T]:
|
|
return empty, errors.New("native operation execution is not available")
|
|
default:
|
|
return empty, fmt.Errorf("unsupported prepared operation %T", operation)
|
|
}
|
|
}
|
|
|
|
func executeDeterministic[T any](ctx context.Context, execution ExecutionContext[T], operation contracts.DeterministicOperation[T]) (OperationResult[T], error) {
|
|
var empty OperationResult[T]
|
|
if operation.Evaluation == nil || operation.Evaluation.View().Candidate == nil {
|
|
return empty, errors.New("deterministic operation must provide a candidate")
|
|
}
|
|
if execution.Assess == nil {
|
|
return empty, errors.New("operation assessment must be supplied")
|
|
}
|
|
view := operation.Evaluation.View()
|
|
assessment, err := execution.Assess(ctx, *view.Candidate)
|
|
if canceled := ctx.Err(); canceled != nil {
|
|
return empty, canceled
|
|
}
|
|
if err != nil {
|
|
return empty, err
|
|
}
|
|
if !assessment.Accepted() || len(view.Guidance) > 0 {
|
|
return finishOperation(ctx, execution.Controls.Mode, assessment.Err(), operation.Evaluation, assessment, operation.Fallback, operation.Diagnostics)
|
|
}
|
|
outcome := contracts.ProducerOutcome{Disposition: contracts.ProducerAccepted, ReuseEligible: execution.ReuseEligible}
|
|
return completeOperation(*view.Candidate, operation.Evaluation, assessment, outcome, operation.Diagnostics)
|
|
}
|
|
|
|
func finishOperation[T any](ctx context.Context, mode contracts.ExecutionMode, cause error, evaluation contracts.Evaluation[T], assessment contracts.CandidateAssessment, fallback contracts.Fallback[T], diagnostics contracts.TerminalDiagnostics[T]) (OperationResult[T], error) {
|
|
var construct func(context.Context) (T, error)
|
|
if fallback != nil {
|
|
construct = func(ctx context.Context) (T, error) { return fallback(ctx, evaluation) }
|
|
}
|
|
value, outcome, err := FinishProducerFailure(ctx, mode, cause, construct)
|
|
if err != nil {
|
|
return OperationResult[T]{}, err
|
|
}
|
|
return completeOperation(value, evaluation, assessment, outcome, diagnostics)
|
|
}
|
|
|
|
func completeOperation[T any](value T, evaluation contracts.Evaluation[T], assessment contracts.CandidateAssessment, outcome contracts.ProducerOutcome, project contracts.TerminalDiagnostics[T]) (OperationResult[T], error) {
|
|
result := OperationResult[T]{Value: value, Evaluation: evaluation, Assessment: assessment, Outcome: outcome}
|
|
if evaluation != nil {
|
|
result.Diagnostics = contracts.CloneProducerDiagnostics(evaluation.View().Diagnostics)
|
|
}
|
|
if project != nil {
|
|
diagnostics, err := project(evaluation, outcome)
|
|
if err != nil {
|
|
return OperationResult[T]{}, err
|
|
}
|
|
result.Diagnostics = append(result.Diagnostics, diagnostics...)
|
|
}
|
|
return result, nil
|
|
}
|