Execute native operations and corrections in core
This commit is contained in:
@@ -237,9 +237,7 @@ and `go test ./internal/modules/dnd/validate/scenedescriptions/combat_semantics`
|
|||||||
|
|
||||||
## Stage 3 — Execute Preparation And Deterministic Outcomes In Core
|
## Stage 3 — Execute Preparation And Deterministic Outcomes In Core
|
||||||
|
|
||||||
**Status:** Complete. Native operation execution and caller migration remain
|
**Status:** Complete.
|
||||||
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.
|
**Goal:** Implement the core entry point and terminal policy before native work.
|
||||||
|
|
||||||
@@ -268,6 +266,10 @@ permissive behavior is neither widened nor removed.
|
|||||||
|
|
||||||
## Stage 4 — Move Native Evaluation And Continuation Into The Executor
|
## Stage 4 — Move Native Evaluation And Continuation Into The Executor
|
||||||
|
|
||||||
|
**Status:** Complete. Existing integrations still use the previous producer
|
||||||
|
entry point until their scheduled migrations; reuse and capture binding for the
|
||||||
|
new entry point remain assigned to the next work.
|
||||||
|
|
||||||
**Goal:** Own model production and replace coupled outer candidate variables.
|
**Goal:** Own model production and replace coupled outer candidate variables.
|
||||||
|
|
||||||
**Inspect:** `pipeline/producer.go`, native continuation/cancellation tests, and
|
**Inspect:** `pipeline/producer.go`, native continuation/cancellation tests, and
|
||||||
|
|||||||
@@ -65,9 +65,12 @@ func ExecuteOperation[T any](ctx context.Context, execution ExecutionContext[T],
|
|||||||
}
|
}
|
||||||
return finishOperation(ctx, execution.Controls.Mode, prepared.Cause, nil, contracts.CandidateAssessment{}, prepared.Fallback, prepared.Diagnostics)
|
return finishOperation(ctx, execution.Controls.Mode, prepared.Cause, nil, contracts.CandidateAssessment{}, prepared.Fallback, prepared.Diagnostics)
|
||||||
case contracts.NativeOperation[T]:
|
case contracts.NativeOperation[T]:
|
||||||
return empty, errors.New("native operation execution is not available")
|
return executeNative(ctx, execution, prepared)
|
||||||
case *contracts.NativeOperation[T]:
|
case *contracts.NativeOperation[T]:
|
||||||
return empty, errors.New("native operation execution is not available")
|
if prepared == nil {
|
||||||
|
return empty, errors.New("native operation must not be nil")
|
||||||
|
}
|
||||||
|
return executeNative(ctx, execution, *prepared)
|
||||||
default:
|
default:
|
||||||
return empty, fmt.Errorf("unsupported prepared operation %T", operation)
|
return empty, fmt.Errorf("unsupported prepared operation %T", operation)
|
||||||
}
|
}
|
||||||
|
|||||||
157
internal/framework/pipeline/operation_native.go
Normal file
157
internal/framework/pipeline/operation_native.go
Normal file
@@ -0,0 +1,157 @@
|
|||||||
|
package pipeline
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||||
|
"go.maximumdirect.net/promptkit"
|
||||||
|
)
|
||||||
|
|
||||||
|
type nativeEvaluation[T any] struct {
|
||||||
|
evaluation contracts.Evaluation[T]
|
||||||
|
assessment contracts.CandidateAssessment
|
||||||
|
guidance []string
|
||||||
|
failure error
|
||||||
|
accepted bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func executeNative[T any](ctx context.Context, execution ExecutionContext[T], operation contracts.NativeOperation[T]) (OperationResult[T], error) {
|
||||||
|
var empty OperationResult[T]
|
||||||
|
if execution.Engine == nil || execution.Assess == nil || operation.Interpret == nil {
|
||||||
|
return empty, errors.New("native engine, interpretation, and assessment must be supplied")
|
||||||
|
}
|
||||||
|
if execution.Controls.MaxAdditionalTurns < 0 {
|
||||||
|
return empty, fmt.Errorf("%w: additional turns must not be negative", promptkit.ErrInvalidRequest)
|
||||||
|
}
|
||||||
|
var current nativeEvaluation[T]
|
||||||
|
var fatal error
|
||||||
|
evaluate := func(result *promptkit.RunResult) {
|
||||||
|
current = nativeEvaluation[T]{}
|
||||||
|
if err := CheckNativeResult(result, nil); err != nil {
|
||||||
|
current.failure = err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
evaluation, err := operation.Interpret(ctx, result)
|
||||||
|
if canceled := ctx.Err(); canceled != nil {
|
||||||
|
fatal = canceled
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
var captureErr *contracts.CaptureError
|
||||||
|
if errors.As(err, &captureErr) {
|
||||||
|
fatal = err
|
||||||
|
} else {
|
||||||
|
current.failure = err
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if evaluation == nil {
|
||||||
|
current.failure = errors.New("native response has no evaluation")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
current.evaluation = evaluation
|
||||||
|
view := evaluation.View()
|
||||||
|
current.guidance = append([]string(nil), view.Guidance...)
|
||||||
|
if view.Candidate == nil {
|
||||||
|
current.failure = errors.New("native response could not be interpreted as a usable candidate")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
current.assessment, err = execution.Assess(ctx, *view.Candidate)
|
||||||
|
if canceled := ctx.Err(); canceled != nil {
|
||||||
|
fatal = canceled
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
fatal = err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
validatorGuidance, err := current.assessment.Guidance()
|
||||||
|
if err != nil {
|
||||||
|
fatal = err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
current.guidance = append(current.guidance, validatorGuidance...)
|
||||||
|
current.accepted = current.assessment.Accepted() && len(current.guidance) == 0
|
||||||
|
current.failure = current.assessment.Err()
|
||||||
|
if !current.accepted && current.failure == nil {
|
||||||
|
current.failure = errors.New("native candidate requires correction")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
request := producerRequest(operation.Initial, execution.Execution)
|
||||||
|
request.CaptureExchange = request.CaptureExchange || (operation.Correction != nil && execution.Controls.MaxAdditionalTurns > 0)
|
||||||
|
result, err := execution.Engine.Run(ctx, request)
|
||||||
|
if canceled := ctx.Err(); canceled != nil {
|
||||||
|
return empty, canceled
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
var captureErr *contracts.CaptureError
|
||||||
|
if errors.As(err, &captureErr) {
|
||||||
|
return empty, err
|
||||||
|
}
|
||||||
|
current.failure = CheckNativeResult(result, err)
|
||||||
|
} else {
|
||||||
|
evaluate(result)
|
||||||
|
}
|
||||||
|
if fatal != nil {
|
||||||
|
return empty, fatal
|
||||||
|
}
|
||||||
|
if !current.accepted && err == nil && len(current.guidance) > 0 && operation.Correction != nil && execution.Controls.MaxAdditionalTurns > 0 {
|
||||||
|
coordination, coordinateErr := execution.Engine.Coordinate(ctx, promptkit.CoordinateRequest{
|
||||||
|
Start: result.Exchange, AdditionalTurns: execution.Controls.MaxAdditionalTurns,
|
||||||
|
Next: func(_ context.Context, turn promptkit.ConversationTurn) (*promptkit.RunRequest, error) {
|
||||||
|
if turn.Result != nil {
|
||||||
|
evaluate(turn.Result)
|
||||||
|
}
|
||||||
|
if fatal != nil {
|
||||||
|
return nil, fatal
|
||||||
|
}
|
||||||
|
if current.accepted || len(current.guidance) == 0 || turn.RemainingTurns == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
guidance := current.guidance
|
||||||
|
// Selecting another execution invalidates the entire previous
|
||||||
|
// evaluation before even attempting to construct its request.
|
||||||
|
current = nativeEvaluation[T]{}
|
||||||
|
next, err := operation.Correction(ctx, guidance)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if operation.Initial.ProfileID != "" {
|
||||||
|
next.ProfileID = operation.Initial.ProfileID
|
||||||
|
}
|
||||||
|
if operation.Initial.SessionID != "" {
|
||||||
|
next.SessionID = operation.Initial.SessionID
|
||||||
|
}
|
||||||
|
next = producerRequest(next, execution.Execution)
|
||||||
|
return &next, nil
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if canceled := ctx.Err(); canceled != nil {
|
||||||
|
return empty, canceled
|
||||||
|
}
|
||||||
|
if coordination.Undelivered != nil {
|
||||||
|
if coordinateErr == nil {
|
||||||
|
coordinateErr = errors.New("native continuation response was not delivered")
|
||||||
|
}
|
||||||
|
return empty, coordinateErr
|
||||||
|
}
|
||||||
|
if fatal != nil {
|
||||||
|
return empty, fatal
|
||||||
|
}
|
||||||
|
if coordinateErr != nil {
|
||||||
|
var captureErr *contracts.CaptureError
|
||||||
|
if errors.As(coordinateErr, &captureErr) {
|
||||||
|
return empty, coordinateErr
|
||||||
|
}
|
||||||
|
current = nativeEvaluation[T]{failure: coordinateErr}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if current.accepted {
|
||||||
|
outcome := contracts.ProducerOutcome{Disposition: contracts.ProducerAccepted, ReuseEligible: execution.ReuseEligible}
|
||||||
|
return completeOperation(*current.evaluation.View().Candidate, current.evaluation, current.assessment, outcome, operation.Diagnostics)
|
||||||
|
}
|
||||||
|
return finishOperation(ctx, execution.Controls.Mode, current.failure, current.evaluation, current.assessment, operation.Fallback, operation.Diagnostics)
|
||||||
|
}
|
||||||
255
internal/framework/pipeline/operation_native_test.go
Normal file
255
internal/framework/pipeline/operation_native_test.go
Normal file
@@ -0,0 +1,255 @@
|
|||||||
|
package pipeline
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"testing/synctest"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||||
|
"go.maximumdirect.net/promptkit"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNativeProducerOperationAssessesEveryDeliveredEvaluation(t *testing.T) {
|
||||||
|
for _, custom := range []bool{false, true} {
|
||||||
|
var requests []promptkit.GenerateRequest
|
||||||
|
engine := nativeProducerEngine(t, producerClientFunc(func(_ context.Context, request promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
||||||
|
requests = append(requests, request)
|
||||||
|
value := 0
|
||||||
|
if len(requests) == 3 {
|
||||||
|
value = 1
|
||||||
|
}
|
||||||
|
return &promptkit.GenerateResponse{Content: fmt.Sprintf(`{"value":%d}`, value)}, nil
|
||||||
|
}))
|
||||||
|
initial := nativeProducerInitial()
|
||||||
|
initial.ProfileID, initial.SessionID = "chosen", "session"
|
||||||
|
var assessed []int
|
||||||
|
var profiles []string
|
||||||
|
var correction contracts.CorrectionBuilder
|
||||||
|
if custom {
|
||||||
|
correction = func(ctx context.Context, guidance []string) (promptkit.RunRequest, error) {
|
||||||
|
return producerCorrection(ctx, guidance)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
correction = contracts.PromptCorrection("initial", "source", func(guidance []string) promptkit.ArtifactRef { return promptkit.Inline(strings.Join(guidance, "\n")) })
|
||||||
|
}
|
||||||
|
result, err := ExecuteOperation(context.Background(), ExecutionContext[int]{Engine: engine, ReuseEligible: true,
|
||||||
|
Controls: contracts.ProducerControls{Mode: contracts.ModeStrict, MaxAdditionalTurns: 2},
|
||||||
|
Execution: contracts.ExecutionOptions{Execution: &promptkit.ExecutionTargetOverride{Model: "override-model"}, OnPrepared: func(_ context.Context, details promptkit.PreparedRun) {
|
||||||
|
profiles = append(profiles, details.SelectedProfileID)
|
||||||
|
}},
|
||||||
|
Assess: func(ctx context.Context, value int) (contracts.CandidateAssessment, error) {
|
||||||
|
assessed = append(assessed, value)
|
||||||
|
if value == 1 {
|
||||||
|
return acceptedProducerAssessment(ctx, value)
|
||||||
|
}
|
||||||
|
return rejectedProducerAssessment(ctx, value)
|
||||||
|
}}, func(context.Context) (contracts.PreparedOperation[int], error) {
|
||||||
|
return contracts.NativeOperation[int]{Initial: initial, Correction: correction,
|
||||||
|
Interpret: func(ctx context.Context, result *promptkit.RunResult) (contracts.Evaluation[int], error) {
|
||||||
|
value, guidance, err := interpretProducerNumber(ctx, result)
|
||||||
|
return contracts.CandidateEvaluation[int]{Result: contracts.EvaluationView[int]{Candidate: value, Guidance: guidance}}, err
|
||||||
|
}}, nil
|
||||||
|
})
|
||||||
|
if err != nil || result.Value != 1 || result.Outcome.Disposition != contracts.ProducerAccepted || len(assessed) != 3 || len(requests) != 3 || !reflect.DeepEqual(profiles, []string{"chosen", "chosen", "chosen"}) {
|
||||||
|
t.Fatalf("custom=%t result=%#v err=%v assessments=%v requests=%d", custom, result, err, assessed, len(requests))
|
||||||
|
}
|
||||||
|
for i, request := range requests {
|
||||||
|
messagesPerTurn := 2
|
||||||
|
if !custom {
|
||||||
|
messagesPerTurn = 3
|
||||||
|
}
|
||||||
|
if request.Target.Model != "override-model" || request.Prompt.SessionID != "session" || len(request.Prompt.Messages) != 2+messagesPerTurn*i {
|
||||||
|
t.Fatalf("custom=%t turn=%d: %#v", custom, i, request)
|
||||||
|
}
|
||||||
|
if i > 0 {
|
||||||
|
last := request.Prompt.Messages[len(request.Prompt.Messages)-1].Content
|
||||||
|
if !strings.Contains(last, "Return value one.") {
|
||||||
|
t.Fatalf("guidance missing from %q", last)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNativeProducerOperationUnavailableAndAssessmentErrors(t *testing.T) {
|
||||||
|
for _, contractFailure := range []bool{false, true} {
|
||||||
|
failure := errors.New("assessment unavailable")
|
||||||
|
calls, corrections, fallbacks := 0, 0, 0
|
||||||
|
engine := nativeProducerEngine(t, producerClientFunc(func(context.Context, promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
||||||
|
calls++
|
||||||
|
return &promptkit.GenerateResponse{Content: `{"value":0}`}, nil
|
||||||
|
}))
|
||||||
|
result, err := ExecuteOperation(context.Background(), ExecutionContext[int]{Engine: engine, Controls: contracts.ProducerControls{Mode: contracts.ModePermissive, MaxAdditionalTurns: 2},
|
||||||
|
Assess: func(context.Context, int) (contracts.CandidateAssessment, error) {
|
||||||
|
if contractFailure {
|
||||||
|
return contracts.CandidateAssessment{}, failure
|
||||||
|
}
|
||||||
|
return contracts.CandidateAssessment{Validators: []contracts.ValidatorAssessment{{Decision: contracts.AssessmentUnavailable, Failure: failure}}}, nil
|
||||||
|
}}, func(context.Context) (contracts.PreparedOperation[int], error) {
|
||||||
|
return contracts.NativeOperation[int]{Initial: nativeProducerInitial(),
|
||||||
|
Interpret: func(ctx context.Context, native *promptkit.RunResult) (contracts.Evaluation[int], error) {
|
||||||
|
value, _, err := interpretProducerNumber(ctx, native)
|
||||||
|
return contracts.CandidateEvaluation[int]{Result: contracts.EvaluationView[int]{Candidate: value}}, err
|
||||||
|
}, Correction: func(ctx context.Context, guidance []string) (promptkit.RunRequest, error) {
|
||||||
|
corrections++
|
||||||
|
return producerCorrection(ctx, guidance)
|
||||||
|
},
|
||||||
|
Fallback: func(context.Context, contracts.Evaluation[int]) (int, error) { fallbacks++; return -1, nil }}, nil
|
||||||
|
})
|
||||||
|
if contractFailure {
|
||||||
|
if !errors.Is(err, failure) || result.Outcome.Disposition != "" || fallbacks != 0 {
|
||||||
|
t.Fatalf("contract failure = %#v %v", result, err)
|
||||||
|
}
|
||||||
|
} else if err != nil || result.Outcome.Disposition != contracts.ProducerFallback || fallbacks != 1 {
|
||||||
|
t.Fatalf("unavailable = %#v %v", result, err)
|
||||||
|
}
|
||||||
|
if calls != 1 || corrections != 0 {
|
||||||
|
t.Fatalf("calls=%d corrections=%d", calls, corrections)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNativeProducerOperationTimeoutRemainsLocal(t *testing.T) {
|
||||||
|
synctest.Test(t, func(t *testing.T) {
|
||||||
|
for _, continuation := range []bool{false, true} {
|
||||||
|
calls := 0
|
||||||
|
engine := nativeProducerEngine(t, producerClientFunc(func(ctx context.Context, _ promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
||||||
|
calls++
|
||||||
|
if continuation && calls == 1 {
|
||||||
|
return &promptkit.GenerateResponse{Content: `{"value":0}`}, nil
|
||||||
|
}
|
||||||
|
<-ctx.Done()
|
||||||
|
return nil, ctx.Err()
|
||||||
|
}))
|
||||||
|
initial := nativeProducerInitial()
|
||||||
|
initial.OperationTimeout = time.Second
|
||||||
|
result, err := ExecuteOperation(context.Background(), ExecutionContext[int]{Engine: engine, Controls: contracts.ProducerControls{Mode: contracts.ModeStrict, MaxAdditionalTurns: 1}, Assess: rejectedProducerAssessment},
|
||||||
|
func(context.Context) (contracts.PreparedOperation[int], error) {
|
||||||
|
return contracts.NativeOperation[int]{Initial: initial, Interpret: func(ctx context.Context, native *promptkit.RunResult) (contracts.Evaluation[int], error) {
|
||||||
|
value, _, err := interpretProducerNumber(ctx, native)
|
||||||
|
return contracts.CandidateEvaluation[int]{Result: contracts.EvaluationView[int]{Candidate: value}}, err
|
||||||
|
}, Correction: func(ctx context.Context, guidance []string) (promptkit.RunRequest, error) {
|
||||||
|
request, err := producerCorrection(ctx, guidance)
|
||||||
|
request.OperationTimeout = time.Second
|
||||||
|
return request, err
|
||||||
|
}}, nil
|
||||||
|
})
|
||||||
|
if !errors.Is(err, promptkit.ErrOperationTimeout) || !errors.Is(err, context.DeadlineExceeded) || result.Outcome.Disposition != "" {
|
||||||
|
t.Fatalf("continuation=%t result=%#v err=%v", continuation, result, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNativeProducerOperationInterpretationGuidanceAndAssessment(t *testing.T) {
|
||||||
|
for _, initialCandidate := range []bool{false, true} {
|
||||||
|
calls, assessed := 0, 0
|
||||||
|
engine := nativeProducerEngine(t, producerClientFunc(func(context.Context, promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
||||||
|
calls++
|
||||||
|
return &promptkit.GenerateResponse{Content: `{"value":1}`}, nil
|
||||||
|
}))
|
||||||
|
result, err := ExecuteOperation(context.Background(), ExecutionContext[int]{Engine: engine,
|
||||||
|
Controls: contracts.ProducerControls{Mode: contracts.ModeStrict, MaxAdditionalTurns: 1},
|
||||||
|
Assess: func(context.Context, int) (contracts.CandidateAssessment, error) {
|
||||||
|
assessed++
|
||||||
|
return contracts.CandidateAssessment{}, nil
|
||||||
|
}},
|
||||||
|
func(context.Context) (contracts.PreparedOperation[int], error) {
|
||||||
|
return contracts.NativeOperation[int]{Initial: nativeProducerInitial(), Correction: producerCorrection,
|
||||||
|
Interpret: func(ctx context.Context, native *promptkit.RunResult) (contracts.Evaluation[int], error) {
|
||||||
|
value, _, err := interpretProducerNumber(ctx, native)
|
||||||
|
view := contracts.EvaluationView[int]{Candidate: value}
|
||||||
|
if calls == 1 {
|
||||||
|
view.Guidance = []string{"Fix the interpretation."}
|
||||||
|
if !initialCandidate {
|
||||||
|
view.Candidate = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return contracts.CandidateEvaluation[int]{Result: view}, err
|
||||||
|
}}, nil
|
||||||
|
})
|
||||||
|
wantAssessments := 1
|
||||||
|
if initialCandidate {
|
||||||
|
wantAssessments = 2
|
||||||
|
}
|
||||||
|
if err != nil || result.Value != 1 || calls != 2 || assessed != wantAssessments {
|
||||||
|
t.Fatalf("candidate=%t result=%#v err=%v calls=%d assessed=%d", initialCandidate, result, err, calls, assessed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNativeProducerOperationClearsEvaluationBeforeFailedCorrection(t *testing.T) {
|
||||||
|
for _, failure := range []string{"builder", "native", "validation", "interpretation"} {
|
||||||
|
t.Run(failure, func(t *testing.T) {
|
||||||
|
calls := 0
|
||||||
|
engine := nativeProducerEngine(t, producerClientFunc(func(context.Context, promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
||||||
|
calls++
|
||||||
|
if calls == 2 && failure == "native" {
|
||||||
|
return nil, errors.New("provider failure")
|
||||||
|
}
|
||||||
|
if calls == 2 && failure == "validation" {
|
||||||
|
return &promptkit.GenerateResponse{Content: "invalid JSON"}, nil
|
||||||
|
}
|
||||||
|
return &promptkit.GenerateResponse{Content: `{"value":0}`}, nil
|
||||||
|
}))
|
||||||
|
result, err := ExecuteOperation(context.Background(), ExecutionContext[int]{Engine: engine, Controls: contracts.ProducerControls{Mode: contracts.ModePermissive, MaxAdditionalTurns: 1}, Assess: rejectedProducerAssessment},
|
||||||
|
func(context.Context) (contracts.PreparedOperation[int], error) {
|
||||||
|
return contracts.NativeOperation[int]{Initial: nativeProducerInitial(),
|
||||||
|
Interpret: func(ctx context.Context, result *promptkit.RunResult) (contracts.Evaluation[int], error) {
|
||||||
|
if calls == 2 && failure == "interpretation" {
|
||||||
|
return nil, errors.New("interpretation failed")
|
||||||
|
}
|
||||||
|
value, _, err := interpretProducerNumber(ctx, result)
|
||||||
|
return contracts.CandidateEvaluation[int]{Result: contracts.EvaluationView[int]{Candidate: value, Guidance: []string{"domain finding"}}}, err
|
||||||
|
},
|
||||||
|
Correction: func(ctx context.Context, guidance []string) (promptkit.RunRequest, error) {
|
||||||
|
if failure == "builder" {
|
||||||
|
return promptkit.RunRequest{}, errors.New("builder failed")
|
||||||
|
}
|
||||||
|
return producerCorrection(ctx, guidance)
|
||||||
|
},
|
||||||
|
Fallback: func(_ context.Context, final contracts.Evaluation[int]) (int, error) {
|
||||||
|
if final != nil {
|
||||||
|
t.Fatal("superseded evaluation reached fallback")
|
||||||
|
}
|
||||||
|
return -1, nil
|
||||||
|
}}, nil
|
||||||
|
})
|
||||||
|
if err != nil || result.Value != -1 || result.Evaluation != nil || result.Outcome.Disposition != contracts.ProducerFallback {
|
||||||
|
t.Fatalf("result=%#v err=%v", result, err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNativeProducerOperationCancellationStopsDelivery(t *testing.T) {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
calls := 0
|
||||||
|
engine := nativeProducerEngine(t, producerClientFunc(func(context.Context, promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
||||||
|
calls++
|
||||||
|
if calls == 2 {
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
|
return &promptkit.GenerateResponse{Content: `{"value":0}`}, nil
|
||||||
|
}))
|
||||||
|
result, err := ExecuteOperation(ctx, ExecutionContext[int]{Engine: engine, Controls: contracts.ProducerControls{Mode: contracts.ModePermissive, MaxAdditionalTurns: 1}, Assess: rejectedProducerAssessment},
|
||||||
|
func(context.Context) (contracts.PreparedOperation[int], error) {
|
||||||
|
return contracts.NativeOperation[int]{Initial: nativeProducerInitial(), Correction: producerCorrection,
|
||||||
|
Interpret: func(ctx context.Context, native *promptkit.RunResult) (contracts.Evaluation[int], error) {
|
||||||
|
value, _, err := interpretProducerNumber(ctx, native)
|
||||||
|
return contracts.CandidateEvaluation[int]{Result: contracts.EvaluationView[int]{Candidate: value}}, err
|
||||||
|
}, Fallback: func(context.Context, contracts.Evaluation[int]) (int, error) {
|
||||||
|
t.Fatal("cancellation invoked fallback")
|
||||||
|
return 0, nil
|
||||||
|
}}, nil
|
||||||
|
})
|
||||||
|
if !errors.Is(err, context.Canceled) || result.Outcome.Disposition != "" || calls != 2 {
|
||||||
|
t.Fatalf("result=%#v err=%v calls=%d", result, err, calls)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user