diff --git a/docs/roadmap/remediation.md b/docs/roadmap/remediation.md index 273a17c..a12ac97 100644 --- a/docs/roadmap/remediation.md +++ b/docs/roadmap/remediation.md @@ -9,7 +9,11 @@ whose production code is unchanged, and adds Findings 2 and 3 below. ## Finding 1 — Preserve the actual layered main checkpoint and its collection policy -**Status:** Open. **Priority:** P2. **Affected stages:** 3 and 6; regression +**Status:** Complete. Retained the policy-aware main checkpoint, protected it +from overlay mutation, and covered overlay/underlay/version failures and invalid +main identities. Focused tests and `./scripts/check.sh` passed. + +**Priority:** P2. **Affected stages:** 3 and 6; regression coverage belongs with the stage 8 integration cases. ### Relevant code diff --git a/facade_failure_checkpoint_test.go b/facade_failure_checkpoint_test.go new file mode 100644 index 0000000..834f2f0 --- /dev/null +++ b/facade_failure_checkpoint_test.go @@ -0,0 +1,76 @@ +package configkit_test + +import ( + "bytes" + "errors" + "strings" + "testing" + "testing/fstest" + + ck "go.maximumdirect.net/configkit" +) + +func TestFailureMainCheckpointUsesLowerCollectionPolicy(t *testing.T) { + files := fstest.MapFS{ + "main.yml": {Data: []byte("config: {imports: [{path: a.yml}, {path: b.yml}]}\n")}, + "a.yml": {Data: []byte("items: [{id: a, value: first}]\n")}, + "b.yml": {Data: []byte("items: [{id: b, value: second}]\n")}, + } + filesystem := ck.NewFilesystem("checkpoint", files) + main := ck.Input{Source: filesystem.File("main.yml"), Imports: true} + lower := ck.Underlay{Inputs: []ck.Input{{Source: ck.Bytes("lower", []byte("config:\n sequences: [{destination: [items], key: id}]\nitems: [{id: lower, value: base}]\n"))}}} + options := ck.LayeredOptions[map[string]any]{Underlays: []ck.Underlay{lower}, OverlayFilesystem: &ck.FilesystemContext{FS: &filesystem, Root: ".", BaseDir: "."}} + if _, err := ck.LoadLayered(main, options); err != nil { + t.Fatal(err) + } + for _, boundary := range []string{"overlay", "underlay", "version"} { + t.Run(boundary, func(t *testing.T) { + opts := options + switch boundary { + case "overlay": + opts.Overlay = "missing" + case "underlay": + opts.Underlays = append([]ck.Underlay{lower}, ck.Underlay{Inputs: []ck.Input{{Source: ck.Bytes("bad", []byte("[\n"))}}}) + case "version": + opts.Definition.Version = &ck.VersionRule{Path: ck.Keys("version"), Check: func(_ ck.Version, v *ck.Validation) error { v.Error(ck.Keys("version"), "rejected"); return nil }} + } + result, err := ck.LoadLayered(main, opts) + var failure *ck.LoadError + if result != nil || !errors.As(err, &failure) { + t.Fatalf("result=%v error=%v", result, err) + } + view, ok := failure.InspectSubtree(ck.Path{}) + if !ok || view.Stage() != ck.InspectionStageMain { + t.Fatalf("available=%v stage=%s", ok, view.Stage()) + } + var output bytes.Buffer + if err := view.WriteEffective(&output); err != nil { + t.Fatal(err) + } + if !strings.Contains(output.String(), "first") || !strings.Contains(output.String(), "second") || strings.Contains(output.String(), "base") { + t.Fatalf("wrong main snapshot: %s", &output) + } + for index, label := range []string{"a.yml", "b.yml"} { + path := ck.Keys("items").Index(index).Key("value") + info, found := view.Metadata().Lookup(path) + if !found || len(info.Origin.Locations) == 0 || len(view.Metadata().History(path)) == 0 { + t.Fatalf("missing evidence at %s", path) + } + source, found := view.Source(info.Origin.Locations[0].SourceID) + if !found || !strings.HasSuffix(source.Label, label) { + t.Fatalf("wrong source: %#v", source) + } + } + }) + } + for _, invalid := range []string{"items: [{id: a}, {id: a}]\n", "items: [{other: a}]\n"} { + _, err := ck.LoadLayered(ck.Input{Source: ck.Bytes("invalid", []byte(invalid))}, options) + var failure *ck.LoadError + if !errors.As(err, &failure) { + t.Fatal("expected composition failure") + } + if _, ok := failure.InspectSubtree(ck.Path{}); ok { + t.Fatal("failed main composition was presented as completed") + } + } +} diff --git a/internal/engine/load_input.go b/internal/engine/load_input.go index 8accd8b..481ebd6 100644 --- a/internal/engine/load_input.go +++ b/internal/engine/load_input.go @@ -6,6 +6,10 @@ import ( // assembledInput is the source/document handoff across schema and version checks. type assembledInput struct { + // Layered main evidence is retained before precedence mutates the assembly. + mainTier *composedDocument + mainTierAttempted bool + mainTierPolicy collectionPolicyScope source Source parsed parsedSource document *document.Node diff --git a/internal/engine/load_request.go b/internal/engine/load_request.go index 91066ad..a93b9cd 100644 --- a/internal/engine/load_request.go +++ b/internal/engine/load_request.go @@ -133,6 +133,9 @@ func assembleLayeredInput[T any](request loadRequest[T], input assembledInput, p return input, diagnostics } } + if assembly != nil { + input.mainTierPolicy = assembly.current.collectionPolicies + } } mainPresent := input.document != nil || input.composition != nil || len(input.acquisition) != 0 @@ -143,11 +146,13 @@ func assembleLayeredInput[T any](request loadRequest[T], input assembledInput, p lower = assembly.current.collectionPolicies } var diagnostics []Diagnostic + input.mainTierAttempted = true mainComposition, diagnostics = compositionForInputWithPolicies(input, lower, policy.sequencePolicy) if len(diagnostics) != 0 { return input, diagnostics } } + input.mainTier = mainComposition var versionDiagnostics []Diagnostic if request.sourceVersionMode == sourceVersionModeDefault { var mainRoot *document.Node @@ -190,7 +195,7 @@ func assembleLayeredInput[T any](request loadRequest[T], input assembledInput, p assembly.current.inputBytes = state.accounting.bytes assembly.current.inputNodes = state.accounting.nodes if activeOverlay(request.overlay) { - if assembly.current == input.composition { + if assembly.current == input.mainTier { assembly = newAssemblyAccumulator(cloneComposedDocument(assembly.current)) } var resolved resolvedFilesystemContext diff --git a/internal/engine/load_result.go b/internal/engine/load_result.go index 092aa36..9fe9770 100644 --- a/internal/engine/load_result.go +++ b/internal/engine/load_result.go @@ -177,11 +177,17 @@ func sourceFailure(schema *schemaDescriptor, predicate func(Path) bool, current input := candidate.input input.contexts = current.contexts input.versionState = current.versionState + if candidate.stage == InspectionStageMain && current.mainTierAttempted { + if current.mainTier == nil { + continue + } + input.composition, input.document = current.mainTier, current.mainTier.root + } if input.document == nil && len(input.acquisition) == 0 && (input.composition == nil || input.composition.root == nil) { continue } if input.composition == nil || input.composition.root == nil { - composition, diagnostics := compositionForInputWithPolicy(input, policy) + composition, diagnostics := compositionForInputWithPolicies(input, current.mainTierPolicy, policy) if len(diagnostics) != 0 || composition == nil || composition.root == nil { continue }