Documentation
¶
Overview ¶
Package validation is the Stage-1 namespace for generated-code validation, schema validation, test loops, and lint loops. See ../REFACTOR_PLAN.md.
Index ¶
- func AutoFix(code string, issues []GenIssue) string
- func BuildSchemaPrompt(schema *Schema) string
- func BuildTestFeedback(result *TestResult, editedFiles []string) string
- func DefaultLintCommands() map[string]string
- func DefaultTestCommands() map[string]string
- func DetectTestCommand(projectDir string) string
- func ExtractCodeFromOutput(text string, language string) (string, error)
- func ExtractJSONFromOutput(text string) (string, error)
- func FilterRelevantOutput(output string, maxChars int) string
- func FormatValidation(v *GenValidation) string
- func ParseFailedTests(output string, language string) []string
- func RepairJSON(broken string) (string, error)
- type FieldSpec
- type GenCheck
- type GenIssue
- type GenValidation
- type GenValidator
- type LintLoop
- func (ll *LintLoop) BuildReflectedMessage(result *LintResult) string
- func (ll *LintLoop) RecordReflection(path string) int
- func (ll *LintLoop) ReflectionCount(path string) int
- func (ll *LintLoop) Reset()
- func (ll *LintLoop) ResetFile(path string)
- func (ll *LintLoop) RunLint(path string) (*LintResult, error)
- func (ll *LintLoop) ShouldRetry(reflectionCount int) bool
- type LintResult
- type Schema
- type SchemaValidationError
- type SchemaValidationResult
- type SchemaValidator
- type TestLoop
- type TestResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildSchemaPrompt ¶
BuildSchemaPrompt generates LLM instructions describing the expected output format.
func BuildTestFeedback ¶
func BuildTestFeedback(result *TestResult, editedFiles []string) string
BuildTestFeedback formats test failures into agent-readable feedback that can be injected into the next agent iteration for self-correction.
func DefaultLintCommands ¶
DefaultLintCommands returns the standard set of lint commands for common languages.
func DefaultTestCommands ¶
DefaultTestCommands returns the standard set of test commands for common project types.
func DetectTestCommand ¶
DetectTestCommand inspects the project directory for known build/config files and returns the appropriate test command. Returns "" if no project type is detected.
func ExtractCodeFromOutput ¶
ExtractCodeFromOutput finds code blocks in LLM output.
func ExtractJSONFromOutput ¶
ExtractJSONFromOutput finds JSON content in LLM output text.
func FilterRelevantOutput ¶
FilterRelevantOutput keeps only failure-related lines from test output, removes noise (progress bars, timing info, success messages), and truncates to maxChars.
func FormatValidation ¶
func FormatValidation(v *GenValidation) string
FormatValidation renders a GenValidation into a human-readable string.
func ParseFailedTests ¶
ParseFailedTests extracts failed test names from test output based on language.
func RepairJSON ¶
RepairJSON attempts to fix common LLM JSON mistakes.
Types ¶
type FieldSpec ¶
type FieldSpec struct {
Type string // "string", "number", "boolean", "array", "object"
Required bool
MinLength int
MaxLength int
Pattern string
Enum []string
Description string
}
FieldSpec describes constraints on a single field.
type GenCheck ¶
type GenCheck struct {
Name string
Language string // empty means all languages
CheckFn func(code, language string) []GenIssue
Severity string // "error", "warning", "info"
}
GenCheck defines a single validation check that can be applied to generated code.
type GenIssue ¶
type GenIssue struct {
Check string
Message string
Line int
Severity string
AutoFixable bool
Fix string
}
GenIssue represents a single problem found in generated code.
func CheckBalancedDelimiters ¶
CheckBalancedDelimiters counts {}, [], (), and <> (for generics) and reports mismatches.
func CheckCompleteness ¶
CheckCompleteness finds leftover placeholder markers like TODO, FIXME, ..., pass, NotImplementedError.
func ValidateGo ¶
ValidateGo uses go/parser to check Go code for syntax errors and common generation issues.
func ValidatePython ¶
ValidatePython checks Python code for indentation consistency and syntax patterns.
func ValidateTypeScript ¶
ValidateTypeScript checks TypeScript code for balanced braces and import/export patterns.
type GenValidation ¶
GenValidation holds the complete result of validating generated code.
type GenValidator ¶
type GenValidator struct {
Checks []GenCheck
// contains filtered or unexported fields
}
GenValidator checks generated code for correctness before writing to disk.
func NewGenValidator ¶
func NewGenValidator() *GenValidator
NewGenValidator creates a GenValidator with built-in checks for common generation errors.
func (*GenValidator) Validate ¶
func (gv *GenValidator) Validate(code, language string) *GenValidation
Validate runs all applicable checks on the given code and returns structured results.
type LintLoop ¶
type LintLoop struct {
// MaxReflections is the maximum number of lint-fix retries per file per session.
// Default is 3.
MaxReflections int
// LintCommands maps file extensions to lint commands. The placeholder {file}
// in the command string is replaced with the actual file path.
LintCommands map[string]string
// Enabled controls whether the lint loop is active.
Enabled bool
// contains filtered or unexported fields
}
LintLoop implements the "reflected_message" pattern from Aider: when an edit tool produces a lint/syntax error, the error is automatically fed back as context for the next iteration so the agent can self-correct.
func NewLintLoop ¶
func NewLintLoop() *LintLoop
NewLintLoop creates a LintLoop with sensible defaults.
func (*LintLoop) BuildReflectedMessage ¶
func (ll *LintLoop) BuildReflectedMessage(result *LintResult) string
BuildReflectedMessage formats a LintResult into a message the agent can understand and act upon in the next iteration.
func (*LintLoop) RecordReflection ¶
RecordReflection increments the reflection counter for a file and returns the new count. Thread-safe.
func (*LintLoop) ReflectionCount ¶
ReflectionCount returns the current reflection count for a file. Thread-safe.
func (*LintLoop) RunLint ¶
func (ll *LintLoop) RunLint(path string) (*LintResult, error)
RunLint executes the appropriate lint command for the given file path. It returns nil if no lint command is configured for the file type, or if the lint passes cleanly. Returns a LintResult with errors if lint fails.
func (*LintLoop) ShouldRetry ¶
ShouldRetry returns true if the number of lint reflections for this file has not yet reached MaxReflections.
type LintResult ¶
type LintResult struct {
// File is the path that was linted.
File string
// Errors contains the individual error/warning lines from the linter.
Errors []string
// ExitCode is the process exit code (0 = pass, non-zero = failure).
ExitCode int
}
LintResult holds the output of a lint command execution.
type Schema ¶
type Schema struct {
Name string
Type string // "json", "yaml", "code", "structured"
Required []string
Fields map[string]FieldSpec
Pattern *regexp.Regexp
Examples []string
}
Schema defines the expected structure of an LLM output.
type SchemaValidationError ¶
SchemaValidationError describes a single schema validation failure.
type SchemaValidationResult ¶
type SchemaValidationResult struct {
Valid bool
Errors []SchemaValidationError
Warnings []string
Extracted interface{}
}
SchemaValidationResult holds the outcome of a schema validation pass.
type SchemaValidator ¶
type SchemaValidator struct {
Schemas map[string]*Schema
// contains filtered or unexported fields
}
SchemaValidator validates LLM output against registered schemas.
func NewSchemaValidator ¶
func NewSchemaValidator() *SchemaValidator
NewSchemaValidator creates a SchemaValidator pre-loaded with built-in schemas.
func (*SchemaValidator) Register ¶
func (sv *SchemaValidator) Register(name string, schema *Schema)
Register adds or replaces a schema by name.
func (*SchemaValidator) Validate ¶
func (sv *SchemaValidator) Validate(schemaName string, output string) *SchemaValidationResult
Validate dispatches validation based on the schema's Type.
func (*SchemaValidator) ValidateJSON ¶
func (sv *SchemaValidator) ValidateJSON(output string, schema *Schema) *SchemaValidationResult
ValidateJSON extracts JSON from output and validates against the schema.
type TestLoop ¶
type TestLoop struct {
// Enabled controls whether the test loop is active.
Enabled bool
// MaxRetries is the maximum number of test-fix retries per file.
// Default is 3.
MaxRetries int
// Commands maps project type identifiers to test commands.
// Auto-detected from project files if not explicitly set.
Commands map[string]string
// Timeout is the maximum duration allowed for a test run.
// Default is 120s.
Timeout time.Duration
// contains filtered or unexported fields
}
TestLoop implements an auto-test loop similar to Aider's auto_test feature. After edits, it automatically runs the project's test suite and feeds failures back to the agent for self-correction.
func NewTestLoop ¶
func NewTestLoop() *TestLoop
NewTestLoop creates a TestLoop with sensible defaults.
func (*TestLoop) RecordRetry ¶
RecordRetry increments the retry counter for a file. Thread-safe.
func (*TestLoop) RetryCount ¶
RetryCount returns the current retry count for a file. Thread-safe.
func (*TestLoop) RunTests ¶
RunTests executes the detected or configured test command for the given project directory and returns a structured TestResult.
func (*TestLoop) ShouldRetry ¶
ShouldRetry returns true if the number of retries for the given file has not yet reached MaxRetries.
type TestResult ¶
type TestResult struct {
// Passed indicates whether all tests passed.
Passed bool
// Output is the combined stdout+stderr from the test command.
Output string
// FailedTests is a list of individual test names that failed.
FailedTests []string
// Duration is how long the test run took.
Duration time.Duration
// ExitCode is the process exit code (0 = pass).
ExitCode int
}
TestResult holds the structured output from a test run.