2026-11-25 15:08:28 +00:00
|
|
|
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)
|
2026-11-25 15:10:50 +00:00
|
|
|
var result *promptkit.RunResult
|
|
|
|
|
var err error
|
|
|
|
|
var cacheKey string
|
|
|
|
|
cache := execution.Cache
|
|
|
|
|
if cache != nil && cache.Store != nil && execution.ReuseEligible && execution.Controls.Cache != contracts.CacheBypass {
|
|
|
|
|
if cache.Encode == nil || cache.Decode == nil {
|
|
|
|
|
return empty, errors.New("producer cache codec must be supplied")
|
|
|
|
|
}
|
|
|
|
|
if mode := execution.Controls.Cache; mode != "" && mode != contracts.CacheAuto && mode != contracts.CacheRefresh {
|
|
|
|
|
return empty, fmt.Errorf("%w: unknown cache mode %q", promptkit.ErrInvalidRequest, mode)
|
|
|
|
|
}
|
|
|
|
|
var prepared *promptkit.PreparedExecution
|
|
|
|
|
prepared, err = execution.Engine.PrepareExecution(ctx, request)
|
|
|
|
|
if err == nil {
|
|
|
|
|
defer prepared.Discard()
|
|
|
|
|
details := prepared.Details()
|
|
|
|
|
cacheKey = details.MessageContentHash
|
|
|
|
|
if err := observeProducerCache(ctx, cache, contracts.CacheObservation{Decision: "prepared", MessageContentHash: cacheKey, Prepared: &details}); err != nil {
|
|
|
|
|
return empty, err
|
|
|
|
|
}
|
|
|
|
|
reused, lookupErr := lookupProducerCache(ctx, cache, execution.Controls.Cache, cacheKey)
|
|
|
|
|
if lookupErr != nil {
|
|
|
|
|
return empty, lookupErr
|
|
|
|
|
}
|
|
|
|
|
if reused != nil {
|
|
|
|
|
return OperationResult[T]{Value: *reused, Outcome: contracts.ProducerOutcome{Disposition: contracts.ProducerAccepted, ReuseEligible: true}}, nil
|
|
|
|
|
}
|
|
|
|
|
result, err = execution.Engine.RunPrepared(ctx, prepared)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if cache != nil {
|
|
|
|
|
decision := "bypass"
|
|
|
|
|
if !execution.ReuseEligible {
|
|
|
|
|
decision = "ineligible"
|
|
|
|
|
}
|
|
|
|
|
if err := observeProducerCache(ctx, cache, contracts.CacheObservation{Decision: decision}); err != nil {
|
|
|
|
|
return empty, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result, err = execution.Engine.Run(ctx, request)
|
|
|
|
|
}
|
2026-11-25 15:08:28 +00:00
|
|
|
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 {
|
2026-11-25 15:10:50 +00:00
|
|
|
if cacheKey != "" {
|
|
|
|
|
if err := storeProducerCache(ctx, cache, execution.Controls.Cache, cacheKey, *current.evaluation.View().Candidate); err != nil {
|
|
|
|
|
return empty, err
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-25 15:08:28 +00:00
|
|
|
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)
|
|
|
|
|
}
|