Validate source equivalence and layered scaling

This commit is contained in:
2026-09-29 18:15:10 +00:00
parent 36362ee6af
commit 34e18c84c1
6 changed files with 423 additions and 13 deletions

View File

@@ -23,10 +23,14 @@ func (r *Inspection) WriteExplanation(output io.Writer, path Path) error {
return errExplanationUnknownPath
}
var rendered strings.Builder
labels := make(map[string]string, len(r.sources))
for _, source := range r.sources {
labels[source.ID] = source.Label
}
rendered.WriteString("path: ")
rendered.WriteString(strconv.Quote(explanation.Path.String()))
rendered.WriteByte('\n')
writeExplanationOrigin(&rendered, "effective source", explanation.Origin, r.sources)
writeExplanationOrigin(&rendered, "effective source", explanation.Origin, labels)
writeExplanationSnapshot(&rendered, "effective (canonical typed)", explanation.Effective, 0)
if explanation.SecretReference != nil {
rendered.WriteString("secret reference: ")
@@ -43,7 +47,7 @@ func (r *Inspection) WriteExplanation(output io.Writer, path Path) error {
} else {
rendered.WriteString("canonical typed\n")
}
writeExplanationOrigin(&rendered, " source", change.Origin, r.sources)
writeExplanationOrigin(&rendered, " source", change.Origin, labels)
writeExplanationSnapshot(&rendered, " before", change.Before, 4)
writeExplanationSnapshot(&rendered, " after", change.After, 4)
for _, reason := range change.Reasons {
@@ -65,7 +69,7 @@ func (r *Inspection) WriteExplanation(output io.Writer, path Path) error {
return err
}
func writeExplanationOrigin(output *strings.Builder, label string, origin Origin, sources []SourceInfo) {
func writeExplanationOrigin(output *strings.Builder, label string, origin Origin, labels map[string]string) {
output.WriteString(label)
output.WriteString(": layer=")
output.WriteString(strconv.Quote(string(origin.Layer)))
@@ -77,7 +81,7 @@ func writeExplanationOrigin(output *strings.Builder, label string, origin Origin
for _, location := range origin.Locations {
output.WriteString(" location: source=")
output.WriteString(strconv.Quote(location.SourceID))
if label := sourceLabel(sources, location.SourceID); label != "" {
if label := labels[location.SourceID]; label != "" {
output.WriteString(" label=")
output.WriteString(strconv.Quote(label))
}
@@ -89,15 +93,6 @@ func writeExplanationOrigin(output *strings.Builder, label string, origin Origin
}
}
func sourceLabel(sources []SourceInfo, sourceID string) string {
for _, source := range sources {
if source.ID == sourceID {
return source.Label
}
}
return ""
}
func writeExplanationSnapshot(output *strings.Builder, label string, snapshot ValueSnapshot, indent int) {
if !snapshot.Present {
output.WriteString(label)

View File

@@ -126,6 +126,7 @@ func assembleLayeredInput[T any](request loadRequest[T], input assembledInput, a
}
func acquireUnderlayGroup(group loadGroup, index int, rule *VersionRule, accounting *importAccounting) (*composedDocument, []SourceInfo, map[string]ResourceContext, []Diagnostic) {
initialBytes, initialNodes := accounting.bytes, accounting.nodes
visited := make(map[resourceIdentity]importRoute)
hostToken := &namespaceToken{}
var documents []*document.Node
@@ -168,6 +169,8 @@ func acquireUnderlayGroup(group loadGroup, index int, rule *VersionRule, account
if len(diagnostics) != 0 {
return nil, sources, contexts, diagnostics
}
composition.inputBytes = accounting.bytes - initialBytes
composition.inputNodes = accounting.nodes - initialNodes
for pointer, origin := range composition.origins {
origin.Layer = LayerUnderlay
composition.origins[pointer] = origin

View File

@@ -208,3 +208,73 @@ func groupedErrorCode(t *testing.T, err error) Code {
}
return loadError.Diagnostics()[0].Code
}
func TestGroupedAccountingChargesMixedOccurrencesOnce(t *testing.T) {
const (
one = "configkit: {imports: [{path: child-one.yml}]}\nleft: first\n"
child = "extra: imported\n"
two = "configkit: {imports: [{path: child-two.yml}]}\nright: second\n"
)
filesystem := NewFilesystem("bundle", fstest.MapFS{
"one.yml": &fstest.MapFile{Data: []byte(one)},
"child-one.yml": &fstest.MapFile{Data: []byte(child)},
"two.yml": &fstest.MapFile{Data: []byte(two)},
"child-two.yml": &fstest.MapFile{Data: []byte(child)},
})
accounting := &importAccounting{}
first, sources, _, diagnostics := acquireUnderlayGroup(loadGroup{inputs: []loadInput{{source: filesystem.File("one.yml"), imports: true}}}, 1, nil, accounting)
if len(diagnostics) != 0 || first == nil || len(sources) != 2 || accounting.sources != 2 || accounting.bytes != int64(len(one)+len(child)) {
t.Fatalf("first acquisition = %#v, %#v, %#v, accounting %#v", first, sources, diagnostics, accounting)
}
second, sources, _, diagnostics := acquireUnderlayGroup(loadGroup{inputs: []loadInput{{source: filesystem.File("two.yml"), imports: true}}}, 2, nil, accounting)
if len(diagnostics) != 0 || second == nil || len(sources) != 2 || sources[0].ID != "source-3" || sources[1].ID != "source-4" || accounting.sources != 4 || accounting.bytes != int64(len(one)+len(two)+2*len(child)) {
t.Fatalf("second acquisition = %#v, %#v, %#v, accounting %#v", second, sources, diagnostics, accounting)
}
var wantNodes int
for _, text := range []string{one, child, two, child} {
parsed, diagnostics := parseSource(Bytes("fixture", []byte(text)), "source-1", defaultParseLimits())
if len(diagnostics) != 0 {
t.Fatal(diagnostics)
}
wantNodes += countInputNodes(parsed.document)
}
if accounting.nodes != wantNodes {
t.Fatalf("charged nodes = %d, want %d", accounting.nodes, wantNodes)
}
if first.inputBytes != int64(len(one)+len(child)) || second.inputBytes != int64(len(two)+len(child)) {
t.Fatalf("composition costs = %d, %d, accounting %d", first.inputBytes, second.inputBytes, accounting.bytes)
}
}
func TestGroupedGlobEntryBudgetIsShared(t *testing.T) {
filesystem := NewFilesystem("bundle", fstest.MapFS{
"main.yml": &fstest.MapFile{Data: []byte("configkit: {imports: [{glob: 'parts/*.yml'}]}\n")},
"parts/a.yml": &fstest.MapFile{Data: []byte("left: one\n")},
"parts/b.yml": &fstest.MapFile{Data: []byte("right: two\n")},
})
accounting := &importAccounting{entries: maxGlobEntries - 1}
_, _, _, diagnostics := acquireUnderlayGroup(loadGroup{inputs: []loadInput{{source: filesystem.File("main.yml"), imports: true}}}, 1, nil, accounting)
if len(diagnostics) == 0 || diagnostics[0].Code != CodeImportLimitExceeded || accounting.entries <= maxGlobEntries {
t.Fatalf("shared glob budget = %#v, accounting %#v", diagnostics, accounting)
}
}
func TestGroupedMixedSourcesShareCLIRecordLimit(t *testing.T) {
filesystem := NewFilesystem("bundle", fstest.MapFS{"main.yml": &fstest.MapFile{Data: []byte("value: base\n")}})
inputs := make([]Input, maxImportSources-1)
inputs[0] = Input{Source: Bytes("empty-bytes", []byte("{}\n"))}
for index := 1; index < len(inputs); index++ {
inputs[index] = Input{Source: Mapping("empty-map", map[string]any{})}
}
type config struct {
Value string `yaml:"value"`
}
_, err := LoadLayered(Input{Source: filesystem.File("main.yml")}, testSecretBinding(), LayeredOptions[config]{
Underlays: []Underlay{{Inputs: inputs}},
Overrides: []Override{{Path: Keys("value"), Value: "cli", Flag: "--value"}},
})
loadError, ok := err.(*LoadError)
if !ok || len(loadError.Diagnostics()) == 0 || loadError.Diagnostics()[0].Code != CodeOverrideLimitExceeded || len(loadError.Sources()) != maxImportSources {
t.Fatalf("mixed source/CLI limit = %#v", err)
}
}

View File

@@ -0,0 +1,156 @@
package engine
import (
"io"
"strconv"
"testing"
"testing/fstest"
)
type benchmarkCatalogConfig struct {
Catalog map[string]string `yaml:"catalog"`
}
type benchmarkNodeConfig struct {
Node string `yaml:"node"`
}
func BenchmarkUnderlayBroadCatalog(b *testing.B) {
for _, width := range []int{128, 256, 512} {
b.Run(strconv.Itoa(width), func(b *testing.B) {
inputs := make([]Input, 2)
for half := range inputs {
catalog := make(map[string]any, width/2)
for index := range width / 2 {
catalog["item-"+strconv.Itoa(half*width/2+index)] = "value"
}
inputs[half] = Input{Source: Mapping("catalog-"+strconv.Itoa(half), map[string]any{"catalog": catalog})}
}
options := LayeredOptions[benchmarkCatalogConfig]{Underlays: []Underlay{{Inputs: inputs}}}
b.ReportAllocs()
b.ResetTimer()
for range b.N {
if _, err := LoadLayered(Input{Source: NoSource()}, testSecretBinding(), options); err != nil {
b.Fatal(err)
}
}
})
}
}
func BenchmarkUnderlayManySmallTiers(b *testing.B) {
for _, count := range []int{32, 64, 128} {
b.Run(strconv.Itoa(count), func(b *testing.B) {
groups := make([]Underlay, count)
for index := range groups {
groups[index].Inputs = []Input{{Source: Mapping("item", map[string]any{
"catalog": map[string]any{"item-" + strconv.Itoa(index): "value"},
})}}
}
options := LayeredOptions[benchmarkCatalogConfig]{Underlays: groups}
b.ReportAllocs()
b.ResetTimer()
for range b.N {
if _, err := LoadLayered(Input{Source: NoSource()}, testSecretBinding(), options); err != nil {
b.Fatal(err)
}
}
})
}
}
func BenchmarkUnderlaySubtreeReplacement(b *testing.B) {
for _, count := range []int{32, 64, 128} {
b.Run(strconv.Itoa(count), func(b *testing.B) {
groups := make([]Underlay, count)
for index := range groups {
var node any = "temporary"
if index%2 == 0 {
node = map[string]any{"left": "one", "right": "two", "nested": map[string]any{"enabled": false}}
}
groups[index].Inputs = []Input{{Source: Mapping("replacement", map[string]any{"node": node})}}
}
options := LayeredOptions[benchmarkNodeConfig]{Underlays: groups}
main := Input{Source: Mapping("main", map[string]any{"node": "final"})}
b.ReportAllocs()
b.ResetTimer()
for range b.N {
if _, err := LoadLayered(main, testSecretBinding(), options); err != nil {
b.Fatal(err)
}
}
})
}
}
func BenchmarkUnderlayNativeImports(b *testing.B) {
for _, count := range []int{16, 32, 64} {
b.Run(strconv.Itoa(count), func(b *testing.B) {
files := fstest.MapFS{
"main.yml": &fstest.MapFile{Data: []byte("configkit: {imports: [{glob: 'defaults/*.yml'}]}\n")},
}
for index := range count {
name := "item-" + strconv.Itoa(index)
files["defaults/"+name+".yml"] = &fstest.MapFile{Data: []byte("catalog: {" + name + ": value}\n")}
}
filesystem := NewFilesystem("bundle", files)
main := Input{Source: filesystem.File("main.yml"), Imports: true}
b.ReportAllocs()
b.ResetTimer()
for range b.N {
if _, err := LoadLayered[benchmarkCatalogConfig](main, testSecretBinding()); err != nil {
b.Fatal(err)
}
}
})
}
}
func BenchmarkUnderlayRetainedHistory(b *testing.B) {
for _, count := range []int{16, 32, 64} {
b.Run(strconv.Itoa(count), func(b *testing.B) {
groups := make([]Underlay, count)
for index := range groups {
groups[index].Inputs = []Input{{Source: Mapping("value", map[string]any{
"catalog": map[string]any{"shared": strconv.Itoa(index)},
})}}
}
options := LayeredOptions[benchmarkCatalogConfig]{Underlays: groups}
b.ReportAllocs()
b.ResetTimer()
for range b.N {
result, err := LoadLayered(Input{Source: NoSource()}, testSecretBinding(), options)
if err != nil {
b.Fatal(err)
}
if _, ok := result.Inspection.Explain(Keys("catalog", "shared")); !ok {
b.Fatal("retained path was not explained")
}
}
})
}
}
func BenchmarkUnderlayRenderedHistory(b *testing.B) {
for _, count := range []int{32, 64, 128} {
b.Run(strconv.Itoa(count), func(b *testing.B) {
groups := make([]Underlay, count)
for index := range groups {
groups[index].Inputs = []Input{{Source: Mapping("value", map[string]any{
"catalog": map[string]any{"shared": strconv.Itoa(index)},
})}}
}
result, err := LoadLayered(Input{Source: NoSource()}, testSecretBinding(), LayeredOptions[benchmarkCatalogConfig]{Underlays: groups})
if err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
for range b.N {
if err := result.Inspection.WriteExplanation(io.Discard, Keys("catalog", "shared")); err != nil {
b.Fatal(err)
}
}
})
}
}