Skip debug projections when capture is disabled

This commit is contained in:
2026-11-26 01:49:29 +00:00
parent d0cc20c539
commit f49238c787
8 changed files with 144 additions and 99 deletions

View File

@@ -27,7 +27,7 @@ type debugEvidenceContextSummary struct {
// buildOutputEvidenceContext projects the prepared output policy from accepted
// normalized artifacts. It is intentionally separate from lane execution so
// cached normalized outputs use the same reconstruction path.
func buildOutputEvidenceContext(prepared *PreparedPipeline, doc *source.SourceDocument, outputs []contracts.SerializedOutput) (*contracts.SerializedArtifact, *debugEvidenceContextSummary, error) {
func buildOutputEvidenceContext(prepared *PreparedPipeline, doc *source.SourceDocument, outputs []contracts.SerializedOutput, includeSummary bool) (*contracts.SerializedArtifact, *debugEvidenceContextSummary, error) {
if prepared == nil || prepared.evidencePlan == nil {
return nil, nil, nil
}
@@ -88,6 +88,9 @@ func buildOutputEvidenceContext(prepared *PreparedPipeline, doc *source.SourceDo
MediaType: evidencecontext.MediaType,
Content: content,
}
if !includeSummary {
return contracts.CloneSerializedArtifactPointer(artifact), nil, nil
}
summary := debugEvidenceContextSummary{
ArtifactKind: artifact.Kind,
MediaType: artifact.MediaType,

View File

@@ -113,7 +113,8 @@ func TestEvidenceContextOmitsUnselectedAndAbsentLanes(t *testing.T) {
if err != nil {
t.Fatal(err)
}
result, _, err := buildOutputEvidenceContext(prepared, doc, []contracts.SerializedOutput{{LaneID: "present", SourceID: doc.ID, Artifact: artifact}, {LaneID: "unselected", SourceID: doc.ID, Artifact: artifact}})
outputs := []contracts.SerializedOutput{{LaneID: "present", SourceID: doc.ID, Artifact: artifact}, {LaneID: "unselected", SourceID: doc.ID, Artifact: artifact}}
result, summary, err := buildOutputEvidenceContext(prepared, doc, outputs, true)
if err != nil {
t.Fatal(err)
}
@@ -121,6 +122,10 @@ func TestEvidenceContextOmitsUnselectedAndAbsentLanes(t *testing.T) {
if err != nil || len(value) != 1 || value[0].ID != 1 {
t.Fatalf("evidence=%#v %v", value, err)
}
withoutSummary, absent, err := buildOutputEvidenceContext(prepared, doc, outputs, false)
if err != nil || absent != nil || summary == nil || !reflect.DeepEqual(result, withoutSummary) {
t.Fatalf("optional debug summary changed output: summary=%#v absent=%#v error=%v", summary, absent, err)
}
}
func TestRunnerEvidenceContextFailurePreventsOutputEncoding(t *testing.T) {
@@ -170,7 +175,7 @@ func TestBuildOutputEvidenceContextRejectsIncompatibleAcceptedOutputs(t *testing
if test.mutate != nil {
test.mutate(&outputs[0])
}
_, _, err := buildOutputEvidenceContext(prepared, doc, outputs)
_, _, err := buildOutputEvidenceContext(prepared, doc, outputs, true)
if err == nil || !strings.Contains(err.Error(), test.want) {
t.Fatalf("buildOutputEvidenceContext() error = %v, want %q", err, test.want)
}

View File

@@ -114,18 +114,18 @@ func (r *Runner) Run(ctx context.Context, input RunInput) (output RunOutput, err
return failOutput(output), fmt.Errorf("promote input diagnostics: %w", diagnosticErr)
}
appendDiagnosticGroups(&output, inputDiagnostics)
if err := writeDebugTimed(debugRecorder, "pipeline.json", debugTimedEnvelope{
Stage: "run",
StartedAt: startedTime(input.StartedAt),
Payload: map[string]any{
"pipeline_id": input.pipeline.ID,
"pipeline_digest": input.pipeline.Digest,
"run_id": input.RunID,
},
}); err != nil {
return failOutput(output), fmt.Errorf("write debug run artifact: %w", err)
}
if debugRecorder.Enabled() {
if err := writeDebugTimed(debugRecorder, "pipeline.json", debugTimedEnvelope{
Stage: "run",
StartedAt: startedTime(input.StartedAt),
Payload: map[string]any{
"pipeline_id": input.pipeline.ID,
"pipeline_digest": input.pipeline.Digest,
"run_id": input.RunID,
},
}); err != nil {
return failOutput(output), fmt.Errorf("write debug run artifact: %w", err)
}
if err := debugRecorder.WriteJSON("composition.json", captureComposition(input.pipeline)); err != nil {
return failOutput(output), &contracts.CaptureError{Cause: err}
}
@@ -134,18 +134,20 @@ func (r *Runner) Run(ctx context.Context, input RunInput) (output RunOutput, err
adapter := input.Prepared.input
var doc *source.SourceDocument
sourceStarted := time.Now().UTC()
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 failOutput(output), fmt.Errorf("write source debug artifact: %w", err)
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 failOutput(output), fmt.Errorf("write source debug artifact: %w", err)
}
}
requestMetadata, err := cloneMetadata(input.Metadata)
if err != nil {
@@ -170,53 +172,59 @@ func (r *Runner) Run(ctx context.Context, input RunInput) (output RunOutput, err
index := source.NewDocumentIndex(doc)
input.sourceIndex = &index
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 failOutput(output), fmt.Errorf("write source debug artifact: %w", err)
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 failOutput(output), fmt.Errorf("write source debug artifact: %w", err)
}
}
sourceInput := sourceInputMaterial(input.Path, input.RawInput)
chunker := input.Prepared.chunker
chunkStarted := time.Now().UTC()
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": debugSourceDocumentEnvelope(doc),
"source_input": debugContentEnvelope(sourceInput.Content, sourceInput.MediaType, nil, nil),
"metadata": input.Metadata,
},
}); err != nil {
return failOutput(output), fmt.Errorf("write chunk debug artifact: %w", err)
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": debugSourceDocumentEnvelope(doc),
"source_input": debugContentEnvelope(sourceInput.Content, sourceInput.MediaType, nil, nil),
"metadata": input.Metadata,
},
}); err != nil {
return failOutput(output), fmt.Errorf("write chunk debug artifact: %w", err)
}
}
chunkResult, err := r.runChunkPlan(ctx, input, doc, sourceInput, sessionID)
if err != nil {
return failOutput(output), err
}
appendDiagnosticGroups(&output, chunkResult.diagnostics)
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 failOutput(output), fmt.Errorf("write chunk debug artifact: %w", 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 failOutput(output), fmt.Errorf("write chunk debug artifact: %w", err)
}
}
var acceptedChunkMap *contracts.SerializedArtifact
@@ -267,7 +275,7 @@ func (r *Runner) Run(ctx context.Context, input RunInput) (output RunOutput, err
return failOutput(output), err
}
outputStarted := time.Now().UTC()
evidenceArtifact, evidenceSummary, err := buildOutputEvidenceContext(input.Prepared, doc, output.normalizeOutputs)
evidenceArtifact, evidenceSummary, err := buildOutputEvidenceContext(input.Prepared, doc, output.normalizeOutputs, debugRecorder.Enabled())
if err != nil {
return failOutput(output), err
}
@@ -287,20 +295,22 @@ func (r *Runner) Run(ctx context.Context, input RunInput) (output RunOutput, err
return failOutput(output), err
}
}
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 failOutput(output), fmt.Errorf("write output debug artifact: %w", 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 failOutput(output), fmt.Errorf("write output debug artifact: %w", err)
}
}
if err := ctx.Err(); err != nil {
return failOutput(output), err
@@ -320,15 +330,17 @@ func (r *Runner) Run(ctx context.Context, input RunInput) (output RunOutput, err
if err != nil {
return failOutput(output), fmt.Errorf("validate output files from encoder %q: %w", encoder.Key(), err)
}
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 failOutput(output), fmt.Errorf("write output debug artifact: %w", 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 failOutput(output), fmt.Errorf("write output debug artifact: %w", err)
}
}
if err := ctx.Err(); err != nil {
return failOutput(output), err

View File

@@ -26,6 +26,10 @@ func (r *Runner) runChunkPlan(ctx context.Context, input RunInput, doc *source.S
origin := contracts.DiagnosticOrigin{Stage: contracts.DiagnosticOriginStageChunk, ModuleKey: chunker.Key()}
execution := operationExecution(input, origin, nil)
defer func() {
// Project final chunks here only when an operation capture was requested.
if execution.Capture == nil {
return
}
errorText := ""
if err != nil {
errorText = err.Error()

View File

@@ -344,11 +344,13 @@ func (r *Runner) continueLane(ctx context.Context, input RunInput, doc *source.S
reuseEligible: state.reuseEligible,
}
local.diagnosticGroups = append(local.diagnosticGroups, contracts.CloneDiagnosticCollection(contracts.DiagnosticCollection{Groups: results.diagnostics}).Groups...)
if err := writeDebugTimed(input.Debug, path.Join("extract", fileio.EncodePathComponent(lane.ID), "input.json"), debugTimedEnvelope{Stage: string(StageExtract), StepID: input.stepID, LaneID: lane.ID, ModuleKey: lane.Extract.Module, StartedAt: time.Now().UTC(), Payload: map[string]any{"source": debugSourceDocumentEnvelope(doc), "chunks": debugSourceChunkEnvelopes(chunks), "metadata": input.Metadata}}); err != nil {
return local, &laneRunError{stage: StageExtract, err: err}
}
if err := writeDebugTimed(input.Debug, path.Join("extract", fileio.EncodePathComponent(lane.ID), "output.json"), debugTimedEnvelope{Stage: string(StageExtract), StepID: input.stepID, LaneID: lane.ID, ModuleKey: lane.Extract.Module, StartedAt: time.Now().UTC(), Payload: map[string]any{"outputs": debugExtractArtifacts(results.accepted)}}); err != nil {
return local, &laneRunError{stage: StageExtract, err: err}
if input.Debug != nil && input.Debug.Enabled() {
if err := writeDebugTimed(input.Debug, path.Join("extract", fileio.EncodePathComponent(lane.ID), "input.json"), debugTimedEnvelope{Stage: string(StageExtract), StepID: input.stepID, LaneID: lane.ID, ModuleKey: lane.Extract.Module, StartedAt: time.Now().UTC(), Payload: map[string]any{"source": debugSourceDocumentEnvelope(doc), "chunks": debugSourceChunkEnvelopes(chunks), "metadata": input.Metadata}}); err != nil {
return local, &laneRunError{stage: StageExtract, err: err}
}
if err := writeDebugTimed(input.Debug, path.Join("extract", fileio.EncodePathComponent(lane.ID), "output.json"), debugTimedEnvelope{Stage: string(StageExtract), StepID: input.stepID, LaneID: lane.ID, ModuleKey: lane.Extract.Module, StartedAt: time.Now().UTC(), Payload: map[string]any{"outputs": debugExtractArtifacts(results.accepted)}}); err != nil {
return local, &laneRunError{stage: StageExtract, err: err}
}
}
if len(results.accepted) == 0 {
return local, nil

View File

@@ -126,8 +126,10 @@ func (r *Runner) runMergeStage(ctx context.Context, input RunInput, doc *source.
}
state.reuseEligible = outcome.ReuseEligible
state.artifact = erasedMergeArtifact{LaneID: lane.ID, MergerKey: lane.Merge.Module, SourceID: doc.ID, Value: value}
if err := writeDebugTimed(input.Debug, path.Join("merge", fileio.EncodePathComponent(lane.ID), "output.json"), debugTimedEnvelope{Stage: string(StageMerge), StepID: input.stepID, LaneID: lane.ID, ModuleKey: lane.Merge.Module, StartedAt: time.Now().UTC(), Payload: map[string]any{"outcome": outcome, "output": debugSerializedOutputEnvelope(contracts.SerializedOutput{StepID: input.stepID, LaneID: lane.ID, SourceID: doc.ID, Artifact: state.serialized})}}); err != nil {
return state, err
if input.Debug != nil && input.Debug.Enabled() {
if err := writeDebugTimed(input.Debug, path.Join("merge", fileio.EncodePathComponent(lane.ID), "output.json"), debugTimedEnvelope{Stage: string(StageMerge), StepID: input.stepID, LaneID: lane.ID, ModuleKey: lane.Merge.Module, StartedAt: time.Now().UTC(), Payload: map[string]any{"outcome": outcome, "output": debugSerializedOutputEnvelope(contracts.SerializedOutput{StepID: input.stepID, LaneID: lane.ID, SourceID: doc.ID, Artifact: state.serialized})}}); err != nil {
return state, err
}
}
return state, nil
}
@@ -180,8 +182,10 @@ func (r *Runner) runNormalizeStage(ctx context.Context, input RunInput, doc *sou
}
state.reuseEligible = outcome.ReuseEligible
state.accepted = true
if err := writeDebugTimed(input.Debug, path.Join("normalize", fileio.EncodePathComponent(lane.ID), "output.json"), debugTimedEnvelope{Stage: string(StageNormalize), StepID: input.stepID, LaneID: lane.ID, ModuleKey: lane.Normalize.Module, StartedAt: time.Now().UTC(), Payload: map[string]any{"outcome": outcome, "output": debugSerializedOutputEnvelope(contracts.SerializedOutput{StepID: input.stepID, LaneID: lane.ID, SourceID: doc.ID, Artifact: state.serialized})}}); err != nil {
return state, err
if input.Debug != nil && input.Debug.Enabled() {
if err := writeDebugTimed(input.Debug, path.Join("normalize", fileio.EncodePathComponent(lane.ID), "output.json"), debugTimedEnvelope{Stage: string(StageNormalize), StepID: input.stepID, LaneID: lane.ID, ModuleKey: lane.Normalize.Module, StartedAt: time.Now().UTC(), Payload: map[string]any{"outcome": outcome, "output": debugSerializedOutputEnvelope(contracts.SerializedOutput{StepID: input.stepID, LaneID: lane.ID, SourceID: doc.ID, Artifact: state.serialized})}}); err != nil {
return state, err
}
}
return state, nil
}