116 lines
4.6 KiB
Go
116 lines
4.6 KiB
Go
|
|
package pipeline
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||
|
|
)
|
||
|
|
|
||
|
|
func captureOperationInputs(execution contracts.ExecutionOptions, target typedValidationTarget, binding ModuleBinding, incoming []contracts.SerializedArtifact) error {
|
||
|
|
if execution.Capture == nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
references := make(map[string]any, len(target.references.Slots))
|
||
|
|
for name, slot := range target.references.Slots {
|
||
|
|
items := make([]any, 0, len(slot.Items))
|
||
|
|
for _, item := range slot.Items {
|
||
|
|
items = append(items, map[string]any{"reference": item, "content": debugContentEnvelope(item.Content, item.MediaType, nil, nil)})
|
||
|
|
}
|
||
|
|
references[name] = map[string]any{"declaration": slot.Slot, "items": items}
|
||
|
|
}
|
||
|
|
inputs := make([]debugSerializedOutput, len(incoming))
|
||
|
|
for index, artifact := range incoming {
|
||
|
|
inputs[index] = debugSerializedOutputEnvelope(contracts.SerializedOutput{Artifact: artifact})
|
||
|
|
}
|
||
|
|
var chunk any
|
||
|
|
if target.chunk != nil {
|
||
|
|
chunk = debugSourceChunkEnvelope(*target.chunk)
|
||
|
|
}
|
||
|
|
return execution.RecordEvidence("inputs.json", map[string]any{
|
||
|
|
"source": debugSourceDocumentEnvelope(target.source),
|
||
|
|
"source_input": debugContentEnvelope(target.sourceInput.Content, target.sourceInput.MediaType, nil, nil),
|
||
|
|
"chunk": chunk, "chunks": debugSourceChunkEnvelopes(target.chunks), "references": references,
|
||
|
|
"incoming_artifacts": inputs, "module": binding.Module, "llm_profile": binding.LLMProfile, "metadata": target.metadata,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
// Configured options belong to ConfigKit's secret-aware snapshot, not another
|
||
|
|
// direct serialization of runtime configuration inside composition evidence.
|
||
|
|
func captureComposition(resolved ResolvedPipeline) ResolvedPipeline {
|
||
|
|
resolved = cloneResolvedPipeline(resolved)
|
||
|
|
resolved.Input, resolved.Chunk, resolved.Output = captureBinding(resolved.Input), captureBinding(resolved.Chunk), captureBinding(resolved.Output)
|
||
|
|
for stepIndex := range resolved.Steps {
|
||
|
|
for laneIndex := range resolved.Steps[stepIndex].ArtifactLanes {
|
||
|
|
lane := &resolved.Steps[stepIndex].ArtifactLanes[laneIndex]
|
||
|
|
lane.Extract, lane.Merge, lane.Normalize = captureBinding(lane.Extract), captureBinding(lane.Merge), captureBinding(lane.Normalize)
|
||
|
|
for index := range lane.Validators {
|
||
|
|
lane.Validators[index] = captureBinding(lane.Validators[index])
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
for index := range resolved.ValidatorChains {
|
||
|
|
for validatorIndex := range resolved.ValidatorChains[index].Validators {
|
||
|
|
resolved.ValidatorChains[index].Validators[validatorIndex].Binding = captureBinding(resolved.ValidatorChains[index].Validators[validatorIndex].Binding)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return resolved
|
||
|
|
}
|
||
|
|
|
||
|
|
func captureBinding(binding ModuleBinding) ModuleBinding {
|
||
|
|
binding.Options = nil
|
||
|
|
for index := range binding.Validators.Validators {
|
||
|
|
binding.Validators.Validators[index] = captureBinding(binding.Validators.Validators[index])
|
||
|
|
}
|
||
|
|
return binding
|
||
|
|
}
|
||
|
|
|
||
|
|
func captureOperationResult(execution contracts.ExecutionOptions, artifact contracts.SerializedArtifact, outcome contracts.ProducerOutcome, failure error) error {
|
||
|
|
if execution.Capture == nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
var value any
|
||
|
|
if artifact.Content != nil {
|
||
|
|
value = debugSerializedOutputEnvelope(contracts.SerializedOutput{Artifact: artifact})
|
||
|
|
}
|
||
|
|
errorText := ""
|
||
|
|
if failure != nil {
|
||
|
|
errorText = failure.Error()
|
||
|
|
}
|
||
|
|
return execution.RecordEvidence("outcome.json", map[string]any{"outcome": outcome, "artifact": value, "error": errorText})
|
||
|
|
}
|
||
|
|
|
||
|
|
func captureCacheObservation(writer contracts.EvidenceWriter, observation contracts.CacheObservation) error {
|
||
|
|
if writer == nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
errorText := ""
|
||
|
|
if observation.Err != nil {
|
||
|
|
errorText = observation.Err.Error()
|
||
|
|
}
|
||
|
|
if err := writer.WriteJSON("cache/"+observation.Decision+".json", map[string]any{
|
||
|
|
"decision": observation.Decision, "message_content_hash": observation.MessageContentHash,
|
||
|
|
"error": errorText,
|
||
|
|
}); err != nil {
|
||
|
|
return &contracts.CaptureError{Cause: err}
|
||
|
|
}
|
||
|
|
if observation.Artifact != nil {
|
||
|
|
if err := writer.WriteBytes("cache/"+observation.Decision+"-artifact.json", observation.Artifact); err != nil {
|
||
|
|
return &contracts.CaptureError{Cause: fmt.Errorf("record cached artifact: %w", err)}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func assessmentEvidence(assessment contracts.CandidateAssessment) []any {
|
||
|
|
findings := make([]any, 0, len(assessment.Validators))
|
||
|
|
for _, finding := range assessment.Validators {
|
||
|
|
errorText := ""
|
||
|
|
if finding.Failure != nil {
|
||
|
|
errorText = finding.Failure.Error()
|
||
|
|
}
|
||
|
|
findings = append(findings, map[string]any{"validator": finding.ValidatorName, "decision": finding.Decision,
|
||
|
|
"result": finding.Result, "correction_guidance": finding.Result.CorrectionGuidance, "error": errorText})
|
||
|
|
}
|
||
|
|
return findings
|
||
|
|
}
|