Bind runner assessment and propagate fallback reuse eligibility
This commit is contained in:
78
internal/framework/pipeline/runner_operation.go
Normal file
78
internal/framework/pipeline/runner_operation.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
)
|
||||
|
||||
// completeProducerOperation assesses deterministic work once. LLM modules have
|
||||
// already used their operation's assessor through RunProducer and must return
|
||||
// its disposition; the runner never assesses those results a second time.
|
||||
func completeProducerOperation[T any](ctx context.Context, class contracts.ExecutionClass, operation contracts.ProducerOperation[T], value T, outcome contracts.ProducerOutcome, fallback func(context.Context) (T, error)) (T, contracts.ProducerOutcome, error) {
|
||||
var zero T
|
||||
if err := ctx.Err(); err != nil {
|
||||
return zero, contracts.ProducerOutcome{}, err
|
||||
}
|
||||
if class == contracts.ExecutionClassDeterministic {
|
||||
assessment, err := operation.Assess(ctx, value)
|
||||
if err != nil {
|
||||
return zero, contracts.ProducerOutcome{}, err
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return zero, contracts.ProducerOutcome{}, err
|
||||
}
|
||||
if !assessment.Accepted() {
|
||||
return finishProducerFailure(ctx, operation.Controls.Mode, assessment.Err(), fallback)
|
||||
}
|
||||
outcome = contracts.ProducerOutcome{Disposition: contracts.ProducerAccepted, ReuseEligible: operation.ReuseEligible}
|
||||
} else if class != contracts.ExecutionClassLLMBacked {
|
||||
return zero, contracts.ProducerOutcome{}, fmt.Errorf("unsupported producer execution class %q", class)
|
||||
}
|
||||
switch outcome.Disposition {
|
||||
case contracts.ProducerAccepted:
|
||||
outcome.ReuseEligible = operation.ReuseEligible && outcome.ReuseEligible
|
||||
case contracts.ProducerFallback:
|
||||
if operation.Controls.Mode != contracts.ModePermissive {
|
||||
return zero, contracts.ProducerOutcome{}, fmt.Errorf("producer returned fallback without permissive mode")
|
||||
}
|
||||
if outcome.FallbackReason == "" {
|
||||
return zero, contracts.ProducerOutcome{}, fmt.Errorf("producer fallback has no reason")
|
||||
}
|
||||
outcome.ReuseEligible = false
|
||||
default:
|
||||
return zero, contracts.ProducerOutcome{}, fmt.Errorf("producer did not return an acceptance or fallback outcome")
|
||||
}
|
||||
return value, outcome, nil
|
||||
}
|
||||
|
||||
func operationDiagnosticGroups(diagnostics []contracts.ProducerDiagnostic, assessment contracts.CandidateAssessment, outcome contracts.ProducerOutcome, origin contracts.DiagnosticOrigin, chunk *source.Chunk) ([]contracts.DiagnosticGroup, error) {
|
||||
groups, err := promoteProducerDiagnostics(diagnostics, origin, chunk)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if outcome.Disposition == contracts.ProducerFallback {
|
||||
warning := contracts.ProducerDiagnostic{
|
||||
Disposition: contracts.DiagnosticDispositionWarning, Category: contracts.DiagnosticCategoryFallback,
|
||||
ReasonCode: "producer_fallback", OccurrenceCount: 1,
|
||||
Samples: []contracts.DiagnosticSample{{Scope: origin.ModuleKey, Message: outcome.FallbackReason}},
|
||||
}
|
||||
fallbackGroups, err := promoteProducerDiagnostics([]contracts.ProducerDiagnostic{warning}, origin, chunk)
|
||||
return append(groups, fallbackGroups...), err
|
||||
}
|
||||
for _, finding := range assessment.Validators {
|
||||
if finding.Decision != contracts.AssessmentApproved {
|
||||
continue
|
||||
}
|
||||
validatorOrigin := origin
|
||||
validatorOrigin.ValidatorKey = finding.ValidatorName
|
||||
validatorGroups, err := promoteProducerDiagnostics(finding.Result.Diagnostics, validatorOrigin, chunk)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
groups = append(groups, validatorGroups...)
|
||||
}
|
||||
return groups, nil
|
||||
}
|
||||
Reference in New Issue
Block a user