Accept and protect processing glossary files
This commit is contained in:
@@ -144,6 +144,8 @@ Acceptance:
|
|||||||
|
|
||||||
## Stage 3: Connect processing flags and protect acquired source files
|
## Stage 3: Connect processing flags and protect acquired source files
|
||||||
|
|
||||||
|
Status: Complete.
|
||||||
|
|
||||||
Work:
|
Work:
|
||||||
|
|
||||||
1. Add repeatable `--glossary-file PATH` to `processInput`/`processCommand` using
|
1. Add repeatable `--glossary-file PATH` to `processInput`/`processCommand` using
|
||||||
|
|||||||
@@ -27,7 +27,11 @@ type engineEntry struct {
|
|||||||
engine *pk.Engine
|
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 {
|
if err := ctx.Err(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -40,11 +44,16 @@ func loadApplication(ctx context.Context, selection ckconfig.Selection, _ struct
|
|||||||
} else {
|
} else {
|
||||||
source = ck.SearchConfig("audita", ck.Searches(ck.WorkingDirectory(), ck.UserConfigDirectory(), ck.UnixSystemDirectories()))
|
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 {
|
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) {
|
func loadPromptEngine(ctx context.Context, selection ckconfig.Selection, input struct{}, state *invocationState) (*pk.Engine, error) {
|
||||||
|
|||||||
@@ -32,7 +32,8 @@ func runProcess(inv commandkit.Invocation, input processInput) commandkit.Outcom
|
|||||||
var loadErr *ck.LoadError
|
var loadErr *ck.LoadError
|
||||||
if errors.As(err, &loadErr) {
|
if errors.As(err, &loadErr) {
|
||||||
_, explanation := ckconfig.AdaptDiagnostics("Application configuration:\n", loadErr.Sources(), loadErr.Diagnostics())
|
_, 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.Usage(loadErr.Error(), explanation.Details...).WithCause(err)
|
||||||
}
|
}
|
||||||
return commandkit.Failure(loadErr.Error(), explanation.Details...).WithCause(err)
|
return commandkit.Failure(loadErr.Error(), explanation.Details...).WithCause(err)
|
||||||
|
|||||||
@@ -15,11 +15,13 @@ type processInput struct {
|
|||||||
Output commandkit.Supplied[string]
|
Output commandkit.Supplied[string]
|
||||||
Debug commandkit.Supplied[bool]
|
Debug commandkit.Supplied[bool]
|
||||||
Overrides []ck.Override
|
Overrides []ck.Override
|
||||||
|
GlossaryFiles []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func processCommand() commandkit.Command {
|
func processCommand() commandkit.Command {
|
||||||
stringValue := func(s string) (string, error) { return s, nil }
|
stringValue := func(s string) (string, error) { return s, nil }
|
||||||
flags := []commandkit.Flag[processInput]{
|
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: "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: "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 }),
|
commandkit.ValueFlag(commandkit.FlagMeta{Name: "output", Usage: "Transcript output file."}, (*string)(nil), stringValue, func(in *processInput, v commandkit.Supplied[string]) { in.Output = v }),
|
||||||
|
|||||||
@@ -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")
|
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}
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
loaded := acquired.Result
|
||||||
runtime, err := llm.NewRuntime(ctx, loaded, nil)
|
runtime, err := llm.NewRuntime(ctx, loaded, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, &runtimePreparationError{cause: err}
|
return nil, &runtimePreparationError{cause: err}
|
||||||
@@ -69,6 +70,7 @@ func loadProcessInputs(ctx context.Context, input processInput, mode commandkit.
|
|||||||
raw = nil
|
raw = nil
|
||||||
}
|
}
|
||||||
protected := append([]string(nil), runtime.SourcePaths...)
|
protected := append([]string(nil), runtime.SourcePaths...)
|
||||||
|
protected = append(protected, acquired.GlossarySources...)
|
||||||
for _, entry := range loaded.Sources() {
|
for _, entry := range loaded.Sources() {
|
||||||
if entry.Kind == ck.SourceKindFile && entry.Path != "" {
|
if entry.Kind == ck.SourceKindFile && entry.Path != "" {
|
||||||
protected = append(protected, entry.Path)
|
protected = append(protected, entry.Path)
|
||||||
|
|||||||
90
internal/cli/runtime_glossary_test.go
Normal file
90
internal/cli/runtime_glossary_test.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user