72 lines
2.3 KiB
Go
72 lines
2.3 KiB
Go
package contracts
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// ValidationUnavailableError marks an assessment that could not be performed,
|
|
// rather than a detected defect in the candidate. Its cause remains inspectable.
|
|
type ValidationUnavailableError struct{ Cause error }
|
|
|
|
func (e *ValidationUnavailableError) Error() string {
|
|
if e.Cause == nil {
|
|
return "validation unavailable"
|
|
}
|
|
return "validation unavailable: " + e.Cause.Error()
|
|
}
|
|
|
|
func (e *ValidationUnavailableError) Unwrap() error { return e.Cause }
|
|
|
|
// Accepted requires approval by every applicable validator. An empty configured
|
|
// chain has no additional acceptance criteria.
|
|
func (a CandidateAssessment) Accepted() bool {
|
|
for _, finding := range a.Validators {
|
|
if finding.Decision != AssessmentApproved {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// Guidance returns only actionable rejections, in validator order, with exact
|
|
// duplicates removed. Domain correction templates own the surrounding assignment.
|
|
func (a CandidateAssessment) Guidance() ([]string, error) {
|
|
var guidance []string
|
|
seen := make(map[string]bool)
|
|
for _, finding := range a.Validators {
|
|
if finding.Decision != AssessmentRejected {
|
|
continue
|
|
}
|
|
text := finding.Result.CorrectionGuidance
|
|
if strings.TrimSpace(text) == "" {
|
|
return nil, fmt.Errorf("validator %q rejected output without correction guidance", finding.ValidatorName)
|
|
}
|
|
if !seen[text] {
|
|
seen[text] = true
|
|
guidance = append(guidance, text)
|
|
}
|
|
}
|
|
return guidance, nil
|
|
}
|
|
|
|
// Err explains nonacceptance while preserving underlying operational causes.
|
|
// Neither this operator-facing text nor reason codes are correction instructions.
|
|
func (a CandidateAssessment) Err() error {
|
|
var failures []error
|
|
for _, finding := range a.Validators {
|
|
if finding.Decision == AssessmentApproved {
|
|
continue
|
|
}
|
|
if finding.Failure != nil {
|
|
failures = append(failures, fmt.Errorf("validator %q %s: %w", finding.ValidatorName, finding.Decision, finding.Failure))
|
|
} else if finding.Result.Message != "" {
|
|
failures = append(failures, fmt.Errorf("validator %q %s: %s", finding.ValidatorName, finding.Decision, finding.Result.Message))
|
|
} else {
|
|
failures = append(failures, fmt.Errorf("validator %q did not approve the candidate (%s)", finding.ValidatorName, finding.Decision))
|
|
}
|
|
}
|
|
return errors.Join(failures...)
|
|
}
|