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

StrategyWhat it doesExample
Loop UnrollingReplaces for-loops with list comprehensionsfor x in items: result.append(x*2)[x*2 for x in items]
Guard ClauseConverts deep nesting to early returnsRemoves unnecessary else blocks after returns
Variable InliningEliminates unnecessary intermediate variablestemp = x+1; return tempreturn x+1
Builtin FunctionsReplaces manual logic with Python builtinsManual min/max loops → min()/max()
Combined OperationsMerges multiple passes into single-pass3 separate loops → 1 generator expression

How AST mutation works

  1. Parse: Python source code is parsed into an AST using ast.parse()
  2. Transform: A NodeTransformer walks the tree and applies the mutation strategy
  3. Unparse: The modified AST is converted back to Python source code
  4. 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

PropertyAST MutationsLLM Mutations
DeterministicYesNo
Requires internetNoUsually
Can hallucinateNoYes
CreativityLowHigh
SpeedInstantSeconds per call
CostFreeAPI 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.

Previous
Fitness Arena
The 5-Ring Gauntlet explained
LLM Strategies
Optional AI-guided semantic fixes
Next