From ac47c239c2a791d32e53f65b743dd5293ea2deef Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Mon, 23 Nov 2026 16:56:17 +0000 Subject: [PATCH] Protect operator schema files from output collisions --- docs/operations.md | 2 +- docs/roadmap/remediation.md | 5 ++ internal/cli/schema_collision_test.go | 72 +++++++++++++++++++++ internal/framework/llm/source_paths.go | 32 +++++---- internal/framework/llm/source_paths_test.go | 8 ++- 5 files changed, 103 insertions(+), 16 deletions(-) create mode 100644 internal/cli/schema_collision_test.go diff --git a/docs/operations.md b/docs/operations.md index a6315d4..da4b680 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -43,7 +43,7 @@ requires a transcript file or `/dev/null`; it suppresses optional messages, not failures. Stdout carries transcript JSON when selected. Before processing, Audita checks destination collisions against the transcript, -consumed application and PromptKit sources, and effective host message files. An +consumed application and PromptKit sources, and effective host message and schema files. An intentional in-place transcript replacement is allowed. A regular transcript output is prepared in a sibling temporary file, leaving the final destination untouched until successful publication. Pipes and devices are opened only for diff --git a/docs/roadmap/remediation.md b/docs/roadmap/remediation.md index 6f32b89..fdc2c2b 100644 --- a/docs/roadmap/remediation.md +++ b/docs/roadmap/remediation.md @@ -107,6 +107,11 @@ to address this finding. ## R3: Include Operator Schema Files in Existing Output-Collision Checks +**Complete.** Effective schema files now share origin-based consumed-input +tracking with message files. Source-local/inherited paths, transcript/report +collisions before writes, and successful noncolliding processing pass focused +LLM and assembled CLI tests. Operational guidance lists schema protection. + **Priority:** P2 — consumed-input preservation and runtime integration. **Affected code:** diff --git a/internal/cli/schema_collision_test.go b/internal/cli/schema_collision_test.go new file mode 100644 index 0000000..0de7d76 --- /dev/null +++ b/internal/cli/schema_collision_test.go @@ -0,0 +1,72 @@ +package cli + +import ( + "bytes" + "context" + "fmt" + commandkit "go.maximumdirect.net/commandkit" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "sync/atomic" + "testing" +) + +func TestProcessingProtectsOperatorSchemas(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() + schemaName := "contract.json" + if mode == "report" { + schemaName = "errors.yml" + } + schema := filepath.Join(dir, schemaName) + original := `{"type":"object","required":["corrections"],"properties":{"corrections":{"type":"array"}}}` + files := map[string]string{ + "config.yml": "version: 3\nllm: {proposal_profile: fixture}\npipeline:\n stages: [{module: grammar}]\n", + "input.json": `[{"speaker":"Alice","start":0,"end":1,"text":"Hello."}]`, + schemaName: original, + "prompts.yml": fmt.Sprintf("profiles:\n - id: fixture\n endpoint: %s/v1\n model: test\n api_key_required: false\ncollections:\n audita:\n prompts:\n - id: modules.grammar.proposal\n messages: [{role: user, content: Return an empty correction set.}]\n output:\n format: json\n validation_mode: json_schema\n schema_file: %s\n", server.URL, schemaName), + } + 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 = schema + } + 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"), "--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 err=%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 err=%s", result.Status, calls.Load(), stderr.String()) + } + if _, err := os.Stat(filepath.Join(dir, "recoveries.yml")); !os.IsNotExist(err) { + t.Fatalf("report created before collision check: %v", err) + } + } + raw, err := os.ReadFile(schema) + if err != nil || string(raw) != original { + t.Fatalf("schema changed: %s %v", raw, err) + } + }) + } +} diff --git a/internal/framework/llm/source_paths.go b/internal/framework/llm/source_paths.go index 3c6d952..9a9190e 100644 --- a/internal/framework/llm/source_paths.go +++ b/internal/framework/llm/source_paths.go @@ -20,22 +20,28 @@ func capturedSourcePaths(loaded *ck.Result[appconfig.Config]) ([]string, error) } } metadata := loaded.Metadata() + captureFile := func(fieldPath ck.Path, filename *string) { + if filename == nil { + return + } + field, ok := metadata.Lookup(fieldPath) + if !ok { + return + } + for _, location := range field.Origin.Locations { + if host := hostByID[location.SourceID]; host != "" { + paths[filepath.Join(filepath.Dir(host), filepath.FromSlash(*filename))] = struct{}{} + } + } + } capture := func(base ck.Path, prompts []pk.PromptSettings) { for i, prompt := range prompts { - messages := base.Index(i).Key("messages") + record := base.Index(i) + if prompt.Output != nil { + captureFile(record.Key("output").Key("schema_file"), prompt.Output.SchemaFile) + } for j, message := range prompt.Messages { - if message.ContentFile == nil { - continue - } - field, ok := metadata.Lookup(messages.Index(j).Key("content_file")) - if !ok { - continue - } - for _, location := range field.Origin.Locations { - if host := hostByID[location.SourceID]; host != "" { - paths[filepath.Join(filepath.Dir(host), filepath.FromSlash(*message.ContentFile))] = struct{}{} - } - } + captureFile(record.Key("messages").Index(j).Key("content_file"), message.ContentFile) } } } diff --git a/internal/framework/llm/source_paths_test.go b/internal/framework/llm/source_paths_test.go index 6744a0d..98b3ebc 100644 --- a/internal/framework/llm/source_paths_test.go +++ b/internal/framework/llm/source_paths_test.go @@ -17,10 +17,12 @@ func TestRuntimeTracksCapturedConfigurationAndInheritedMessageFiles(t *testing.T for name, content := range map[string]string{ "config.yml": "version: 3\n", "prompts.yml": "config:\n imports:\n - path: catalog/arbitrary-name.yaml\n required: true\n", - "catalog/arbitrary-name.yaml": "prompts:\n - id: source\n messages:\n - role: user\n content_file: instruction.md\n output:\n format: text\n validation_mode: none\n", + "catalog/arbitrary-name.yaml": "prompts:\n - id: source\n messages:\n - role: user\n content_file: instruction.md\n output:\n format: json\n validation_mode: json_schema\n schema_file: schema.json\n", + "catalog/schema.json": "{}", + "prompts/shared/schema.json": "{}", "catalog/instruction.md": "Captured operator message.\n", "prompts/nested/override.yaml": "prompts:\n - id: literal-child\n extends: {id: source}\n", - "prompts/nested/collection.yml": "collections:\n operator:\n prompts:\n - id: local\n messages:\n - role: user\n content_file: ../shared/collection.md\n output:\n format: text\n validation_mode: none\n", + "prompts/nested/collection.yml": "collections:\n operator:\n prompts:\n - id: local\n messages:\n - role: user\n content_file: ../shared/collection.md\n output:\n format: json\n validation_mode: json_schema\n schema_file: ../shared/schema.json\n", "prompts/shared/collection.md": "Message in a collection.\n", } { file := filepath.Join(dir, name) @@ -44,6 +46,8 @@ func TestRuntimeTracksCapturedConfigurationAndInheritedMessageFiles(t *testing.T filepath.Join(dir, "prompts.yml"), filepath.Join(dir, "catalog/arbitrary-name.yaml"), filepath.Join(dir, "catalog/instruction.md"), + filepath.Join(dir, "catalog/schema.json"), + filepath.Join(dir, "prompts/shared/schema.json"), filepath.Join(dir, "prompts/nested/override.yaml"), filepath.Join(dir, "prompts/nested/collection.yml"), filepath.Join(dir, "prompts/shared/collection.md"),