2026-05-12 00:26:27 +00:00
|
|
|
package validators
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2026-10-11 01:40:38 +00:00
|
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/core/config"
|
2026-11-10 17:25:08 +00:00
|
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/core/glossary"
|
2026-10-11 01:40:38 +00:00
|
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/core/schema"
|
|
|
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/outcomes"
|
|
|
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/proposals"
|
2026-11-16 03:43:29 +00:00
|
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/structuredoutput"
|
2026-05-12 00:26:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2026-09-25 11:25:22 +00:00
|
|
|
ReasonApproved = "approved"
|
|
|
|
|
ReasonMissingOriginalText = "missing_original_text"
|
|
|
|
|
ReasonMissingTargetSegment = "missing_target_segment"
|
|
|
|
|
ReasonEmptyResultingText = "empty_resulting_segment"
|
|
|
|
|
ReasonNoEffect = "no_effect"
|
|
|
|
|
ReasonProtectedGlossaryTerm = "protected_glossary_term"
|
|
|
|
|
ReasonInvalidTargetSegment = "invalid_target_segment_id"
|
|
|
|
|
ReasonEmptyOriginalText = "empty_original_text"
|
|
|
|
|
ReasonValidatorMalformed = "validator_response_malformed"
|
|
|
|
|
ReasonValidatorInputTooLarge = "validator_input_too_large"
|
|
|
|
|
ReasonMissingValidationDecision = "missing_validation_decision"
|
|
|
|
|
ReasonDuplicateValidationDecision = "duplicate_validation_decision"
|
2026-05-12 00:26:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Request is the runtime input shared by deterministic validators.
|
|
|
|
|
type Request struct {
|
2026-11-16 03:43:29 +00:00
|
|
|
StageIndex *int `json:"stage_index,omitempty"`
|
|
|
|
|
StageID *string `json:"stage_id,omitempty"`
|
|
|
|
|
IndependentEvidence bool `json:"-"`
|
|
|
|
|
RepairProposalIndex *int `json:"repair_proposal_index,omitempty"`
|
|
|
|
|
RepairAttempt *int `json:"repair_attempt,omitempty"`
|
|
|
|
|
SectionIndex *int `json:"section_index,omitempty"`
|
|
|
|
|
WorkingTranscript *schema.Transcript `json:"-"`
|
|
|
|
|
Section *SectionLookup `json:"-"`
|
|
|
|
|
Previews map[int]proposals.SegmentPreviewResult `json:"-"`
|
|
|
|
|
CandidateProposal []proposals.EnrichedCorrectionProposal `json:"candidate_proposals"`
|
|
|
|
|
ModuleKey string `json:"module_key"`
|
|
|
|
|
ModuleInstance string `json:"module_instance"`
|
|
|
|
|
ValidatorOrder *int `json:"validator_order,omitempty"`
|
|
|
|
|
ReplacementPolicy proposals.ReplacementPolicy `json:"replacement_policy"`
|
|
|
|
|
Glossary []glossary.Entry `json:"-"`
|
|
|
|
|
ProtectedTerms []glossary.Term `json:"-"`
|
|
|
|
|
GlossaryView *GlossaryView `json:"-"`
|
|
|
|
|
Settings config.Stage `json:"-"`
|
|
|
|
|
LLMClient StructuredLLMClient `json:"-"`
|
|
|
|
|
Interactions structuredoutput.InteractionSink `json:"-"`
|
2026-05-12 00:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Decision is one validator decision for one proposal index.
|
|
|
|
|
type Decision struct {
|
2026-09-24 17:01:19 +00:00
|
|
|
ProposalIndex int `json:"proposal_index"`
|
|
|
|
|
Approved bool `json:"approved"`
|
|
|
|
|
Disposition DecisionDisposition `json:"disposition"`
|
|
|
|
|
ReasonCode string `json:"reason_code"`
|
|
|
|
|
Message string `json:"message"`
|
|
|
|
|
DiagnosticArtifactPath string `json:"diagnostic_artifact_path,omitempty"`
|
2026-05-12 00:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-16 14:11:02 +00:00
|
|
|
// EffectiveDisposition resolves an omitted disposition from the declared
|
|
|
|
|
// approval and detects inconsistent explicit decisions.
|
|
|
|
|
func (d Decision) EffectiveDisposition() (DecisionDisposition, error) {
|
|
|
|
|
switch d.Disposition {
|
|
|
|
|
case "":
|
|
|
|
|
if d.Approved {
|
|
|
|
|
return DispositionApproved, nil
|
|
|
|
|
}
|
|
|
|
|
return DispositionRejected, nil
|
|
|
|
|
case DispositionApproved, DispositionRejected, DispositionUnavailable:
|
|
|
|
|
if d.Approved == (d.Disposition == DispositionApproved) {
|
|
|
|
|
return d.Disposition, nil
|
|
|
|
|
}
|
|
|
|
|
return "", fmt.Errorf("contradictory approval and disposition %q for proposal %d", d.Disposition, d.ProposalIndex)
|
|
|
|
|
default:
|
|
|
|
|
return "", fmt.Errorf("invalid decision disposition %q for proposal %d", d.Disposition, d.ProposalIndex)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-24 17:01:19 +00:00
|
|
|
// DecisionDisposition separates normal editorial rejection from a candidate
|
|
|
|
|
// excluded because its model operation could not provide a trustworthy decision.
|
|
|
|
|
type DecisionDisposition string
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
DispositionApproved DecisionDisposition = "approved"
|
|
|
|
|
DispositionRejected DecisionDisposition = "rejected"
|
|
|
|
|
DispositionUnavailable DecisionDisposition = "unavailable"
|
|
|
|
|
)
|
|
|
|
|
|
2026-09-24 21:48:18 +00:00
|
|
|
// Result contains one decision per proposal index on success. Fatal returns may
|
|
|
|
|
// include completed partial decisions as evidence, never as application approval.
|
2026-05-12 00:26:27 +00:00
|
|
|
type Result struct {
|
2026-09-24 17:01:19 +00:00
|
|
|
ValidatorName string `json:"validator_name"`
|
|
|
|
|
Decisions []Decision `json:"decisions"`
|
|
|
|
|
Operations []outcomes.Operation `json:"operations,omitempty"`
|
2026-05-12 00:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-16 14:11:02 +00:00
|
|
|
// Validator evaluates one section's candidates. An instance may be called
|
|
|
|
|
// concurrently across sections and may keep synchronized state. Request data
|
|
|
|
|
// is borrowed for the call; inputs must be copied before modification or
|
|
|
|
|
// retention. Request.Preview may populate the caller's pass-local cache.
|
2026-05-12 00:26:27 +00:00
|
|
|
type Validator interface {
|
|
|
|
|
Name() string
|
|
|
|
|
Validate(ctx context.Context, req Request) (Result, error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EnforceDecisionCardinality verifies every candidate proposal index receives exactly one decision.
|
|
|
|
|
func EnforceDecisionCardinality(candidate []proposals.EnrichedCorrectionProposal, decisions []Decision) error {
|
2026-09-24 21:48:18 +00:00
|
|
|
return enforceDecisionCardinality(candidate, decisions, true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EnforcePartialDecisionCardinality accepts missing decisions on fatal returns,
|
|
|
|
|
// but still rejects duplicate and unknown proposal identities.
|
|
|
|
|
func EnforcePartialDecisionCardinality(candidate []proposals.EnrichedCorrectionProposal, decisions []Decision) error {
|
|
|
|
|
return enforceDecisionCardinality(candidate, decisions, false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func enforceDecisionCardinality(candidate []proposals.EnrichedCorrectionProposal, decisions []Decision, requireAll bool) error {
|
2026-05-12 00:26:27 +00:00
|
|
|
expected := make(map[int]struct{}, len(candidate))
|
|
|
|
|
for _, c := range candidate {
|
|
|
|
|
expected[c.ProposalIndex] = struct{}{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
seen := make(map[int]int, len(decisions))
|
|
|
|
|
for _, d := range decisions {
|
|
|
|
|
if _, ok := expected[d.ProposalIndex]; !ok {
|
|
|
|
|
return fmt.Errorf("unknown decision proposal index %d", d.ProposalIndex)
|
|
|
|
|
}
|
|
|
|
|
seen[d.ProposalIndex]++
|
|
|
|
|
if seen[d.ProposalIndex] > 1 {
|
|
|
|
|
return fmt.Errorf("duplicate decision proposal index %d", d.ProposalIndex)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for idx := range expected {
|
2026-09-24 21:48:18 +00:00
|
|
|
if requireAll && seen[idx] == 0 {
|
2026-05-12 00:26:27 +00:00
|
|
|
return fmt.Errorf("missing decision proposal index %d", idx)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func approval(index int) Decision {
|
2026-09-24 17:09:35 +00:00
|
|
|
return Decision{ProposalIndex: index, Approved: true, Disposition: DispositionApproved, ReasonCode: ReasonApproved, Message: "approved"}
|
2026-05-12 00:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func rejection(index int, reasonCode string, msg string) Decision {
|
2026-09-24 17:09:35 +00:00
|
|
|
return Decision{ProposalIndex: index, Approved: false, Disposition: DispositionRejected, ReasonCode: reasonCode, Message: strings.TrimSpace(msg)}
|
2026-05-12 00:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
2026-09-24 17:14:33 +00:00
|
|
|
func unavailable(index int, reasonCode string, msg string) Decision {
|
|
|
|
|
return Decision{ProposalIndex: index, Approved: false, Disposition: DispositionUnavailable, ReasonCode: reasonCode, Message: strings.TrimSpace(msg)}
|
|
|
|
|
}
|