Commit module changes only after successful execution
This commit is contained in:
@@ -40,9 +40,14 @@ explicit `not_started`/`interrupted`/`completed` work units, compact model
|
|||||||
operation outcomes, and per-module results. Candidate payloads are retained
|
operation outcomes, and per-module results. Candidate payloads are retained
|
||||||
in the returned output only when debug capture is requested. A work unit can
|
in the returned output only when debug capture is requested. A work unit can
|
||||||
complete with model errors and applied corrections. Recoverable model failures
|
complete with model errors and applied corrections. Recoverable model failures
|
||||||
never trigger shared cancellation. Fatal setup, internal, application, or
|
never trigger shared cancellation. Fatal failures and parent cancellation stop
|
||||||
parent-cancellation errors cancel peers, join workers, and preserve earlier
|
further dispatch and join active workers. The initiating error carries its
|
||||||
committed module text for partial reports; no completed transcript is published.
|
processing phase and available origin. Work-unit evidence preserves each peer's
|
||||||
|
interruption or error. Usable proposal results from later sections are indexed
|
||||||
|
in order after joining, even when the runner could not validate them. The
|
||||||
|
working transcript advances only at the module's successful commit boundary.
|
||||||
|
Earlier committed text remains available to an interrupted run, and transcript
|
||||||
|
delivery requires successful processing.
|
||||||
|
|
||||||
Required transcript/report write failures are fatal. Optional debug writes
|
Required transcript/report write failures are fatal. Optional debug writes
|
||||||
report safely to stderr without changing decisions or exit status. See
|
report safely to stderr without changing decisions or exit status. See
|
||||||
|
|||||||
@@ -543,6 +543,15 @@ earlier chunk, and a fatal stage after earlier committed text. Assert causal
|
|||||||
origin and preserved evidence as well as returned failure. Leave report/ledger
|
origin and preserved evidence as well as returned failure. Leave report/ledger
|
||||||
projection to Stage 17.
|
projection to Stage 17.
|
||||||
|
|
||||||
|
**Complete.** A single module completion path advances the transcript only
|
||||||
|
after a successful application and context check. The runner returns the
|
||||||
|
initiating error with its phase and available origin, and keeps peer failures
|
||||||
|
on their work units. After joining, it assigns deterministic indices to usable
|
||||||
|
proposal responses that never reached validation. Tests exercised canceled
|
||||||
|
entry, cancellation after approval, a later response behind an earlier
|
||||||
|
failure, and preservation of earlier committed text. The CLI build, full
|
||||||
|
offline suite, and targeted race checks passed.
|
||||||
|
|
||||||
## Stage 16 — Bound local preparation and actively drain results
|
## Stage 16 — Bound local preparation and actively drain results
|
||||||
|
|
||||||
Audit #15 (dispatch) and #30, under D3.
|
Audit #15 (dispatch) and #30, under D3.
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import (
|
|||||||
"gitea.maximumdirect.net/eric/audita/v3/internal/core/schema"
|
"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/contracts"
|
||||||
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/llm"
|
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/llm"
|
||||||
|
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/outcomes"
|
||||||
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/proposals"
|
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/proposals"
|
||||||
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/responseschema"
|
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/responseschema"
|
||||||
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/structuredoutput"
|
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/structuredoutput"
|
||||||
@@ -128,6 +129,17 @@ func TestValidatorFailureCancelsActiveAndPendingProviderWork(t *testing.T) {
|
|||||||
if len(outcome.result.WorkUnits) != 3 {
|
if len(outcome.result.WorkUnits) != 3 {
|
||||||
t.Fatalf("lost work inventory: %+v", outcome.result.WorkUnits)
|
t.Fatalf("lost work inventory: %+v", outcome.result.WorkUnits)
|
||||||
}
|
}
|
||||||
|
var failure *ExecutionFailure
|
||||||
|
if !errors.As(outcome.err, &failure) || failure.Phase != "validation" || !failure.HasSection ||
|
||||||
|
failure.Origin.SectionIndex != 0 || failure.Origin.ValidatorName != validator.Name() ||
|
||||||
|
outcome.result.WorkUnits[0].Failure != failure {
|
||||||
|
t.Fatalf("lost primary validation owner: failure=%+v units=%+v", failure, outcome.result.WorkUnits)
|
||||||
|
}
|
||||||
|
for _, peer := range outcome.result.WorkUnits[1:] {
|
||||||
|
if peer.State != outcomes.WorkInterrupted || peer.Failure == nil || !errors.Is(peer.Failure, context.Canceled) || peer.Failure == failure {
|
||||||
|
t.Fatalf("lost peer interruption: %+v", peer)
|
||||||
|
}
|
||||||
|
}
|
||||||
if len(outcome.result.Approved) != 0 {
|
if len(outcome.result.Approved) != 0 {
|
||||||
t.Fatalf("fatal validation returned unchecked candidates: %+v", outcome.result.Approved)
|
t.Fatalf("fatal validation returned unchecked candidates: %+v", outcome.result.Approved)
|
||||||
}
|
}
|
||||||
@@ -150,3 +162,98 @@ func TestValidatorFailureCancelsActiveAndPendingProviderWork(t *testing.T) {
|
|||||||
t.Fatalf("attempt count: %+v", got)
|
t.Fatalf("attempt count: %+v", got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCanceledEmptyPipelineReturnsCancellation(t *testing.T) {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
cancel()
|
||||||
|
out, err := New(fakeFactory{}).Run(ctx, RunInput{
|
||||||
|
Transcript: &schema.Transcript{Segments: []schema.Segment{{ID: 71, Text: "original"}}},
|
||||||
|
})
|
||||||
|
if !errors.Is(err, context.Canceled) || out.FinalTranscript == nil || out.FinalTranscript.Segments[0].Text != "original" {
|
||||||
|
t.Fatalf("canceled entry: result=%+v error=%v", out, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRunnerRetainsLaterProposalEvidenceWhenEarlierSectionFails(t *testing.T) {
|
||||||
|
origin := errors.New("earlier proposal failed")
|
||||||
|
laterCompleted := make(chan struct{})
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
var validations atomic.Int32
|
||||||
|
module := outcomeModule{
|
||||||
|
fakeModule: fakeModule{key: "grammar", policy: proposals.ReplacementPolicyRequireUnique, validators: []validators.Validator{
|
||||||
|
contextValidator{validate: func(context.Context, validators.Request) (validators.Result, error) {
|
||||||
|
validations.Add(1)
|
||||||
|
return validators.Result{}, errors.New("validation should not be submitted")
|
||||||
|
}},
|
||||||
|
}},
|
||||||
|
proposeResult: func(req contracts.ProposalRequest) (contracts.ProposalResult, error) {
|
||||||
|
if req.Section.Index == 0 {
|
||||||
|
select {
|
||||||
|
case <-laterCompleted:
|
||||||
|
return contracts.ProposalResult{}, origin
|
||||||
|
case <-ctx.Done():
|
||||||
|
return contracts.ProposalResult{}, ctx.Err()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
close(laterCompleted)
|
||||||
|
return contracts.ProposalResult{
|
||||||
|
Proposals: []proposals.CorrectionProposal{{TargetSegmentID: 93, OriginalText: "other", CorrectedText: "Other"}},
|
||||||
|
SegmentReferences: []int{1},
|
||||||
|
Operations: []outcomes.Operation{{Reference: outcomes.Reference{Kind: outcomes.OperationProposal}, Succeeded: true}},
|
||||||
|
}, nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
cfg := runnerTestConfig()
|
||||||
|
cfg.Chunking.MaxSectionTokens = 3
|
||||||
|
cfg.Chunking.MinSectionTokens = 0
|
||||||
|
out, err := New(fakeFactory{modules: map[string]contracts.TranscriptModule{"grammar": module}}).Run(ctx, RunInput{
|
||||||
|
Config: &cfg, CaptureCandidates: true,
|
||||||
|
Transcript: &schema.Transcript{Segments: []schema.Segment{{ID: 71, Text: "original text"}, {ID: 93, Text: "other text"}}},
|
||||||
|
ModuleSpecs: []contracts.ModuleRunSpec{{ModuleKey: "grammar", InstanceName: "grammar"}},
|
||||||
|
})
|
||||||
|
if !errors.Is(err, origin) || validations.Load() != 0 || len(out.Candidates) != 1 || len(out.WorkUnits) != 2 {
|
||||||
|
t.Fatalf("interrupted evidence: candidates=%+v work=%+v validations=%d error=%v", out.Candidates, out.WorkUnits, validations.Load(), err)
|
||||||
|
}
|
||||||
|
var failure *ExecutionFailure
|
||||||
|
if !errors.As(err, &failure) || failure.Phase != "proposal" || !failure.HasSection ||
|
||||||
|
failure.Origin.SectionIndex != 0 || failure.Origin.Kind != outcomes.OperationProposal || out.WorkUnits[0].Failure != failure {
|
||||||
|
t.Fatalf("lost originating proposal failure: %+v", failure)
|
||||||
|
}
|
||||||
|
candidate := out.Candidates[0]
|
||||||
|
later := out.WorkUnits[1]
|
||||||
|
if candidate.ProposalIndex != 0 || candidate.TargetSegmentID != 93 || later.State != outcomes.WorkInterrupted ||
|
||||||
|
len(later.Operations) != 1 || len(later.Operations[0].ProposalIndices) != 1 || later.Operations[0].ProposalIndices[0] != candidate.ProposalIndex ||
|
||||||
|
out.FinalTranscript.Segments[1].Text != "other text" {
|
||||||
|
t.Fatalf("later candidate or association lost: candidate=%+v work=%+v", candidate, later)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRunnerCancellationAfterApprovalKeepsCommittedModuleText(t *testing.T) {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
propose := func(original, replacement string) func(contracts.ProposalRequest) ([]proposals.CorrectionProposal, error) {
|
||||||
|
return func(contracts.ProposalRequest) ([]proposals.CorrectionProposal, error) {
|
||||||
|
return []proposals.CorrectionProposal{{TargetSegmentID: 1, OriginalText: original, CorrectedText: replacement}}, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
last := contextValidator{validate: func(_ context.Context, req validators.Request) (validators.Result, error) {
|
||||||
|
cancel()
|
||||||
|
return validators.Result{Decisions: []validators.Decision{{ProposalIndex: req.CandidateProposal[0].ProposalIndex, Approved: true, Disposition: validators.DispositionApproved}}}, nil
|
||||||
|
}}
|
||||||
|
r := New(fakeFactory{modules: map[string]contracts.TranscriptModule{
|
||||||
|
"first": fakeModule{key: "first", policy: proposals.ReplacementPolicyRequireUnique, proposeF: propose("teh", "the")},
|
||||||
|
"second": fakeModule{key: "second", policy: proposals.ReplacementPolicyRequireUnique, proposeF: propose("cat", "dog"), validators: []validators.Validator{last}},
|
||||||
|
}})
|
||||||
|
out, err := r.Run(ctx, RunInput{
|
||||||
|
Transcript: &schema.Transcript{Segments: []schema.Segment{{ID: 1, Text: "teh cat"}}},
|
||||||
|
ModuleSpecs: []contracts.ModuleRunSpec{{ModuleKey: "first", InstanceName: "first"}, {Index: 1, ModuleKey: "second", InstanceName: "second"}},
|
||||||
|
})
|
||||||
|
var failure *ExecutionFailure
|
||||||
|
if !errors.Is(err, context.Canceled) || !errors.As(err, &failure) || failure.Origin == nil || *failure.Origin.StageIndex != 1 ||
|
||||||
|
len(out.ModuleResults) != 2 || out.ModuleResults[0].Status != ModuleStatusSuccess || out.ModuleResults[1].Status != ModuleStatusFailed ||
|
||||||
|
len(out.ModuleResults[1].AppliedChanges) != 0 || len(out.ModuleResults[1].ValidatorDecisions) != 1 ||
|
||||||
|
!out.ModuleResults[1].ValidatorDecisions[0].Approved || out.FinalTranscript.Segments[0].Text != "the cat" {
|
||||||
|
t.Fatalf("commit boundary lost earlier text or approval evidence: result=%+v error=%+v", out, failure)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
51
internal/framework/runner/failure.go
Normal file
51
internal/framework/runner/failure.go
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
package runner
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/contracts"
|
||||||
|
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/outcomes"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ExecutionFailure associates the initiating cause with its processing owner.
|
||||||
|
// The section is optional, for example when module setup fails.
|
||||||
|
type ExecutionFailure struct {
|
||||||
|
Phase string
|
||||||
|
Origin *outcomes.Reference
|
||||||
|
HasSection bool
|
||||||
|
Cause error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *ExecutionFailure) Error() string { return f.Cause.Error() }
|
||||||
|
func (f *ExecutionFailure) Unwrap() error { return f.Cause }
|
||||||
|
|
||||||
|
func locateFailure(cause error, phase string, origin outcomes.Reference, hasSection bool, operations []outcomes.Operation) *ExecutionFailure {
|
||||||
|
for _, operation := range operations {
|
||||||
|
if operation.Cause == nil || !errors.Is(cause, operation.Cause) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ref := operation.Reference
|
||||||
|
if ref.StageIndex == nil {
|
||||||
|
ref.StageIndex, ref.StageID = origin.StageIndex, origin.StageID
|
||||||
|
}
|
||||||
|
if ref.ProposalIndex == nil {
|
||||||
|
ref.ProposalIndex = origin.ProposalIndex
|
||||||
|
}
|
||||||
|
if ref.RepairAttempt == nil {
|
||||||
|
ref.RepairAttempt = origin.RepairAttempt
|
||||||
|
}
|
||||||
|
return &ExecutionFailure{Phase: phase, Origin: &ref, HasSection: hasSection, Cause: cause}
|
||||||
|
}
|
||||||
|
return &ExecutionFailure{Phase: phase, Origin: &origin, HasSection: hasSection, Cause: cause}
|
||||||
|
}
|
||||||
|
|
||||||
|
func failureReference(spec contracts.ModuleRunSpec, section *int, kind outcomes.OperationKind) outcomes.Reference {
|
||||||
|
ref := outcomes.Reference{
|
||||||
|
StageIndex: &spec.Index, StageID: spec.ID,
|
||||||
|
ModuleKey: spec.ModuleKey, ModuleInstance: spec.InstanceName, Kind: kind,
|
||||||
|
}
|
||||||
|
if section != nil {
|
||||||
|
ref.SectionIndex = *section
|
||||||
|
}
|
||||||
|
return ref
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package runner
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
@@ -278,6 +279,12 @@ func runRepairBranch(ctx context.Context, branchInput repairBranchInput) (repair
|
|||||||
if coordinationErr != nil {
|
if coordinationErr != nil {
|
||||||
branch.Outcome = "interrupted"
|
branch.Outcome = "interrupted"
|
||||||
result.approved = nil
|
result.approved = nil
|
||||||
|
var failure *ExecutionFailure
|
||||||
|
if !errors.As(coordinationErr, &failure) {
|
||||||
|
ref := failureReference(input.Spec, input.SectionIndex, outcomes.OperationCorrectionRepair)
|
||||||
|
ref.ProposalIndex = &original.ProposalIndex
|
||||||
|
coordinationErr = locateFailure(coordinationErr, "correction_repair", ref, input.SectionIndex != nil, result.operations)
|
||||||
|
}
|
||||||
} else if exhausted {
|
} else if exhausted {
|
||||||
branch.Outcome = "exhausted"
|
branch.Outcome = "exhausted"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ type WorkUnitOutcome struct {
|
|||||||
ModuleInstance string `json:"module_instance"`
|
ModuleInstance string `json:"module_instance"`
|
||||||
Section contracts.SectionMetadata `json:"section"`
|
Section contracts.SectionMetadata `json:"section"`
|
||||||
State outcomes.WorkState `json:"state"`
|
State outcomes.WorkState `json:"state"`
|
||||||
|
Failure *ExecutionFailure `json:"-"`
|
||||||
CorrectionsApplied int `json:"corrections_applied"`
|
CorrectionsApplied int `json:"corrections_applied"`
|
||||||
Repairs []RepairOutcome `json:"-"`
|
Repairs []RepairOutcome `json:"-"`
|
||||||
Operations []outcomes.Operation `json:"operations,omitempty"`
|
Operations []outcomes.Operation `json:"operations,omitempty"`
|
||||||
@@ -116,15 +117,18 @@ func New(factory ModuleFactory) *Runner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Runner) Run(ctx context.Context, input RunInput) (RunOutput, error) {
|
func (r *Runner) Run(ctx context.Context, input RunInput) (RunOutput, error) {
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return RunOutput{FinalTranscript: input.Transcript.Clone()}, &ExecutionFailure{Phase: "processing", Cause: err}
|
||||||
|
}
|
||||||
if r == nil || r.factory == nil {
|
if r == nil || r.factory == nil {
|
||||||
return RunOutput{}, fmt.Errorf("runner module factory is required")
|
return RunOutput{}, &ExecutionFailure{Phase: "setup", Cause: errors.New("runner module factory is required")}
|
||||||
}
|
}
|
||||||
|
|
||||||
working := input.Transcript.Clone()
|
working := input.Transcript.Clone()
|
||||||
results := make([]ModuleResult, 0, len(input.ModuleSpecs))
|
results := make([]ModuleResult, 0, len(input.ModuleSpecs))
|
||||||
sections, err := chunkWorkingTranscript(input.Config, working)
|
sections, err := chunkWorkingTranscript(input.Config, working)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return RunOutput{FinalTranscript: working}, fmt.Errorf("chunk normalized transcript: %w", err)
|
return RunOutput{FinalTranscript: working}, &ExecutionFailure{Phase: "chunking", Cause: fmt.Errorf("chunk normalized transcript: %w", err)}
|
||||||
}
|
}
|
||||||
inventory := make([]contracts.SectionMetadata, len(sections))
|
inventory := make([]contracts.SectionMetadata, len(sections))
|
||||||
workUnits := make([]WorkUnitOutcome, 0, len(input.ModuleSpecs)*len(sections))
|
workUnits := make([]WorkUnitOutcome, 0, len(input.ModuleSpecs)*len(sections))
|
||||||
@@ -149,135 +153,124 @@ func (r *Runner) Run(ctx context.Context, input RunInput) (RunOutput, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for moduleIndex, spec := range input.ModuleSpecs {
|
for moduleIndex, spec := range input.ModuleSpecs {
|
||||||
module, err := r.factory.ModuleForSpec(spec)
|
execution, err := r.executeModule(ctx, spec, sections, input.Interactions)
|
||||||
if err != nil {
|
units := workUnits[moduleIndex*len(sections) : (moduleIndex+1)*len(sections)]
|
||||||
failed := ModuleResult{
|
copy(units, execution.pipeline.WorkUnits)
|
||||||
StageIndex: spec.Index, StageID: spec.ID,
|
if err == nil && ctx.Err() != nil {
|
||||||
ModuleKey: spec.ModuleKey,
|
err = locateFailure(ctx.Err(), "application", failureReference(spec, nil, ""), false, nil)
|
||||||
ModuleInstance: spec.InstanceName,
|
}
|
||||||
Status: ModuleStatusFailed,
|
if err == nil {
|
||||||
ErrorMessage: err.Error(),
|
previous := sections
|
||||||
}
|
sections = execution.updated
|
||||||
results = append(results, failed)
|
attributeApplyFiltering(previous, execution.pipeline.Approved, execution.skipped, execution.policy, units)
|
||||||
return snapshot(), fmt.Errorf("module %q setup failed: %w", spec.InstanceName, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
policy := module.ReplacementPolicy()
|
var failure *ExecutionFailure
|
||||||
if err := spec.PrepareGlossary(); err != nil {
|
applicationInterrupted := errors.As(err, &failure) && failure.Phase == "application"
|
||||||
return snapshot(), fmt.Errorf("module %q glossary preparation: %w", spec.InstanceName, err)
|
appliedIndices := make(map[int]struct{}, len(execution.applied))
|
||||||
|
for _, change := range execution.applied {
|
||||||
|
appliedIndices[change.ProposalIndex] = struct{}{}
|
||||||
}
|
}
|
||||||
pipelineResult, pipelineErr := runModulePipeline(ctx, collectSectionProposalsInput{
|
var operations []outcomes.Operation
|
||||||
Module: module,
|
for i := range units {
|
||||||
Spec: spec,
|
unit := &units[i]
|
||||||
Policy: policy,
|
|
||||||
Sections: sections,
|
|
||||||
ProposalClient: spec.ProposalClient,
|
|
||||||
Interactions: input.Interactions,
|
|
||||||
ValidationClient: spec.ValidationClient,
|
|
||||||
GlossaryVariables: spec.GlossaryVariables,
|
|
||||||
GlossaryView: spec.GlossaryView,
|
|
||||||
})
|
|
||||||
if input.CaptureCandidates {
|
|
||||||
candidates = append(candidates, pipelineResult.Candidates...)
|
|
||||||
}
|
|
||||||
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"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if pipelineErr != nil {
|
|
||||||
failed := 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: pipelineErr.Error(),
|
|
||||||
}
|
|
||||||
results = append(results, failed)
|
|
||||||
copy(workUnits[moduleIndex*len(sections):(moduleIndex+1)*len(sections)], pipelineResult.WorkUnits)
|
|
||||||
for sectionPos := range sections {
|
|
||||||
workUnits[moduleIndex*len(sections)+sectionPos].ModuleIndex = moduleIndex
|
|
||||||
}
|
|
||||||
return snapshot(), fmt.Errorf("module %q failed: %w", spec.InstanceName, pipelineErr)
|
|
||||||
}
|
|
||||||
|
|
||||||
updated, applied, skipped, appliedBySection, applyErr := applyWithinSections(sections, pipelineResult.Approved, policy, spec.GlossaryView.Protection, spec.ModuleKey == modulecatalog.KeySpokenWord)
|
|
||||||
if applyErr != nil {
|
|
||||||
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()})
|
|
||||||
return snapshot(), fmt.Errorf("module %q application failed: %w", spec.InstanceName, applyErr)
|
|
||||||
}
|
|
||||||
copy(workUnits[moduleIndex*len(sections):(moduleIndex+1)*len(sections)], pipelineResult.WorkUnits)
|
|
||||||
for sectionPos := range sections {
|
|
||||||
unit := &workUnits[moduleIndex*len(sections)+sectionPos]
|
|
||||||
unit.ModuleIndex = moduleIndex
|
unit.ModuleIndex = moduleIndex
|
||||||
unit.State = outcomes.WorkCompleted
|
if err == nil {
|
||||||
unit.CorrectionsApplied = appliedBySection[sectionPos]
|
unit.State = outcomes.WorkCompleted
|
||||||
}
|
unit.CorrectionsApplied = execution.appliedBySection[i]
|
||||||
var appliedIndices map[int]struct{}
|
} else if applicationInterrupted && unit.State != outcomes.WorkNotStarted {
|
||||||
for i := range workUnits[moduleIndex*len(sections) : (moduleIndex+1)*len(sections)] {
|
unit.State = outcomes.WorkInterrupted
|
||||||
unit := &workUnits[moduleIndex*len(sections)+i]
|
}
|
||||||
for j := range unit.Repairs {
|
for j := range unit.Repairs {
|
||||||
branch := &unit.Repairs[j]
|
branch := &unit.Repairs[j]
|
||||||
if branch.Outcome != "approved" {
|
if branch.Outcome != "approved" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if appliedIndices == nil {
|
if err != nil {
|
||||||
appliedIndices = make(map[int]struct{}, len(applied))
|
branch.Outcome = "interrupted"
|
||||||
for _, change := range applied {
|
} else if _, ok := appliedIndices[branch.ProposalIndex]; ok {
|
||||||
appliedIndices[change.ProposalIndex] = struct{}{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
branch.Outcome = "approved_skipped"
|
|
||||||
if _, ok := appliedIndices[branch.ProposalIndex]; ok {
|
|
||||||
branch.Outcome = "applied"
|
branch.Outcome = "applied"
|
||||||
|
} else {
|
||||||
|
branch.Outcome = "approved_skipped"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
operations = append(operations, unit.Operations...)
|
||||||
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...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
results = append(results, ModuleResult{
|
if input.CaptureCandidates {
|
||||||
StageIndex: spec.Index, StageID: spec.ID,
|
candidates = append(candidates, execution.pipeline.Candidates...)
|
||||||
ModuleKey: spec.ModuleKey,
|
}
|
||||||
ModuleInstance: spec.InstanceName,
|
result := ModuleResult{
|
||||||
ReplacementPolicy: policy,
|
StageIndex: spec.Index, StageID: spec.ID, ModuleKey: spec.ModuleKey,
|
||||||
Status: ModuleStatusSuccess,
|
ModuleInstance: spec.InstanceName, ReplacementPolicy: execution.policy,
|
||||||
ProposalCount: pipelineResult.ProposalCount,
|
ProposalCount: execution.pipeline.ProposalCount, Operations: operations,
|
||||||
Operations: moduleOperations,
|
ValidatorDecisions: execution.pipeline.ValidatorDecisions, ValidatorRejected: execution.pipeline.ValidatorRejected,
|
||||||
ValidatorDecisions: pipelineResult.ValidatorDecisions,
|
}
|
||||||
ValidatorRejected: pipelineResult.ValidatorRejected,
|
if err != nil {
|
||||||
AppliedChanges: applied,
|
result.Status = ModuleStatusFailed
|
||||||
SkippedChanges: skipped,
|
result.ErrorMessage = err.Error()
|
||||||
})
|
} else {
|
||||||
|
result.Status = ModuleStatusSuccess
|
||||||
|
result.AppliedChanges = execution.applied
|
||||||
|
result.SkippedChanges = execution.skipped
|
||||||
|
}
|
||||||
|
results = append(results, result)
|
||||||
|
if err != nil {
|
||||||
|
return snapshot(), fmt.Errorf("module %q failed: %w", spec.InstanceName, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
output := snapshot()
|
if err := ctx.Err(); err != nil {
|
||||||
return output, nil
|
return snapshot(), &ExecutionFailure{Phase: "processing", Cause: err}
|
||||||
|
}
|
||||||
|
return snapshot(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type moduleExecution struct {
|
||||||
|
policy proposals.ReplacementPolicy
|
||||||
|
pipeline modulePipelineResult
|
||||||
|
updated []chunking.Section
|
||||||
|
applied []proposals.AppliedChange
|
||||||
|
skipped []proposals.SkippedChange
|
||||||
|
appliedBySection []int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Runner) executeModule(ctx context.Context, spec contracts.ModuleRunSpec, sections []chunking.Section, interactions structuredoutput.InteractionSink) (moduleExecution, error) {
|
||||||
|
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,
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
func transcriptFromSections(sections []chunking.Section) *schema.Transcript {
|
func transcriptFromSections(sections []chunking.Section) *schema.Transcript {
|
||||||
@@ -345,7 +338,7 @@ type sectionProposalResult struct {
|
|||||||
section chunking.Section
|
section chunking.Section
|
||||||
proposal contracts.ProposalResult
|
proposal contracts.ProposalResult
|
||||||
started bool
|
started bool
|
||||||
err error
|
failure *ExecutionFailure
|
||||||
}
|
}
|
||||||
|
|
||||||
type sectionValidationResult struct {
|
type sectionValidationResult struct {
|
||||||
@@ -357,7 +350,7 @@ type sectionValidationResult struct {
|
|||||||
rejected []ValidatorRejectedChange
|
rejected []ValidatorRejectedChange
|
||||||
operations []outcomes.Operation
|
operations []outcomes.Operation
|
||||||
filtered []outcomes.FilteredProposal
|
filtered []outcomes.FilteredProposal
|
||||||
err error
|
failure *ExecutionFailure
|
||||||
}
|
}
|
||||||
|
|
||||||
type modulePipelineResult struct {
|
type modulePipelineResult struct {
|
||||||
@@ -370,9 +363,8 @@ type modulePipelineResult struct {
|
|||||||
ValidatorRejected []ValidatorRejectedChange
|
ValidatorRejected []ValidatorRejectedChange
|
||||||
}
|
}
|
||||||
|
|
||||||
func collectSectionProposals(ctx context.Context, input collectSectionProposalsInput) (context.Context, <-chan sectionProposalResult, context.CancelFunc) {
|
func collectSectionProposals(ctx context.Context, input collectSectionProposalsInput, fail func(*ExecutionFailure)) <-chan sectionProposalResult {
|
||||||
results := make(chan sectionProposalResult, len(input.Sections))
|
results := make(chan sectionProposalResult, len(input.Sections))
|
||||||
runCtx, cancel := context.WithCancel(ctx)
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer close(results)
|
defer close(results)
|
||||||
@@ -383,12 +375,12 @@ func collectSectionProposals(ctx context.Context, input collectSectionProposalsI
|
|||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
if runCtx.Err() != nil {
|
if ctx.Err() != nil {
|
||||||
results <- sectionProposalResult{sectionPos: sectionPos, section: section}
|
results <- sectionProposalResult{sectionPos: sectionPos, section: section}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
meta := contracts.SectionMetadataFromSection(section)
|
meta := contracts.SectionMetadataFromSection(section)
|
||||||
proposalResult, err := input.Module.Propose(runCtx, contracts.ProposalRequest{
|
proposalResult, err := input.Module.Propose(ctx, contracts.ProposalRequest{
|
||||||
ExecutionContext: contracts.ExecutionContext{
|
ExecutionContext: contracts.ExecutionContext{
|
||||||
StageIndex: &input.Spec.Index, StageID: input.Spec.ID,
|
StageIndex: &input.Spec.Index, StageID: input.Spec.ID,
|
||||||
Settings: input.Spec.Settings,
|
Settings: input.Spec.Settings,
|
||||||
@@ -407,19 +399,24 @@ func collectSectionProposals(ctx context.Context, input collectSectionProposalsI
|
|||||||
},
|
},
|
||||||
LLMClient: input.ProposalClient,
|
LLMClient: input.ProposalClient,
|
||||||
})
|
})
|
||||||
|
var failure *ExecutionFailure
|
||||||
|
if err != nil {
|
||||||
|
failure = locateFailure(err, "proposal", failureReference(input.Spec, &meta.Index, outcomes.OperationProposal), true, proposalResult.Operations)
|
||||||
|
fail(failure)
|
||||||
|
}
|
||||||
results <- sectionProposalResult{
|
results <- sectionProposalResult{
|
||||||
sectionPos: sectionPos,
|
sectionPos: sectionPos,
|
||||||
section: section,
|
section: section,
|
||||||
proposal: proposalResult,
|
proposal: proposalResult,
|
||||||
started: true,
|
started: true,
|
||||||
err: err,
|
failure: failure,
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return runCtx, results, cancel
|
return results
|
||||||
}
|
}
|
||||||
|
|
||||||
func runModulePipeline(ctx context.Context, input collectSectionProposalsInput) (modulePipelineResult, error) {
|
func runModulePipeline(ctx context.Context, input collectSectionProposalsInput) (modulePipelineResult, error) {
|
||||||
@@ -445,18 +442,17 @@ func runModulePipeline(ctx context.Context, input collectSectionProposalsInput)
|
|||||||
if input.Spec.Settings.LLM.CorrectionRepairAttempts > 0 {
|
if input.Spec.Settings.LLM.CorrectionRepairAttempts > 0 {
|
||||||
provider, ok := input.Module.(interface{ RepairPromptID() string })
|
provider, ok := input.Module.(interface{ RepairPromptID() string })
|
||||||
if !ok || input.Spec.RepairCoordinator == nil {
|
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"}
|
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)
|
||||||
}
|
}
|
||||||
repairPromptID = provider.RepairPromptID()
|
repairPromptID = provider.RepairPromptID()
|
||||||
if repairPromptID == "" {
|
if repairPromptID == "" {
|
||||||
return out, &structuredoutput.Failure{Class: outcomes.FailureFatal, Reason: outcomes.ReasonSetupFailure, Message: "correction repair prompt is unavailable"}
|
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)
|
||||||
}
|
}
|
||||||
input.Spec.CaptureProposalExchange = true
|
input.Spec.CaptureProposalExchange = true
|
||||||
}
|
}
|
||||||
|
|
||||||
runCtx, proposalResults, cancel := collectSectionProposals(ctx, input)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
pending := make(map[int]sectionProposalResult, len(input.Sections))
|
pending := make(map[int]sectionProposalResult, len(input.Sections))
|
||||||
validationResults := make(chan sectionValidationResult, len(input.Sections))
|
validationResults := make(chan sectionValidationResult, len(input.Sections))
|
||||||
validationBySection := make(map[int]sectionValidationResult, len(input.Sections))
|
validationBySection := make(map[int]sectionValidationResult, len(input.Sections))
|
||||||
@@ -466,26 +462,49 @@ func runModulePipeline(ctx context.Context, input collectSectionProposalsInput)
|
|||||||
nextProposalIndex := 0
|
nextProposalIndex := 0
|
||||||
|
|
||||||
var (
|
var (
|
||||||
firstErr error
|
firstFailure *ExecutionFailure
|
||||||
errMu sync.Mutex
|
errMu sync.Mutex
|
||||||
vwg sync.WaitGroup
|
vwg sync.WaitGroup
|
||||||
)
|
)
|
||||||
|
|
||||||
setErr := func(err error) {
|
runCtx, cancel := context.WithCancel(ctx)
|
||||||
if err == nil {
|
defer cancel()
|
||||||
return
|
setFailure := func(failure *ExecutionFailure) {
|
||||||
}
|
|
||||||
errMu.Lock()
|
errMu.Lock()
|
||||||
defer errMu.Unlock()
|
defer errMu.Unlock()
|
||||||
if firstErr == nil {
|
if firstFailure == nil {
|
||||||
firstErr = err
|
firstFailure = failure
|
||||||
cancel()
|
cancel()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
getErr := func() error {
|
proposalResults := collectSectionProposals(runCtx, input, setFailure)
|
||||||
errMu.Lock()
|
|
||||||
defer errMu.Unlock()
|
collectCandidates := func(sectionResult sectionProposalResult, index int) []proposals.EnrichedCorrectionProposal {
|
||||||
return firstErr
|
sectionEnriched := make([]proposals.EnrichedCorrectionProposal, 0, len(sectionResult.proposal.Proposals))
|
||||||
|
for i, corrected := range sectionResult.proposal.Proposals {
|
||||||
|
sectionIndex := index
|
||||||
|
sectionEnriched = append(sectionEnriched, proposals.EnrichedCorrectionProposal{
|
||||||
|
CorrectionProposal: corrected,
|
||||||
|
ProposalMetadata: proposals.ProposalMetadata{
|
||||||
|
ProposalIndex: nextProposalIndex + i,
|
||||||
|
ModuleKey: input.Spec.ModuleKey,
|
||||||
|
ModuleInstance: input.Spec.InstanceName,
|
||||||
|
SectionIndex: §ionIndex,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
nextProposalIndex += len(sectionEnriched)
|
||||||
|
out.ProposalCount += len(sectionEnriched)
|
||||||
|
out.Candidates = append(out.Candidates, sectionEnriched...)
|
||||||
|
for operationIndex := range out.WorkUnits[sectionResult.sectionPos].Operations {
|
||||||
|
operation := &out.WorkUnits[sectionResult.sectionPos].Operations[operationIndex]
|
||||||
|
if operation.Reference.Kind == outcomes.OperationProposal && operation.Succeeded {
|
||||||
|
for _, proposal := range sectionEnriched {
|
||||||
|
operation.ProposalIndices = append(operation.ProposalIndices, proposal.ProposalIndex)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sectionEnriched
|
||||||
}
|
}
|
||||||
|
|
||||||
for result := range proposalResults {
|
for result := range proposalResults {
|
||||||
@@ -494,11 +513,8 @@ func runModulePipeline(ctx context.Context, input collectSectionProposalsInput)
|
|||||||
unit.State = outcomes.WorkInterrupted
|
unit.State = outcomes.WorkInterrupted
|
||||||
}
|
}
|
||||||
unit.Operations = append(unit.Operations, result.proposal.Operations...)
|
unit.Operations = append(unit.Operations, result.proposal.Operations...)
|
||||||
if result.err != nil {
|
if result.failure != nil {
|
||||||
setErr(result.err)
|
unit.Failure = result.failure
|
||||||
continue
|
|
||||||
}
|
|
||||||
if getErr() != nil {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if !result.started {
|
if !result.started {
|
||||||
@@ -507,7 +523,7 @@ func runModulePipeline(ctx context.Context, input collectSectionProposalsInput)
|
|||||||
pending[result.sectionPos] = result
|
pending[result.sectionPos] = result
|
||||||
|
|
||||||
for {
|
for {
|
||||||
if getErr() != nil {
|
if runCtx.Err() != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
sectionResult, ok := pending[nextSectionToProcess]
|
sectionResult, ok := pending[nextSectionToProcess]
|
||||||
@@ -516,30 +532,7 @@ func runModulePipeline(ctx context.Context, input collectSectionProposalsInput)
|
|||||||
}
|
}
|
||||||
delete(pending, nextSectionToProcess)
|
delete(pending, nextSectionToProcess)
|
||||||
sectionMeta := contracts.SectionMetadataFromSection(sectionResult.section)
|
sectionMeta := contracts.SectionMetadataFromSection(sectionResult.section)
|
||||||
sectionEnriched := make([]proposals.EnrichedCorrectionProposal, 0, len(sectionResult.proposal.Proposals))
|
sectionEnriched := collectCandidates(sectionResult, sectionMeta.Index)
|
||||||
for i, corrected := range sectionResult.proposal.Proposals {
|
|
||||||
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: §ionIndex,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
nextProposalIndex += len(sectionEnriched)
|
|
||||||
out.ProposalCount += len(sectionEnriched)
|
|
||||||
out.Candidates = append(out.Candidates, sectionEnriched...)
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
vwg.Add(1)
|
vwg.Add(1)
|
||||||
go func(sectionPos int, enriched []proposals.EnrichedCorrectionProposal, sectionMetadata contracts.SectionMetadata) {
|
go func(sectionPos int, enriched []proposals.EnrichedCorrectionProposal, sectionMetadata contracts.SectionMetadata) {
|
||||||
@@ -561,10 +554,18 @@ func runModulePipeline(ctx context.Context, input collectSectionProposalsInput)
|
|||||||
}
|
}
|
||||||
validated, err := validateSectionCandidates(runCtx, validationInput)
|
validated, err := validateSectionCandidates(runCtx, validationInput)
|
||||||
var repairs []RepairOutcome
|
var repairs []RepairOutcome
|
||||||
|
phase, kind := "validation", outcomes.OperationValidation
|
||||||
if err == nil && repairPromptID != "" {
|
if err == nil && repairPromptID != "" {
|
||||||
|
phase, kind = "correction_repair", outcomes.OperationCorrectionRepair
|
||||||
validated, repairs, err = repairCandidates(runCtx, validationInput, sectionResult.proposal.SegmentReferences, validated, repairPromptID)
|
validated, repairs, err = repairCandidates(runCtx, validationInput, sectionResult.proposal.SegmentReferences, validated, repairPromptID)
|
||||||
}
|
}
|
||||||
setErr(err)
|
var failure *ExecutionFailure
|
||||||
|
if err != nil {
|
||||||
|
if !errors.As(err, &failure) {
|
||||||
|
failure = locateFailure(err, phase, failureReference(input.Spec, §ionMetadata.Index, kind), true, validated.operations)
|
||||||
|
}
|
||||||
|
setFailure(failure)
|
||||||
|
}
|
||||||
validationResults <- sectionValidationResult{
|
validationResults <- sectionValidationResult{
|
||||||
repairs: repairs,
|
repairs: repairs,
|
||||||
sectionPos: sectionPos,
|
sectionPos: sectionPos,
|
||||||
@@ -574,7 +575,7 @@ func runModulePipeline(ctx context.Context, input collectSectionProposalsInput)
|
|||||||
rejected: validated.rejected,
|
rejected: validated.rejected,
|
||||||
operations: validated.operations,
|
operations: validated.operations,
|
||||||
filtered: validated.filtered,
|
filtered: validated.filtered,
|
||||||
err: err,
|
failure: failure,
|
||||||
}
|
}
|
||||||
}(nextSectionToProcess, sectionEnriched, sectionMeta)
|
}(nextSectionToProcess, sectionEnriched, sectionMeta)
|
||||||
nextSectionToProcess++
|
nextSectionToProcess++
|
||||||
@@ -584,11 +585,17 @@ func runModulePipeline(ctx context.Context, input collectSectionProposalsInput)
|
|||||||
vwg.Wait()
|
vwg.Wait()
|
||||||
close(validationResults)
|
close(validationResults)
|
||||||
|
|
||||||
|
// A failed or canceled module can still have usable proposals from later
|
||||||
|
// sections. Assign their indices in order after preserving the dispatched
|
||||||
|
// prefix, without submitting new validation work.
|
||||||
|
for pos := nextSectionToProcess; pos < len(input.Sections); pos++ {
|
||||||
|
if result, ok := pending[pos]; ok {
|
||||||
|
collectCandidates(result, result.section.Index)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for v := range validationResults {
|
for v := range validationResults {
|
||||||
validationBySection[v.sectionPos] = v
|
validationBySection[v.sectionPos] = v
|
||||||
if v.err != nil {
|
|
||||||
setErr(v.err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range input.Sections {
|
for i := range input.Sections {
|
||||||
@@ -600,7 +607,8 @@ func runModulePipeline(ctx context.Context, input collectSectionProposalsInput)
|
|||||||
unit.Operations = append(unit.Operations, res.operations...)
|
unit.Operations = append(unit.Operations, res.operations...)
|
||||||
unit.Repairs = res.repairs
|
unit.Repairs = res.repairs
|
||||||
mergeFilteredProposals(unit, res.filtered)
|
mergeFilteredProposals(unit, res.filtered)
|
||||||
if res.err == nil {
|
unit.Failure = res.failure
|
||||||
|
if res.failure == nil {
|
||||||
unit.State = outcomes.WorkCompleted
|
unit.State = outcomes.WorkCompleted
|
||||||
} else {
|
} else {
|
||||||
unit.State = outcomes.WorkInterrupted
|
unit.State = outcomes.WorkInterrupted
|
||||||
@@ -625,11 +633,11 @@ func runModulePipeline(ctx context.Context, input collectSectionProposalsInput)
|
|||||||
}
|
}
|
||||||
return validatorOrder[out.ValidatorRejected[i].ValidatorName] < validatorOrder[out.ValidatorRejected[j].ValidatorName]
|
return validatorOrder[out.ValidatorRejected[i].ValidatorName] < validatorOrder[out.ValidatorRejected[j].ValidatorName]
|
||||||
})
|
})
|
||||||
if ctx.Err() != nil {
|
if err := ctx.Err(); err != nil {
|
||||||
setErr(ctx.Err())
|
setFailure(locateFailure(err, "processing", failureReference(input.Spec, nil, ""), false, nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := getErr(); err != nil {
|
if firstFailure != nil {
|
||||||
for i := range out.WorkUnits {
|
for i := range out.WorkUnits {
|
||||||
for j := range out.WorkUnits[i].Repairs {
|
for j := range out.WorkUnits[i].Repairs {
|
||||||
if out.WorkUnits[i].Repairs[j].Outcome == "approved" {
|
if out.WorkUnits[i].Repairs[j].Outcome == "approved" {
|
||||||
@@ -637,7 +645,7 @@ func runModulePipeline(ctx context.Context, input collectSectionProposalsInput)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return out, err
|
return out, firstFailure
|
||||||
}
|
}
|
||||||
|
|
||||||
return out, nil
|
return out, nil
|
||||||
@@ -713,6 +721,13 @@ func validateSectionCandidates(ctx context.Context, input validateSectionCandida
|
|||||||
})
|
})
|
||||||
operations = append(operations, vResult.Operations...)
|
operations = append(operations, vResult.Operations...)
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
var decisionErr error
|
var decisionErr error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
decisionErr = validators.EnforcePartialDecisionCardinality(submitted, vResult.Decisions)
|
decisionErr = validators.EnforcePartialDecisionCardinality(submitted, vResult.Decisions)
|
||||||
@@ -722,7 +737,7 @@ func validateSectionCandidates(ctx context.Context, input validateSectionCandida
|
|||||||
if decisionErr != nil {
|
if decisionErr != nil {
|
||||||
return validateSectionCandidatesResult{
|
return validateSectionCandidatesResult{
|
||||||
decisions: decisions, rejected: rejected, operations: operations, filtered: filtered,
|
decisions: decisions, rejected: rejected, operations: operations, filtered: filtered,
|
||||||
}, fmt.Errorf("validator %q cardinality failed: %w", validator.Name(), errors.Join(err, decisionErr))
|
}, locate(fmt.Errorf("validator %q cardinality failed: %w", validator.Name(), errors.Join(err, decisionErr)))
|
||||||
}
|
}
|
||||||
|
|
||||||
nextEligible := make([]proposals.EnrichedCorrectionProposal, 0, len(submitted))
|
nextEligible := make([]proposals.EnrichedCorrectionProposal, 0, len(submitted))
|
||||||
@@ -737,7 +752,7 @@ func validateSectionCandidates(ctx context.Context, input validateSectionCandida
|
|||||||
if contractErr != nil {
|
if contractErr != nil {
|
||||||
return validateSectionCandidatesResult{
|
return validateSectionCandidatesResult{
|
||||||
decisions: decisions, rejected: rejected, operations: operations, filtered: filtered,
|
decisions: decisions, rejected: rejected, operations: operations, filtered: filtered,
|
||||||
}, fmt.Errorf("validator %q returned inconsistent decisions: %w", validator.Name(), errors.Join(err, contractErr))
|
}, locate(fmt.Errorf("validator %q returned inconsistent decisions: %w", validator.Name(), errors.Join(err, contractErr)))
|
||||||
}
|
}
|
||||||
dispositions[i] = disposition
|
dispositions[i] = disposition
|
||||||
}
|
}
|
||||||
@@ -789,7 +804,7 @@ func validateSectionCandidates(ctx context.Context, input validateSectionCandida
|
|||||||
// approval to candidates that have not finished the full chain.
|
// approval to candidates that have not finished the full chain.
|
||||||
return validateSectionCandidatesResult{
|
return validateSectionCandidatesResult{
|
||||||
decisions: decisions, rejected: rejected, operations: operations, filtered: filtered,
|
decisions: decisions, rejected: rejected, operations: operations, filtered: filtered,
|
||||||
}, fmt.Errorf("validator %q failed: %w", validator.Name(), err)
|
}, locate(fmt.Errorf("validator %q failed: %w", validator.Name(), err))
|
||||||
}
|
}
|
||||||
eligible = nextEligible
|
eligible = nextEligible
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -634,19 +634,22 @@ func TestRunnerSkippedRecorded(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRunnerFailureReturnsPartialProgress(t *testing.T) {
|
func TestRunnerFailureReturnsPartialProgress(t *testing.T) {
|
||||||
|
origin := errors.New("boom")
|
||||||
transcript := &schema.Transcript{Segments: []schema.Segment{{ID: 1, Speaker: "A", Start: 0, End: 1, Text: "teh cat"}}}
|
transcript := &schema.Transcript{Segments: []schema.Segment{{ID: 1, Speaker: "A", Start: 0, End: 1, Text: "teh cat"}}}
|
||||||
r := New(fakeFactory{modules: map[string]contracts.TranscriptModule{
|
r := New(fakeFactory{modules: map[string]contracts.TranscriptModule{
|
||||||
"m1": fakeModule{key: "m1", policy: proposals.ReplacementPolicyRequireUnique, proposeF: func(req contracts.ProposalRequest) ([]proposals.CorrectionProposal, error) {
|
"m1": fakeModule{key: "m1", policy: proposals.ReplacementPolicyRequireUnique, proposeF: func(req contracts.ProposalRequest) ([]proposals.CorrectionProposal, error) {
|
||||||
return []proposals.CorrectionProposal{{TargetSegmentID: 1, OriginalText: "teh", CorrectedText: "the"}}, nil
|
return []proposals.CorrectionProposal{{TargetSegmentID: 1, OriginalText: "teh", CorrectedText: "the"}}, nil
|
||||||
}},
|
}},
|
||||||
"m2": fakeModule{key: "m2", policy: proposals.ReplacementPolicyRequireUnique, proposeF: func(req contracts.ProposalRequest) ([]proposals.CorrectionProposal, error) {
|
"m2": fakeModule{key: "m2", policy: proposals.ReplacementPolicyRequireUnique, proposeF: func(req contracts.ProposalRequest) ([]proposals.CorrectionProposal, error) {
|
||||||
return nil, errors.New("boom")
|
return nil, origin
|
||||||
}},
|
}},
|
||||||
}})
|
}})
|
||||||
|
|
||||||
out, err := r.Run(context.Background(), RunInput{Transcript: transcript, ModuleSpecs: []contracts.ModuleRunSpec{{ModuleKey: "m1", InstanceName: "m1"}, {ModuleKey: "m2", InstanceName: "m2"}}})
|
out, err := r.Run(context.Background(), RunInput{Transcript: transcript, ModuleSpecs: []contracts.ModuleRunSpec{{ModuleKey: "m1", InstanceName: "m1"}, {Index: 1, ModuleKey: "m2", InstanceName: "m2"}}})
|
||||||
if err == nil {
|
var failure *ExecutionFailure
|
||||||
t.Fatal("expected error")
|
if !errors.Is(err, origin) || !errors.As(err, &failure) || failure.Phase != "proposal" ||
|
||||||
|
!failure.HasSection || *failure.Origin.StageIndex != 1 || failure.Origin.ModuleInstance != "m2" || failure.Origin.SectionIndex != 0 {
|
||||||
|
t.Fatalf("lost initiating proposal failure: %+v", failure)
|
||||||
}
|
}
|
||||||
if out.FinalTranscript.Segments[0].Text != "the cat" {
|
if out.FinalTranscript.Segments[0].Text != "the cat" {
|
||||||
t.Fatalf("expected partial transcript progress preserved, got %q", out.FinalTranscript.Segments[0].Text)
|
t.Fatalf("expected partial transcript progress preserved, got %q", out.FinalTranscript.Segments[0].Text)
|
||||||
|
|||||||
Reference in New Issue
Block a user