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-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" `
ProposalCount int ` json:"proposal_count" `
Operations [ ] outcomes . Operation ` json:"operations,omitempty" `
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-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
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" `
CorrectionsApplied int ` json:"corrections_applied" `
2026-11-11 04:29:18 +00:00
Repairs [ ] RepairOutcome ` json:"-" `
2026-09-24 17:01:19 +00:00
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
}
// RunInput is the deterministic runner input.
type RunInput struct {
2026-11-16 03:43:29 +00:00
Config * config . Config
Transcript * schema . Transcript
ModuleSpecs [ ] contracts . ModuleRunSpec
Interactions structuredoutput . InteractionSink
CaptureCandidates bool
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" `
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 ) {
if r == nil || r . factory == nil {
return RunOutput { } , fmt . Errorf ( "runner module factory is required" )
}
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 {
return RunOutput { FinalTranscript : working } , fmt . Errorf ( "chunk normalized transcript: %w" , err )
}
inventory := make ( [ ] contracts . SectionMetadata , len ( sections ) )
workUnits := make ( [ ] WorkUnitOutcome , 0 , len ( input . ModuleSpecs ) * len ( sections ) )
2026-09-24 17:41:10 +00:00
var candidates [ ] proposals . EnrichedCorrectionProposal
2026-09-24 17:17:14 +00:00
for i , section := range sections {
inventory [ i ] = contracts . SectionMetadataFromSection ( section )
}
for moduleIndex , spec := range input . ModuleSpecs {
for _ , section := range inventory {
workUnits = append ( workUnits , WorkUnitOutcome {
2026-11-10 17:37:41 +00:00
ModuleIndex : moduleIndex , StageIndex : spec . Index , StageID : spec . ID , ModuleKey : spec . ModuleKey ,
2026-09-24 17:17:14 +00:00
ModuleInstance : spec . InstanceName , Section : section ,
State : outcomes . WorkNotStarted ,
} )
}
}
snapshot := func ( ) RunOutput {
return RunOutput {
FinalTranscript : transcriptFromSections ( sections ) , ModuleResults : results ,
2026-09-24 17:48:52 +00:00
Sections : inventory , InventoryAvailable : true , 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-05-11 14:22:22 +00:00
module , err := r . factory . ModuleForSpec ( spec )
if err != nil {
failed := ModuleResult {
2026-11-10 17:37:41 +00:00
StageIndex : spec . Index , StageID : spec . ID ,
2026-05-11 14:22:22 +00:00
ModuleKey : spec . ModuleKey ,
ModuleInstance : spec . InstanceName ,
Status : ModuleStatusFailed ,
ErrorMessage : err . Error ( ) ,
}
results = append ( results , failed )
2026-09-24 17:17:14 +00:00
return snapshot ( ) , fmt . Errorf ( "module %q setup failed: %w" , spec . InstanceName , err )
2026-05-11 14:22:22 +00:00
}
policy := module . ReplacementPolicy ( )
2026-11-16 14:18:38 +00:00
if err := spec . PrepareGlossary ( ) ; err != nil {
2026-11-10 20:42:28 +00:00
return snapshot ( ) , fmt . Errorf ( "module %q glossary preparation: %w" , spec . InstanceName , err )
}
2026-05-12 18:39:26 -05:00
pipelineResult , pipelineErr := runModulePipeline ( ctx , collectSectionProposalsInput {
2026-11-16 03:43:29 +00:00
Module : module ,
Spec : spec ,
Policy : policy ,
Sections : sections ,
ProposalClient : spec . ProposalClient ,
Interactions : input . Interactions ,
ValidationClient : spec . ValidationClient ,
2026-11-16 14:18:38 +00:00
GlossaryVariables : spec . GlossaryVariables ,
GlossaryView : spec . GlossaryView ,
2026-05-12 12:59:46 -05:00
} )
2026-09-24 17:41:10 +00:00
if input . CaptureCandidates {
candidates = append ( candidates , pipelineResult . Candidates ... )
}
2026-11-11 04:29:18 +00:00
if pipelineErr == nil && ctx . Err ( ) != nil {
pipelineErr = ctx . Err ( )
for i := range pipelineResult . WorkUnits {
for j := range pipelineResult . WorkUnits [ i ] . Repairs {
if pipelineResult . WorkUnits [ i ] . Repairs [ j ] . Outcome == "approved" {
pipelineResult . WorkUnits [ i ] . Repairs [ j ] . Outcome = "interrupted"
}
}
}
}
2026-05-12 18:39:26 -05:00
if pipelineErr != nil {
2026-05-12 12:59:46 -05:00
failed := ModuleResult {
2026-11-10 17:37:41 +00:00
StageIndex : spec . Index , StageID : spec . ID ,
2026-05-12 18:39:26 -05:00
ModuleKey : spec . ModuleKey ,
ModuleInstance : spec . InstanceName ,
ReplacementPolicy : policy ,
Status : ModuleStatusFailed ,
ProposalCount : pipelineResult . ProposalCount ,
2026-09-24 17:23:59 +00:00
Operations : pipelineResult . Operations ,
2026-05-12 18:39:26 -05:00
ValidatorDecisions : pipelineResult . ValidatorDecisions ,
ValidatorRejected : pipelineResult . ValidatorRejected ,
ErrorMessage : pipelineErr . Error ( ) ,
2026-05-12 12:25:14 -05:00
}
2026-05-12 12:59:46 -05:00
results = append ( results , failed )
2026-09-24 17:23:59 +00:00
copy ( workUnits [ moduleIndex * len ( sections ) : ( moduleIndex + 1 ) * len ( sections ) ] , pipelineResult . WorkUnits )
for sectionPos := range sections {
workUnits [ moduleIndex * len ( sections ) + sectionPos ] . ModuleIndex = moduleIndex
}
2026-09-24 17:17:14 +00:00
return snapshot ( ) , fmt . Errorf ( "module %q failed: %w" , spec . InstanceName , pipelineErr )
2026-05-12 00:26:27 +00:00
}
2026-11-16 14:18:38 +00:00
updated , applied , skipped , appliedBySection , applyErr := applyWithinSections ( sections , pipelineResult . Approved , policy , spec . GlossaryView . Protection , spec . ModuleKey == modulecatalog . KeySpokenWord )
2026-09-24 17:17:14 +00:00
if applyErr != nil {
2026-11-11 04:29:18 +00:00
for i := range pipelineResult . WorkUnits {
unit := & pipelineResult . WorkUnits [ i ]
unit . ModuleIndex = moduleIndex
unit . State = outcomes . WorkInterrupted
for j := range unit . Repairs {
if unit . Repairs [ j ] . Outcome == "approved" {
unit . Repairs [ j ] . Outcome = "interrupted"
}
}
}
copy ( workUnits [ moduleIndex * len ( sections ) : ( moduleIndex + 1 ) * len ( sections ) ] , pipelineResult . WorkUnits )
results = append ( results , ModuleResult { StageIndex : spec . Index , StageID : spec . ID , ModuleKey : spec . ModuleKey , ModuleInstance : spec . InstanceName , ReplacementPolicy : policy , Status : ModuleStatusFailed , ProposalCount : pipelineResult . ProposalCount , Operations : pipelineResult . Operations , ValidatorDecisions : pipelineResult . ValidatorDecisions , ValidatorRejected : pipelineResult . ValidatorRejected , ErrorMessage : applyErr . Error ( ) } )
2026-09-24 17:17:14 +00:00
return snapshot ( ) , fmt . Errorf ( "module %q application failed: %w" , spec . InstanceName , applyErr )
}
2026-09-24 17:23:59 +00:00
copy ( workUnits [ moduleIndex * len ( sections ) : ( moduleIndex + 1 ) * len ( sections ) ] , pipelineResult . WorkUnits )
2026-09-24 17:17:14 +00:00
for sectionPos := range sections {
unit := & workUnits [ moduleIndex * len ( sections ) + sectionPos ]
2026-09-24 17:23:59 +00:00
unit . ModuleIndex = moduleIndex
2026-09-24 17:17:14 +00:00
unit . State = outcomes . WorkCompleted
unit . CorrectionsApplied = appliedBySection [ sectionPos ]
}
2026-11-11 12:29:42 +00:00
var appliedIndices map [ int ] struct { }
2026-11-11 04:29:18 +00:00
for i := range workUnits [ moduleIndex * len ( sections ) : ( moduleIndex + 1 ) * len ( sections ) ] {
unit := & workUnits [ moduleIndex * len ( sections ) + i ]
for j := range unit . Repairs {
branch := & unit . Repairs [ j ]
if branch . Outcome != "approved" {
continue
}
2026-11-11 12:29:42 +00:00
if appliedIndices == nil {
appliedIndices = make ( map [ int ] struct { } , len ( applied ) )
for _ , change := range applied {
appliedIndices [ change . ProposalIndex ] = struct { } { }
2026-11-11 04:29:18 +00:00
}
}
2026-11-11 12:29:42 +00:00
branch . Outcome = "approved_skipped"
if _ , ok := appliedIndices [ branch . ProposalIndex ] ; ok {
branch . Outcome = "applied"
}
2026-11-11 04:29:18 +00:00
}
}
2026-09-24 17:23:59 +00:00
attributeApplyFiltering ( sections , pipelineResult . Approved , skipped , policy , workUnits [ moduleIndex * len ( sections ) : ( moduleIndex + 1 ) * len ( sections ) ] )
sections = updated
moduleOperations := make ( [ ] outcomes . Operation , 0 )
for _ , unit := range workUnits [ moduleIndex * len ( sections ) : ( moduleIndex + 1 ) * len ( sections ) ] {
moduleOperations = append ( moduleOperations , unit . Operations ... )
}
2026-05-11 14:22:22 +00:00
results = append ( results , ModuleResult {
2026-11-10 17:37:41 +00:00
StageIndex : spec . Index , StageID : spec . ID ,
2026-05-12 00:26:27 +00:00
ModuleKey : spec . ModuleKey ,
ModuleInstance : spec . InstanceName ,
ReplacementPolicy : policy ,
Status : ModuleStatusSuccess ,
2026-05-12 18:39:26 -05:00
ProposalCount : pipelineResult . ProposalCount ,
2026-09-24 17:23:59 +00:00
Operations : moduleOperations ,
2026-05-12 18:39:26 -05:00
ValidatorDecisions : pipelineResult . ValidatorDecisions ,
ValidatorRejected : pipelineResult . ValidatorRejected ,
2026-09-24 17:17:14 +00:00
AppliedChanges : applied ,
SkippedChanges : skipped ,
2026-05-11 14:22:22 +00:00
} )
}
2026-09-24 17:17:14 +00:00
output := snapshot ( )
2026-05-13 19:03:37 +00:00
return output , 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 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-05-12 18:39:26 -05:00
err error
}
type sectionValidationResult struct {
2026-11-11 04:29:18 +00:00
repairs [ ] RepairOutcome
2026-05-12 18:39:26 -05:00
sectionPos int
enriched [ ] proposals . EnrichedCorrectionProposal
approved [ ] proposals . EnrichedCorrectionProposal
decisions [ ] ValidatorDecisionRecord
rejected [ ] ValidatorRejectedChange
2026-09-24 17:23:59 +00:00
operations [ ] outcomes . Operation
filtered [ ] outcomes . FilteredProposal
2026-05-12 18:39:26 -05:00
err error
}
type modulePipelineResult struct {
ProposalCount int
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
Operations [ ] outcomes . Operation
WorkUnits [ ] WorkUnitOutcome
2026-05-12 18:39:26 -05:00
ValidatorDecisions [ ] ValidatorDecisionRecord
ValidatorRejected [ ] ValidatorRejectedChange
}
func collectSectionProposals ( ctx context . Context , input collectSectionProposalsInput ) ( context . Context , <- chan sectionProposalResult , context . CancelFunc ) {
results := make ( chan sectionProposalResult , len ( input . Sections ) )
runCtx , cancel := context . WithCancel ( ctx )
2026-05-12 12:59:46 -05:00
2026-05-12 18:39:26 -05:00
go func ( ) {
defer close ( results )
var wg sync . WaitGroup
for sectionPos , section := range input . Sections {
sectionPos := sectionPos
section := section
wg . Add ( 1 )
go func ( ) {
defer wg . Done ( )
2026-09-24 17:23:59 +00:00
if runCtx . Err ( ) != nil {
results <- sectionProposalResult { sectionPos : sectionPos , section : section }
return
}
2026-05-12 18:39:26 -05:00
meta := contracts . SectionMetadataFromSection ( section )
2026-09-24 02:18:31 +00:00
proposalResult , err := input . Module . Propose ( runCtx , contracts . ProposalRequest {
2026-05-12 18:39:26 -05:00
ExecutionContext : contracts . ExecutionContext {
2026-11-10 17:37:41 +00:00
StageIndex : & input . Spec . Index , StageID : input . Spec . ID ,
2026-11-16 03:43:29 +00:00
Settings : input . Spec . Settings ,
WorkingTranscript : transcriptFromSection ( section ) ,
Glossary : input . Spec . Glossary ,
ProtectedTerms : input . Spec . ProtectedTerms ,
GlossaryVariables : input . GlossaryVariables ,
Section : & meta ,
Interactions : input . Interactions ,
2026-05-12 18:39:26 -05:00
} ,
RunSpec : contracts . ModuleRunSpec {
2026-11-11 04:18:05 +00:00
ModuleKey : input . Spec . ModuleKey ,
InstanceName : input . Spec . InstanceName ,
ReplacementPolicy : input . Policy ,
CaptureProposalExchange : input . Spec . CaptureProposalExchange ,
2026-05-12 18:39:26 -05:00
} ,
2026-09-24 02:18:31 +00:00
LLMClient : input . ProposalClient ,
2026-05-12 18:39:26 -05:00
} )
2026-09-24 17:23:59 +00:00
results <- sectionProposalResult {
2026-05-12 18:39:26 -05:00
sectionPos : sectionPos ,
section : section ,
2026-11-16 14:30:57 +00:00
proposal : proposalResult ,
2026-09-24 17:23:59 +00:00
started : true ,
2026-05-12 18:39:26 -05:00
err : err ,
}
} ( )
}
wg . Wait ( )
} ( )
return runCtx , results , cancel
}
func runModulePipeline ( ctx context . Context , input collectSectionProposalsInput ) ( modulePipelineResult , error ) {
out := modulePipelineResult {
Approved : make ( [ ] proposals . EnrichedCorrectionProposal , 0 ) ,
2026-09-24 17:23:59 +00:00
Operations : make ( [ ] outcomes . Operation , 0 ) ,
WorkUnits : make ( [ ] WorkUnitOutcome , len ( input . Sections ) ) ,
2026-05-12 18:39:26 -05:00
ValidatorDecisions : make ( [ ] ValidatorDecisionRecord , 0 ) ,
ValidatorRejected : make ( [ ] ValidatorRejectedChange , 0 ) ,
2026-05-12 12:59:46 -05:00
}
2026-09-24 17:23:59 +00:00
for i , section := range input . Sections {
out . WorkUnits [ i ] = WorkUnitOutcome {
2026-11-10 17:37:41 +00:00
ModuleKey : input . Spec . ModuleKey , ModuleInstance : input . Spec . InstanceName , StageIndex : input . Spec . Index , StageID : input . Spec . ID ,
2026-09-24 17:23:59 +00:00
Section : contracts . SectionMetadataFromSection ( section ) , State : outcomes . WorkNotStarted ,
}
}
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 {
return out , & structuredoutput . Failure { Class : outcomes . FailureFatal , Reason : outcomes . ReasonSetupFailure , Message : "correction repair coordinator or prompt provider is unavailable" }
}
repairPromptID = provider . RepairPromptID ( )
if repairPromptID == "" {
return out , & structuredoutput . Failure { Class : outcomes . FailureFatal , Reason : outcomes . ReasonSetupFailure , Message : "correction repair prompt is unavailable" }
}
input . Spec . CaptureProposalExchange = true
}
2026-05-12 18:39:26 -05:00
runCtx , proposalResults , cancel := collectSectionProposals ( ctx , input )
2026-05-12 12:59:46 -05:00
defer cancel ( )
2026-05-12 18:39:26 -05:00
pending := make ( map [ int ] sectionProposalResult , len ( input . Sections ) )
validationResults := make ( chan sectionValidationResult , len ( input . Sections ) )
validationBySection := make ( map [ int ] sectionValidationResult , len ( input . Sections ) )
validatorsOrdered , validatorOrder := reorderValidatorsForPipeline ( input . Module . Validators ( ) )
nextSectionToProcess := 0
nextProposalIndex := 0
2026-05-12 12:59:46 -05:00
var (
firstErr error
2026-09-24 15:32:13 +00:00
errMu sync . Mutex
2026-05-12 18:39:26 -05:00
vwg sync . WaitGroup
2026-05-12 12:59:46 -05:00
)
2026-05-12 18:39:26 -05:00
setErr := func ( err error ) {
if err == nil {
return
}
2026-09-24 15:32:13 +00:00
errMu . Lock ( )
defer errMu . Unlock ( )
if firstErr == nil {
2026-05-12 18:39:26 -05:00
firstErr = err
cancel ( )
2026-09-24 15:32:13 +00:00
}
}
getErr := func ( ) error {
errMu . Lock ( )
defer errMu . Unlock ( )
return firstErr
2026-05-12 18:39:26 -05:00
}
for result := range proposalResults {
2026-09-24 17:23:59 +00:00
unit := & out . WorkUnits [ result . sectionPos ]
if result . started {
unit . State = outcomes . WorkInterrupted
}
2026-11-16 14:30:57 +00:00
unit . Operations = append ( unit . Operations , result . proposal . Operations ... )
2026-05-12 18:39:26 -05:00
if result . err != nil {
setErr ( result . err )
continue
}
2026-09-24 15:32:13 +00:00
if getErr ( ) != nil {
2026-05-12 18:39:26 -05:00
continue
}
2026-09-24 17:23:59 +00:00
if ! result . started {
continue
}
2026-05-12 18:39:26 -05:00
pending [ result . sectionPos ] = result
for {
2026-09-24 15:32:13 +00:00
if getErr ( ) != nil {
break
}
2026-05-12 18:39:26 -05:00
sectionResult , ok := pending [ nextSectionToProcess ]
if ! ok {
break
2026-05-12 12:59:46 -05:00
}
2026-05-12 18:39:26 -05:00
delete ( pending , nextSectionToProcess )
sectionMeta := contracts . SectionMetadataFromSection ( sectionResult . section )
2026-11-16 14:30:57 +00:00
sectionEnriched := make ( [ ] proposals . EnrichedCorrectionProposal , 0 , len ( sectionResult . proposal . Proposals ) )
for i , corrected := range sectionResult . proposal . Proposals {
2026-05-12 18:39:26 -05:00
sectionIndex := sectionMeta . Index
sectionEnriched = append ( sectionEnriched , proposals . EnrichedCorrectionProposal {
CorrectionProposal : corrected ,
ProposalMetadata : proposals . ProposalMetadata {
ProposalIndex : nextProposalIndex + i ,
ModuleKey : input . Spec . ModuleKey ,
ModuleInstance : input . Spec . InstanceName ,
SectionIndex : & sectionIndex ,
} ,
2026-05-12 12:59:46 -05:00
} )
}
2026-05-12 18:39:26 -05:00
nextProposalIndex += len ( sectionEnriched )
out . ProposalCount += len ( sectionEnriched )
2026-09-24 17:41:10 +00:00
out . Candidates = append ( out . Candidates , sectionEnriched ... )
2026-09-24 17:23:59 +00:00
for operationIndex := range out . WorkUnits [ nextSectionToProcess ] . Operations {
operation := & out . WorkUnits [ nextSectionToProcess ] . Operations [ operationIndex ]
if operation . Reference . Kind == outcomes . OperationProposal && operation . Succeeded {
for _ , proposal := range sectionEnriched {
operation . ProposalIndices = append ( operation . ProposalIndices , proposal . ProposalIndex )
}
}
}
2026-05-12 18:39:26 -05:00
vwg . Add ( 1 )
go func ( sectionPos int , enriched [ ] proposals . EnrichedCorrectionProposal , sectionMetadata contracts . SectionMetadata ) {
defer vwg . Done ( )
2026-11-11 12:28:00 +00:00
validationInput := validateSectionCandidatesInput {
2026-11-16 03:43:29 +00:00
Spec : input . Spec ,
Policy : input . Policy ,
WorkingTranscript : transcriptFromSection ( sectionResult . section ) ,
SectionIndex : & sectionMetadata . Index ,
Validators : validatorsOrdered ,
SectionEnriched : enriched ,
2026-11-16 14:30:57 +00:00
Exchange : sectionResult . proposal . Exchange ,
2026-11-16 03:43:29 +00:00
ValidationLLMClient : input . ValidationClient ,
GlossaryView : input . GlossaryView ,
Interactions : input . Interactions ,
2026-11-11 12:28:00 +00:00
}
if input . Spec . Settings . LLM . CorrectionRepairAttempts > 0 && len ( enriched ) > 0 {
validationInput . Section = validators . NewSectionLookup ( validationInput . WorkingTranscript )
}
validated , err := validateSectionCandidates ( runCtx , validationInput )
2026-11-11 04:29:18 +00:00
var repairs [ ] RepairOutcome
2026-11-16 14:36:07 +00:00
if err == nil && repairPromptID != "" {
validated , repairs , err = repairCandidates ( runCtx , validationInput , sectionResult . proposal . SegmentReferences , validated , repairPromptID )
2026-11-11 04:29:18 +00:00
}
2026-09-24 15:32:13 +00:00
setErr ( err )
2026-05-12 18:39:26 -05:00
validationResults <- sectionValidationResult {
2026-11-11 04:29:18 +00:00
repairs : repairs ,
2026-05-12 18:39:26 -05:00
sectionPos : sectionPos ,
enriched : enriched ,
approved : validated . approved ,
decisions : validated . decisions ,
rejected : validated . rejected ,
2026-09-24 17:23:59 +00:00
operations : validated . operations ,
filtered : validated . filtered ,
2026-05-12 18:39:26 -05:00
err : err ,
}
} ( nextSectionToProcess , sectionEnriched , sectionMeta )
nextSectionToProcess ++
}
}
2026-05-12 12:59:46 -05:00
2026-05-12 18:39:26 -05:00
vwg . Wait ( )
close ( validationResults )
for v := range validationResults {
validationBySection [ v . sectionPos ] = v
if v . err != nil {
setErr ( v . err )
}
2026-05-12 12:59:46 -05:00
}
2026-09-24 17:23:59 +00:00
for i := range input . Sections {
2026-05-12 18:39:26 -05:00
res , ok := validationBySection [ i ]
if ! ok {
2026-09-24 17:23:59 +00:00
continue
}
unit := & out . WorkUnits [ i ]
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 )
if res . err == nil {
unit . State = outcomes . WorkCompleted
} else {
unit . State = outcomes . WorkInterrupted
2026-05-12 18:39:26 -05:00
}
out . Approved = append ( out . Approved , res . approved ... )
out . ValidatorDecisions = append ( out . ValidatorDecisions , res . decisions ... )
out . ValidatorRejected = append ( out . ValidatorRejected , res . rejected ... )
}
2026-09-24 17:23:59 +00:00
for _ , unit := range out . WorkUnits {
out . Operations = append ( out . Operations , unit . Operations ... )
}
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-09-24 17:23:59 +00:00
if ctx . Err ( ) != nil {
setErr ( ctx . Err ( ) )
}
2026-05-12 18:39:26 -05:00
2026-09-24 15:32:13 +00:00
if err := getErr ( ) ; err != 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-09-24 15:32:13 +00:00
return out , err
2026-05-12 12:59:46 -05:00
}
2026-05-12 18:39:26 -05:00
return out , nil
}
type validateSectionCandidatesInput struct {
2026-11-11 12:28:00 +00:00
Section * validators . SectionLookup
2026-11-11 04:37:52 +00:00
RepairProposalIndex * int
RepairAttempt * int
Exchange * structuredoutput . Exchange
2026-11-16 03:43:29 +00:00
Spec contracts . ModuleRunSpec
Policy proposals . ReplacementPolicy
WorkingTranscript * schema . Transcript
SectionIndex * int
2026-11-16 14:11:02 +00:00
Validators [ ] validators . Validator
2026-11-16 03:43:29 +00:00
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-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 ,
} , 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 ,
} , 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-05-12 18:39:26 -05:00
decisions = append ( decisions , ValidatorDecisionRecord {
ValidatorName : validator . Name ( ) ,
2026-11-16 14:11:02 +00:00
ExecutionClass : class ,
2026-05-12 18:39:26 -05:00
ProposalIndex : d . ProposalIndex ,
2026-11-16 14:11:02 +00:00
Approved : approved ,
Disposition : disposition ,
2026-05-12 18:39:26 -05:00
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-05-12 18:39:26 -05:00
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 ,
2026-11-16 14:11:02 +00:00
Disposition : disposition ,
2026-05-12 18:39:26 -05:00
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 ,
} , fmt . Errorf ( "validator %q failed: %w" , validator . Name ( ) , err )
}
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 ) {
if unit == nil || len ( filtered ) == 0 {
return
}
for i := range unit . Operations {
operation := & unit . Operations [ i ]
if operation . Reference . Kind != outcomes . OperationProposal || ! operation . Succeeded {
continue
}
seen := make ( map [ int ] struct { } , len ( operation . Filtered ) )
for _ , existing := range operation . Filtered {
seen [ existing . ProposalIndex ] = struct { } { }
}
for _ , item := range filtered {
if _ , ok := seen [ item . ProposalIndex ] ; ok {
continue
}
seen [ item . ProposalIndex ] = struct { } { }
operation . Filtered = append ( operation . Filtered , item )
}
if len ( operation . Filtered ) == 0 {
continue
}
sort . Slice ( operation . Filtered , func ( a , b int ) bool {
return operation . Filtered [ a ] . ProposalIndex < operation . Filtered [ b ] . ProposalIndex
} )
found := false
for _ , reason := range operation . RecoveryReasons {
if reason == outcomes . RecoveryProposalsFiltered {
found = true
break
}
}
if ! found {
operation . RecoveryReasons = append ( operation . RecoveryReasons , outcomes . RecoveryProposalsFiltered )
}
return
}
}
func attributeApplyFiltering ( sections [ ] chunking . Section , approved [ ] proposals . EnrichedCorrectionProposal , skipped [ ] proposals . SkippedChange , policy proposals . ReplacementPolicy , units [ ] WorkUnitOutcome ) {
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
}
for _ , skip := range skipped {
proposal , ok := byIndex [ skip . ProposalIndex ]
if ! ok || proposal . SectionIndex == nil {
continue
}
pos , ok := sectionPosition [ * proposal . SectionIndex ]
if ! ok {
continue
}
var segment * schema . Segment
for i := range sections [ pos ] . Segments {
if sections [ pos ] . Segments [ i ] . ID == proposal . TargetSegmentID {
segment = & sections [ pos ] . Segments [ i ]
break
}
}
// A proposal applicable to the input was made stale by a previously
// accepted correction. That conflict is not a response defect.
if proposals . PreviewProposalForSegment ( segment , proposal . CorrectionProposal , policy ) . Applicable {
continue
}
if reason , ok := validators . DefectiveProposalReason ( string ( skip . SkipReason ) ) ; ok {
mergeFilteredProposals ( & units [ pos ] , [ ] outcomes . FilteredProposal { { ProposalIndex : skip . ProposalIndex , Reason : reason , Count : 1 } } )
}
}
}
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 }
}