From ed5d57d061337ef09aacf3a43566b7bba3aa1aaa Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Sat, 21 Nov 2026 12:00:29 +0000 Subject: [PATCH] Preserve terminal values during result lookup --- docs/roadmap/remediation.md | 11 +++++-- facade_result_value_test.go | 55 ++++++++++++++++++++++++++++++++ internal/engine/schema.go | 21 ++++++++---- internal/engine/schema_fields.go | 10 ++++-- 4 files changed, 86 insertions(+), 11 deletions(-) diff --git a/docs/roadmap/remediation.md b/docs/roadmap/remediation.md index ca47ba8..9118e6a 100644 --- a/docs/roadmap/remediation.md +++ b/docs/roadmap/remediation.md @@ -7,11 +7,18 @@ Scope: the nine stages of [implementation.md](implementation.md), the settled No implementation changes were made during these reviews. Findings 1–2 come from the correctness/architecture review; finding 3 comes from the subsequent -algorithmic review. All findings below remain open. +algorithmic review. Completion notes below track remediation after those reviews. ## 1. Terminal availability still depends on recursive schema validity -**Priority:** P2. **Status:** Open. **Plan stages:** 1–2. +**Priority:** P2. **Status:** Complete. **Plan stages:** 1–2. + +**Completion:** lookup descriptors now describe only the containing shape and +necessary inline bindings; ordinary children remain opaque until navigation +continues. Added public regressions for map/slice/array terminals, post-load +dynamic children and siblings, and continued rejection of ambiguous navigation +and invalid load schemas. The regression failed before the fix. Focused tests, +`./scripts/check.sh`, and `go test -race ./...` passed with Go 1.27.4. **Relevant code:** [ResultValue](../../internal/engine/result_value.go), especially its descriptor construction at line 33; the recursive map/slice/array and struct diff --git a/facade_result_value_test.go b/facade_result_value_test.go index 7345c0e..f2cffd6 100644 --- a/facade_result_value_test.go +++ b/facade_result_value_test.go @@ -130,3 +130,58 @@ func TestResultValueFrozenInspectionAndSecret(t *testing.T) { t.Fatalf("frozen output = %s", output.String()) } } + +func TestResultValueReturnsTerminalsWithoutSchemaValidation(t *testing.T) { + type terminal struct { + A int `yaml:"same"` + B int `yaml:"same"` + } + want := terminal{A: 1, B: 2} + cases := []struct { + name string + lookup func(configkit.Path) (any, bool) + path configkit.Path + }{ + {"map", (&configkit.Result[map[string]terminal]{Config: map[string]terminal{"item": want}}).Value, configkit.Keys("item")}, + {"slice", (&configkit.Result[[]terminal]{Config: []terminal{want}}).Value, configkit.Path{}.Index(0)}, + {"array", (&configkit.Result[[1]terminal]{Config: [1]terminal{want}}).Value, configkit.Path{}.Index(0)}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + got, ok := tc.lookup(tc.path) + if !ok || got != want { + t.Fatalf("terminal = %#v, %v", got, ok) + } + if _, ok := tc.lookup(tc.path.Key("same")); ok { + t.Fatal("ambiguous containing bindings accepted") + } + }) + } + // A post-load replacement need not itself satisfy the load-time schema. + result, err := configkit.Load[map[string]any](configkit.Bytes("dynamic.yml", []byte("{}"))) + if err != nil { + t.Fatal(err) + } + result.Config["dynamic"] = struct { + Child terminal `yaml:"child"` + Good int `yaml:"good"` + }{Child: want, Good: 3} + for _, tc := range []struct { + path configkit.Path + want any + }{ + {configkit.Keys("dynamic", "child"), want}, + {configkit.Keys("dynamic", "good"), 3}, + } { + got, ok := result.Value(tc.path) + if !ok || got != tc.want { + t.Fatalf("%s = %#v, %v", tc.path.String(), got, ok) + } + } + if _, ok := result.Value(configkit.Keys("dynamic", "child", "same")); ok { + t.Fatal("ambiguous dynamic child accepted navigation") + } + if _, err := configkit.Load[map[string]terminal](configkit.NoSource()); err == nil { + t.Fatal("Load accepted an invalid element schema") + } +} diff --git a/internal/engine/schema.go b/internal/engine/schema.go index a6f2009..e620749 100644 --- a/internal/engine/schema.go +++ b/internal/engine/schema.go @@ -115,7 +115,9 @@ type schemaBuilder struct { building map[reflect.Type]bool journal []reflect.Type secrets SecretBinding - lookup bool + // lookup describes only the current container and its flattened inline fields. + // Ordinary descendants are described only when navigation continues into them. + lookup bool } func (b *schemaBuilder) describe(current reflect.Type, path Path) (*typeDescriptor, *schemaIssue) { @@ -129,17 +131,15 @@ func (b *schemaBuilder) describe(current reflect.Type, path Path) (*typeDescript defer delete(b.building, current) fail := func(code Code, message string) (*typeDescriptor, *schemaIssue) { - // Lookup may reach unsupported values as terminals; they remain leaves. - if b.lookup && (code == CodeSchemaUnsupportedType || code == CodeSchemaTextCodec) { - descriptor.kind = descriptorInvalid - return descriptor, nil - } delete(b.cache, current) return nil, &schemaIssue{code: code, path: path.clone(), message: message} } if current.Kind() == reflect.Pointer { descriptor.kind = descriptorPointer + if b.lookup { + return descriptor, nil + } element, issue := b.describe(current.Elem(), path) if issue != nil { return nil, issue @@ -177,6 +177,9 @@ func (b *schemaBuilder) describe(current reflect.Type, path Path) (*typeDescript return fail(CodeSchemaUnsupportedType, "configuration maps must have string keys") } descriptor.kind = descriptorMap + if b.lookup { + return descriptor, nil + } element, issue := b.describe(current.Elem(), path) if issue != nil { return nil, issue @@ -184,6 +187,9 @@ func (b *schemaBuilder) describe(current reflect.Type, path Path) (*typeDescript descriptor.element = element case reflect.Slice: descriptor.kind = descriptorSlice + if b.lookup { + return descriptor, nil + } element, issue := b.describe(current.Elem(), path.Index(0)) if issue != nil { return nil, issue @@ -192,6 +198,9 @@ func (b *schemaBuilder) describe(current reflect.Type, path Path) (*typeDescript case reflect.Array: descriptor.kind = descriptorArray descriptor.length = current.Len() + if b.lookup { + return descriptor, nil + } element, issue := b.describe(current.Elem(), path.Index(0)) if issue != nil { return nil, issue diff --git a/internal/engine/schema_fields.go b/internal/engine/schema_fields.go index 148977b..29e62b4 100644 --- a/internal/engine/schema_fields.go +++ b/internal/engine/schema_fields.go @@ -4,9 +4,13 @@ import "reflect" func (b *schemaBuilder) describeOrdinaryField(owner *typeDescriptor, field reflect.StructField, index int, name string, secret bool, path Path) *schemaIssue { fieldPath := path.Key(name) - fieldType, issue := b.describe(field.Type, fieldPath) - if issue != nil { - return issue + var fieldType *typeDescriptor + if !b.lookup { + var issue *schemaIssue + fieldType, issue = b.describe(field.Type, fieldPath) + if issue != nil { + return issue + } } if issue := addField(owner, fieldDescriptor{name: name, index: []int{index}, descriptor: fieldType, secret: secret}, path); issue != nil { return issue