diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 3521906..381c7d7 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -177,6 +177,8 @@ container sensitivity. Verify no invented defaults, inheritance or decoding. ## Stage 4 — Support decoded and resolver-entry history prefixes +**Complete.** + Refactor preparePostDecodeOverrideContext so a successful decoded candidate can be inspected even when a later override batch fails. Reuse metadataFromDecoded, source/inheritance prefix observation and the pre-CLI checkpoint. Do not add CLI diff --git a/internal/engine/failure_inspection_test.go b/internal/engine/failure_inspection_test.go new file mode 100644 index 0000000..1245e86 --- /dev/null +++ b/internal/engine/failure_inspection_test.go @@ -0,0 +1,79 @@ +package engine + +import ( + "bytes" + "errors" + "strings" + "testing" +) + +func TestFailedLoadRetainsDecodedOrResolverEntry(t *testing.T) { + type settings struct { + Count int `yaml:"count"` + Label string `yaml:"label"` + } + tests := []struct { + name string + overrides []Override + resolve func(*settings, *Resolution) error + checkpoint InspectionStage + wantCount string + wantLabel string + }{ + { + name: "failed override batch", + overrides: []Override{ + {Path: Keys("label"), Value: "changed", Flag: "label"}, + {Path: Keys("count"), Value: "invalid", Flag: "count"}, + }, + checkpoint: InspectionStageDecoded, wantCount: "count: 3", wantLabel: "label: original", + }, + { + name: "resolver error", + overrides: []Override{{Path: Keys("label"), Value: "cli", Flag: "label"}}, + resolve: func(value *settings, _ *Resolution) error { + value.Label = "failed-edit" + return errors.New("reject") + }, + checkpoint: InspectionStageOverridden, wantCount: "count: 3", wantLabel: "label: cli", + }, + { + name: "unrecorded resolver mutation", + overrides: []Override{{Path: Keys("label"), Value: "cli", Flag: "label"}}, + resolve: func(value *settings, _ *Resolution) error { + value.Label = "failed-edit" + return nil + }, + checkpoint: InspectionStageOverridden, wantCount: "count: 3", wantLabel: "label: cli", + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + result, err := testLoad(Bytes("input.yml", []byte("count: 3\nlabel: original\n")), Options[settings]{ + Definition: Definition[settings]{Resolve: test.resolve}, Overrides: test.overrides, + }) + var failure *LoadError + if result != nil || !errors.As(err, &failure) || failure.Inspection() == nil { + t.Fatalf("failed load: result=%v error=%v", result, err) + } + view, ok := NewInspectionView(failure.Inspection(), Path{}) + if !ok || view.Stage() != test.checkpoint { + t.Fatalf("inspection checkpoint = %q, available=%v", view.Stage(), ok) + } + var output bytes.Buffer + if err := view.WriteEffective(&output); err != nil { + t.Fatal(err) + } + if !strings.Contains(output.String(), test.wantCount) || !strings.Contains(output.String(), test.wantLabel) || strings.Contains(output.String(), "failed-edit") { + t.Fatalf("wrong entry values: %q", output.String()) + } + info, found := view.Inspection().Metadata().Lookup(Keys("label")) + if !found || (test.checkpoint == InspectionStageOverridden && info.Origin.Layer != LayerCLI) || (test.checkpoint == InspectionStageDecoded && info.Origin.Layer != LayerMain) { + t.Fatalf("wrong label origin: %#v", info) + } + if explanation, found := view.Inspection().Explain(Keys("label")); !found || len(explanation.History) == 0 { + t.Fatalf("missing entry history: %#v", explanation) + } + }) + } +} diff --git a/internal/engine/load.go b/internal/engine/load.go index 03a5419..404d1dd 100644 --- a/internal/engine/load.go +++ b/internal/engine/load.go @@ -328,13 +328,16 @@ func executeLoadRequest[T any](request loadRequest[T], secrets SecretBinding, lo for _, issue := range issues { accumulator.add(diagnosticFromDecodeIssue(issue)) } - return nil, accumulator.loadError() + metadata := metadataFromDecodedInput(input, decoded) + inspection, _ := preparePrefixInspection(metadata, decoded.final, historyPhasePreCLI, newRetainedNodeCounter(historyLimit)) + return nil, finishFailureInspection(schema, request.definition.Secret, input, inspection, &accumulator, InspectionStageDecoded) } postDecode := preparePostDecodeOverrideContext(input, decoded, assigned) resolved, diagnostics := resolveLoadInput(schema, request.definition, input, postDecode, request.sequencePolicy) accumulator.add(diagnostics...) if hasErrors(diagnostics) { - return nil, accumulator.loadError() + inspection, _ := preparePrefixInspection(postDecode.metadata, postDecode.decoded.final, historyPhasePostCLI, newRetainedNodeCounter(historyLimit)) + return nil, finishFailureInspection(schema, request.definition.Secret, input, inspection, &accumulator, InspectionStageOverridden) } counter := newRetainedNodeCounter(historyLimit) inspection, admitted := prepareInspectionHistory(resolved, counter) diff --git a/internal/engine/load_result.go b/internal/engine/load_result.go index 9d2bbcb..757a65e 100644 --- a/internal/engine/load_result.go +++ b/internal/engine/load_result.go @@ -142,6 +142,29 @@ func prepareSourceInspection(input assembledInput, counter *retainedNodeCounter) return inspection, true } +func preparePrefixInspection(metadata Metadata, final *document.Node, observation historyPhase, counter *retainedNodeCounter) (*Inspection, bool) { + sealMetadataHistory(&metadata, final, observation) + inspection := &Inspection{metadata: metadata, history: metadata.historyModel, final: final} + if !addInspectionHistory(counter, inspection) { + return nil, false + } + return inspection, true +} + +func finishFailureInspection(schema *schemaDescriptor, predicate func(Path) bool, input assembledInput, inspection *Inspection, accumulator *diagnosticAccumulator, checkpoint InspectionStage) *LoadError { + failure := accumulator.loadError() + if inspection == nil { + return failure + } + var evidence []*sourceVersionEvidence + if input.versionState != nil { + evidence = input.versionState.evidence + } + finishInspection(schema, predicate, evidence, input.contexts, inspection, nil, accumulator, failure, checkpoint) + failure.inspection = inspection + return failure +} + func publishLoadResult[T any](schema *schemaDescriptor, definition Definition[T], resolved resolvedLoad[T], inspection *Inspection, references map[string]string, accumulator *diagnosticAccumulator) (*Loaded[T], error) { // A read-only validator can replace a value with an equal borrowed object. // Detach without re-reading the environment or discarding validated state. diff --git a/internal/engine/override_context.go b/internal/engine/override_context.go index 1a86c00..34c2c5d 100644 --- a/internal/engine/override_context.go +++ b/internal/engine/override_context.go @@ -18,10 +18,8 @@ func preparePostDecodeOverrideContext(input assembledInput, before decodedConfig after.origins = cloneOrigins(assigned.origins) after.final = document.Clone(assigned.final) - metadata := metadataFromDecoded(before) + metadata := metadataFromDecodedInput(input, before) cliRecords := overrideMetadataRecords(before, assigned.overrides) - observeSourcePrefix(metadata.historyBuilder, input) - metadata.historyBuilder.observePreCLICheckpoint(before.final, before.origins) var postDecodeApps []historyApplication for _, override := range assigned.overrides { postDecodeApps = append(postDecodeApps, historyApplication{ @@ -68,6 +66,13 @@ func preparePostDecodeOverrideContext(input assembledInput, before decodedConfig } } +func metadataFromDecodedInput(input assembledInput, decoded decodedConfiguration) Metadata { + metadata := metadataFromDecoded(decoded) + observeSourcePrefix(metadata.historyBuilder, input) + metadata.historyBuilder.observePreCLICheckpoint(decoded.final, decoded.origins) + return metadata +} + func observeSourcePrefix(builder *historyBuilder, input assembledInput) { applications := structuralHistoryApplications(input) var structuralOverlays []overlayRecord