Separate runner source chunk and output responsibilities
This commit is contained in:
@@ -13,7 +13,6 @@ import (
|
||||
"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"
|
||||
)
|
||||
|
||||
@@ -161,127 +160,16 @@ func (r *Runner) run(ctx context.Context, input RunInput) (output runState, err
|
||||
}
|
||||
}
|
||||
|
||||
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 output, fmt.Errorf("write source debug artifact: %w", err)
|
||||
}
|
||||
}
|
||||
requestMetadata, err := cloneMetadata(input.Metadata)
|
||||
doc, err := prepareRunSource(ctx, &input)
|
||||
if err != nil {
|
||||
return output, fmt.Errorf("clone input adapter metadata: %w", err)
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return output, 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 output, ctxErr
|
||||
}
|
||||
if err != nil {
|
||||
return output, fmt.Errorf("parse input with adapter %q: %w", adapter.Key(), err)
|
||||
}
|
||||
if err := source.ValidateDocument(doc); err != nil {
|
||||
return output, 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 output, fmt.Errorf("write source debug artifact: %w", err)
|
||||
}
|
||||
}
|
||||
sourceInput := sourceInputMaterial(input.Path, input.RawInput)
|
||||
|
||||
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 output, fmt.Errorf("write chunk debug artifact: %w", err)
|
||||
}
|
||||
}
|
||||
chunkResult, err := r.runChunkPlan(ctx, input, doc, sourceInput, sessionID)
|
||||
appendDiagnosticGroups(&output, chunkResult.diagnostics)
|
||||
chunkResult, acceptedChunkMap, err := r.prepareRunChunks(ctx, input, doc, sourceInput, sessionID, &output)
|
||||
if err != nil {
|
||||
return output, 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 output, fmt.Errorf("write chunk debug artifact: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
var acceptedChunkMap *contracts.SerializedArtifact
|
||||
if chunkResult.accepted {
|
||||
if chunkResult.plan == nil {
|
||||
return output, 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: adapter.Key(),
|
||||
ChunkModule: chunker.Key(),
|
||||
LLMProfile: chunkProfile,
|
||||
},
|
||||
})
|
||||
if buildErr != nil {
|
||||
return output, fmt.Errorf("serialize accepted chunk map: %w", buildErr)
|
||||
}
|
||||
acceptedChunkMap = &artifact
|
||||
}
|
||||
|
||||
if chunkResult.accepted {
|
||||
input.reuseIneligible = !chunkResult.outcome.ReuseEligible
|
||||
@@ -300,82 +188,9 @@ func (r *Runner) run(ctx context.Context, input RunInput) (output runState, err
|
||||
return output, err
|
||||
}
|
||||
|
||||
encoder := input.Prepared.output
|
||||
if err := ctx.Err(); err != nil {
|
||||
if err := encodeRunOutput(ctx, input, doc, acceptedChunkMap, &output); err != nil {
|
||||
return output, err
|
||||
}
|
||||
outputStarted := time.Now().UTC()
|
||||
evidenceArtifact, evidenceSummary, err := buildOutputEvidenceContext(input.Prepared, doc, output.normalizeOutputs, debugRecorder.Enabled())
|
||||
if err != nil {
|
||||
return output, err
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return output, 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 output, fmt.Errorf("write evidence context debug artifact: %w", err)
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return output, 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 output, fmt.Errorf("write output debug artifact: %w", err)
|
||||
}
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return output, 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 output, ctxErr
|
||||
}
|
||||
if err != nil {
|
||||
return output, fmt.Errorf("encode output with encoder %q: %w", encoder.Key(), err)
|
||||
}
|
||||
files, err := outputFilesFromResult(encoded)
|
||||
if err != nil {
|
||||
return output, 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 output, fmt.Errorf("write output debug artifact: %w", err)
|
||||
}
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return output, err
|
||||
}
|
||||
output.OutputFiles = files
|
||||
|
||||
return output, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user