Adopt PromptKit v0.23.0 conversations and execution accounting

This commit is contained in:
2026-11-15 01:25:55 +00:00
parent d28b560265
commit d98d419b36
25 changed files with 689 additions and 924 deletions

View File

@@ -2,6 +2,7 @@ package runner
import (
"context"
"encoding/json"
"errors"
"fmt"
"path/filepath"
@@ -117,8 +118,11 @@ func repairCandidates(ctx context.Context, input validateSectionCandidatesInput,
return findings
}
findings := safeFindings(rejected)
replay := &structuredoutput.Replay{Exchange: input.Exchange}
for attempt := 0; attempt < input.Spec.Settings.LLM.CorrectionRepairAttempts; attempt++ {
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},
@@ -134,12 +138,23 @@ func repairCandidates(ctx context.Context, input validateSectionCandidatesInput,
}
payload, err := request.JSON()
if err != nil {
branches = append(branches, branch)
return finish(err)
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
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)
if callErr == nil {
_ = json.Unmarshal(completion.Content, &response)
}
operation := outcomes.Operation{
Reference: outcomes.Reference{
StageIndex: &input.Spec.Index, StageID: input.Spec.ID, ModuleKey: input.Spec.ModuleKey,
@@ -183,26 +198,23 @@ func repairCandidates(ctx context.Context, input validateSectionCandidatesInput,
finishAttempt(callErr, nil, validateSectionCandidatesResult{})
if structuredoutput.IsRecoverable(callErr) {
branch.Outcome = "unavailable"
break
return nil, nil
}
branches = append(branches, branch)
return finish(callErr)
return nil, callErr
}
decoded, err := proposal_generation.DecodeRepairResponse(completion.Content, position+1)
if err != nil {
operation.FailureReason = outcomes.ReasonInvalidOutput
finishAttempt(err, nil, validateSectionCandidatesResult{})
branch.Outcome = "unavailable"
break
return nil, nil
}
operation.Succeeded = true
operation.RecoveryReasons = outcomes.ExecutionRecoveries(operation.Counts)
replay.Turns = append(replay.Turns, completion.RepairInstruction...)
replay.Turns = append(replay.Turns, structuredoutput.Message{Role: "assistant", Content: completion.FinalContent})
if decoded.Action == "withdraw" {
finishAttempt(nil, nil, validateSectionCandidatesResult{})
branch.Outcome = "withdrawn"
break
return nil, nil
}
revision := decoded.Correction
localRef = revision.SegmentReference
@@ -225,7 +237,7 @@ func repairCandidates(ctx context.Context, input validateSectionCandidatesInput,
operation.RecoveryReasons = append(operation.RecoveryReasons, outcomes.RecoveryProposalsFiltered)
}
for i := range validated.operations {
proposalIndex, attemptNumber := original.ProposalIndex, attempt+1
proposalIndex, attemptNumber := original.ProposalIndex, attemptNumber
validated.operations[i].Reference.ProposalIndex = &proposalIndex
validated.operations[i].Reference.RepairAttempt = &attemptNumber
}
@@ -236,32 +248,35 @@ func repairCandidates(ctx context.Context, input validateSectionCandidatesInput,
finishAttempt(validationErr, &current, validated)
initial.operations = append(initial.operations, validated.operations...)
if validationErr != nil {
branches = append(branches, branch)
return finish(validationErr)
return nil, validationErr
}
if approved {
branch.Outcome = "approved"
initial.approved = append(initial.approved, current)
superseded[original.ProposalIndex] = true
break
return nil, nil
}
if actionable(original.ProposalIndex, validated.rejected) {
if attempt+1 == input.Spec.Settings.LLM.CorrectionRepairAttempts {
branch.Outcome = "exhausted"
break
}
findings = safeFindings(validated.rejected)
continue
return buildRequest()
}
branch.Outcome = "unavailable"
for _, d := range validated.rejected {
if d.Disposition != validators.DispositionUnavailable {
branch.Outcome = "rejected"
break
return nil, nil
}
}
break
return nil, nil
})
if exhausted {
branch.Outcome = "exhausted"
}
if coordinationErr != nil {
branches = append(branches, branch)
return finish(coordinationErr)
}
branches = append(branches, branch)
}
return finish(nil)