Skip to main content
Module: gaia_agent_code.validators Import: from gaia_agent_code.validators import SyntaxValidator, ASTAnalyzer, AntipatternChecker

Components: SyntaxValidator, ASTAnalyzer, AntipatternChecker

Overview

The code validators subsystem provides comprehensive Python code quality checking including syntax validation, AST parsing/analysis, and anti-pattern detection. These components work together to ensure generated code meets quality standards before execution or storage. Key Features:
  • Python syntax validation with detailed error messages
  • AST parsing for code structure analysis
  • Symbol extraction (functions, classes, variables, imports)
  • Anti-pattern detection (naming, complexity, code smells)
  • Configurable quality thresholds
  • Integration with CodeAgent

Requirements

Functional Requirements

SyntaxValidator

  1. Syntax Validation
    • validate() - Validate Python code syntax
    • Compile and AST parse checking
    • Line number and error position tracking
  2. Code Quality Checks
    • check_indentation() - Mixed tabs/spaces, non-standard indentation
    • validate_imports() - Wildcard imports, duplicate imports
    • check_line_length() - Configurable max line length
  3. Error Reporting
    • ValidationResult model with errors list
    • Dictionary format for legacy compatibility
    • SyntaxError extraction

ASTAnalyzer

  1. Code Parsing
    • parse_code() - Parse Python code into AST
    • Symbol extraction (functions, classes, variables)
    • Import statement analysis
  2. Symbol Information
    • Function signatures with type annotations
    • Docstring extraction
    • Line number tracking
  3. AST Utilities
    • extract_functions() - Get all function definitions
    • extract_classes() - Get all class definitions
    • get_docstring() - Extract docstrings

AntipatternChecker

  1. Anti-pattern Detection
    • Combinatorial naming patterns
    • Excessive function/class name length
    • High parameter counts
    • Long functions/files
  2. Complexity Analysis
    • check_function_complexity() - Nesting depth, branches, loops
    • check_naming_patterns() - Naming convention issues
    • Cyclomatic complexity heuristics
  3. Configurable Thresholds
    • MAX_FUNCTION_NAME_LENGTH = 80
    • MAX_FUNCTION_PARAMETERS = 6
    • MAX_FUNCTION_LINES = 50
    • MAX_NESTING_DEPTH = 4

Non-Functional Requirements

  1. Performance
    • Fast syntax checking (< 100ms for typical files)
    • Efficient AST parsing
    • Minimal memory overhead
  2. Reliability
    • Graceful error handling
    • No crashes on malformed code
    • Consistent results
  3. Usability
    • Clear error messages
    • Helpful suggestions
    • Easy to integrate

API Specification

File Locations

SyntaxValidator Interface

ASTAnalyzer Interface

AntipatternChecker Interface

RequirementsValidator Interface

Detects hallucinated packages in requirements.txt — LLMs occasionally emit recursive or nonsensical package names (e.g. -ibm-cloud-ibm-cloud-* or a segment repeated three or more times). RequirementsValidator pattern-matches for these and optionally rewrites the file.
Registered and re-exported from gaia_agent_code.validators:

Implementation Details

Syntax Validation Flow

AST Symbol Extraction

Antipattern Detection


Testing Requirements

Unit Tests

File: tests/agents/code/validators/test_validators.py

Usage Examples

Example 1: Basic Validation

Example 2: AST Analysis

Example 3: Anti-pattern Detection

Example 4: Integrated Validation


Code Validators Technical Specification