Files
audita/internal/framework/runner/section_evidence.go

145 lines
4.3 KiB
Go

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 i, build := range s.builders {
paths, _ := s.InteractionSink.WriteInteraction(build(offset, s.responsePath))
s.written = append(s.written, paths)
s.builders[i] = nil
}
s.builders = nil
}
// repairHistory shares the append-only prefix of prepared repair requests.
// Earlier snapshots keep their own lengths while additional turns add messages.
type repairHistory struct {
messages []structuredoutput.Message
}
func (h *repairHistory) retain(prepared *structuredoutput.PreparedSnapshot) *structuredoutput.PreparedSnapshot {
if prepared == nil {
return nil
}
messages := prepared.Messages
n := len(h.messages)
if n == 0 || n > len(messages) || !slices.EqualFunc(h.messages, messages[:n], sameMessage) {
h.messages = messages
} else {
h.messages = append(h.messages, messages[n:]...)
}
snapshot := *prepared
snapshot.Messages = slices.Clip(h.messages)
return &snapshot
}
func sameMessage(a, b structuredoutput.Message) bool {
if a.Role != b.Role || a.Content != b.Content {
return false
}
if a.CacheControl == nil || b.CacheControl == nil {
return a.CacheControl == b.CacheControl
}
return *a.CacheControl == *b.CacheControl
}
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
}