Cache source directories during configuration normalization

This commit is contained in:
2026-11-18 00:36:26 +00:00
parent 1c93aa0947
commit 76364a7b7e
4 changed files with 81 additions and 10 deletions

View File

@@ -1,5 +1,13 @@
# Configuration Upgrade Review Remediation # Configuration Upgrade Review Remediation
All eight findings were completed on 2026-11-18, with one commit per finding.
Assessment and evidence paragraphs below retain their review-time context;
each finding's completion marker records the implemented resolution.
Final verification passed: full offline tests, vet, race checks for configuration,
CLI and LLM packages, module tidy consistency, a native temporary build, and
credential-free minimal/complete/development configuration and inspection checks.
The earlier PromptKit findings below retain their original scope and status. The earlier PromptKit findings below retain their original scope and status.
The ConfigKit 1.2.0 review and subsequent focused audits appear below; they The ConfigKit 1.2.0 review and subsequent focused audits appear below; they
do not reopen or attribute earlier issues to that upgrade. do not reopen or attribute earlier issues to that upgrade.
@@ -444,7 +452,12 @@ retain their earlier scope and status.
### Finding CK-4 — Memoize Source Directories During Path Normalization ### Finding CK-4 — Memoize Source Directories During Path Normalization
Priority: P3. Category: efficiency/scalability opportunity. Status: open. Priority: P3. Category: efficiency/scalability opportunity. Status: complete.
Completed 2026-11-18: source-directory results, including missing/non-file
results, are memoized by source ID in load-local state. Main-directory setup
shares the lookup. Cache-sharing/isolation and existing path-origin tests pass;
the full offline suite, vet, race, and tidy checks pass. Path policy is unchanged.
Affected stages: 2–3; exposed by adoption of richer ConfigKit source metadata. Affected stages: 2–3; exposed by adoption of richer ConfigKit source metadata.
`internal/core/config/document_paths.go:144–176`, `documentPathBase`, calls `internal/core/config/document_paths.go:144–176`, `documentPathBase`, calls

View File

@@ -70,8 +70,8 @@ func initializeDocumentMainDirectory(resolution *configkit.Resolution, state *do
} }
info, ok := resolution.EntryInfo(configkit.Keys("version")) info, ok := resolution.EntryInfo(configkit.Keys("version"))
if ok && len(info.Origin.Locations) == 1 { if ok && len(info.Origin.Locations) == 1 {
if source, sourceOK := resolution.Source(info.Origin.Locations[0].SourceID); sourceOK && source.Path != "" { if directory := state.sourceDirectory(info.Origin.Locations[0].SourceID, resolution.Source); directory != "" {
state.mainDirectory = filepath.Dir(source.Path) state.mainDirectory = directory
return return
} }
} }
@@ -166,14 +166,35 @@ func documentPathBase(resolution *configkit.Resolution, state *documentLoadState
if len(info.Origin.Locations) != 1 { if len(info.Origin.Locations) != 1 {
return "", false return "", false
} }
source, ok := resolution.Source(info.Origin.Locations[0].SourceID) directory := state.sourceDirectory(info.Origin.Locations[0].SourceID, resolution.Source)
if !ok || source.Path == "" { if directory == "" {
if state != nil && state.mainDirectory != "" { if state != nil && state.mainDirectory != "" {
return state.mainDirectory, true return state.mainDirectory, true
} }
return "", false return "", false
} }
return filepath.Dir(source.Path), true return directory, true
} }
return "", false return "", false
} }
// Source evidence is fixed during resolution. Cache only the directory needed
// by paths, avoiding repeated copies of unrelated import-selection evidence.
func (state *documentLoadState) sourceDirectory(id string, lookup func(string) (configkit.SourceInfo, bool)) string {
if state != nil {
if directory, ok := state.sourceDirectories[id]; ok {
return directory
}
}
directory := ""
if source, ok := lookup(id); ok && source.Path != "" {
directory = filepath.Dir(source.Path)
}
if state != nil {
if state.sourceDirectories == nil {
state.sourceDirectories = make(map[string]string)
}
state.sourceDirectories[id] = directory
}
return directory
}

View File

@@ -8,6 +8,44 @@ import (
configkit "go.maximumdirect.net/configkit" configkit "go.maximumdirect.net/configkit"
) )
func TestSourceDirectoryLookupIsMemoizedPerLoad(t *testing.T) {
dir := t.TempDir()
state := newDocumentLoadState(dir)
calls := make(map[string]int)
lookup := func(id string) (configkit.SourceInfo, bool) {
calls[id]++
if id == "missing" {
return configkit.SourceInfo{}, false
}
if id == "memory" {
return configkit.SourceInfo{}, true
}
return configkit.SourceInfo{Path: filepath.Join(dir, id, "config.yml")}, true
}
for range 2 {
for _, id := range []string{"first", "second", "missing", "memory"} {
want := ""
if id == "first" || id == "second" {
want = filepath.Join(dir, id)
}
if got := state.sourceDirectory(id, lookup); got != want {
t.Fatalf("directory(%s) = %q, want %q", id, got, want)
}
}
}
for id, count := range calls {
if count != 1 {
t.Fatalf("source %s queried %d times", id, count)
}
}
other := newDocumentLoadState(dir)
if got := other.sourceDirectory("first", func(string) (configkit.SourceInfo, bool) {
return configkit.SourceInfo{Path: filepath.Join(dir, "changed", "config.yml")}, true
}); got != filepath.Join(dir, "changed") {
t.Fatalf("another load reused directory %q", got)
}
}
func TestDocumentPathsUseDeclaringFilesAcrossImports(t *testing.T) { func TestDocumentPathsUseDeclaringFilesAcrossImports(t *testing.T) {
t.Parallel() t.Parallel()

View File

@@ -8,11 +8,10 @@ import (
configkit "go.maximumdirect.net/configkit" configkit "go.maximumdirect.net/configkit"
) )
// documentLoadState keeps the spelling supplied by the user while resolver // documentLoadState keeps source-directory lookups and original map-key
// operations move map entries to their canonical keys. It is intentionally // spellings local to one load. It never becomes runtime configuration state.
// load-local: source-location handling consumes it during a later resolver
// pass and it never becomes runtime configuration state.
type documentLoadState struct { type documentLoadState struct {
sourceDirectories map[string]string
originalPaths map[string]configkit.Path originalPaths map[string]configkit.Path
mainDirectory string mainDirectory string
invocationDirectory string invocationDirectory string