Preserve the composed main tier on load failure

This commit is contained in:
2026-12-05 17:09:53 +00:00
parent 57c943a156
commit 1858c9331f
5 changed files with 98 additions and 3 deletions

View File

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

View File

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

View File

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

View File

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

View File

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