Give each correction repair branch its own outcome
This commit is contained in:
@@ -49,7 +49,7 @@ func actionable(index int, rejected []ValidatorRejectedChange) bool {
|
||||
|
||||
// repairCandidates branches each rejected proposal from the same section exchange.
|
||||
func repairCandidates(ctx context.Context, input validateSectionCandidatesInput, references []int, initial validateSectionCandidatesResult, promptID string) (validateSectionCandidatesResult, []RepairOutcome, error) {
|
||||
if input.Spec.Settings.LLM.CorrectionRepairAttempts <= 0 {
|
||||
if input.Spec.Settings.LLM.CorrectionRepairAttempts <= 0 || len(initial.rejected) == 0 {
|
||||
return initial, nil, nil
|
||||
}
|
||||
if input.Section == nil {
|
||||
@@ -61,19 +61,12 @@ func repairCandidates(ctx context.Context, input validateSectionCandidatesInput,
|
||||
}
|
||||
superseded := make(map[int]bool)
|
||||
var branches []RepairOutcome
|
||||
var repairOps []outcomes.Operation
|
||||
debug := input.Interactions != nil
|
||||
section := 0
|
||||
if input.SectionIndex != nil {
|
||||
section = *input.SectionIndex
|
||||
}
|
||||
finish := func(err error) (validateSectionCandidatesResult, []RepairOutcome, error) {
|
||||
if len(superseded) > 0 {
|
||||
initial.filtered = slices.DeleteFunc(initial.filtered, func(item outcomes.FilteredProposal) bool {
|
||||
return superseded[item.ProposalIndex]
|
||||
})
|
||||
}
|
||||
initial.operations = append(initial.operations, repairOps...)
|
||||
return initial, branches, err
|
||||
}
|
||||
|
||||
@@ -82,178 +75,211 @@ func repairCandidates(ctx context.Context, input validateSectionCandidatesInput,
|
||||
if !actionable(original.ProposalIndex, rejected) {
|
||||
continue
|
||||
}
|
||||
branch := RepairOutcome{ProposalIndex: original.ProposalIndex, Outcome: "interrupted"}
|
||||
if debug {
|
||||
branch.Original, branch.Final = original, original
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
branches = append(branches, branch)
|
||||
return finish(err)
|
||||
}
|
||||
if input.Spec.RepairClient == nil || input.Exchange == nil || promptID == "" {
|
||||
branches = append(branches, branch)
|
||||
return finish(fmt.Errorf("correction repair setup is unavailable"))
|
||||
}
|
||||
localRef := 0
|
||||
if position < len(references) {
|
||||
localRef = references[position]
|
||||
} else {
|
||||
if pos, ok := input.Section.Position(original.TargetSegmentID); ok {
|
||||
localRef = pos + 1
|
||||
}
|
||||
} else if pos, ok := input.Section.Position(original.TargetSegmentID); ok {
|
||||
localRef = pos + 1
|
||||
}
|
||||
current := original
|
||||
findings := repairFindings(original.ProposalIndex, rejected)
|
||||
coordinator, ok := input.Spec.RepairClient.(structuredoutput.Coordinator)
|
||||
if !ok {
|
||||
return finish(fmt.Errorf("correction repair coordinator is unavailable"))
|
||||
}
|
||||
buildRequest := func() (*structuredoutput.Request, error) {
|
||||
request := proposal_generation.RepairRequest{
|
||||
ProposalRef: position + 1,
|
||||
Correction: proposal_generation.StructuredCorrectionProposal{SegmentReference: localRef, OriginalText: current.OriginalText, CorrectedText: current.CorrectedText},
|
||||
Findings: findings,
|
||||
}
|
||||
if pos, ok := input.Section.Position(current.TargetSegmentID); ok {
|
||||
segment := &input.WorkingTranscript.Segments[pos]
|
||||
request.OriginalSegmentText = &segment.Text
|
||||
preview := proposals.PreviewProposalForSegment(segment, current.CorrectionProposal, input.Policy)
|
||||
if preview.Applicable {
|
||||
request.CorrectedSegmentText = &preview.CorrectedSegmentText
|
||||
}
|
||||
}
|
||||
payload, err := request.JSON()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
proposalIndex, attemptNumber := original.ProposalIndex, branch.AttemptCount+1
|
||||
return &structuredoutput.Request{PromptID: promptID, Variables: map[string]string{"RepairRequestJSON": payload}, Schema: responseschema.CorrectionRepairKey, Context: structuredoutput.DiagnosticContext{StageIndex: &input.Spec.Index, StageID: input.Spec.ID, Role: "correction_repair", ModuleKey: input.Spec.ModuleKey, ModuleInstance: input.Spec.InstanceName, SectionIndex: input.SectionIndex, ProposalIndex: &proposalIndex, RepairAttempt: &attemptNumber}}, nil
|
||||
}
|
||||
exhausted, coordinationErr := coordinator.Coordinate(ctx, input.Exchange, input.Spec.Settings.LLM.CorrectionRepairAttempts, func(ctx context.Context, turn structuredoutput.Turn) (*structuredoutput.Request, error) {
|
||||
if turn.Response == nil {
|
||||
return buildRequest()
|
||||
}
|
||||
completion, callErr := *turn.Response, turn.Err
|
||||
proposalIndex, attemptNumber := original.ProposalIndex, branch.AttemptCount+1
|
||||
var response proposal_generation.RepairResponse
|
||||
|
||||
operation := outcomes.Operation{
|
||||
Reference: outcomes.Reference{
|
||||
StageIndex: &input.Spec.Index, StageID: input.Spec.ID, ModuleKey: input.Spec.ModuleKey,
|
||||
ModuleInstance: input.Spec.InstanceName, SectionIndex: section, Kind: outcomes.OperationCorrectionRepair,
|
||||
ProposalIndex: &proposalIndex, RepairAttempt: &attemptNumber,
|
||||
},
|
||||
Counts: completion.Execution, ProposalIndices: []int{proposalIndex},
|
||||
}
|
||||
finishAttempt := func(err error, revision *proposals.EnrichedCorrectionProposal, validated validateSectionCandidatesResult) {
|
||||
branch.AttemptCount++
|
||||
repairOps = append(repairOps, operation)
|
||||
if !debug {
|
||||
return
|
||||
}
|
||||
record := RepairAttempt{Decisions: validated.decisions, FailureReason: operation.FailureReason}
|
||||
if revision != nil {
|
||||
snapshot := *revision
|
||||
record.Revision = &snapshot
|
||||
}
|
||||
name := fmt.Sprintf("%s:section-%04d:proposal-%04d:attempt-%04d:repair", input.Spec.InstanceName, section, proposalIndex, attemptNumber)
|
||||
var failure any
|
||||
if err != nil {
|
||||
failure = map[string]string{"error": err.Error()}
|
||||
}
|
||||
record.Artifacts, _ = input.Interactions.WriteInteraction(structuredoutput.Interaction{
|
||||
Name: name,
|
||||
Context: structuredoutput.DiagnosticContext{StageIndex: &input.Spec.Index, StageID: input.Spec.ID, Role: "correction_repair", ModuleKey: input.Spec.ModuleKey, ModuleInstance: input.Spec.InstanceName, SectionIndex: input.SectionIndex, ProposalIndex: &proposalIndex, RepairAttempt: &attemptNumber},
|
||||
RequestMetadata: map[string]any{"stage_index": input.Spec.Index, "stage_id": input.Spec.ID, "proposal_index": proposalIndex, "repair_attempt": attemptNumber, "prompt_id": promptID, "prompt": completion.Prompt, "feedback": findings},
|
||||
RequestPayload: completion.Prepared,
|
||||
ResponsePayload: map[string]any{"decoded": response, "final_content": completion.FinalContent, "revision": record.Revision, "decisions": record.Decisions, "rejections": validated.rejected},
|
||||
ErrorPayload: failure,
|
||||
})
|
||||
branch.Attempts = append(branch.Attempts, record)
|
||||
}
|
||||
if callErr != nil {
|
||||
structuredoutput.RecordFailure(&operation, callErr)
|
||||
finishAttempt(callErr, nil, validateSectionCandidatesResult{})
|
||||
if operation.FailureClass == outcomes.FailureRecoverable {
|
||||
branch.Outcome = "unavailable"
|
||||
return nil, nil
|
||||
}
|
||||
return nil, callErr
|
||||
}
|
||||
var err error
|
||||
response, err = proposal_generation.DecodeRepairResponse(completion.Content, position+1)
|
||||
if err != nil {
|
||||
structuredoutput.RecordFailure(&operation, err)
|
||||
finishAttempt(err, nil, validateSectionCandidatesResult{})
|
||||
branch.Outcome = "unavailable"
|
||||
return nil, nil
|
||||
}
|
||||
operation.Succeeded = true
|
||||
operation.RecoveryReasons = outcomes.ExecutionRecoveries(operation.Counts)
|
||||
if response.Decision.Action == "withdraw" {
|
||||
finishAttempt(nil, nil, validateSectionCandidatesResult{})
|
||||
branch.Outcome = "withdrawn"
|
||||
return nil, nil
|
||||
}
|
||||
revision := response.Decision.Correction
|
||||
localRef = revision.SegmentReference
|
||||
current = original
|
||||
current.CorrectionProposal = proposals.CorrectionProposal{TargetSegmentID: proposals.SegmentIDAtReference(input.WorkingTranscript, localRef), OriginalText: revision.OriginalText, CorrectedText: revision.CorrectedText}
|
||||
if debug {
|
||||
branch.Final = current
|
||||
}
|
||||
next := input
|
||||
next.RepairProposalIndex, next.RepairAttempt = &proposalIndex, &attemptNumber
|
||||
next.SectionEnriched = []proposals.EnrichedCorrectionProposal{current}
|
||||
validated, validationErr := validateSectionCandidates(ctx, next)
|
||||
if len(validated.filtered) > 0 {
|
||||
// Each repair response contains only one proposal.
|
||||
operation.Filtered = validated.filtered[:1]
|
||||
operation.RecoveryReasons = append(operation.RecoveryReasons, outcomes.RecoveryProposalsFiltered)
|
||||
}
|
||||
for i := range validated.operations {
|
||||
proposalIndex, attemptNumber := original.ProposalIndex, attemptNumber
|
||||
validated.operations[i].Reference.ProposalIndex = &proposalIndex
|
||||
validated.operations[i].Reference.RepairAttempt = &attemptNumber
|
||||
}
|
||||
approved := validationErr == nil && len(validated.approved) == 1
|
||||
if approved {
|
||||
operation.RecoveryReasons = append(operation.RecoveryReasons, outcomes.RecoveryCorrectionRevised)
|
||||
}
|
||||
finishAttempt(validationErr, ¤t, validated)
|
||||
initial.operations = append(initial.operations, validated.operations...)
|
||||
if validationErr != nil {
|
||||
return nil, validationErr
|
||||
}
|
||||
if approved {
|
||||
branch.Outcome = "approved"
|
||||
initial.approved = append(initial.approved, current)
|
||||
superseded[original.ProposalIndex] = true
|
||||
return nil, nil
|
||||
}
|
||||
if actionable(original.ProposalIndex, validated.rejected) {
|
||||
findings = repairFindings(original.ProposalIndex, validated.rejected)
|
||||
return buildRequest()
|
||||
}
|
||||
branch.Outcome = "unavailable"
|
||||
for _, d := range validated.rejected {
|
||||
if d.Disposition != validators.DispositionUnavailable {
|
||||
branch.Outcome = "rejected"
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
result, err := runRepairBranch(ctx, repairBranchInput{
|
||||
validation: input,
|
||||
original: original,
|
||||
proposalRef: position + 1,
|
||||
segmentRef: localRef,
|
||||
rejected: rejected,
|
||||
promptID: promptID,
|
||||
})
|
||||
if exhausted {
|
||||
branch.Outcome = "exhausted"
|
||||
branches = append(branches, result.outcome)
|
||||
initial.operations = append(initial.operations, result.operations...)
|
||||
if result.approved != nil {
|
||||
initial.approved = append(initial.approved, *result.approved)
|
||||
superseded[original.ProposalIndex] = true
|
||||
}
|
||||
if coordinationErr != nil {
|
||||
branches = append(branches, branch)
|
||||
return finish(coordinationErr)
|
||||
if err != nil {
|
||||
return finish(err)
|
||||
}
|
||||
|
||||
branches = append(branches, branch)
|
||||
}
|
||||
return finish(nil)
|
||||
}
|
||||
|
||||
type repairBranchInput struct {
|
||||
validation validateSectionCandidatesInput
|
||||
original proposals.EnrichedCorrectionProposal
|
||||
proposalRef int
|
||||
segmentRef int
|
||||
rejected []ValidatorRejectedChange
|
||||
promptID string
|
||||
}
|
||||
|
||||
type repairBranchResult struct {
|
||||
outcome RepairOutcome
|
||||
operations []outcomes.Operation
|
||||
approved *proposals.EnrichedCorrectionProposal
|
||||
}
|
||||
|
||||
func runRepairBranch(ctx context.Context, branchInput repairBranchInput) (repairBranchResult, error) {
|
||||
input, original := branchInput.validation, branchInput.original
|
||||
result := repairBranchResult{outcome: RepairOutcome{ProposalIndex: original.ProposalIndex, Outcome: "interrupted"}}
|
||||
branch := &result.outcome
|
||||
debug := input.Interactions != nil
|
||||
if debug {
|
||||
branch.Original, branch.Final = original, original
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return result, err
|
||||
}
|
||||
if input.Spec.RepairCoordinator == nil || input.Exchange == nil || branchInput.promptID == "" {
|
||||
return result, &structuredoutput.Failure{Class: outcomes.FailureFatal, Reason: outcomes.ReasonSetupFailure, Message: "correction repair exchange or setup is unavailable"}
|
||||
}
|
||||
section := 0
|
||||
if input.SectionIndex != nil {
|
||||
section = *input.SectionIndex
|
||||
}
|
||||
localRef := branchInput.segmentRef
|
||||
proposalRef := branchInput.proposalRef
|
||||
promptID := branchInput.promptID
|
||||
current := original
|
||||
findings := repairFindings(original.ProposalIndex, branchInput.rejected)
|
||||
buildRequest := func() (*structuredoutput.Request, error) {
|
||||
request := proposal_generation.RepairRequest{
|
||||
ProposalRef: proposalRef,
|
||||
Correction: proposal_generation.StructuredCorrectionProposal{SegmentReference: localRef, OriginalText: current.OriginalText, CorrectedText: current.CorrectedText},
|
||||
Findings: findings,
|
||||
}
|
||||
if pos, ok := input.Section.Position(current.TargetSegmentID); ok {
|
||||
segment := &input.WorkingTranscript.Segments[pos]
|
||||
request.OriginalSegmentText = &segment.Text
|
||||
preview := proposals.PreviewProposalForSegment(segment, current.CorrectionProposal, input.Policy)
|
||||
if preview.Applicable {
|
||||
request.CorrectedSegmentText = &preview.CorrectedSegmentText
|
||||
}
|
||||
}
|
||||
payload, err := request.JSON()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
proposalIndex, attemptNumber := original.ProposalIndex, branch.AttemptCount+1
|
||||
return &structuredoutput.Request{PromptID: promptID, Variables: map[string]string{"RepairRequestJSON": payload}, Schema: responseschema.CorrectionRepairKey, Context: structuredoutput.DiagnosticContext{StageIndex: &input.Spec.Index, StageID: input.Spec.ID, Role: "correction_repair", ModuleKey: input.Spec.ModuleKey, ModuleInstance: input.Spec.InstanceName, SectionIndex: input.SectionIndex, ProposalIndex: &proposalIndex, RepairAttempt: &attemptNumber}}, nil
|
||||
}
|
||||
exhausted, coordinationErr := input.Spec.RepairCoordinator.Coordinate(ctx, input.Exchange, input.Spec.Settings.LLM.CorrectionRepairAttempts, func(ctx context.Context, turn structuredoutput.Turn) (*structuredoutput.Request, error) {
|
||||
if turn.Response == nil {
|
||||
return buildRequest()
|
||||
}
|
||||
completion, callErr := turn.Response, turn.Err
|
||||
proposalIndex, attemptNumber := original.ProposalIndex, branch.AttemptCount+1
|
||||
var response proposal_generation.RepairResponse
|
||||
|
||||
operation := outcomes.Operation{
|
||||
Reference: outcomes.Reference{
|
||||
StageIndex: &input.Spec.Index, StageID: input.Spec.ID, ModuleKey: input.Spec.ModuleKey,
|
||||
ModuleInstance: input.Spec.InstanceName, SectionIndex: section, Kind: outcomes.OperationCorrectionRepair,
|
||||
ProposalIndex: &proposalIndex, RepairAttempt: &attemptNumber,
|
||||
},
|
||||
Counts: completion.Execution, ProposalIndices: []int{proposalIndex},
|
||||
}
|
||||
finishAttempt := func(err error, revision *proposals.EnrichedCorrectionProposal, validated validateSectionCandidatesResult) {
|
||||
branch.AttemptCount++
|
||||
result.operations = append(result.operations, operation)
|
||||
if !debug {
|
||||
return
|
||||
}
|
||||
record := RepairAttempt{Decisions: validated.decisions, FailureReason: operation.FailureReason}
|
||||
if revision != nil {
|
||||
snapshot := *revision
|
||||
record.Revision = &snapshot
|
||||
}
|
||||
name := fmt.Sprintf("%s:section-%04d:proposal-%04d:attempt-%04d:repair", input.Spec.InstanceName, section, proposalIndex, attemptNumber)
|
||||
var failure any
|
||||
if err != nil {
|
||||
failure = map[string]string{"error": err.Error()}
|
||||
}
|
||||
record.Artifacts, _ = input.Interactions.WriteInteraction(structuredoutput.Interaction{
|
||||
Name: name,
|
||||
Context: structuredoutput.DiagnosticContext{StageIndex: &input.Spec.Index, StageID: input.Spec.ID, Role: "correction_repair", ModuleKey: input.Spec.ModuleKey, ModuleInstance: input.Spec.InstanceName, SectionIndex: input.SectionIndex, ProposalIndex: &proposalIndex, RepairAttempt: &attemptNumber},
|
||||
RequestMetadata: map[string]any{"stage_index": input.Spec.Index, "stage_id": input.Spec.ID, "proposal_index": proposalIndex, "repair_attempt": attemptNumber, "prompt_id": promptID, "prompt": completion.Prompt, "feedback": findings},
|
||||
RequestPayload: completion.Prepared,
|
||||
ResponsePayload: map[string]any{"decoded": response, "final_content": completion.FinalContent, "revision": record.Revision, "decisions": record.Decisions, "rejections": validated.rejected},
|
||||
ErrorPayload: failure,
|
||||
})
|
||||
branch.Attempts = append(branch.Attempts, record)
|
||||
}
|
||||
if callErr != nil {
|
||||
structuredoutput.RecordFailure(&operation, callErr)
|
||||
finishAttempt(callErr, nil, validateSectionCandidatesResult{})
|
||||
if operation.FailureClass == outcomes.FailureRecoverable {
|
||||
branch.Outcome = "unavailable"
|
||||
return nil, nil
|
||||
}
|
||||
return nil, callErr
|
||||
}
|
||||
var err error
|
||||
response, err = proposal_generation.DecodeRepairResponse(completion.Content, proposalRef)
|
||||
if err != nil {
|
||||
structuredoutput.RecordFailure(&operation, err)
|
||||
finishAttempt(err, nil, validateSectionCandidatesResult{})
|
||||
branch.Outcome = "unavailable"
|
||||
return nil, nil
|
||||
}
|
||||
operation.Succeeded = true
|
||||
operation.RecoveryReasons = outcomes.ExecutionRecoveries(operation.Counts)
|
||||
if response.Decision.Action == "withdraw" {
|
||||
finishAttempt(nil, nil, validateSectionCandidatesResult{})
|
||||
branch.Outcome = "withdrawn"
|
||||
return nil, nil
|
||||
}
|
||||
revision := response.Decision.Correction
|
||||
localRef = revision.SegmentReference
|
||||
current = original
|
||||
current.CorrectionProposal = proposals.CorrectionProposal{TargetSegmentID: proposals.SegmentIDAtReference(input.WorkingTranscript, localRef), OriginalText: revision.OriginalText, CorrectedText: revision.CorrectedText}
|
||||
if debug {
|
||||
branch.Final = current
|
||||
}
|
||||
next := input
|
||||
next.RepairProposalIndex, next.RepairAttempt = &proposalIndex, &attemptNumber
|
||||
next.SectionEnriched = []proposals.EnrichedCorrectionProposal{current}
|
||||
validated, validationErr := validateSectionCandidates(ctx, next)
|
||||
if len(validated.filtered) > 0 {
|
||||
// Each repair response contains only one proposal.
|
||||
operation.Filtered = validated.filtered[:1]
|
||||
operation.RecoveryReasons = append(operation.RecoveryReasons, outcomes.RecoveryProposalsFiltered)
|
||||
}
|
||||
for i := range validated.operations {
|
||||
validated.operations[i].Reference.ProposalIndex = &proposalIndex
|
||||
validated.operations[i].Reference.RepairAttempt = &attemptNumber
|
||||
}
|
||||
approved := validationErr == nil && len(validated.approved) == 1
|
||||
if approved {
|
||||
operation.RecoveryReasons = append(operation.RecoveryReasons, outcomes.RecoveryCorrectionRevised)
|
||||
}
|
||||
result.operations = append(result.operations, validated.operations...)
|
||||
finishAttempt(validationErr, ¤t, validated)
|
||||
if validationErr != nil {
|
||||
return nil, validationErr
|
||||
}
|
||||
if approved {
|
||||
branch.Outcome = "approved"
|
||||
result.approved = ¤t
|
||||
return nil, nil
|
||||
}
|
||||
if actionable(original.ProposalIndex, validated.rejected) {
|
||||
findings = repairFindings(original.ProposalIndex, validated.rejected)
|
||||
return buildRequest()
|
||||
}
|
||||
branch.Outcome = "unavailable"
|
||||
for _, d := range validated.rejected {
|
||||
if d.Disposition != validators.DispositionUnavailable {
|
||||
branch.Outcome = "rejected"
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
})
|
||||
if coordinationErr != nil {
|
||||
branch.Outcome = "interrupted"
|
||||
result.approved = nil
|
||||
} else if exhausted {
|
||||
branch.Outcome = "exhausted"
|
||||
}
|
||||
return result, coordinationErr
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user