Bound concurrent section work and finalize proposal identities in order
This commit is contained in:
111
internal/framework/runner/section_evidence.go
Normal file
111
internal/framework/runner/section_evidence.go
Normal file
@@ -0,0 +1,111 @@
|
||||
package runner
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/structuredoutput"
|
||||
)
|
||||
|
||||
// Section validation can finish before preceding proposal counts are known.
|
||||
type sectionInteractions struct {
|
||||
structuredoutput.InteractionSink
|
||||
mu sync.Mutex
|
||||
builders []structuredoutput.IndexedInteraction
|
||||
written []structuredoutput.InteractionArtifacts
|
||||
}
|
||||
|
||||
const pendingInteractionPrefix = "\x00interaction:"
|
||||
|
||||
func (s *sectionInteractions) WriteIndexedInteraction(build structuredoutput.IndexedInteraction) (structuredoutput.InteractionArtifacts, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
receipt := pendingInteractionPrefix + strconv.Itoa(len(s.builders))
|
||||
s.builders = append(s.builders, build)
|
||||
return structuredoutput.InteractionArtifacts{ResponsePayloadPath: receipt}, nil
|
||||
}
|
||||
|
||||
func (s *sectionInteractions) write(offset int) {
|
||||
for _, build := range s.builders {
|
||||
paths, _ := s.InteractionSink.WriteInteraction(build(offset, s.responsePath))
|
||||
s.written = append(s.written, paths)
|
||||
}
|
||||
s.builders = nil
|
||||
}
|
||||
|
||||
func (s *sectionInteractions) artifacts(paths structuredoutput.InteractionArtifacts) structuredoutput.InteractionArtifacts {
|
||||
value, pending := strings.CutPrefix(paths.ResponsePayloadPath, pendingInteractionPrefix)
|
||||
if !pending {
|
||||
return paths
|
||||
}
|
||||
index, err := strconv.Atoi(value)
|
||||
if err != nil || index < 0 || index >= len(s.written) {
|
||||
return structuredoutput.InteractionArtifacts{}
|
||||
}
|
||||
return s.written[index]
|
||||
}
|
||||
|
||||
func (s *sectionInteractions) responsePath(path string) string {
|
||||
return s.artifacts(structuredoutput.InteractionArtifacts{ResponsePayloadPath: path}).ResponsePayloadPath
|
||||
}
|
||||
|
||||
func (r *sectionValidationResult) assignProposalIndices(offset int) {
|
||||
capture := r.interactions != nil
|
||||
if capture {
|
||||
r.interactions.write(offset)
|
||||
}
|
||||
for i := range r.approved {
|
||||
r.approved[i].ProposalIndex += offset
|
||||
}
|
||||
for i := range r.rejected {
|
||||
r.rejected[i].ProposalIndex += offset
|
||||
}
|
||||
assignDecisions := func(decisions []ValidatorDecisionRecord) {
|
||||
for i := range decisions {
|
||||
decisions[i].ProposalIndex += offset
|
||||
if capture {
|
||||
decisions[i].DiagnosticArtifactPath = r.interactions.responsePath(decisions[i].DiagnosticArtifactPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
assignDecisions(r.decisions)
|
||||
for i := range r.operations {
|
||||
op := &r.operations[i]
|
||||
op.Reference.ProposalIndex = structuredoutput.OffsetProposalIndex(op.Reference.ProposalIndex, offset)
|
||||
op.ProposalIndices = slices.Clone(op.ProposalIndices)
|
||||
for j := range op.ProposalIndices {
|
||||
op.ProposalIndices[j] += offset
|
||||
}
|
||||
op.Filtered = slices.Clone(op.Filtered)
|
||||
for j := range op.Filtered {
|
||||
op.Filtered[j].ProposalIndex += offset
|
||||
}
|
||||
}
|
||||
for i := range r.filtered {
|
||||
r.filtered[i].ProposalIndex += offset
|
||||
}
|
||||
for i := range r.repairs {
|
||||
repair := &r.repairs[i]
|
||||
repair.ProposalIndex += offset
|
||||
if capture {
|
||||
repair.Original.ProposalIndex += offset
|
||||
repair.Final.ProposalIndex += offset
|
||||
}
|
||||
for j := range repair.Attempts {
|
||||
attempt := &repair.Attempts[j]
|
||||
if attempt.Revision != nil {
|
||||
attempt.Revision.ProposalIndex += offset
|
||||
}
|
||||
assignDecisions(attempt.Decisions)
|
||||
if capture {
|
||||
attempt.Artifacts = r.interactions.artifacts(attempt.Artifacts)
|
||||
}
|
||||
}
|
||||
}
|
||||
if r.failure != nil && r.failure.Origin != nil {
|
||||
r.failure.Origin.ProposalIndex = structuredoutput.OffsetProposalIndex(r.failure.Origin.ProposalIndex, offset)
|
||||
}
|
||||
r.interactions = nil
|
||||
}
|
||||
Reference in New Issue
Block a user