AST Mutations
AST (Abstract Syntax Tree) mutations are the core of HezGene's deterministic evolution engine. Unlike LLM-based code generation, AST mutations are mathematically precise, repeatable, and cannot hallucinate.
What is an AST?
An Abstract Syntax Tree is a tree representation of your source code's structure. Every variable, loop, function call, and operator becomes a node in this tree. HezGene parses your Python code into an AST using Python's built-in ast module, transforms specific nodes, and then converts the modified tree back into valid Python code.
Mutation strategies
| Strategy | What it does | Example |
|---|---|---|
| Loop Unrolling | Replaces for-loops with list comprehensions | for x in items: result.append(x*2) → [x*2 for x in items] |
| Guard Clause | Converts deep nesting to early returns | Removes unnecessary else blocks after returns |
| Variable Inlining | Eliminates unnecessary intermediate variables | temp = x+1; return temp → return x+1 |
| Builtin Functions | Replaces manual logic with Python builtins | Manual min/max loops → min()/max() |
| Combined Operations | Merges multiple passes into single-pass | 3 separate loops → 1 generator expression |
How AST mutation works
- Parse: Python source code is parsed into an AST using
ast.parse() - Transform: A
NodeTransformerwalks the tree and applies the mutation strategy - Unparse: The modified AST is converted back to Python source code
- Validate: The new code is syntax-checked and submitted to the Fitness Arena
Zero hallucination guarantee
AST mutations are pure syntax transformations. They cannot invent new variable names, import unknown libraries, or produce syntactically invalid code. Every transformation is structure-preserving.
AST vs LLM mutations
| Property | AST Mutations | LLM Mutations |
|---|---|---|
| Deterministic | Yes | No |
| Requires internet | No | Usually |
| Can hallucinate | No | Yes |
| Creativity | Low | High |
| Speed | Instant | Seconds per call |
| Cost | Free | API costs |
HezGene uses AST mutations as the primary engine and LLM mutations as an optional booster. The --llm flag adds LLM-generated mutants to the arena alongside AST mutants. The best one wins regardless of origin.
Was this page helpful?