From 39087675e1c7c4c0ea46ab147e7ac7ff222c2b3b Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Thu, 26 Nov 2026 18:18:34 +0000 Subject: [PATCH] Accept and protect processing glossary files --- docs/roadmap/implementation.md | 2 + internal/cli/app_wiring.go | 17 +++-- internal/cli/process.go | 3 +- internal/cli/process_command.go | 14 +++-- internal/cli/process_inputs.go | 4 +- internal/cli/runtime_glossary_test.go | 90 +++++++++++++++++++++++++++ 6 files changed, 118 insertions(+), 12 deletions(-) create mode 100644 internal/cli/runtime_glossary_test.go diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 12660a7..236cfe6 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -144,6 +144,8 @@ Acceptance: ## Stage 3: Connect processing flags and protect acquired source files +Status: Complete. + Work: 1. Add repeatable `--glossary-file PATH` to `processInput`/`processCommand` using diff --git a/internal/cli/app_wiring.go b/internal/cli/app_wiring.go index bb5c2ef..c828454 100644 --- a/internal/cli/app_wiring.go +++ b/internal/cli/app_wiring.go @@ -27,7 +27,11 @@ type engineEntry struct { engine *pk.Engine } -func loadApplication(ctx context.Context, selection ckconfig.Selection, _ struct{}, _ *invocationState) (*ck.Result[appconfig.Config], error) { +type acquisitionInput struct { + GlossaryFiles []string +} + +func acquireApplication(ctx context.Context, selection ckconfig.Selection, input acquisitionInput) (*appconfig.Acquisition, error) { if err := ctx.Err(); err != nil { return nil, err } @@ -40,11 +44,16 @@ func loadApplication(ctx context.Context, selection ckconfig.Selection, _ struct } else { source = ck.SearchConfig("audita", ck.Searches(ck.WorkingDirectory(), ck.UserConfigDirectory(), ck.UnixSystemDirectories())) } - loaded, report, err := appconfig.Load(ctx, ck.Input{Source: source}, appconfig.Options{Overlay: selection.Overlay.Value, Overrides: selection.Overrides}) + loaded, report, err := appconfig.Load(ctx, ck.Input{Source: source}, appconfig.Options{Overlay: selection.Overlay.Value, Overrides: selection.Overrides, GlossaryFiles: input.GlossaryFiles}) + return loaded, reportBootstrap(ctx, report, err) +} + +func loadApplication(ctx context.Context, selection ckconfig.Selection, _ struct{}, _ *invocationState) (*ck.Result[appconfig.Config], error) { + loaded, err := acquireApplication(ctx, selection, acquisitionInput{}) if err != nil { - return nil, reportBootstrap(ctx, report, err) + return nil, err } - return loaded.Result, reportBootstrap(ctx, report, nil) + return loaded.Result, nil } func loadPromptEngine(ctx context.Context, selection ckconfig.Selection, input struct{}, state *invocationState) (*pk.Engine, error) { diff --git a/internal/cli/process.go b/internal/cli/process.go index e559960..8d8ecc9 100644 --- a/internal/cli/process.go +++ b/internal/cli/process.go @@ -32,7 +32,8 @@ func runProcess(inv commandkit.Invocation, input processInput) commandkit.Outcom var loadErr *ck.LoadError if errors.As(err, &loadErr) { _, explanation := ckconfig.AdaptDiagnostics("Application configuration:\n", loadErr.Sources(), loadErr.Diagnostics()) - if ckconfig.ClassifyConfigurationError(err) == commandkit.OutcomeUsage { + var glossaryErr *appconfig.GlossaryInputError + if inv.Context.Err() == nil && (errors.As(err, &glossaryErr) || ckconfig.ClassifyConfigurationError(err) == commandkit.OutcomeUsage) { return commandkit.Usage(loadErr.Error(), explanation.Details...).WithCause(err) } return commandkit.Failure(loadErr.Error(), explanation.Details...).WithCause(err) diff --git a/internal/cli/process_command.go b/internal/cli/process_command.go index bf751da..d0fefac 100644 --- a/internal/cli/process_command.go +++ b/internal/cli/process_command.go @@ -9,17 +9,19 @@ import ( ) type processInput struct { - Config commandkit.Supplied[string] - Overlay commandkit.Supplied[string] - Transcript string - Output commandkit.Supplied[string] - Debug commandkit.Supplied[bool] - Overrides []ck.Override + Config commandkit.Supplied[string] + Overlay commandkit.Supplied[string] + Transcript string + Output commandkit.Supplied[string] + Debug commandkit.Supplied[bool] + Overrides []ck.Override + GlossaryFiles []string } func processCommand() commandkit.Command { stringValue := func(s string) (string, error) { return s, nil } flags := []commandkit.Flag[processInput]{ + commandkit.RepeatFlag(commandkit.FlagMeta{Name: "glossary-file", Usage: "Runtime glossary file (repeatable)."}, nil, stringValue, func(in *processInput, v commandkit.Supplied[[]string]) { in.GlossaryFiles = v.Value }), commandkit.ValueFlag(commandkit.FlagMeta{Name: "config", Usage: "Application configuration file."}, (*string)(nil), stringValue, func(in *processInput, v commandkit.Supplied[string]) { in.Config = v }), commandkit.ValueFlag(commandkit.FlagMeta{Name: "overlay", Usage: "Configuration overlay."}, (*string)(nil), stringValue, func(in *processInput, v commandkit.Supplied[string]) { in.Overlay = v }), commandkit.ValueFlag(commandkit.FlagMeta{Name: "output", Usage: "Transcript output file."}, (*string)(nil), stringValue, func(in *processInput, v commandkit.Supplied[string]) { in.Output = v }), diff --git a/internal/cli/process_inputs.go b/internal/cli/process_inputs.go index dbc22f9..bc8b857 100644 --- a/internal/cli/process_inputs.go +++ b/internal/cli/process_inputs.go @@ -43,10 +43,11 @@ func loadProcessInputs(ctx context.Context, input processInput, mode commandkit. return nil, fmt.Errorf("quiet processing requires an output file or /dev/null") } selection := ckconfig.Selection{Config: input.Config, Overlay: input.Overlay, Overrides: input.Overrides} - loaded, err := loadApplication(ctx, selection, struct{}{}, nil) + acquired, err := acquireApplication(ctx, selection, acquisitionInput{GlossaryFiles: input.GlossaryFiles}) if err != nil { return nil, err } + loaded := acquired.Result runtime, err := llm.NewRuntime(ctx, loaded, nil) if err != nil { return nil, &runtimePreparationError{cause: err} @@ -69,6 +70,7 @@ func loadProcessInputs(ctx context.Context, input processInput, mode commandkit. raw = nil } protected := append([]string(nil), runtime.SourcePaths...) + protected = append(protected, acquired.GlossarySources...) for _, entry := range loaded.Sources() { if entry.Kind == ck.SourceKindFile && entry.Path != "" { protected = append(protected, entry.Path) diff --git a/internal/cli/runtime_glossary_test.go b/internal/cli/runtime_glossary_test.go new file mode 100644 index 0000000..3e26bd2 --- /dev/null +++ b/internal/cli/runtime_glossary_test.go @@ -0,0 +1,90 @@ +package cli + +import ( + "bytes" + "context" + "fmt" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "sync/atomic" + "testing" + + commandkit "go.maximumdirect.net/commandkit" +) + +func TestProcessProtectsRuntimeGlossarySources(t *testing.T) { + for _, mode := range []string{"transcript", "report", "separate"} { + t.Run(mode, func(t *testing.T) { + dir := t.TempDir() + var calls atomic.Int32 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + calls.Add(1) + fmt.Fprint(w, `{"choices":[{"message":{"content":"{\"corrections\":[]}"}}]}`) + })) + defer server.Close() + imported := "imported.yml" + if mode == "report" { + imported = "errors.yml" + } + files := map[string]string{ + "config.yml": "version: 3\nllm: {proposal_profile: fixture}\npipeline: {stages: [{module: grammar}]}\n", + "input.json": `[{"speaker":"Alice","start":0,"end":1,"text":"Hello."}]`, + "runtime.yml": fmt.Sprintf("config:\n imports: [{path: %s, required: true}]\nparty: {entries: []}\n", imported), + imported: "reference: {entries: []}\n", + "prompts.yml": fmt.Sprintf("profiles:\n - id: fixture\n endpoint: %s/v1\n model: test\n api_key_required: false\n", server.URL), + } + for name, data := range files { + if err := os.WriteFile(filepath.Join(dir, name), []byte(data), 0600); err != nil { + t.Fatal(err) + } + } + output := filepath.Join(dir, "output.json") + if mode == "transcript" { + output = filepath.Join(dir, "runtime.yml") + } + app, err := buildApplication() + if err != nil { + t.Fatal(err) + } + var out, stderr bytes.Buffer + result := app.Execute(context.Background(), []string{"process", "--config", filepath.Join(dir, "config.yml"), "--glossary-file", filepath.Join(dir, "runtime.yml"), "--output", output, filepath.Join(dir, "input.json")}, commandkit.Streams{Out: &out, Err: &stderr}) + if mode == "separate" { + if result.Status != 0 || calls.Load() != 1 { + t.Fatalf("status=%d calls=%d error=%s", result.Status, calls.Load(), stderr.String()) + } + } else if result.Status == 0 || calls.Load() != 0 || !strings.Contains(stderr.String(), "conflicts with input") { + t.Fatalf("status=%d calls=%d error=%s", result.Status, calls.Load(), stderr.String()) + } + for _, name := range []string{"runtime.yml", imported} { + raw, err := os.ReadFile(filepath.Join(dir, name)) + if err != nil || string(raw) != files[name] { + t.Fatalf("source %s changed: %s %v", name, raw, err) + } + } + }) + } +} + +func TestProcessRuntimeGlossaryFailures(t *testing.T) { + dir := t.TempDir() + files := map[string]string{"config.yml": "version: 3\nglossaries: [party]\npipeline: {stages: [{module: glossary}]}\n", "input.json": `[{"speaker":"Alice","start":0,"end":1,"text":"Hello."}]`, "empty.yml": "party: {entries: []}\n", "bad.yml": "party: [\n"} + for name, data := range files { + if err := os.WriteFile(filepath.Join(dir, name), []byte(data), 0600); err != nil { + t.Fatal(err) + } + } + for _, name := range []string{"missing.yml", "bad.yml", "empty.yml"} { + var stdout, stderr bytes.Buffer + output := filepath.Join(dir, "output.json") + status := Run(context.Background(), []string{"process", "--config", filepath.Join(dir, "config.yml"), "--glossary-file", filepath.Join(dir, name), "--output", output, filepath.Join(dir, "input.json")}, &stdout, &stderr) + if status != 2 { + t.Fatalf("%s: status=%d stderr=%s", name, status, stderr.String()) + } + if _, err := os.Stat(output); !os.IsNotExist(err) { + t.Fatalf("%s: output prepared: %v", name, err) + } + } +}