Execute deterministic prepared operations in core
This commit is contained in:
@@ -237,6 +237,10 @@ and `go test ./internal/modules/dnd/validate/scenedescriptions/combat_semantics`
|
||||
|
||||
## Stage 3 — Execute Preparation And Deterministic Outcomes In Core
|
||||
|
||||
**Status:** Complete. Native operation execution and caller migration remain
|
||||
assigned to the subsequent work; the new entry point currently handles only
|
||||
deterministic candidates and explicit preparation limits.
|
||||
|
||||
**Goal:** Implement the core entry point and terminal policy before native work.
|
||||
|
||||
**Inspect:** `pipeline/{producer_outcome,runner_operation,candidate_assessment}.go`
|
||||
|
||||
124
internal/framework/pipeline/operation.go
Normal file
124
internal/framework/pipeline/operation.go
Normal file
@@ -0,0 +1,124 @@
|
||||
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
|
||||
}
|
||||
111
internal/framework/pipeline/operation_test.go
Normal file
111
internal/framework/pipeline/operation_test.go
Normal file
@@ -0,0 +1,111 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
)
|
||||
|
||||
func TestOperationDeterministicAssessmentAndFallback(t *testing.T) {
|
||||
for _, accept := range []bool{true, false} {
|
||||
for _, mode := range []contracts.ExecutionMode{contracts.ModeStrict, contracts.ModePermissive} {
|
||||
prepared, assessed, fallbacks := 0, 0, 0
|
||||
candidate := "candidate"
|
||||
evaluation := contracts.CandidateEvaluation[string]{Result: contracts.EvaluationView[string]{Candidate: &candidate}}
|
||||
execution := ExecutionContext[string]{Controls: contracts.ProducerControls{Mode: mode}, ReuseEligible: true,
|
||||
Assess: func(_ context.Context, value string) (contracts.CandidateAssessment, error) {
|
||||
assessed++
|
||||
if value != candidate {
|
||||
t.Fatalf("candidate = %q", value)
|
||||
}
|
||||
if accept {
|
||||
return contracts.CandidateAssessment{}, nil
|
||||
}
|
||||
return contracts.CandidateAssessment{Validators: []contracts.ValidatorAssessment{{ValidatorName: "rule", Decision: contracts.AssessmentRejected}}}, nil
|
||||
},
|
||||
}
|
||||
result, err := ExecuteOperation(context.Background(), execution, func(context.Context) (contracts.PreparedOperation[string], error) {
|
||||
prepared++
|
||||
return contracts.DeterministicOperation[string]{Evaluation: evaluation, Fallback: func(_ context.Context, final contracts.Evaluation[string]) (string, error) {
|
||||
fallbacks++
|
||||
if final == nil || *final.View().Candidate != candidate {
|
||||
t.Fatal("fallback lost the final evaluation")
|
||||
}
|
||||
return "supported", nil
|
||||
}}, nil
|
||||
})
|
||||
if prepared != 1 || assessed != 1 {
|
||||
t.Fatalf("preparations=%d assessments=%d", prepared, assessed)
|
||||
}
|
||||
switch {
|
||||
case accept:
|
||||
if err != nil || result.Value != candidate || result.Outcome.Disposition != contracts.ProducerAccepted || !result.Outcome.ReuseEligible || fallbacks != 0 {
|
||||
t.Fatalf("accepted = %#v %v", result, err)
|
||||
}
|
||||
case mode == contracts.ModeStrict:
|
||||
if err == nil || fallbacks != 0 {
|
||||
t.Fatalf("strict = %#v %v", result, err)
|
||||
}
|
||||
default:
|
||||
if err != nil || result.Value != "supported" || result.Outcome.Disposition != contracts.ProducerFallback || result.Outcome.ReuseEligible || fallbacks != 1 {
|
||||
t.Fatalf("fallback = %#v %v", result, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestOperationPreparationErrorAndLimitRemainDistinct(t *testing.T) {
|
||||
cause := errors.New("preparation failed")
|
||||
assessed, fallbacks := 0, 0
|
||||
execution := ExecutionContext[string]{Controls: contracts.ProducerControls{Mode: contracts.ModePermissive}, Assess: func(context.Context, string) (contracts.CandidateAssessment, error) {
|
||||
assessed++
|
||||
return contracts.CandidateAssessment{}, nil
|
||||
}}
|
||||
fallback := func(_ context.Context, evaluation contracts.Evaluation[string]) (string, error) {
|
||||
fallbacks++
|
||||
if evaluation != nil {
|
||||
t.Fatal("limit has an evaluation")
|
||||
}
|
||||
return "supported", nil
|
||||
}
|
||||
_, err := ExecuteOperation(context.Background(), execution, func(context.Context) (contracts.PreparedOperation[string], error) { return nil, cause })
|
||||
if !errors.Is(err, cause) || fallbacks != 0 {
|
||||
t.Fatalf("ordinary error = %v, fallbacks=%d", err, fallbacks)
|
||||
}
|
||||
result, err := ExecuteOperation(context.Background(), execution, func(context.Context) (contracts.PreparedOperation[string], error) {
|
||||
return contracts.PreparationLimit[string]{Cause: cause, Fallback: fallback}, nil
|
||||
})
|
||||
if err != nil || result.Value != "supported" || result.Outcome.Disposition != contracts.ProducerFallback || result.Outcome.ReuseEligible || fallbacks != 1 || assessed != 0 {
|
||||
t.Fatalf("limit = %#v %v, assessments=%d fallbacks=%d", result, err, assessed, fallbacks)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOperationAssessmentFailureAndCancellationAreFatal(t *testing.T) {
|
||||
for _, cancel := range []bool{false, true} {
|
||||
ctx, stop := context.WithCancel(context.Background())
|
||||
failure := errors.New("assessment contract failure")
|
||||
candidate := 1
|
||||
result, err := ExecuteOperation(ctx, ExecutionContext[int]{Controls: contracts.ProducerControls{Mode: contracts.ModePermissive}, Assess: func(context.Context, int) (contracts.CandidateAssessment, error) {
|
||||
if cancel {
|
||||
stop()
|
||||
}
|
||||
return contracts.CandidateAssessment{}, failure
|
||||
}}, func(context.Context) (contracts.PreparedOperation[int], error) {
|
||||
return contracts.DeterministicOperation[int]{Evaluation: contracts.CandidateEvaluation[int]{Result: contracts.EvaluationView[int]{Candidate: &candidate}}, Fallback: func(context.Context, contracts.Evaluation[int]) (int, error) {
|
||||
t.Fatal("fatal failure invoked fallback")
|
||||
return 0, nil
|
||||
}}, nil
|
||||
})
|
||||
stop()
|
||||
want := failure
|
||||
if cancel {
|
||||
want = context.Canceled
|
||||
}
|
||||
if !errors.Is(err, want) || result.Outcome.Disposition != "" {
|
||||
t.Fatalf("fatal result = %#v %v", result, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user