Resolve producer modes turn allowances cache controls and shared workers
This commit is contained in:
@@ -5,203 +5,109 @@ import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
func TestWorkerKeyNormalizationPreservesExplicitCount(t *testing.T) {
|
||||
func TestDocumentWorkerCount(t *testing.T) {
|
||||
t.Parallel()
|
||||
for _, test := range []struct {
|
||||
name, workers string
|
||||
want int
|
||||
errorPath string
|
||||
name, body string
|
||||
want int
|
||||
invalid bool
|
||||
}{
|
||||
{name: "omitted", workers: "{}", want: 4},
|
||||
{name: "padded", workers: "{' extract ': 2}", want: 2},
|
||||
{name: "explicit invalid", workers: "{' extract ': 0}", errorPath: "/concurrency/stage_workers/extract"},
|
||||
{name: "collision", workers: "{' extract ': 2, extract: 3}", errorPath: "/concurrency/stage_workers/extract"},
|
||||
{"omitted", "", 64, false},
|
||||
{"serial", "concurrency: {workers: 1}", 1, false},
|
||||
{"large", "concurrency: {workers: 1000000}", 1000000, false},
|
||||
{"zero", "concurrency: {workers: 0}", 0, true},
|
||||
{"negative", "concurrency: {workers: -1}", 0, true},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
loaded, err := loadConfigSource(configkit.Bytes("workers.yml", []byte(
|
||||
"version: 6\nconcurrency:\n total_llm: 4\n stage_workers: "+test.workers+"\n")), LoadOptions{})
|
||||
if test.errorPath != "" {
|
||||
assertDocumentDiagnosticAt(t, err, test.errorPath)
|
||||
loaded, err := loadConfigSource(configkit.Bytes("workers.yml", []byte("version: 6\n"+test.body+"\n")), LoadOptions{})
|
||||
if test.invalid {
|
||||
assertDocumentDiagnosticAt(t, err, "/concurrency/workers")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := loaded.Config.Concurrency.StageWorkers["extract"]; got != test.want {
|
||||
t.Fatalf("workers = %d, want %d", got, test.want)
|
||||
}
|
||||
var explanation bytes.Buffer
|
||||
if err := loaded.WriteExplanation(&explanation, "/concurrency/stage_workers"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if test.name == "padded" && (!strings.Contains(explanation.String(), "normalization") ||
|
||||
!strings.Contains(explanation.String(), " extract ") ||
|
||||
strings.Contains(explanation.String(), "derived extraction workers")) {
|
||||
t.Fatalf("explicit worker provenance = %s", explanation.String())
|
||||
if loaded.Config.Concurrency.Workers != test.want {
|
||||
t.Fatalf("workers = %d", loaded.Config.Concurrency.Workers)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDocumentLoadAppliesOperationalDefaultsAndNormalization(t *testing.T) {
|
||||
func TestDocumentStateDirectorySelection(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
loaded, err := loadDocumentSource(configkit.Bytes("document.yml", []byte(`version: 6
|
||||
concurrency:
|
||||
total_llm: 4
|
||||
output:
|
||||
directory: " ./output "
|
||||
cache:
|
||||
chunk_plans:
|
||||
mode: " refresh "
|
||||
directory: " "
|
||||
checkpoints:
|
||||
directory: " "
|
||||
debug:
|
||||
directory: " ./debug "
|
||||
`)), LoadOptions{})
|
||||
userRoot, err := os.UserCacheDir()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if loaded.Config.Concurrency.TotalLLM == nil || *loaded.Config.Concurrency.TotalLLM != 4 || loaded.Config.Concurrency.StageWorkers["extract"] != 4 {
|
||||
t.Fatalf("concurrency = %#v", loaded.Config.Concurrency)
|
||||
}
|
||||
workingDirectory, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if loaded.Config.Output.Directory == nil || *loaded.Config.Output.Directory != filepath.Join(workingDirectory, "output") ||
|
||||
loaded.Config.Cache.ChunkPlans.Mode == nil || *loaded.Config.Cache.ChunkPlans.Mode != "refresh" ||
|
||||
loaded.Config.Cache.ChunkPlans.Directory == nil || *loaded.Config.Cache.ChunkPlans.Directory != "" ||
|
||||
loaded.Config.Cache.Checkpoints.Directory == nil || *loaded.Config.Cache.Checkpoints.Directory != "" ||
|
||||
loaded.Config.Debug.Directory == nil || *loaded.Config.Debug.Directory != filepath.Join(workingDirectory, "debug") {
|
||||
t.Fatalf("state = %#v, %#v, %#v", loaded.Config.Output, loaded.Config.Cache, loaded.Config.Debug)
|
||||
}
|
||||
if diagnostics := loaded.Diagnostics(); len(diagnostics) != 0 {
|
||||
t.Fatalf("diagnostics = %#v", diagnostics)
|
||||
}
|
||||
extractHistory := loaded.Metadata().History(configkit.Keys("concurrency", "stage_workers", "extract"))
|
||||
if len(extractHistory) == 0 || extractHistory[len(extractHistory)-1].Origin.Operation != configkit.ChangeDerivedDefault {
|
||||
t.Fatalf("extract history = %#v", extractHistory)
|
||||
}
|
||||
outputHistory := loaded.Metadata().History(configkit.Keys("output", "directory"))
|
||||
if len(outputHistory) == 0 || outputHistory[len(outputHistory)-1].Origin.Operation != configkit.ChangeNormalization {
|
||||
t.Fatalf("output history = %#v", outputHistory)
|
||||
for _, test := range []struct{ name, body, cache, debug string }{
|
||||
{"omitted", "", filepath.Join(userRoot, "notarius"), "/tmp/notarius-debug"},
|
||||
{"empty", "cache_directory: ''\ndebug: {directory: ''}", filepath.Join(userRoot, "notarius"), ""},
|
||||
{"explicit", "cache_directory: /var/cache/fixture\ndebug: {directory: /tmp/fixture-debug}", "/var/cache/fixture", "/tmp/fixture-debug"},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
loaded, err := loadConfigSource(configkit.Bytes("directories.yml", []byte("version: 6\n"+test.body+"\n")), LoadOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if loaded.Config.CacheDirectory != test.cache || loaded.Config.Debug.Directory != test.debug {
|
||||
t.Fatalf("directories = %q, %q", loaded.Config.CacheDirectory, loaded.Config.Debug.Directory)
|
||||
}
|
||||
var effective bytes.Buffer
|
||||
if err := loaded.WriteEffective(&effective); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDocumentLoadDerivesWorkersAfterOverlayAndCLIOverrides(t *testing.T) {
|
||||
func TestDirectoryPathsFollowImportedSource(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
directory := t.TempDir()
|
||||
main := filepath.Join(directory, "config.yml")
|
||||
overlays := filepath.Join(directory, "overlays")
|
||||
if err := os.Mkdir(overlays, 0o755); err != nil {
|
||||
dir := t.TempDir()
|
||||
fragmentDir := filepath.Join(dir, "fragments")
|
||||
if err := os.Mkdir(fragmentDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(main, []byte("version: 6\nconcurrency:\n total_llm: 4\n"), 0o600); err != nil {
|
||||
main := filepath.Join(dir, "config.yml")
|
||||
if err := os.WriteFile(main, []byte("version: 6\nconfig:\n imports:\n - path: fragments/state.yml\n required: true\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(overlays, "development.yml"), []byte("concurrency:\n total_llm: 3\n"), 0o600); err != nil {
|
||||
if err := os.WriteFile(filepath.Join(fragmentDir, "state.yml"), []byte("cache_directory: selected\ndebug: {directory: evidence}\noutput: {directory: deliverables}\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
overlaid, err := loadDocument(main, LoadOptions{Overlay: "development"})
|
||||
loaded, err := Load(main, LoadOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if overlaid.Config.Concurrency.TotalLLM == nil || *overlaid.Config.Concurrency.TotalLLM != 3 || overlaid.Config.Concurrency.StageWorkers["extract"] != 3 {
|
||||
t.Fatalf("overlaid concurrency = %#v", overlaid.Config.Concurrency)
|
||||
}
|
||||
|
||||
overridden, err := loadDocument(main, LoadOptions{Overrides: []configkit.Override{{
|
||||
Path: configkit.Keys("concurrency", "total_llm"), Value: 2, Flag: "--total-llm",
|
||||
}}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if overridden.Config.Concurrency.TotalLLM == nil || *overridden.Config.Concurrency.TotalLLM != 2 || overridden.Config.Concurrency.StageWorkers["extract"] != 2 {
|
||||
t.Fatalf("overridden concurrency = %#v", overridden.Config.Concurrency)
|
||||
}
|
||||
|
||||
overlaidAndOverridden, err := loadDocument(main, LoadOptions{Overlay: "development", Overrides: []configkit.Override{{
|
||||
Path: configkit.Keys("concurrency", "total_llm"), Value: 2, Flag: "--total-llm",
|
||||
}}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if overlaidAndOverridden.Config.Concurrency.TotalLLM == nil || *overlaidAndOverridden.Config.Concurrency.TotalLLM != 2 || overlaidAndOverridden.Config.Concurrency.StageWorkers["extract"] != 2 {
|
||||
t.Fatalf("overlaid and overridden concurrency = %#v", overlaidAndOverridden.Config.Concurrency)
|
||||
if loaded.Config.CacheDirectory != filepath.Join(fragmentDir, "selected") || loaded.Config.Debug.Directory != filepath.Join(fragmentDir, "evidence") || loaded.Config.Output.Directory != filepath.Join(fragmentDir, "deliverables") {
|
||||
t.Fatalf("source-relative directories lost: %#v", loaded.Config)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDocumentLoadRetainsExplicitWorkerAndRejectsInvalidOperationalValues(t *testing.T) {
|
||||
func TestDocumentRejectsRetiredControls(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
loaded, err := loadDocumentSource(configkit.Bytes("explicit.yml", []byte(`version: 6
|
||||
concurrency:
|
||||
total_llm: 4
|
||||
stage_workers:
|
||||
extract: 0
|
||||
`)), LoadOptions{})
|
||||
if loaded != nil {
|
||||
t.Fatalf("loaded = %#v, want validation failure", loaded.Config)
|
||||
}
|
||||
assertDocumentDiagnostic(t, err, configkit.CodeValidateError, "/concurrency/stage_workers/extract")
|
||||
|
||||
_, err = loadDocumentSource(configkit.Bytes("invalid.yml", []byte(`version: 6
|
||||
concurrency:
|
||||
total_llm: 0
|
||||
output:
|
||||
directory: ""
|
||||
`)), LoadOptions{})
|
||||
var loadError *configkit.LoadError
|
||||
if !errors.As(err, &loadError) {
|
||||
t.Fatalf("error = %v, want ConfigKit load error", err)
|
||||
}
|
||||
seen := make(map[string]bool)
|
||||
for _, diagnostic := range loadError.Diagnostics() {
|
||||
if diagnostic.Code != configkit.CodeValidateError || diagnostic.Primary == nil {
|
||||
t.Fatalf("diagnostic = %#v", diagnostic)
|
||||
}
|
||||
seen[diagnostic.Path.String()] = true
|
||||
}
|
||||
for _, path := range []string{"/concurrency/total_llm", "/output/directory"} {
|
||||
if !seen[path] {
|
||||
t.Fatalf("diagnostics missing %s: %#v", path, loadError.Diagnostics())
|
||||
for _, body := range []string{
|
||||
"concurrency: {total_llm: 4}",
|
||||
"concurrency: {stage_workers: {extract: 4}}",
|
||||
"cache: {chunk_plans: {mode: auto}}",
|
||||
"pipelines: {main: {input: {module: input}, retries: 1}}",
|
||||
"pipelines: {main: {input: {module: input}, structured_output_repair_attempts: 0}}",
|
||||
"pipelines: {main: {input: {module: input}, validation_policy: {validator_failure: fail_run}}}",
|
||||
"pipelines: {main: {input: {module: input}, chunk: {module: chunk, retries: 1}}}",
|
||||
"pipelines: {main: {input: {module: input}, chunk: {module: chunk, validators: [{module: validate, max_additional_turns: 1}]}}}",
|
||||
} {
|
||||
_, err := loadConfigSource(configkit.Bytes("retired.yml", []byte("version: 6\n"+body+"\n")), LoadOptions{})
|
||||
if err == nil {
|
||||
t.Fatalf("accepted retired controls: %s", body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDocumentLoadDistinguishesRequiredAndOptionalPathValues(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
loaded, err := loadDocumentSource(configkit.Bytes("cache.yml", []byte(`version: 6
|
||||
cache:
|
||||
chunk_plans:
|
||||
directory: " "
|
||||
checkpoints:
|
||||
directory: " "
|
||||
`)), LoadOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if loaded.Config.Cache.ChunkPlans.Directory == nil || *loaded.Config.Cache.ChunkPlans.Directory != "" ||
|
||||
loaded.Config.Cache.Checkpoints.Directory == nil || *loaded.Config.Cache.Checkpoints.Directory != "" {
|
||||
t.Fatalf("cache directories = %#v", loaded.Config.Cache)
|
||||
}
|
||||
|
||||
_, err = loadDocumentSource(configkit.Bytes("output.yml", []byte("version: 6\noutput:\n directory: ' '\n")), LoadOptions{})
|
||||
assertDocumentDiagnostic(t, err, configkit.CodeValidateError, "/output/directory")
|
||||
|
||||
}
|
||||
|
||||
func assertDocumentDiagnostic(t *testing.T, err error, code configkit.Code, path string) {
|
||||
t.Helper()
|
||||
var loadError *configkit.LoadError
|
||||
|
||||
Reference in New Issue
Block a user