Files
notarius/internal/framework/pipeline/runner_boundaries.go

223 lines
7.4 KiB
Go
Raw Normal View History

package pipeline
import (
"context"
"fmt"
"time"
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
"gitea.maximumdirect.net/eric/notarius/internal/framework/chunkmap"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
)
func prepareRunSource(ctx context.Context, input *RunInput) (*source.SourceDocument, error) {
debugRecorder := input.Debug
adapter := input.Prepared.input
var doc *source.SourceDocument
sourceStarted := time.Now().UTC()
if debugRecorder.Enabled() {
if err := writeDebugTimed(debugRecorder, "source/input.json", debugTimedEnvelope{
Stage: "source",
ModuleKey: adapter.Key(),
StartedAt: sourceStarted,
Payload: debugSourceInput{
SourceID: input.SourceID,
Path: input.Path,
Raw: debugContentEnvelope(input.RawInput, sourceInputMediaType(input.Path), nil, nil),
Metadata: input.Metadata,
},
}); err != nil {
return nil, fmt.Errorf("write source debug artifact: %w", err)
}
}
requestMetadata, err := cloneMetadata(input.Metadata)
if err != nil {
return nil, fmt.Errorf("clone input adapter metadata: %w", err)
}
if err := ctx.Err(); err != nil {
return nil, err
}
doc, err = adapter.Parse(ctx, contracts.ParseRequest{
SourceID: input.SourceID, Path: input.Path, Raw: input.RawInput,
LLMProfile: input.pipeline.Input.LLMProfile, Metadata: requestMetadata,
})
if ctxErr := ctx.Err(); ctxErr != nil {
return nil, ctxErr
}
if err != nil {
return nil, fmt.Errorf("parse input with adapter %q: %w", adapter.Key(), err)
}
if err := source.ValidateDocument(doc); err != nil {
return nil, fmt.Errorf("validate source document: %w", err)
}
index := source.NewDocumentIndex(doc)
input.sourceIndex = &index
if debugRecorder.Enabled() {
if err := writeDebugTimed(debugRecorder, "source/output.json", debugTimedEnvelope{
Stage: "source",
ModuleKey: adapter.Key(),
StartedAt: sourceStarted,
Payload: map[string]any{
"document": debugSourceDocumentEnvelope(doc),
},
}); err != nil {
return nil, fmt.Errorf("write source debug artifact: %w", err)
}
}
return doc, nil
}
func (r *Runner) prepareRunChunks(ctx context.Context, input RunInput, doc *source.SourceDocument, sourceInput contracts.LLMInputMaterial, sessionID string, output *runState) (chunkResult chunkPlanExecution, acceptedChunkMap *contracts.SerializedArtifact, err error) {
debugRecorder := input.Debug
chunker := input.Prepared.chunker
chunkStarted := time.Now().UTC()
if debugRecorder.Enabled() {
if err := writeDebugTimed(debugRecorder, "chunk/input.json", debugTimedEnvelope{
Stage: string(StageChunk),
ModuleKey: chunker.Key(),
StartedAt: chunkStarted,
Payload: map[string]any{
"cache_mode": input.pipeline.ChunkControls.Cache,
"source": input.sourceEvidence.parsedReference(),
"source_input": input.sourceEvidence.inputReference(),
"metadata": input.Metadata,
},
}); err != nil {
return chunkResult, acceptedChunkMap, fmt.Errorf("write chunk debug artifact: %w", err)
}
}
chunkResult, err = r.runChunkPlan(ctx, input, doc, sourceInput, sessionID)
appendDiagnosticGroups(output, chunkResult.diagnostics)
if err != nil {
return chunkResult, acceptedChunkMap, err
}
if debugRecorder.Enabled() {
chunkDebugPayload := map[string]any{
"cache_mode": input.pipeline.ChunkControls.Cache,
"accepted": chunkResult.accepted,
"materialized_chunks": debugSourceChunkEnvelopes(chunkResult.chunks),
}
if chunkResult.plan != nil {
chunkDebugPayload["plan"] = debugChunkPlanEnvelope(*chunkResult.plan)
}
if err := writeDebugTimed(debugRecorder, "chunk/output.json", debugTimedEnvelope{
Stage: string(StageChunk),
ModuleKey: chunker.Key(),
StartedAt: chunkStarted,
Payload: chunkDebugPayload,
}); err != nil {
return chunkResult, acceptedChunkMap, fmt.Errorf("write chunk debug artifact: %w", err)
}
}
if chunkResult.accepted {
if chunkResult.plan == nil {
return chunkResult, acceptedChunkMap, fmt.Errorf("accepted chunk plan is missing")
}
chunkProfile := ""
if input.pipeline.ChunkExecutionClass == contracts.ExecutionClassLLMBacked {
chunkProfile = input.pipeline.Chunk.LLMProfile
}
artifact, buildErr := chunkmap.Serialize(chunkmap.BuildRequest{
Source: doc,
Plan: *chunkResult.plan,
Chunks: chunkResult.chunks,
RequestedChunker: input.pipeline.Chunk.Module,
Producer: chunkmap.Producer{
InputModule: input.Prepared.input.Key(),
ChunkModule: chunker.Key(),
LLMProfile: chunkProfile,
},
})
if buildErr != nil {
return chunkResult, acceptedChunkMap, fmt.Errorf("serialize accepted chunk map: %w", buildErr)
}
acceptedChunkMap = &artifact
}
return chunkResult, acceptedChunkMap, nil
}
func encodeRunOutput(ctx context.Context, input RunInput, doc *source.SourceDocument, acceptedChunkMap *contracts.SerializedArtifact, output *runState) error {
debugRecorder := input.Debug
encoder := input.Prepared.output
if err := ctx.Err(); err != nil {
return err
}
outputStarted := time.Now().UTC()
evidenceArtifact, evidenceSummary, err := buildOutputEvidenceContext(input.Prepared, doc, output.normalizeOutputs, debugRecorder.Enabled())
if err != nil {
return err
}
if err := ctx.Err(); err != nil {
return err
}
if evidenceSummary != nil {
if err := writeDebugTimed(debugRecorder, "output/evidence-context.json", debugTimedEnvelope{
Stage: string(StageOutput),
ModuleKey: encoder.Key(),
StartedAt: outputStarted,
Payload: *evidenceSummary,
}); err != nil {
return fmt.Errorf("write evidence context debug artifact: %w", err)
}
if err := ctx.Err(); err != nil {
return err
}
}
if debugRecorder.Enabled() {
outputDebugPayload := map[string]any{
"normalize_outputs": debugSerializedOutputEnvelopes(output.normalizeOutputs),
"metadata": input.Metadata,
}
if acceptedChunkMap != nil {
outputDebugPayload["chunk_map"] = debugSerializedOutputEnvelope(contracts.SerializedOutput{Artifact: *acceptedChunkMap})
}
if err := writeDebugTimed(debugRecorder, "output/input.json", debugTimedEnvelope{
Stage: string(StageOutput),
ModuleKey: encoder.Key(),
StartedAt: outputStarted,
Payload: outputDebugPayload,
}); err != nil {
return fmt.Errorf("write output debug artifact: %w", err)
}
}
if err := ctx.Err(); err != nil {
return err
}
encoded, err := encoder.Encode(ctx, contracts.OutputRequest{
NormalizeOutputs: cloneSerializedOutputs(output.normalizeOutputs),
ChunkMap: contracts.CloneSerializedArtifactPointer(acceptedChunkMap),
EvidenceContext: contracts.CloneSerializedArtifactPointer(evidenceArtifact),
})
if ctxErr := ctx.Err(); ctxErr != nil {
return ctxErr
}
if err != nil {
return fmt.Errorf("encode output with encoder %q: %w", encoder.Key(), err)
}
files, err := outputFilesFromResult(encoded)
if err != nil {
return fmt.Errorf("validate output files from encoder %q: %w", encoder.Key(), err)
}
if debugRecorder.Enabled() {
if err := writeDebugTimed(debugRecorder, "output/output.json", debugTimedEnvelope{
Stage: string(StageOutput),
ModuleKey: encoder.Key(),
StartedAt: outputStarted,
Payload: map[string]any{
"files": debugOutputFiles(files),
},
}); err != nil {
return fmt.Errorf("write output debug artifact: %w", err)
}
}
if err := ctx.Err(); err != nil {
return err
}
output.OutputFiles = files
return nil
}