From b85467cc3cca37bc24a64d482b6fa127e16b2cfc Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Thu, 26 Nov 2026 02:17:09 +0000 Subject: [PATCH] Separate runner source chunk and output responsibilities --- docs/internal/pipeline.md | 8 +- docs/internal/state.md | 3 +- docs/roadmap/audit/catalog.md | 5 + docs/roadmap/implementation.md | 6 + internal/framework/pipeline/runner.go | 191 +-------------- .../framework/pipeline/runner_boundaries.go | 222 ++++++++++++++++++ 6 files changed, 243 insertions(+), 192 deletions(-) create mode 100644 internal/framework/pipeline/runner_boundaries.go diff --git a/docs/internal/pipeline.md b/docs/internal/pipeline.md index 65c03ac6..a30164e7 100644 --- a/docs/internal/pipeline.md +++ b/docs/internal/pipeline.md @@ -158,9 +158,11 @@ run semantic assessment. Deterministic work has no synthetic request/cache key. ## Output And Capture Boundaries -[Runner](../../internal/framework/pipeline/runner.go) parses/validates source, -materializes the final plan, runs ordered steps, aggregates diagnostics, and -builds optional chunk-map/evidence values. Normalized values are codec-checked +[Runner](../../internal/framework/pipeline/runner.go) coordinates source +preparation, chunk planning, ordered steps, diagnostic finalization, and logical +output encoding. Its [private collaborators](../../internal/framework/pipeline/runner_boundaries.go) +own source parsing/indexing, chunk-map construction, and output/evidence assembly. +Normalized values are codec-checked before handoff/output. Evidence reconstruction also works from reused artifacts; it projects selected citations in source order, not a second typed output channel. [Evidence internals](../../internal/framework/pipeline/evidence_output.go) and diff --git a/docs/internal/state.md b/docs/internal/state.md index 28b4d6bf..4be717f4 100644 --- a/docs/internal/state.md +++ b/docs/internal/state.md @@ -92,7 +92,8 @@ historic execution claim. ## Artifact Publication And Native Reporting The runner returns logical deliverable files only after successful execution/ -encoding. The [JSON encoder](../../internal/modules/generic/output/json/encoder.go) +encoding; its [logical output collaborator](../../internal/framework/pipeline/runner_boundaries.go) +does not publish them. The [JSON encoder](../../internal/modules/generic/output/json/encoder.go) owns domain-file selection and optional auxiliary inclusion. [JSON output](../integrations/json-output.md), [chunk maps](../integrations/chunk-map.md), and diff --git a/docs/roadmap/audit/catalog.md b/docs/roadmap/audit/catalog.md index c34a2c3c..2517fd2e 100644 --- a/docs/roadmap/audit/catalog.md +++ b/docs/roadmap/audit/catalog.md @@ -470,6 +470,11 @@ one universal normalizer or permission to combine evidence ranges. ### CAT-022 — Runner Stage Flow Is Obscured By Evidence And Output Assembly +**Status:** Resolved. The runner coordinates source preparation, chunk planning, +ordered steps, single diagnostic finalization, and logical output explicitly; +private collaborators own parsing, chunk-map construction, and output/evidence +assembly. Existing pipeline, integration, CLI, and full verification checks pass. + **Assessment:** Local maintainability opportunity; original medium. **Source:** [CPLX-005](03-complexity.md#cplx-005--runner-stages-are-obscured-by-debug-and-output-assembly). diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 44acccd9..904f2634 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -570,6 +570,12 @@ loss, shared mutation, or premature retirement. ## Stage 19 — Expose Cohesive Runner Phases +**Completed.** The runner now delegates source parsing/indexing, chunk planning +and map creation, and logical output/evidence encoding to private collaborators; +ordered steps, cancellation, and single finalization remain explicit. Existing +pipeline, integration, CLI, full test, vet, and build checks pass. Physical +publication remains CLI-owned. + **Coverage:** CAT-022 completion. **Prerequisites:** Stages 12–18. Extract same-package source parse/validate, chunk/materialization/map, and logical diff --git a/internal/framework/pipeline/runner.go b/internal/framework/pipeline/runner.go index 6f54d6e2..c8c68167 100644 --- a/internal/framework/pipeline/runner.go +++ b/internal/framework/pipeline/runner.go @@ -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 } diff --git a/internal/framework/pipeline/runner_boundaries.go b/internal/framework/pipeline/runner_boundaries.go new file mode 100644 index 00000000..150e2582 --- /dev/null +++ b/internal/framework/pipeline/runner_boundaries.go @@ -0,0 +1,222 @@ +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 +}