Retain decoded and resolver entry inspection

This commit is contained in:
2026-12-05 16:00:18 +00:00
parent ca35af3a65
commit cc1cfcc017
5 changed files with 117 additions and 5 deletions

View File

@@ -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

View File

@@ -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)
}
})
}
}

View File

@@ -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)

View File

@@ -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.

View File

@@ -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