2026-05-11 14:22:22 +00:00
package runner
import (
"context"
2026-09-24 21:48:18 +00:00
"errors"
2026-05-11 14:22:22 +00:00
"fmt"
2026-11-16 15:00:05 +00:00
"slices"
2026-05-12 18:39:26 -05:00
"sort"
2026-05-12 12:59:46 -05:00
"sync"
2026-05-11 14:22:22 +00:00
2026-10-11 01:40:38 +00:00
"gitea.maximumdirect.net/eric/audita/v3/internal/core/chunking"
"gitea.maximumdirect.net/eric/audita/v3/internal/core/config"
2026-11-16 04:09:25 +00:00
"gitea.maximumdirect.net/eric/audita/v3/internal/core/glossary"
2026-11-10 17:33:30 +00:00
"gitea.maximumdirect.net/eric/audita/v3/internal/core/modulecatalog"
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/contracts"
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/outcomes"
2026-11-10 20:42:28 +00:00
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/promptcontext"
2026-10-11 01:40:38 +00:00
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/proposals"
2026-11-11 04:18:05 +00:00
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/structuredoutput"
2026-10-11 01:40:38 +00:00
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/validators"
validatormetadata "gitea.maximumdirect.net/eric/audita/v3/internal/validators/metadata"
2026-05-11 14:22:22 +00:00
)
const (
ModuleStatusSuccess = "success"
ModuleStatusFailed = "failed"
)
// ModuleFactory resolves one module instance for one run spec.
type ModuleFactory interface {
ModuleForSpec ( spec contracts . ModuleRunSpec ) ( contracts . TranscriptModule , error )
}
// Runner executes module instances sequentially against a working transcript.
type Runner struct {
factory ModuleFactory
}
// ModuleResult captures deterministic per-module execution output.
type ModuleResult struct {
2026-11-10 17:37:41 +00:00
StageIndex int ` json:"stage_index" `
StageID * string ` json:"stage_id,omitempty" `
2026-09-24 17:01:19 +00:00
ModuleKey string ` json:"module_key" `
ModuleInstance string ` json:"module_instance" `
ReplacementPolicy proposals . ReplacementPolicy ` json:"replacement_policy" `
Status string ` json:"status" `
ValidatorDecisions [ ] ValidatorDecisionRecord ` json:"validator_decisions,omitempty" `
ValidatorRejected [ ] ValidatorRejectedChange ` json:"validator_rejected,omitempty" `
AppliedChanges [ ] proposals . AppliedChange ` json:"applied_changes,omitempty" `
SkippedChanges [ ] proposals . SkippedChange ` json:"skipped_changes,omitempty" `
ErrorMessage string ` json:"error_message,omitempty" `
}
// WorkUnitOutcome keeps initial section identity while text evolves between
// module instances. Detailed candidate and decision records are debug evidence.
type WorkUnitOutcome struct {
2026-11-16 14:53:14 +00:00
ProposalIndexOffset int ` json:"-" `
StageIndex int ` json:"stage_index" `
StageID * string ` json:"stage_id,omitempty" `
ModuleIndex int ` json:"module_index" `
ModuleKey string ` json:"module_key" `
ModuleInstance string ` json:"module_instance" `
Section contracts . SectionMetadata ` json:"section" `
State outcomes . WorkState ` json:"state" `
Failure * ExecutionFailure ` json:"-" `
CorrectionsApplied int ` json:"corrections_applied" `
Repairs [ ] RepairOutcome ` json:"-" `
Operations [ ] outcomes . Operation ` json:"operations,omitempty" `
2026-05-12 00:26:27 +00:00
}
type ValidatorDecisionRecord struct {
2026-11-16 14:11:02 +00:00
ExecutionClass validatormetadata . ExecutionClass ` json:"-" `
ValidatorName string ` json:"validator_name" `
ProposalIndex int ` json:"proposal_index" `
Approved bool ` json:"approved" `
Disposition validators . 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
}
type ValidatorRejectedChange struct {
2026-09-25 11:28:44 +00:00
ValidatorName string ` json:"validator_name" `
ProposalIndex int ` json:"proposal_index" `
ModuleKey string ` json:"module_key" `
ModuleInstance string ` json:"module_instance" `
TargetSegmentID int ` json:"target_segment_id" `
OriginalText string ` json:"original_text" `
CorrectedText string ` json:"corrected_text" `
Disposition validators . DecisionDisposition ` json:"disposition" `
ReasonCode string ` json:"reason_code" `
Message string ` json:"message" `
2026-05-11 14:22:22 +00:00
}
2026-11-16 15:08:02 +00:00
// EvidenceSelection selects the detailed views consumed by the invocation.
type EvidenceSelection struct {
Reports bool
Debug bool
}
type evidenceNeeds struct {
reports bool
decisions bool
candidates bool
}
func selectEvidence ( selection * EvidenceSelection ) evidenceNeeds {
if selection == nil {
return evidenceNeeds { reports : true , decisions : true }
}
return evidenceNeeds { reports : selection . Reports , decisions : selection . Debug , candidates : selection . Debug }
}
2026-05-11 14:22:22 +00:00
// RunInput is the deterministic runner input.
type RunInput struct {
2026-11-16 15:08:02 +00:00
Config * config . Config
Transcript * schema . Transcript
ModuleSpecs [ ] contracts . ModuleRunSpec
Interactions structuredoutput . InteractionSink
Evidence * EvidenceSelection
2026-11-16 15:12:06 +00:00
Progress func ( Progress )
2026-05-11 14:22:22 +00:00
}
// RunOutput is the deterministic runner output.
type RunOutput struct {
2026-11-10 20:26:43 +00:00
FinalTranscript * schema . Transcript ` json:"-" `
ModuleResults [ ] ModuleResult ` json:"module_results" `
Sections [ ] contracts . SectionMetadata ` json:"sections" `
2026-11-16 15:08:02 +00:00
Totals * outcomes . WorkTotals ` json:"-" `
2026-11-10 20:26:43 +00:00
InventoryAvailable bool ` json:"-" `
WorkUnits [ ] WorkUnitOutcome ` json:"work_units" `
Candidates [ ] proposals . EnrichedCorrectionProposal ` json:"-" `
2026-05-11 14:22:22 +00:00
}
func New ( factory ModuleFactory ) * Runner {
return & Runner { factory : factory }
}
func ( r * Runner ) Run ( ctx context . Context , input RunInput ) ( RunOutput , error ) {
2026-11-16 14:42:24 +00:00
if err := ctx . Err ( ) ; err != nil {
return RunOutput { FinalTranscript : input . Transcript . Clone ( ) } , & ExecutionFailure { Phase : "processing" , Cause : err }
}
2026-05-11 14:22:22 +00:00
if r == nil || r . factory == nil {
2026-11-16 14:42:24 +00:00
return RunOutput { } , & ExecutionFailure { Phase : "setup" , Cause : errors . New ( "runner module factory is required" ) }
2026-05-11 14:22:22 +00:00
}
2026-11-16 03:56:54 +00:00
working := input . Transcript . Clone ( )
2026-05-11 14:22:22 +00:00
results := make ( [ ] ModuleResult , 0 , len ( input . ModuleSpecs ) )
2026-09-24 17:17:14 +00:00
sections , err := chunkWorkingTranscript ( input . Config , working )
if err != nil {
2026-11-16 14:42:24 +00:00
return RunOutput { FinalTranscript : working } , & ExecutionFailure { Phase : "chunking" , Cause : fmt . Errorf ( "chunk normalized transcript: %w" , err ) }
2026-09-24 17:17:14 +00:00
}
2026-11-16 15:08:02 +00:00
evidence := selectEvidence ( input . Evidence )
totals := outcomes . WorkTotals { Total : len ( input . ModuleSpecs ) * len ( sections ) }
var inventory [ ] contracts . SectionMetadata
var workUnits [ ] WorkUnitOutcome
2026-09-24 17:41:10 +00:00
var candidates [ ] proposals . EnrichedCorrectionProposal
2026-11-16 15:08:02 +00:00
if evidence . reports {
inventory = make ( [ ] contracts . SectionMetadata , len ( sections ) )
for i , section := range sections {
inventory [ i ] = contracts . SectionMetadataFromSection ( section )
}
workUnits = make ( [ ] WorkUnitOutcome , 0 , totals . Total )
for moduleIndex , spec := range input . ModuleSpecs {
for _ , section := range inventory {
workUnits = append ( workUnits , newWorkUnit ( spec , section , moduleIndex ) )
}
2026-09-24 17:17:14 +00:00
}
}
snapshot := func ( ) RunOutput {
return RunOutput {
FinalTranscript : transcriptFromSections ( sections ) , ModuleResults : results ,
2026-11-16 15:08:02 +00:00
Sections : inventory , InventoryAvailable : true , Totals : & totals , WorkUnits : workUnits , Candidates : candidates ,
2026-09-24 17:17:14 +00:00
}
}
2026-05-11 14:22:22 +00:00
2026-09-24 17:17:14 +00:00
for moduleIndex , spec := range input . ModuleSpecs {
2026-11-16 15:12:06 +00:00
progress := Progress { ModuleIndex : spec . Index , ModuleKey : spec . ModuleKey , ModuleID : spec . ID }
sectionCount := len ( sections )
progress . emit ( input . Progress , "started" , nil , & sectionCount )
2026-11-16 15:08:02 +00:00
var reportUnits [ ] WorkUnitOutcome
if evidence . reports {
reportUnits = workUnits [ moduleIndex * len ( sections ) : ( moduleIndex + 1 ) * len ( sections ) ]
2026-05-11 14:22:22 +00:00
}
2026-11-16 15:12:06 +00:00
execution , err := r . executeModule ( ctx , spec , sections , input . Interactions , evidence , reportUnits , input . Progress )
2026-11-16 15:08:02 +00:00
units := execution . pipeline . WorkUnits
2026-11-16 14:42:24 +00:00
if err == nil {
2026-11-16 15:08:02 +00:00
filters := findApplyFiltering ( sections , execution . pipeline . Approved , execution . skipped , execution . policy )
if canceled := ctx . Err ( ) ; canceled != nil {
err = locateFailure ( canceled , "application" , failureReference ( spec , nil , "" ) , false , nil )
} else {
execution . pipeline . recordApplyFiltering ( filters , evidence . reports )
sections = execution . updated
execution . pipeline . Totals . Started = len ( sections )
execution . pipeline . Totals . Completed = len ( sections )
execution . pipeline . Totals . Corrections = len ( execution . applied )
}
2026-05-12 00:26:27 +00:00
}
2026-11-16 14:42:24 +00:00
var failure * ExecutionFailure
applicationInterrupted := errors . As ( err , & failure ) && failure . Phase == "application"
2026-11-16 15:08:02 +00:00
if applicationInterrupted {
execution . pipeline . Totals . Completed = 0
}
totals . Add ( execution . pipeline . Totals )
appliedIndices := make ( map [ int ] struct { } )
if evidence . reports || evidence . candidates {
for _ , change := range execution . applied {
appliedIndices [ change . ProposalIndex ] = struct { } { }
}
2026-09-24 17:17:14 +00:00
}
2026-11-16 14:42:24 +00:00
for i := range units {
unit := & units [ i ]
2026-09-24 17:23:59 +00:00
unit . ModuleIndex = moduleIndex
2026-11-16 14:42:24 +00:00
if err == nil {
unit . State = outcomes . WorkCompleted
2026-11-16 15:08:02 +00:00
if evidence . reports {
unit . CorrectionsApplied = execution . appliedBySection [ unit . Section . Index ]
}
2026-11-16 14:42:24 +00:00
} else if applicationInterrupted && unit . State != outcomes . WorkNotStarted {
unit . State = outcomes . WorkInterrupted
}
2026-11-11 04:29:18 +00:00
for j := range unit . Repairs {
branch := & unit . Repairs [ j ]
if branch . Outcome != "approved" {
continue
}
2026-11-16 14:42:24 +00:00
if err != nil {
branch . Outcome = "interrupted"
} else if _ , ok := appliedIndices [ branch . ProposalIndex ] ; ok {
2026-11-11 12:29:42 +00:00
branch . Outcome = "applied"
2026-11-16 14:42:24 +00:00
} else {
branch . Outcome = "approved_skipped"
2026-11-11 12:29:42 +00:00
}
2026-11-11 04:29:18 +00:00
}
}
2026-11-16 15:08:02 +00:00
if ! evidence . reports && evidence . candidates {
workUnits = append ( workUnits , units ... )
2026-11-16 14:42:24 +00:00
}
2026-11-16 15:08:02 +00:00
candidates = append ( candidates , execution . pipeline . Candidates ... )
2026-11-16 14:42:24 +00:00
result := ModuleResult {
StageIndex : spec . Index , StageID : spec . ID , ModuleKey : spec . ModuleKey ,
ModuleInstance : spec . InstanceName , ReplacementPolicy : execution . policy ,
ValidatorDecisions : execution . pipeline . ValidatorDecisions , ValidatorRejected : execution . pipeline . ValidatorRejected ,
}
if err != nil {
result . Status = ModuleStatusFailed
result . ErrorMessage = err . Error ( )
} else {
result . Status = ModuleStatusSuccess
2026-11-16 15:08:02 +00:00
if evidence . decisions {
result . AppliedChanges = execution . applied
result . SkippedChanges = execution . skipped
}
2026-11-16 14:42:24 +00:00
}
results = append ( results , result )
if err != nil {
2026-11-16 15:12:06 +00:00
progress . emit ( input . Progress , "failed" , nil , nil )
2026-11-16 14:42:24 +00:00
return snapshot ( ) , fmt . Errorf ( "module %q failed: %w" , spec . InstanceName , err )
}
2026-11-16 15:12:06 +00:00
corrections := len ( execution . applied )
progress . emit ( input . Progress , "completed" , nil , & corrections )
2026-11-16 14:42:24 +00:00
}
if err := ctx . Err ( ) ; err != nil {
return snapshot ( ) , & ExecutionFailure { Phase : "processing" , Cause : err }
2026-05-11 14:22:22 +00:00
}
2026-11-16 14:42:24 +00:00
return snapshot ( ) , nil
}
2026-05-11 14:22:22 +00:00
2026-11-16 14:42:24 +00:00
type moduleExecution struct {
policy proposals . ReplacementPolicy
pipeline modulePipelineResult
updated [ ] chunking . Section
applied [ ] proposals . AppliedChange
skipped [ ] proposals . SkippedChange
appliedBySection [ ] int
}
2026-11-16 15:12:06 +00:00
func ( r * Runner ) executeModule ( ctx context . Context , spec contracts . ModuleRunSpec , sections [ ] chunking . Section , interactions structuredoutput . InteractionSink , evidence evidenceNeeds , reportUnits [ ] WorkUnitOutcome , progress func ( Progress ) ) ( moduleExecution , error ) {
2026-11-16 14:42:24 +00:00
var execution moduleExecution
origin := failureReference ( spec , nil , "" )
if err := ctx . Err ( ) ; err != nil {
return execution , locateFailure ( err , "processing" , origin , false , nil )
}
module , err := r . factory . ModuleForSpec ( spec )
if err != nil {
return execution , locateFailure ( fmt . Errorf ( "setup failed: %w" , err ) , "setup" , origin , false , nil )
}
execution . policy = module . ReplacementPolicy ( )
if err := spec . PrepareGlossary ( ) ; err != nil {
return execution , locateFailure ( fmt . Errorf ( "glossary preparation failed: %w" , err ) , "setup" , origin , false , nil )
}
execution . pipeline , err = runModulePipeline ( ctx , collectSectionProposalsInput {
Module : module ,
2026-11-16 15:08:02 +00:00
Evidence : & evidence ,
ReportUnits : reportUnits ,
2026-11-16 15:12:06 +00:00
Progress : progress ,
2026-11-16 14:42:24 +00:00
Spec : spec ,
Policy : execution . policy ,
Sections : sections ,
ProposalClient : spec . ProposalClient ,
Interactions : interactions ,
ValidationClient : spec . ValidationClient ,
GlossaryVariables : spec . GlossaryVariables ,
GlossaryView : spec . GlossaryView ,
} )
if err != nil {
return execution , err
}
if err := ctx . Err ( ) ; err != nil {
return execution , locateFailure ( err , "application" , origin , false , nil )
}
execution . updated , execution . applied , execution . skipped , execution . appliedBySection , err = applyWithinSections ( sections , execution . pipeline . Approved , execution . policy , spec . GlossaryView . Protection , spec . ModuleKey == modulecatalog . KeySpokenWord )
if err != nil {
return execution , locateFailure ( fmt . Errorf ( "application failed: %w" , err ) , "application" , origin , false , nil )
}
return execution , nil
2026-05-11 14:22:22 +00:00
}
2026-09-24 17:17:14 +00:00
func transcriptFromSections ( sections [ ] chunking . Section ) * schema . Transcript {
segments := make ( [ ] schema . Segment , 0 )
for _ , section := range sections {
segments = append ( segments , section . Segments ... )
}
return & schema . Transcript { Segments : segments }
}
// applyWithinSections leaves section membership and initial metadata unchanged.
// Approved proposals are applied only to their identified section.
2026-11-16 04:09:25 +00:00
func applyWithinSections ( sections [ ] chunking . Section , approved [ ] proposals . EnrichedCorrectionProposal , policy proposals . ReplacementPolicy , vocabulary glossary . ProtectedVocabulary , allowRepetitionCleanup bool ) ( [ ] chunking . Section , [ ] proposals . AppliedChange , [ ] proposals . SkippedChange , [ ] int , error ) {
2026-11-10 20:50:50 +00:00
applied := make ( [ ] proposals . AppliedChange , 0 )
skipped := make ( [ ] proposals . SkippedChange , 0 )
appliedBySection := make ( [ ] int , len ( sections ) )
if len ( approved ) == 0 {
return sections , applied , skipped , appliedBySection , nil
}
2026-09-24 17:17:14 +00:00
bySection := make ( map [ int ] [ ] proposals . EnrichedCorrectionProposal , len ( sections ) )
known := make ( map [ int ] struct { } , len ( sections ) )
for _ , section := range sections {
known [ section . Index ] = struct { } { }
}
for _ , candidate := range approved {
if candidate . SectionIndex == nil {
return nil , nil , nil , nil , fmt . Errorf ( "approved proposal %d has no section identity" , candidate . ProposalIndex )
}
if _ , ok := known [ * candidate . SectionIndex ] ; ! ok {
return nil , nil , nil , nil , fmt . Errorf ( "approved proposal %d has unknown section identity" , candidate . ProposalIndex )
}
bySection [ * candidate . SectionIndex ] = append ( bySection [ * candidate . SectionIndex ] , candidate )
}
updated := make ( [ ] chunking . Section , len ( sections ) )
copy ( updated , sections )
for i , section := range sections {
2026-11-10 20:50:50 +00:00
candidates := bySection [ section . Index ]
if len ( candidates ) == 0 {
continue
}
result := proposals . ApplyProposals ( transcriptFromSection ( section ) , candidates , policy , func ( before , after string ) bool { return vocabulary . AllowsChange ( before , after , allowRepetitionCleanup ) } )
2026-09-24 17:17:14 +00:00
updated [ i ] . Segments = result . Transcript . Segments
applied = append ( applied , result . Applied ... )
skipped = append ( skipped , result . Skipped ... )
appliedBySection [ i ] = len ( result . Applied )
}
return updated , applied , skipped , appliedBySection , nil
}
2026-05-12 12:59:46 -05:00
type collectSectionProposalsInput struct {
2026-11-16 15:12:06 +00:00
Progress func ( Progress )
2026-11-16 15:08:02 +00:00
Evidence * evidenceNeeds
ReportUnits [ ] WorkUnitOutcome
2026-11-16 03:43:29 +00:00
Interactions structuredoutput . InteractionSink
Module contracts . TranscriptModule
Spec contracts . ModuleRunSpec
Policy proposals . ReplacementPolicy
Sections [ ] chunking . Section
ProposalClient contracts . StructuredLLMClient
ValidationClient contracts . StructuredLLMClient
GlossaryVariables * promptcontext . GlossaryVariables
GlossaryView * validators . GlossaryView
2026-05-12 12:59:46 -05:00
}
2026-05-12 18:39:26 -05:00
type sectionProposalResult struct {
sectionPos int
section chunking . Section
2026-11-16 14:30:57 +00:00
proposal contracts . ProposalResult
2026-09-24 17:23:59 +00:00
started bool
2026-11-16 14:42:24 +00:00
failure * ExecutionFailure
2026-05-12 18:39:26 -05:00
}
type sectionValidationResult struct {
2026-11-16 14:53:14 +00:00
interactions * sectionInteractions
repairs [ ] RepairOutcome
sectionPos int
approved [ ] proposals . EnrichedCorrectionProposal
decisions [ ] ValidatorDecisionRecord
rejected [ ] ValidatorRejectedChange
operations [ ] outcomes . Operation
filtered [ ] outcomes . FilteredProposal
failure * ExecutionFailure
}
type sectionWork struct {
2026-11-16 15:08:02 +00:00
unit WorkUnitOutcome
2026-11-16 14:53:14 +00:00
candidates [ ] proposals . EnrichedCorrectionProposal
validation * sectionValidationResult
finished bool
2026-05-12 18:39:26 -05:00
}
type modulePipelineResult struct {
2026-11-16 15:08:02 +00:00
Totals outcomes . WorkTotals
proposalFacts map [ int ] proposalRecovery
revised map [ int ] struct { }
2026-09-24 17:41:10 +00:00
Candidates [ ] proposals . EnrichedCorrectionProposal
2026-05-12 18:39:26 -05:00
Approved [ ] proposals . EnrichedCorrectionProposal
2026-09-24 17:23:59 +00:00
WorkUnits [ ] WorkUnitOutcome
2026-05-12 18:39:26 -05:00
ValidatorDecisions [ ] ValidatorDecisionRecord
ValidatorRejected [ ] ValidatorRejectedChange
}
2026-11-16 14:53:14 +00:00
// moduleWorkWindow bounds active and downstream work across all roles in
// one module. PromptKit still owns admission to each provider backend.
const moduleWorkWindow = 8
2026-05-12 18:39:26 -05:00
2026-11-16 14:53:14 +00:00
func proposeSection ( ctx context . Context , input collectSectionProposalsInput , sectionPos int ) sectionProposalResult {
section := input . Sections [ sectionPos ]
result := sectionProposalResult { sectionPos : sectionPos , section : section }
if ctx . Err ( ) != nil {
return result
}
2026-11-16 15:12:06 +00:00
progress := Progress { ModuleIndex : input . Spec . Index , ModuleKey : input . Spec . ModuleKey , ModuleID : input . Spec . ID }
progress . emit ( input . Progress , "started" , & section . Index , nil )
2026-11-16 14:53:14 +00:00
meta := contracts . SectionMetadataFromSection ( section )
proposalResult , err := input . Module . Propose ( ctx , contracts . ProposalRequest {
ExecutionContext : contracts . ExecutionContext {
StageIndex : & input . Spec . Index , StageID : input . Spec . ID ,
Settings : input . Spec . Settings ,
WorkingTranscript : transcriptFromSection ( section ) ,
Glossary : input . Spec . Glossary ,
ProtectedTerms : input . Spec . ProtectedTerms ,
GlossaryVariables : input . GlossaryVariables ,
Section : & meta ,
Interactions : input . Interactions ,
} ,
RunSpec : contracts . ModuleRunSpec {
ModuleKey : input . Spec . ModuleKey ,
InstanceName : input . Spec . InstanceName ,
ReplacementPolicy : input . Policy ,
CaptureProposalExchange : input . Spec . CaptureProposalExchange ,
} ,
LLMClient : input . ProposalClient ,
} )
result . started = true
result . proposal = proposalResult
if err != nil {
result . failure = locateFailure ( err , "proposal" , failureReference ( input . Spec , & meta . Index , outcomes . OperationProposal ) , true , proposalResult . Operations )
}
return result
2026-05-12 18:39:26 -05:00
}
func runModulePipeline ( ctx context . Context , input collectSectionProposalsInput ) ( modulePipelineResult , error ) {
2026-11-16 15:08:02 +00:00
var out modulePipelineResult
evidence := selectEvidence ( nil )
if input . Evidence != nil {
evidence = * input . Evidence
2026-05-12 12:59:46 -05:00
}
2026-11-16 15:08:02 +00:00
out . WorkUnits = input . ReportUnits
if evidence . reports && out . WorkUnits == nil {
out . WorkUnits = make ( [ ] WorkUnitOutcome , len ( input . Sections ) )
for i , section := range input . Sections {
out . WorkUnits [ i ] = newWorkUnit ( input . Spec , contracts . SectionMetadataFromSection ( section ) , 0 )
2026-09-24 17:23:59 +00:00
}
}
2026-05-12 18:39:26 -05:00
if len ( input . Sections ) == 0 {
return out , nil
2026-05-12 12:59:46 -05:00
}
2026-11-16 14:36:07 +00:00
repairPromptID := ""
if input . Spec . Settings . LLM . CorrectionRepairAttempts > 0 {
provider , ok := input . Module . ( interface { RepairPromptID ( ) string } )
if ! ok || input . Spec . RepairCoordinator == nil {
2026-11-16 14:42:24 +00:00
cause := & structuredoutput . Failure { Class : outcomes . FailureFatal , Reason : outcomes . ReasonSetupFailure , Message : "correction repair coordinator or prompt provider is unavailable" }
return out , locateFailure ( cause , "setup" , failureReference ( input . Spec , nil , "" ) , false , nil )
2026-11-16 14:36:07 +00:00
}
repairPromptID = provider . RepairPromptID ( )
if repairPromptID == "" {
2026-11-16 14:42:24 +00:00
cause := & structuredoutput . Failure { Class : outcomes . FailureFatal , Reason : outcomes . ReasonSetupFailure , Message : "correction repair prompt is unavailable" }
return out , locateFailure ( cause , "setup" , failureReference ( input . Spec , nil , "" ) , false , nil )
2026-11-16 14:36:07 +00:00
}
input . Spec . CaptureProposalExchange = true
}
2026-11-16 14:53:14 +00:00
pending := make ( map [ int ] * sectionWork , moduleWorkWindow )
proposalResults := make ( chan sectionProposalResult )
validationResults := make ( chan sectionValidationResult )
2026-05-12 18:39:26 -05:00
validatorsOrdered , validatorOrder := reorderValidatorsForPipeline ( input . Module . Validators ( ) )
2026-11-16 14:53:14 +00:00
nextSectionToRecord := 0
2026-05-12 18:39:26 -05:00
nextProposalIndex := 0
2026-05-12 12:59:46 -05:00
var (
2026-11-16 14:42:24 +00:00
firstFailure * ExecutionFailure
errMu sync . Mutex
2026-11-16 14:53:14 +00:00
workers sync . WaitGroup
2026-05-12 12:59:46 -05:00
)
2026-11-16 14:42:24 +00:00
runCtx , cancel := context . WithCancel ( ctx )
defer cancel ( )
setFailure := func ( failure * ExecutionFailure ) {
2026-11-16 14:53:14 +00:00
if failure == nil {
return
}
2026-09-24 15:32:13 +00:00
errMu . Lock ( )
defer errMu . Unlock ( )
2026-11-16 14:42:24 +00:00
if firstFailure == nil {
firstFailure = failure
2026-05-12 18:39:26 -05:00
cancel ( )
2026-09-24 15:32:13 +00:00
}
}
2026-11-16 14:53:14 +00:00
makeCandidates := func ( sectionResult sectionProposalResult ) [ ] proposals . EnrichedCorrectionProposal {
2026-11-16 14:42:24 +00:00
sectionEnriched := make ( [ ] proposals . EnrichedCorrectionProposal , 0 , len ( sectionResult . proposal . Proposals ) )
for i , corrected := range sectionResult . proposal . Proposals {
2026-11-16 14:53:14 +00:00
sectionIndex := sectionResult . section . Index
2026-11-16 14:42:24 +00:00
sectionEnriched = append ( sectionEnriched , proposals . EnrichedCorrectionProposal {
CorrectionProposal : corrected ,
ProposalMetadata : proposals . ProposalMetadata {
2026-11-16 14:53:14 +00:00
ProposalIndex : i ,
2026-11-16 14:42:24 +00:00
ModuleKey : input . Spec . ModuleKey ,
ModuleInstance : input . Spec . InstanceName ,
SectionIndex : & sectionIndex ,
} ,
} )
}
2026-11-16 14:53:14 +00:00
return sectionEnriched
}
2026-11-16 15:08:02 +00:00
recordCandidates := func ( unit * WorkUnitOutcome , enriched [ ] proposals . EnrichedCorrectionProposal ) {
2026-11-16 14:53:14 +00:00
unit . ProposalIndexOffset = nextProposalIndex
2026-11-16 15:08:02 +00:00
if evidence . candidates {
for _ , candidate := range enriched {
candidate . ProposalIndex += nextProposalIndex
out . Candidates = append ( out . Candidates , candidate )
}
2026-11-16 14:53:14 +00:00
}
for operationIndex := range unit . Operations {
operation := & unit . Operations [ operationIndex ]
2026-11-16 15:08:02 +00:00
if evidence . reports && operation . Reference . Kind == outcomes . OperationProposal && operation . Succeeded {
2026-11-16 14:53:14 +00:00
for _ , proposal := range enriched {
operation . ProposalIndices = append ( operation . ProposalIndices , nextProposalIndex + proposal . ProposalIndex )
2026-11-16 14:42:24 +00:00
}
}
}
2026-11-16 14:53:14 +00:00
nextProposalIndex += len ( enriched )
2026-05-12 18:39:26 -05:00
}
2026-11-16 14:53:14 +00:00
validate := func ( sectionResult sectionProposalResult , enriched [ ] proposals . EnrichedCorrectionProposal ) sectionValidationResult {
sectionPos := sectionResult . sectionPos
sectionMetadata := contracts . SectionMetadataFromSection ( sectionResult . section )
var interactions structuredoutput . InteractionSink
var capture * sectionInteractions
if input . Interactions != nil {
capture = & sectionInteractions { InteractionSink : input . Interactions }
interactions = capture
2026-09-24 17:23:59 +00:00
}
2026-11-16 14:53:14 +00:00
validationInput := validateSectionCandidatesInput {
2026-11-16 15:08:02 +00:00
Spec : input . Spec ,
SuppressDecisionHistory : ! evidence . decisions ,
Policy : input . Policy ,
WorkingTranscript : transcriptFromSection ( sectionResult . section ) ,
SectionIndex : & sectionMetadata . Index ,
Validators : validatorsOrdered ,
SectionEnriched : enriched ,
Exchange : sectionResult . proposal . Exchange ,
ValidationLLMClient : input . ValidationClient ,
GlossaryView : input . GlossaryView ,
Interactions : interactions ,
2026-05-12 18:39:26 -05:00
}
2026-11-16 14:53:14 +00:00
if input . Spec . Settings . LLM . CorrectionRepairAttempts > 0 && len ( enriched ) > 0 {
validationInput . Section = validators . NewSectionLookup ( validationInput . WorkingTranscript )
2026-09-24 17:23:59 +00:00
}
2026-11-16 14:53:14 +00:00
validated , err := validateSectionCandidates ( runCtx , validationInput )
var repairs [ ] RepairOutcome
phase , kind := "validation" , outcomes . OperationValidation
if err == nil && repairPromptID != "" {
phase , kind = "correction_repair" , outcomes . OperationCorrectionRepair
validated , repairs , err = repairCandidates ( runCtx , validationInput , sectionResult . proposal . SegmentReferences , validated , repairPromptID )
}
var failure * ExecutionFailure
if err != nil {
if ! errors . As ( err , & failure ) {
failure = locateFailure ( err , phase , failureReference ( input . Spec , & sectionMetadata . Index , kind ) , true , validated . operations )
2026-05-12 12:59:46 -05:00
}
2026-05-12 18:39:26 -05:00
}
2026-11-16 14:53:14 +00:00
return sectionValidationResult {
interactions : capture ,
repairs : repairs ,
sectionPos : sectionPos ,
approved : validated . approved ,
decisions : validated . decisions ,
rejected : validated . rejected ,
operations : validated . operations ,
filtered : validated . filtered ,
failure : failure ,
2026-11-16 14:42:24 +00:00
}
}
2026-11-16 15:08:02 +00:00
recordValidation := func ( unit * WorkUnitOutcome , res sectionValidationResult ) {
2026-09-24 17:23:59 +00:00
unit . Operations = append ( unit . Operations , res . operations ... )
2026-11-11 04:29:18 +00:00
unit . Repairs = res . repairs
2026-09-24 17:23:59 +00:00
mergeFilteredProposals ( unit , res . filtered )
2026-11-16 14:42:24 +00:00
unit . Failure = res . failure
if res . failure == nil {
2026-09-24 17:23:59 +00:00
unit . State = outcomes . WorkCompleted
} else {
unit . State = outcomes . WorkInterrupted
2026-05-12 18:39:26 -05:00
}
out . Approved = append ( out . Approved , res . approved ... )
2026-11-16 15:08:02 +00:00
if evidence . decisions {
out . ValidatorDecisions = append ( out . ValidatorDecisions , res . decisions ... )
out . ValidatorRejected = append ( out . ValidatorRejected , res . rejected ... )
}
if len ( res . approved ) > 0 {
out . rememberProposalRecovery ( unit )
}
2026-05-12 18:39:26 -05:00
}
2026-11-16 14:53:14 +00:00
2026-11-16 15:12:06 +00:00
progress := Progress { ModuleIndex : input . Spec . Index , ModuleKey : input . Spec . ModuleKey , ModuleID : input . Spec . ID }
2026-11-16 14:53:14 +00:00
nextAdmission , active := 0 , 0
for {
for work := pending [ nextSectionToRecord ] ; work != nil && work . finished ; work = pending [ nextSectionToRecord ] {
offset := nextProposalIndex
2026-11-16 15:08:02 +00:00
recordCandidates ( & work . unit , work . candidates )
2026-11-16 14:53:14 +00:00
if work . validation != nil {
work . validation . assignProposalIndices ( offset )
2026-11-16 15:08:02 +00:00
recordValidation ( & work . unit , * work . validation )
}
out . Totals . Record ( work . unit . State , work . unit . Operations )
if evidence . reports {
out . WorkUnits [ nextSectionToRecord ] = work . unit
} else if evidence . candidates && len ( work . unit . Repairs ) > 0 {
work . unit . Operations = nil
out . WorkUnits = append ( out . WorkUnits , work . unit )
2026-11-16 14:53:14 +00:00
}
delete ( pending , nextSectionToRecord )
nextSectionToRecord ++
}
for len ( pending ) < moduleWorkWindow && nextAdmission < len ( input . Sections ) && runCtx . Err ( ) == nil {
sectionPos := nextAdmission
nextAdmission ++
2026-11-16 15:08:02 +00:00
pending [ sectionPos ] = & sectionWork { unit : newWorkUnit ( input . Spec , contracts . SectionMetadataFromSection ( input . Sections [ sectionPos ] ) , 0 ) }
2026-11-16 14:53:14 +00:00
active ++
workers . Go ( func ( ) {
proposed := proposeSection ( runCtx , input , sectionPos )
setFailure ( proposed . failure )
proposalResults <- proposed
} )
}
if active == 0 {
break
}
select {
case result := <- proposalResults :
active --
2026-11-16 15:08:02 +00:00
work := pending [ result . sectionPos ]
unit := & work . unit
2026-11-16 14:53:14 +00:00
unit . Operations = append ( unit . Operations , result . proposal . Operations ... )
if result . started {
unit . State = outcomes . WorkInterrupted
}
unit . Failure = result . failure
if result . started && result . failure == nil {
work . candidates = makeCandidates ( result )
if runCtx . Err ( ) == nil {
enriched := work . candidates
active ++
workers . Go ( func ( ) {
validated := validate ( result , enriched )
setFailure ( validated . failure )
validationResults <- validated
} )
continue
}
}
work . finished = true
2026-11-16 15:12:06 +00:00
if result . started {
progress . emit ( input . Progress , "interrupted" , & work . unit . Section . Index , nil )
}
2026-11-16 14:53:14 +00:00
case validated := <- validationResults :
active --
work := pending [ validated . sectionPos ]
work . validation = & validated
work . finished = true
2026-11-16 15:12:06 +00:00
state := "completed"
if validated . failure != nil {
state = "interrupted"
}
proposals := len ( work . candidates )
progress . emit ( input . Progress , state , & work . unit . Section . Index , & proposals )
2026-11-16 14:53:14 +00:00
}
}
workers . Wait ( )
sort . SliceStable ( out . Approved , func ( i , j int ) bool {
return out . Approved [ i ] . ProposalIndex < out . Approved [ j ] . ProposalIndex
} )
2026-05-12 18:39:26 -05:00
sort . SliceStable ( out . ValidatorDecisions , func ( i , j int ) bool {
if out . ValidatorDecisions [ i ] . ProposalIndex != out . ValidatorDecisions [ j ] . ProposalIndex {
return out . ValidatorDecisions [ i ] . ProposalIndex < out . ValidatorDecisions [ j ] . ProposalIndex
}
return validatorOrder [ out . ValidatorDecisions [ i ] . ValidatorName ] < validatorOrder [ out . ValidatorDecisions [ j ] . ValidatorName ]
} )
sort . SliceStable ( out . ValidatorRejected , func ( i , j int ) bool {
if out . ValidatorRejected [ i ] . ProposalIndex != out . ValidatorRejected [ j ] . ProposalIndex {
return out . ValidatorRejected [ i ] . ProposalIndex < out . ValidatorRejected [ j ] . ProposalIndex
}
return validatorOrder [ out . ValidatorRejected [ i ] . ValidatorName ] < validatorOrder [ out . ValidatorRejected [ j ] . ValidatorName ]
} )
2026-11-16 14:42:24 +00:00
if err := ctx . Err ( ) ; err != nil {
setFailure ( locateFailure ( err , "processing" , failureReference ( input . Spec , nil , "" ) , false , nil ) )
2026-09-24 17:23:59 +00:00
}
2026-05-12 18:39:26 -05:00
2026-11-16 14:42:24 +00:00
if firstFailure != nil {
2026-11-11 04:29:18 +00:00
for i := range out . WorkUnits {
for j := range out . WorkUnits [ i ] . Repairs {
if out . WorkUnits [ i ] . Repairs [ j ] . Outcome == "approved" {
out . WorkUnits [ i ] . Repairs [ j ] . Outcome = "interrupted"
}
}
}
2026-11-16 14:42:24 +00:00
return out , firstFailure
2026-05-12 12:59:46 -05:00
}
2026-05-12 18:39:26 -05:00
return out , nil
}
type validateSectionCandidatesInput struct {
2026-11-16 15:08:02 +00:00
SuppressDecisionHistory bool
Section * validators . SectionLookup
RepairProposalIndex * int
RepairAttempt * int
Exchange * structuredoutput . Exchange
Spec contracts . ModuleRunSpec
Policy proposals . ReplacementPolicy
WorkingTranscript * schema . Transcript
SectionIndex * int
Validators [ ] validators . Validator
SectionEnriched [ ] proposals . EnrichedCorrectionProposal
ValidationLLMClient contracts . StructuredLLMClient
Interactions structuredoutput . InteractionSink
GlossaryView * validators . GlossaryView
2026-05-12 18:39:26 -05:00
}
type validateSectionCandidatesResult struct {
2026-09-24 17:23:59 +00:00
approved [ ] proposals . EnrichedCorrectionProposal
decisions [ ] ValidatorDecisionRecord
rejected [ ] ValidatorRejectedChange
operations [ ] outcomes . Operation
filtered [ ] outcomes . FilteredProposal
2026-05-12 18:39:26 -05:00
}
func validateSectionCandidates ( ctx context . Context , input validateSectionCandidatesInput ) ( validateSectionCandidatesResult , error ) {
decisions := make ( [ ] ValidatorDecisionRecord , 0 )
rejected := make ( [ ] ValidatorRejectedChange , 0 )
2026-09-24 17:23:59 +00:00
operations := make ( [ ] outcomes . Operation , 0 )
filtered := make ( [ ] outcomes . FilteredProposal , 0 )
2026-05-12 18:39:26 -05:00
eligible := append ( [ ] proposals . EnrichedCorrectionProposal ( nil ) , input . SectionEnriched ... )
2026-11-11 04:23:34 +00:00
feedback := input . Spec . Settings . LLM . CorrectionRepairAttempts > 0
2026-11-11 12:28:00 +00:00
var blocked , noEffect map [ int ] bool
2026-11-16 14:11:02 +00:00
evidence := validators . Request { WorkingTranscript : input . WorkingTranscript , Section : input . Section , ReplacementPolicy : input . Policy , IndependentEvidence : feedback }
2026-11-11 12:28:00 +00:00
if feedback {
blocked , noEffect = make ( map [ int ] bool ) , make ( map [ int ] bool )
evidence . Previews = make ( map [ int ] proposals . SegmentPreviewResult )
if evidence . Section == nil && len ( input . SectionEnriched ) > 0 {
evidence . Section = validators . NewSectionLookup ( input . WorkingTranscript )
}
}
2026-05-12 18:39:26 -05:00
2026-09-24 17:23:59 +00:00
for validatorOrder , validator := range input . Validators {
if err := ctx . Err ( ) ; err != nil {
return validateSectionCandidatesResult { decisions : decisions , rejected : rejected , operations : operations , filtered : filtered } , err
}
2026-11-16 14:11:02 +00:00
class := validatormetadata . ClassOf ( validator )
2026-11-11 04:23:34 +00:00
submitted := eligible
if feedback {
2026-11-16 14:11:02 +00:00
submitted = feedbackCandidates ( input , validator . Name ( ) , class == validatormetadata . ExecutionClassLLMBacked , noEffect , evidence )
2026-11-11 04:23:34 +00:00
}
2026-11-16 14:11:02 +00:00
vResult , err := validator . Validate ( ctx , validators . Request {
2026-11-10 17:37:41 +00:00
StageIndex : & input . Spec . Index , StageID : input . Spec . ID ,
2026-11-11 12:28:00 +00:00
WorkingTranscript : input . WorkingTranscript ,
Section : evidence . Section , Previews : evidence . Previews ,
2026-11-11 04:37:52 +00:00
CandidateProposal : submitted ,
ModuleKey : input . Spec . ModuleKey ,
ModuleInstance : input . Spec . InstanceName ,
SectionIndex : input . SectionIndex ,
IndependentEvidence : feedback , RepairProposalIndex : input . RepairProposalIndex , RepairAttempt : input . RepairAttempt ,
2026-11-16 03:43:29 +00:00
ValidatorOrder : & validatorOrder ,
ReplacementPolicy : input . Policy ,
Glossary : input . Spec . Glossary ,
ProtectedTerms : input . Spec . ProtectedTerms ,
GlossaryView : input . GlossaryView ,
Settings : input . Spec . Settings ,
LLMClient : input . ValidationLLMClient ,
Interactions : input . Interactions ,
2026-05-12 18:39:26 -05:00
} )
2026-09-24 17:23:59 +00:00
operations = append ( operations , vResult . Operations ... )
2026-09-24 02:18:31 +00:00
2026-11-16 14:42:24 +00:00
locate := func ( cause error ) * ExecutionFailure {
ref := failureReference ( input . Spec , input . SectionIndex , outcomes . OperationValidation )
ref . ValidatorName , ref . ValidatorOrder = validator . Name ( ) , & validatorOrder
ref . ProposalIndex , ref . RepairAttempt = input . RepairProposalIndex , input . RepairAttempt
return locateFailure ( cause , "validation" , ref , input . SectionIndex != nil , vResult . Operations )
}
2026-09-24 21:48:18 +00:00
var decisionErr error
2026-05-12 18:39:26 -05:00
if err != nil {
2026-11-11 04:23:34 +00:00
decisionErr = validators . EnforcePartialDecisionCardinality ( submitted , vResult . Decisions )
2026-09-24 21:48:18 +00:00
} else {
2026-11-11 04:23:34 +00:00
decisionErr = validators . EnforceDecisionCardinality ( submitted , vResult . Decisions )
2026-05-12 18:39:26 -05:00
}
2026-09-24 21:48:18 +00:00
if decisionErr != nil {
2026-05-12 18:39:26 -05:00
return validateSectionCandidatesResult {
2026-09-24 21:48:18 +00:00
decisions : decisions , rejected : rejected , operations : operations , filtered : filtered ,
2026-11-16 14:42:24 +00:00
} , locate ( fmt . Errorf ( "validator %q cardinality failed: %w" , validator . Name ( ) , errors . Join ( err , decisionErr ) ) )
2026-05-12 18:39:26 -05:00
}
2026-11-11 04:23:34 +00:00
nextEligible := make ( [ ] proposals . EnrichedCorrectionProposal , 0 , len ( submitted ) )
byIndex := make ( map [ int ] proposals . EnrichedCorrectionProposal , len ( submitted ) )
for _ , p := range submitted {
2026-05-12 18:39:26 -05:00
byIndex [ p . ProposalIndex ] = p
}
2026-11-16 14:11:02 +00:00
dispositions := make ( [ ] validators . DecisionDisposition , len ( vResult . Decisions ) )
for i , decision := range vResult . Decisions {
disposition , contractErr := decision . EffectiveDisposition ( )
if contractErr != nil {
return validateSectionCandidatesResult {
decisions : decisions , rejected : rejected , operations : operations , filtered : filtered ,
2026-11-16 14:42:24 +00:00
} , locate ( fmt . Errorf ( "validator %q returned inconsistent decisions: %w" , validator . Name ( ) , errors . Join ( err , contractErr ) ) )
2026-11-11 04:23:34 +00:00
}
2026-11-16 14:11:02 +00:00
dispositions [ i ] = disposition
}
for i , d := range vResult . Decisions {
disposition := dispositions [ i ]
approved := disposition == validators . DispositionApproved
if feedback && ! approved {
2026-11-11 04:23:34 +00:00
blocked [ d . ProposalIndex ] = true
if d . ReasonCode == validators . ReasonNoEffect {
noEffect [ d . ProposalIndex ] = true
}
}
2026-11-16 15:08:02 +00:00
if ! input . SuppressDecisionHistory {
decisions = append ( decisions , ValidatorDecisionRecord {
ValidatorName : validator . Name ( ) ,
ExecutionClass : class ,
ProposalIndex : d . ProposalIndex ,
Approved : approved ,
Disposition : disposition ,
ReasonCode : d . ReasonCode ,
Message : d . Message ,
DiagnosticArtifactPath : d . DiagnosticArtifactPath ,
} )
}
2026-11-16 14:11:02 +00:00
if approved {
2026-05-12 18:39:26 -05:00
nextEligible = append ( nextEligible , byIndex [ d . ProposalIndex ] )
continue
}
2026-11-16 14:11:02 +00:00
if disposition != validators . DispositionUnavailable {
2026-09-24 17:23:59 +00:00
if reason , ok := validators . DefectiveProposalReason ( d . ReasonCode ) ; ok {
filtered = append ( filtered , outcomes . FilteredProposal { ProposalIndex : d . ProposalIndex , Reason : reason , Count : 1 } )
}
}
2026-11-16 15:08:02 +00:00
if feedback || ! input . SuppressDecisionHistory {
p := byIndex [ d . ProposalIndex ]
rejected = append ( rejected , ValidatorRejectedChange {
ValidatorName : validator . Name ( ) ,
ProposalIndex : p . ProposalIndex ,
ModuleKey : p . ModuleKey ,
ModuleInstance : p . ModuleInstance ,
TargetSegmentID : p . TargetSegmentID ,
OriginalText : p . OriginalText ,
CorrectedText : p . CorrectedText ,
Disposition : disposition ,
ReasonCode : d . ReasonCode ,
Message : d . Message ,
} )
}
2026-05-12 12:59:46 -05:00
}
2026-05-12 18:39:26 -05:00
2026-09-24 21:48:18 +00:00
if err != nil {
// Completed decisions are evidence only. A fatal return never grants
// approval to candidates that have not finished the full chain.
return validateSectionCandidatesResult {
decisions : decisions , rejected : rejected , operations : operations , filtered : filtered ,
2026-11-16 14:42:24 +00:00
} , locate ( fmt . Errorf ( "validator %q failed: %w" , validator . Name ( ) , err ) )
2026-09-24 21:48:18 +00:00
}
2026-05-12 18:39:26 -05:00
eligible = nextEligible
}
2026-11-11 04:23:34 +00:00
if feedback {
eligible = eligible [ : 0 ]
for _ , candidate := range input . SectionEnriched {
if ! blocked [ candidate . ProposalIndex ] {
eligible = append ( eligible , candidate )
}
}
}
2026-05-12 18:39:26 -05:00
return validateSectionCandidatesResult {
2026-09-24 17:23:59 +00:00
approved : eligible ,
decisions : decisions ,
rejected : rejected ,
operations : operations ,
filtered : filtered ,
2026-05-12 18:39:26 -05:00
} , nil
}
2026-09-24 17:23:59 +00:00
// mergeFilteredProposals attributes defective candidates to their originating
// proposal response. Each response has at most one filtering recovery reason.
func mergeFilteredProposals ( unit * WorkUnitOutcome , filtered [ ] outcomes . FilteredProposal ) {
2026-11-16 15:00:05 +00:00
if len ( filtered ) == 0 {
2026-09-24 17:23:59 +00:00
return
}
for i := range unit . Operations {
operation := & unit . Operations [ i ]
2026-11-16 15:00:05 +00:00
if operation . Reference . Kind == outcomes . OperationProposal && operation . Succeeded {
mergeOperationFiltering ( operation , filtered )
return
2026-09-24 17:23:59 +00:00
}
2026-11-16 15:00:05 +00:00
}
}
func mergeOperationFiltering ( operation * outcomes . Operation , filtered [ ] outcomes . FilteredProposal ) {
seen := make ( map [ int ] struct { } , len ( operation . Filtered ) + len ( filtered ) )
for _ , existing := range operation . Filtered {
seen [ existing . ProposalIndex ] = struct { } { }
}
for _ , item := range filtered {
if _ , ok := seen [ item . ProposalIndex ] ; ok {
2026-09-24 17:23:59 +00:00
continue
}
2026-11-16 15:00:05 +00:00
seen [ item . ProposalIndex ] = struct { } { }
operation . Filtered = append ( operation . Filtered , item )
}
sort . Slice ( operation . Filtered , func ( a , b int ) bool {
return operation . Filtered [ a ] . ProposalIndex < operation . Filtered [ b ] . ProposalIndex
} )
if ! slices . Contains ( operation . RecoveryReasons , outcomes . RecoveryProposalsFiltered ) {
operation . RecoveryReasons = append ( operation . RecoveryReasons , outcomes . RecoveryProposalsFiltered )
2026-09-24 17:23:59 +00:00
}
}
2026-11-16 15:08:02 +00:00
func findApplyFiltering ( sections [ ] chunking . Section , approved [ ] proposals . EnrichedCorrectionProposal , skipped [ ] proposals . SkippedChange , policy proposals . ReplacementPolicy ) map [ int ] [ ] outcomes . FilteredProposal {
2026-11-16 15:00:05 +00:00
if len ( skipped ) == 0 {
2026-11-16 15:08:02 +00:00
return nil
2026-11-16 15:00:05 +00:00
}
2026-09-24 17:23:59 +00:00
byIndex := make ( map [ int ] proposals . EnrichedCorrectionProposal , len ( approved ) )
sectionPosition := make ( map [ int ] int , len ( sections ) )
for pos , section := range sections {
sectionPosition [ section . Index ] = pos
}
for _ , proposal := range approved {
byIndex [ proposal . ProposalIndex ] = proposal
}
2026-11-16 15:00:05 +00:00
lookups := make ( map [ int ] * validators . SectionLookup )
filtered := make ( map [ int ] [ ] outcomes . FilteredProposal )
2026-09-24 17:23:59 +00:00
for _ , skip := range skipped {
2026-11-16 15:00:05 +00:00
reason , defective := validators . DefectiveProposalReason ( string ( skip . SkipReason ) )
2026-09-24 17:23:59 +00:00
proposal , ok := byIndex [ skip . ProposalIndex ]
2026-11-16 15:00:05 +00:00
if ! defective || ! ok || proposal . SectionIndex == nil {
2026-09-24 17:23:59 +00:00
continue
}
pos , ok := sectionPosition [ * proposal . SectionIndex ]
if ! ok {
continue
}
2026-11-16 15:00:05 +00:00
lookup := lookups [ pos ]
if lookup == nil {
lookup = validators . NewSectionLookup ( transcriptFromSection ( sections [ pos ] ) )
lookups [ pos ] = lookup
}
2026-09-24 17:23:59 +00:00
var segment * schema . Segment
2026-11-16 15:00:05 +00:00
if i , found := lookup . Position ( proposal . TargetSegmentID ) ; found {
segment = & sections [ pos ] . Segments [ i ]
2026-09-24 17:23:59 +00:00
}
2026-11-16 15:00:05 +00:00
// A previously applicable proposal can become stale during application.
2026-09-24 17:23:59 +00:00
if proposals . PreviewProposalForSegment ( segment , proposal . CorrectionProposal , policy ) . Applicable {
continue
}
2026-11-16 15:00:05 +00:00
filtered [ pos ] = append ( filtered [ pos ] , outcomes . FilteredProposal { ProposalIndex : skip . ProposalIndex , Reason : reason , Count : 1 } )
}
2026-11-16 15:08:02 +00:00
return filtered
2026-09-24 17:23:59 +00:00
}
2026-11-16 14:11:02 +00:00
func reorderValidatorsForPipeline ( in [ ] validators . Validator ) ( [ ] validators . Validator , map [ string ] int ) {
deterministic := make ( [ ] validators . Validator , 0 , len ( in ) )
llmBacked := make ( [ ] validators . Validator , 0 , len ( in ) )
2026-05-12 18:39:26 -05:00
for _ , validator := range in {
2026-11-16 14:11:02 +00:00
class := validatormetadata . ClassOf ( validator )
validator = validatormetadata . Wrap ( validator , class )
if class == validatormetadata . ExecutionClassLLMBacked {
2026-05-12 18:39:26 -05:00
llmBacked = append ( llmBacked , validator )
continue
}
deterministic = append ( deterministic , validator )
}
ordered := append ( deterministic , llmBacked ... )
order := make ( map [ string ] int , len ( ordered ) )
for idx , validator := range ordered {
order [ validator . Name ( ) ] = idx
2026-05-12 12:59:46 -05:00
}
2026-05-12 18:39:26 -05:00
return ordered , order
2026-05-12 12:59:46 -05:00
}
2026-05-12 12:25:14 -05:00
func chunkWorkingTranscript ( cfg * config . Config , transcript * schema . Transcript ) ( [ ] chunking . Section , error ) {
if cfg == nil {
if transcript == nil || len ( transcript . Segments ) == 0 {
return [ ] chunking . Section { } , nil
}
return [ ] chunking . Section {
{
Index : 0 ,
EstimatedTokens : estimateTranscriptTokens ( transcript ) ,
Segments : append ( [ ] schema . Segment ( nil ) , transcript . Segments ... ) ,
StartSegmentID : transcript . Segments [ 0 ] . ID ,
EndSegmentID : transcript . Segments [ len ( transcript . Segments ) - 1 ] . ID ,
} ,
} , nil
}
chunker := chunking . NewChunker ( chunking . ChunkingConfig {
2026-09-24 02:18:31 +00:00
MaxSectionTokens : cfg . Chunking . MaxSectionTokens ,
MinSectionTokens : cfg . Chunking . MinSectionTokens ,
TargetSections : cfg . Chunking . TargetSections ,
2026-05-12 12:25:14 -05:00
} )
return chunker . ChunkTranscript ( transcript )
}
func estimateTranscriptTokens ( transcript * schema . Transcript ) int {
if transcript == nil || len ( transcript . Segments ) == 0 {
return 0
}
estimator := chunking . NewSimpleTokenEstimator ( )
total := 0
for _ , segment := range transcript . Segments {
total += estimator . EstimateTokens ( segment . Text )
}
return total
}
func transcriptFromSection ( section chunking . Section ) * schema . Transcript {
segments := make ( [ ] schema . Segment , len ( section . Segments ) )
copy ( segments , section . Segments )
return & schema . Transcript { Segments : segments }
}