validation

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 26, 2026 License: MIT Imports: 16 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func AutoFix

func AutoFix(code string, issues []GenIssue) string

AutoFix applies auto-fixable issues to the code and returns the corrected version.

func BuildSchemaPrompt

func BuildSchemaPrompt(schema *Schema) string

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

func DefaultLintCommands() map[string]string

DefaultLintCommands returns the standard set of lint commands for common languages.

func DefaultTestCommands

func DefaultTestCommands() map[string]string

DefaultTestCommands returns the standard set of test commands for common project types.

func DetectTestCommand

func DetectTestCommand(projectDir string) string

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

func ExtractCodeFromOutput(text string, language string) (string, error)

ExtractCodeFromOutput finds code blocks in LLM output.

func ExtractJSONFromOutput

func ExtractJSONFromOutput(text string) (string, error)

ExtractJSONFromOutput finds JSON content in LLM output text.

func FilterRelevantOutput

func FilterRelevantOutput(output string, maxChars int) string

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

func ParseFailedTests(output string, language string) []string

ParseFailedTests extracts failed test names from test output based on language.

func RepairJSON

func RepairJSON(broken string) (string, error)

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

func CheckBalancedDelimiters(code string) []GenIssue

CheckBalancedDelimiters counts {}, [], (), and <> (for generics) and reports mismatches.

func CheckCompleteness

func CheckCompleteness(code string) []GenIssue

CheckCompleteness finds leftover placeholder markers like TODO, FIXME, ..., pass, NotImplementedError.

func ValidateGo

func ValidateGo(code string) []GenIssue

ValidateGo uses go/parser to check Go code for syntax errors and common generation issues.

func ValidatePython

func ValidatePython(code string) []GenIssue

ValidatePython checks Python code for indentation consistency and syntax patterns.

func ValidateTypeScript

func ValidateTypeScript(code string) []GenIssue

ValidateTypeScript checks TypeScript code for balanced braces and import/export patterns.

type GenValidation

type GenValidation struct {
	Valid    bool
	Issues   []GenIssue
	Language string
	Score    float64
}

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

func (ll *LintLoop) RecordReflection(path string) int

RecordReflection increments the reflection counter for a file and returns the new count. Thread-safe.

func (*LintLoop) ReflectionCount

func (ll *LintLoop) ReflectionCount(path string) int

ReflectionCount returns the current reflection count for a file. Thread-safe.

func (*LintLoop) Reset

func (ll *LintLoop) Reset()

Reset clears all reflection counts.

func (*LintLoop) ResetFile

func (ll *LintLoop) ResetFile(path string)

ResetFile clears the reflection count for a specific file.

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

func (ll *LintLoop) ShouldRetry(reflectionCount int) bool

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

type SchemaValidationError struct {
	Field    string
	Message  string
	Expected string
	Got      string
}

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

func (tl *TestLoop) RecordRetry(file string)

RecordRetry increments the retry counter for a file. Thread-safe.

func (*TestLoop) ResetFile

func (tl *TestLoop) ResetFile(file string)

ResetFile clears the retry count for a specific file.

func (*TestLoop) RetryCount

func (tl *TestLoop) RetryCount(file string) int

RetryCount returns the current retry count for a file. Thread-safe.

func (*TestLoop) RunTests

func (tl *TestLoop) RunTests(ctx context.Context, projectDir string) (*TestResult, error)

RunTests executes the detected or configured test command for the given project directory and returns a structured TestResult.

func (*TestLoop) ShouldRetry

func (tl *TestLoop) ShouldRetry(file string) bool

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL