Add schema selection values and ownership

This commit is contained in:
2026-11-20 15:20:29 +00:00
parent e775a3c116
commit fbe3d427a2
17 changed files with 186 additions and 9 deletions

View File

@@ -2,6 +2,7 @@ package domain
import (
"context"
"io/fs"
"time"
)
@@ -156,6 +157,8 @@ type PromptDefinition struct {
Inputs []PromptInput `yaml:"inputs"`
Templates []PromptMessageTemplate `yaml:"templates"`
Validation OutputContract `yaml:"validation"`
SchemaRoot *ResourceBinding `json:"-" yaml:"-"`
SchemaFile *ResourceBinding `json:"-" yaml:"-"`
}
// PromptInspection is lightweight metadata for prompt requirement selection.
@@ -253,10 +256,18 @@ type ExecutionTarget struct {
type OutputContract struct {
Format OutputFormat `yaml:"format"`
ValidationMode ValidationMode `yaml:"validation_mode"`
Schema any `yaml:"schema,omitempty" json:"schema,omitempty"`
SchemaFile *string `yaml:"schema_file,omitempty" json:"schema_file,omitempty"`
SchemaPath string `yaml:"schema_path"`
RepairAttempts int `yaml:"repair_attempts"`
}
// ResourceBinding identifies a resource in its originating filesystem.
type ResourceBinding struct {
FS fs.FS
Path string
}
// RenderedPrompt represents the prompt after template application.
type RenderedPrompt struct {
SessionID string `json:"session_id,omitempty"`

View File

@@ -22,8 +22,26 @@ func ValidateOutputContract(contract OutputContract) error {
return fmt.Errorf("invalid validation mode: %q", contract.ValidationMode)
}
if contract.ValidationMode == ValidationJSONSchema && strings.TrimSpace(contract.SchemaPath) == "" {
return errors.New("schema_path is required when validation_mode is json_schema")
if contract.ValidationMode == ValidationJSONSchema {
count := 0
if contract.Schema != nil {
count++
}
if contract.SchemaFile != nil {
if strings.TrimSpace(*contract.SchemaFile) == "" {
return errors.New("schema_file must not be blank")
}
count++
}
if contract.SchemaPath != "" {
if strings.TrimSpace(contract.SchemaPath) == "" {
return errors.New("schema_path must not be blank")
}
count++
}
if count != 1 {
return errors.New("exactly one of schema, schema_file, or schema_path is required when validation_mode is json_schema")
}
}
if contract.RepairAttempts < 0 {
return errors.New("repair_attempts must be greater than or equal to 0")

View File

@@ -85,3 +85,34 @@ func TestValidateOutputContract(t *testing.T) {
})
}
}
func TestSchemaSelection(t *testing.T) {
file, blank := "schema.json", " "
for _, tt := range []struct {
name string
schema any
file *string
path string
valid bool
}{
{"missing", nil, nil, "", false},
{"false", false, nil, "", true},
{"true", true, nil, "", true},
{"empty object", map[string]any{}, nil, "", true},
{"file", nil, &file, "", true},
{"blank file", nil, &blank, "", false},
{"conflict", false, &file, "", false},
{"path conflict", map[string]any{}, nil, "schema.json", false},
} {
t.Run(tt.name, func(t *testing.T) {
contract := OutputContract{Format: FormatJSON, ValidationMode: ValidationJSONSchema, Schema: tt.schema, SchemaFile: tt.file, SchemaPath: tt.path}
if err := ValidateOutputContract(contract); (err == nil) != tt.valid {
t.Fatalf("active selection error = %v", err)
}
contract.ValidationMode = ValidationNone
if err := ValidateOutputContract(contract); err != nil {
t.Fatalf("inactive selection: %v", err)
}
})
}
}