package validators import ( "gitea.maximumdirect.net/eric/audita/v4/internal/core/schema" "gitea.maximumdirect.net/eric/audita/v4/internal/framework/proposals" ) // SectionLookup indexes the immutable source section shared by validation // and revalidation. Application checks its own, changing transcript. type SectionLookup struct { segments []schema.Segment positions map[int]int } func NewSectionLookup(transcript *schema.Transcript) *SectionLookup { section := &SectionLookup{} if transcript != nil { section.segments = transcript.Segments section.positions = make(map[int]int, len(section.segments)) for i, segment := range section.segments { section.positions[segment.ID] = i } } return section } func (s *SectionLookup) Position(id int) (int, bool) { position, ok := s.positions[id] return position, ok } func (r Request) sectionLookup() *SectionLookup { if r.Section != nil { return r.Section } return NewSectionLookup(r.WorkingTranscript) } // Preview reuses the replacement preview for this proposal index within one // validation pass. A revision starts a new pass with its own preview map. func (r Request) Preview(candidate proposals.EnrichedCorrectionProposal) proposals.SegmentPreviewResult { if preview, ok := r.Previews[candidate.ProposalIndex]; ok { return preview } section := r.sectionLookup() var segment *schema.Segment if position, ok := section.Position(candidate.TargetSegmentID); ok { segment = §ion.segments[position] } correction := candidate.CorrectionProposal preview := proposals.PreviewProposalForSegment(segment, correction, r.ReplacementPolicy) if r.Previews != nil { r.Previews[candidate.ProposalIndex] = preview } if r.PreviewApplicability != nil { r.PreviewApplicability[candidate.ProposalIndex] = preview.Applicable } return preview } // AcceptsIndependentCandidate decides which evidence can be submitted when // repair needs findings from each applicable validator. func (r Request) AcceptsIndependentCandidate(validatorName string, modelDriven bool, candidate proposals.EnrichedCorrectionProposal) bool { hasSpan := candidate.OriginalText != "" switch validatorName { case "proposal_shape": return true case "no_effect": return hasSpan case "original_text_presence": return hasSpan && candidate.TargetSegmentID > 0 case "protected_terms", "non_empty_resulting_segment": return r.Preview(candidate).HasResult() default: return !modelDriven || r.Preview(candidate).Applicable } }