diff --git a/docs/roadmap/audit/catalog.md b/docs/roadmap/audit/catalog.md index fd5b49c2..a21b7ad4 100644 --- a/docs/roadmap/audit/catalog.md +++ b/docs/roadmap/audit/catalog.md @@ -20,8 +20,8 @@ The [Core And Integration API Roadmap](../core-integration-api.md) established the core/integration boundary. The reassessment below distinguishes its completed work from the remaining audit remediation. -The **45 original finding entries** reduce to **21 outstanding catalog entries -and thirteen resolved entries**. Repeated responsibility findings are +The **45 original finding entries** reduce to **20 outstanding catalog entries +and fourteen resolved entries**. Repeated responsibility findings are merged. Regression gaps are attached to the defects they protect rather than counted as additional runtime bugs. In particular, TEST-001, TEST-002, and TEST-004 each cover several distinct defects; TEST-003's native-fixture oracle @@ -65,7 +65,7 @@ Original P2/P3 ratings and relative refactoring priorities are retained for context; they are not one universal severity scale. No audit establishes a P1 incident or a measured end-to-end production speedup. -CAT-001, CAT-002, CAT-004, CAT-005, CAT-007, CAT-011, CAT-012, CAT-013, CAT-015, +CAT-001, CAT-002, CAT-003, CAT-004, CAT-005, CAT-007, CAT-011, CAT-012, CAT-013, CAT-015, CAT-016, CAT-027, CAT-032, and CAT-034 are resolved; the other entries remain outstanding. The original reports and their baseline observations are historical. @@ -76,7 +76,7 @@ may share files without representing the same underlying issue; they should be coordinated rather than silently treated as duplicates. **Migration overlap:** Lazy [operation evidence](../../../internal/framework/pipeline/operation.go) -addresses part of CAT-003, not all run-wide capture allocation. Core operation +and guarded run-wide projections resolve CAT-003. Core operation ownership and the [internal guides](../../internal/modules.md) clarify portions of CAT-014/017/018/022/025/032, without completing their broader layout, copying, runner, writer-obligation, or package-cohesion work. CAT-006/008/009 @@ -123,6 +123,13 @@ No new timestamp-magnitude restriction is needed. **Assessment:** Source-established resource-efficiency weakness; original P2. +**Status:** Resolved. Guards precede source, input, chunk-plan and deferred chunk +outcome, lane, merge, normalize, output, and evidence-summary projections. The +operation input/result, candidate assessment, cache observation, and native +evaluation capture paths were inspected and already guard their projections. +Mandatory artifact encoding remains unconditional; focused tests cover lazy +evaluation and enabled, cached, fallback, and failing capture paths. + **Source:** [RES-001](05-resources.md#res-001--disabled-capture-still-constructs-large-evidence-payloads). Ordinary runs construct text/base64, chunk, artifact, and output envelopes before diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 175820f1..1e3b18cd 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -392,6 +392,14 @@ these packages under `-race`. Update overview/LLM/state package pointers. ## Stage 12 — Avoid Disabled-Capture Construction +**Completed.** Run-wide source, chunk, lane, artifact, output, and evidence-summary +projections are constructed only for enabled debug capture; deferred chunk +outcome projection is guarded by the operation's effective capture writer. +Existing operation-input, result, assessment, and evaluation projections were +inspected and already guard construction. Mandatory artifact serialization is +unchanged. Focused projection, pipeline, capture, and integration tests pass; +subsequent source-sharing and runner decomposition must retain these guards. + **Coverage:** CAT-003. **Prerequisites:** Stage 11. Inspect debug.go, runner.go, runner_chunk_plan.go, runner_concurrent.go, diff --git a/internal/framework/pipeline/evidence_output.go b/internal/framework/pipeline/evidence_output.go index 56518970..c7089004 100644 --- a/internal/framework/pipeline/evidence_output.go +++ b/internal/framework/pipeline/evidence_output.go @@ -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, diff --git a/internal/framework/pipeline/evidence_output_test.go b/internal/framework/pipeline/evidence_output_test.go index e4156057..eafdf6ac 100644 --- a/internal/framework/pipeline/evidence_output_test.go +++ b/internal/framework/pipeline/evidence_output_test.go @@ -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) } diff --git a/internal/framework/pipeline/runner.go b/internal/framework/pipeline/runner.go index d3df3c77..ef0ff6ef 100644 --- a/internal/framework/pipeline/runner.go +++ b/internal/framework/pipeline/runner.go @@ -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 diff --git a/internal/framework/pipeline/runner_chunk_plan.go b/internal/framework/pipeline/runner_chunk_plan.go index 7a5052f5..915ac8a7 100644 --- a/internal/framework/pipeline/runner_chunk_plan.go +++ b/internal/framework/pipeline/runner_chunk_plan.go @@ -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() diff --git a/internal/framework/pipeline/runner_concurrent.go b/internal/framework/pipeline/runner_concurrent.go index 98a42898..77d17b25 100644 --- a/internal/framework/pipeline/runner_concurrent.go +++ b/internal/framework/pipeline/runner_concurrent.go @@ -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 diff --git a/internal/framework/pipeline/runner_typed.go b/internal/framework/pipeline/runner_typed.go index 9975f892..1d89b889 100644 --- a/internal/framework/pipeline/runner_typed.go +++ b/internal/framework/pipeline/runner_typed.go @@ -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 }