Files
notarius/internal/framework/pipeline/producer.go

185 lines
6.3 KiB
Go

package pipeline
import (
"context"
"errors"
"fmt"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
"go.maximumdirect.net/promptkit"
)
// RunProducer connects native execution to typed interpretation and application
// assessment. Interpretation returns nil when no typed candidate can be built;
// actionable findings remain separate from operational errors. Correction builds
// only the next assignment, never conversation history. Fallback receives only
// the final usable candidate, or nil after an unusable/failed execution.
func RunProducer[T any](
ctx context.Context,
engine *promptkit.Engine,
operation contracts.ProducerOperation[T],
initial promptkit.RunRequest,
interpret func(context.Context, *promptkit.RunResult) (*T, []string, error),
correct func(context.Context, []string) (promptkit.RunRequest, error),
fallback func(context.Context, *T) (T, error),
) (T, contracts.ProducerOutcome, error) {
var zero T
if ctx == nil {
return zero, contracts.ProducerOutcome{}, errors.New("producer context must not be nil")
}
if err := ctx.Err(); err != nil {
return zero, contracts.ProducerOutcome{}, err
}
if interpret == nil || operation.Assess == nil {
return zero, contracts.ProducerOutcome{}, errors.New("producer interpretation and assessment must be supplied")
}
if operation.Controls.MaxAdditionalTurns < 0 {
return zero, contracts.ProducerOutcome{}, fmt.Errorf("%w: additional turns must not be negative", promptkit.ErrInvalidRequest)
}
var candidate *T
var guidance []string
var failure error
var assessmentError error
accepted := false
evaluate := func(result *promptkit.RunResult) {
candidate, guidance, failure, accepted = nil, nil, nil, false
if canceled := ctx.Err(); canceled != nil {
failure = canceled
return
}
if result == nil || result.Validation.Status == promptkit.ValidationFailed || !result.Validation.IsValid {
failure = fmt.Errorf("%w: producer output did not satisfy its native contract", promptkit.ErrValidation)
return
}
candidate, guidance, failure = interpret(ctx, result)
if canceled := ctx.Err(); canceled != nil {
failure = canceled
return
}
if failure != nil {
candidate, guidance = nil, nil
return
}
if candidate == nil {
failure = errors.New("producer response could not be interpreted as a usable candidate")
return
}
assessment, err := operation.Assess(ctx, *candidate)
if err != nil {
assessmentError = err
return
}
validatorGuidance, err := assessment.Guidance()
if err != nil {
assessmentError = err
return
}
guidance = append(guidance, validatorGuidance...)
accepted = assessment.Accepted() && len(guidance) == 0
failure = assessment.Err()
if !accepted && failure == nil {
failure = errors.New("producer candidate requires correction")
}
}
request := producerRequest(initial, operation.Execution, true)
request.CaptureExchange = request.CaptureExchange || (correct != nil && operation.Controls.MaxAdditionalTurns > 0)
result, err := engine.Run(ctx, request)
if canceled := ctx.Err(); canceled != nil {
return zero, contracts.ProducerOutcome{}, canceled
}
if err != nil {
failure = err
} else {
evaluate(result)
}
if canceled := ctx.Err(); canceled != nil {
return zero, contracts.ProducerOutcome{}, canceled
}
if assessmentError != nil {
return zero, contracts.ProducerOutcome{}, assessmentError
}
if !accepted && err == nil && len(guidance) > 0 && correct != nil && operation.Controls.MaxAdditionalTurns > 0 {
coordination, coordinateErr := engine.Coordinate(ctx, promptkit.CoordinateRequest{
Start: result.Exchange, AdditionalTurns: operation.Controls.MaxAdditionalTurns, Observer: operation.Execution.Observer,
Next: func(_ context.Context, turn promptkit.ConversationTurn) (*promptkit.RunRequest, error) {
// The first callback has no new result. Its findings already exist.
if turn.Result != nil {
evaluate(turn.Result)
}
if assessmentError != nil {
return nil, assessmentError
}
if accepted || len(guidance) == 0 || turn.RemainingTurns == 0 {
return nil, nil
}
// Once another execution is selected, an earlier candidate is not a
// terminal fallback source if construction or native execution fails.
candidate = nil
next, buildErr := correct(ctx, guidance)
if buildErr != nil {
return nil, buildErr
}
if initial.ProfileID != "" {
next.ProfileID = initial.ProfileID
}
if initial.SessionID != "" {
next.SessionID = initial.SessionID
}
next = producerRequest(next, operation.Execution, false)
return &next, nil
},
})
if canceled := ctx.Err(); canceled != nil {
return zero, contracts.ProducerOutcome{}, canceled
}
if coordination.Undelivered != nil {
if coordinateErr == nil {
coordinateErr = errors.New("native continuation response was not delivered")
}
return zero, contracts.ProducerOutcome{}, coordinateErr
}
if assessmentError != nil {
return zero, contracts.ProducerOutcome{}, assessmentError
}
if coordinateErr != nil {
candidate, failure, accepted = nil, coordinateErr, false
}
}
if accepted {
return *candidate, contracts.ProducerOutcome{Disposition: contracts.ProducerAccepted, ReuseEligible: operation.ReuseEligible}, nil
}
var construct func(context.Context) (T, error)
if fallback != nil {
construct = func(ctx context.Context) (T, error) { return fallback(ctx, candidate) }
}
return finishProducerFailure(ctx, operation.Controls.Mode, failure, construct)
}
// producerRequest applies native options to a value copy. The invocation
// observer is enrolled here for the initial run and on CoordinateRequest for
// continuations, never twice on the same continuation.
func producerRequest(request promptkit.RunRequest, options contracts.ExecutionOptions, initial bool) promptkit.RunRequest {
if options.Execution != nil {
request.Execution = options.Execution
}
if options.TransientRetries != nil {
request.TransientRetries = options.TransientRetries
}
if initial {
request.Observer = promptkit.ComposeObservers(request.Observer, options.Observer)
}
if options.OnPrepared != nil {
existing := request.OnPrepared
request.OnPrepared = func(ctx context.Context, details promptkit.PreparedRun) {
if existing != nil {
existing(ctx, details)
}
options.OnPrepared(ctx, details)
}
}
return request
}