2026-11-25 15:05:13 +00:00
|
|
|
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.
|
2026-11-25 15:15:29 +00:00
|
|
|
func ExecuteOperation[T any](ctx context.Context, execution ExecutionContext[T], prepare contracts.Preparation[T]) (result OperationResult[T], failure error) {
|
2026-11-25 15:05:13 +00:00
|
|
|
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")
|
|
|
|
|
}
|
2026-11-25 15:15:29 +00:00
|
|
|
if execution.Execution.Capture != nil {
|
|
|
|
|
defer func() {
|
|
|
|
|
errorText := ""
|
|
|
|
|
if failure != nil {
|
|
|
|
|
errorText = failure.Error()
|
|
|
|
|
}
|
|
|
|
|
if err := execution.Execution.RecordEvidence("operation-terminal.json", map[string]any{"outcome": result.Outcome, "error": errorText}); err != nil {
|
|
|
|
|
result = empty
|
|
|
|
|
failure = errors.Join(failure, err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
}
|
2026-11-25 15:05:13 +00:00
|
|
|
operation, err := prepare(ctx)
|
|
|
|
|
if canceled := ctx.Err(); canceled != nil {
|
|
|
|
|
return empty, canceled
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return empty, err
|
|
|
|
|
}
|
2026-11-25 15:15:29 +00:00
|
|
|
if execution.Execution.Capture != nil {
|
|
|
|
|
if err := execution.Execution.RecordEvidence("operation-preparation.json", map[string]any{"operation": fmt.Sprintf("%T", operation)}); err != nil {
|
|
|
|
|
return empty, err
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-25 15:05:13 +00:00
|
|
|
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]:
|
2026-11-25 15:15:29 +00:00
|
|
|
if err := recordDomainEvidence(execution.Execution, "preparation-domain.json", prepared.Evidence); err != nil {
|
|
|
|
|
return empty, err
|
|
|
|
|
}
|
2026-11-25 15:05:13 +00:00
|
|
|
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")
|
|
|
|
|
}
|
2026-11-25 15:15:29 +00:00
|
|
|
if err := recordDomainEvidence(execution.Execution, "preparation-domain.json", prepared.Evidence); err != nil {
|
|
|
|
|
return empty, err
|
|
|
|
|
}
|
2026-11-25 15:05:13 +00:00
|
|
|
return finishOperation(ctx, execution.Controls.Mode, prepared.Cause, nil, contracts.CandidateAssessment{}, prepared.Fallback, prepared.Diagnostics)
|
|
|
|
|
case contracts.NativeOperation[T]:
|
2026-11-25 15:08:28 +00:00
|
|
|
return executeNative(ctx, execution, prepared)
|
2026-11-25 15:05:13 +00:00
|
|
|
case *contracts.NativeOperation[T]:
|
2026-11-25 15:08:28 +00:00
|
|
|
if prepared == nil {
|
|
|
|
|
return empty, errors.New("native operation must not be nil")
|
|
|
|
|
}
|
|
|
|
|
return executeNative(ctx, execution, *prepared)
|
2026-11-25 15:05:13 +00:00
|
|
|
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)
|
2026-11-25 15:15:29 +00:00
|
|
|
if execution.Execution.Capture != nil {
|
|
|
|
|
if captureErr := captureEvaluation(execution.Execution, "deterministic", operation.Evaluation, assessment, view.Guidance, err == nil && assessment.Accepted() && len(view.Guidance) == 0, err); captureErr != nil {
|
|
|
|
|
return empty, errors.Join(ctx.Err(), err, captureErr)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-25 15:05:13 +00:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-25 15:15:29 +00:00
|
|
|
// recordDomainEvidence invokes optional domain projections only when capture
|
|
|
|
|
// is enabled. Projection and writing failures are required-capture failures.
|
|
|
|
|
func recordDomainEvidence(execution contracts.ExecutionOptions, name string, project func() (any, error)) error {
|
|
|
|
|
if execution.Capture == nil || project == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
value, err := project()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return &contracts.CaptureError{Cause: err}
|
|
|
|
|
}
|
|
|
|
|
return execution.RecordEvidence(name, value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func captureEvaluation[T any](execution contracts.ExecutionOptions, id string, evaluation contracts.Evaluation[T], assessment contracts.CandidateAssessment, guidance []string, accepted bool, failure error) error {
|
|
|
|
|
if execution.Capture == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
var candidate bool
|
|
|
|
|
if evaluation != nil {
|
|
|
|
|
view := evaluation.View()
|
|
|
|
|
candidate = view.Candidate != nil
|
|
|
|
|
if err := recordDomainEvidence(execution, "interpretations/"+id+"-domain.json", view.Evidence); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
errorText := ""
|
|
|
|
|
if failure != nil {
|
|
|
|
|
errorText = failure.Error()
|
|
|
|
|
}
|
|
|
|
|
return execution.RecordEvidence("interpretations/"+id+".json", map[string]any{
|
|
|
|
|
"usable_candidate": candidate, "accepted": accepted, "correction_guidance": guidance,
|
|
|
|
|
"assessment": assessmentEvidence(assessment), "error": errorText,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-25 15:05:13 +00:00
|
|
|
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
|
|
|
|
|
}
|