Retain repair diagnostics and correction lineage
This commit is contained in:
@@ -4,7 +4,9 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"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/proposal_generation"
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/proposals"
|
||||
@@ -15,6 +17,7 @@ import (
|
||||
|
||||
// RepairAttempt retains compact facts for a single semantic request.
|
||||
type RepairAttempt struct {
|
||||
Artifacts llm.InteractionArtifacts
|
||||
Revision *proposals.EnrichedCorrectionProposal
|
||||
Decisions []ValidatorDecisionRecord
|
||||
Rejected []ValidatorRejectedChange
|
||||
@@ -90,8 +93,38 @@ func repairCandidates(ctx context.Context, input validateSectionCandidatesInput,
|
||||
return initial, branches, err
|
||||
}
|
||||
var response proposal_generation.RepairResponse
|
||||
completion, callErr := input.Spec.RepairClient.CompleteStructured(ctx, structuredoutput.Request{PromptID: promptID, Variables: map[string]string{"RepairRequestJSON": payload}, Schema: responseschema.CorrectionRepairKey, Replay: replay, 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}}, &response)
|
||||
proposalIndex, attemptNumber := original.ProposalIndex, attempt+1
|
||||
completion, callErr := input.Spec.RepairClient.CompleteStructured(ctx, structuredoutput.Request{PromptID: promptID, Variables: map[string]string{"RepairRequestJSON": payload}, Schema: responseschema.CorrectionRepairKey, Replay: replay, 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}}, &response)
|
||||
record := RepairAttempt{Counts: completion.Execution}
|
||||
save := func(err error) {
|
||||
if input.DiagnosticsDir == "" {
|
||||
return
|
||||
}
|
||||
var secrets []string
|
||||
if source, ok := input.Spec.RepairClient.(interface {
|
||||
ConfiguredSecrets(context.Context) []string
|
||||
}); ok {
|
||||
secrets = source.ConfiguredSecrets(ctx)
|
||||
}
|
||||
section := 0
|
||||
if input.SectionIndex != nil {
|
||||
section = *input.SectionIndex
|
||||
}
|
||||
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()}
|
||||
}
|
||||
writer := llm.NewDiagnosticsWriter(filepath.Join(input.DiagnosticsDir, input.Spec.InstanceName), secrets)
|
||||
artifacts, writeErr := writer.WriteInteraction(name, 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}, completion.Prepared, map[string]any{"decoded": response, "final_content": completion.FinalContent, "revision": record.Revision, "decisions": record.Decisions, "rejections": record.Rejected}, failure)
|
||||
if writeErr != nil {
|
||||
if input.ReportDiagnosticError != nil {
|
||||
input.ReportDiagnosticError(fmt.Errorf("repair interaction %s: %w", name, writeErr))
|
||||
}
|
||||
return
|
||||
}
|
||||
record.Artifacts = artifacts
|
||||
}
|
||||
if callErr != nil {
|
||||
var failure *structuredoutput.Failure
|
||||
if errors.As(callErr, &failure) {
|
||||
@@ -99,6 +132,7 @@ func repairCandidates(ctx context.Context, input validateSectionCandidatesInput,
|
||||
} else {
|
||||
record.FailureReason = outcomes.ReasonInternalFailure
|
||||
}
|
||||
save(callErr)
|
||||
branch.Attempts = append(branch.Attempts, record)
|
||||
if structuredoutput.IsRecoverable(callErr) {
|
||||
branch.Outcome = "unavailable"
|
||||
@@ -110,6 +144,7 @@ func repairCandidates(ctx context.Context, input validateSectionCandidatesInput,
|
||||
decoded, err := proposal_generation.DecodeRepairResponse(completion.Content, position+1)
|
||||
if err != nil {
|
||||
record.FailureReason = outcomes.ReasonInvalidOutput
|
||||
save(err)
|
||||
branch.Attempts = append(branch.Attempts, record)
|
||||
branch.Outcome = "unavailable"
|
||||
break
|
||||
@@ -118,6 +153,7 @@ func repairCandidates(ctx context.Context, input validateSectionCandidatesInput,
|
||||
replay.Turns = append(replay.Turns, completion.RepairInstruction...)
|
||||
replay.Turns = append(replay.Turns, structuredoutput.Message{Role: "assistant", Content: completion.FinalContent})
|
||||
if decoded.Action == "withdraw" {
|
||||
save(nil)
|
||||
branch.Attempts = append(branch.Attempts, record)
|
||||
branch.Outcome = "withdrawn"
|
||||
break
|
||||
@@ -132,6 +168,7 @@ func repairCandidates(ctx context.Context, input validateSectionCandidatesInput,
|
||||
current.CorrectionProposal = proposals.CorrectionProposal{TargetSegmentID: target, OriginalText: revision.OriginalText, CorrectedText: revision.CorrectedText, Confidence: revision.Confidence}
|
||||
branch.Final = current
|
||||
next := input
|
||||
next.RepairProposalIndex, next.RepairAttempt = &proposalIndex, &attemptNumber
|
||||
next.SectionEnriched = []proposals.EnrichedCorrectionProposal{current}
|
||||
validated, validationErr := validateSectionCandidates(ctx, next)
|
||||
revisionSnapshot := current
|
||||
@@ -144,9 +181,9 @@ func repairCandidates(ctx context.Context, input validateSectionCandidatesInput,
|
||||
validated.operations[i].Reference.RepairAttempt = &attemptNumber
|
||||
}
|
||||
record.Operations = validated.operations
|
||||
save(validationErr)
|
||||
branch.Attempts = append(branch.Attempts, record)
|
||||
initial.operations = append(initial.operations, validated.operations...)
|
||||
initial.decisions = append(initial.decisions, validated.decisions...)
|
||||
if validationErr != nil {
|
||||
branches = append(branches, branch)
|
||||
return initial, branches, validationErr
|
||||
|
||||
Reference in New Issue
Block a user