Template Evolution

Template Evolution is a self-updating code template feature: when a DAG step completes with a high Critic score, BioCortex saves that code as a stable template for the same (skill, step). On the next run, the Executor reuses the template and only adapts paths and parameters, instead of generating code from scratch. This reduces repeated failures (e.g. API version mismatches) and improves success rates for the same workflow over time.

How It Works

  1. During execution — For each DAG node, the Executor checks whether a stable template exists for the current task’s matched skill and step name. If yes, it injects that template into the prompt and instructs the model to use it and only adapt paths/params.
  2. After a step passes — If the step completes successfully and the Critic score is ≥ the threshold (default 0.9), the Orchestrator saves:
    • The executed code
    • The score and timestamp
    • An optional environment snapshot (e.g. scanpy, squidpy, anndata, numpy versions) to the template repository, keyed by (skill_id, step_name).
  3. Next run — The same analysis type matches the same skill; the same step names come from the Planner. The Executor loads the stored template and prefers it, so that step “does not re-optimize” and reuses the previously validated code.

Configuration

OptionDefaultDescription
template_evolution.enabledtrueTurn template evolution on or off.
template_evolution.score_threshold0.9Only record and reuse templates when Critic score ≥ this value.
template_evolution.storage_dirdata/template_evolutionDirectory where (skill_id, step_name).json files are stored.
template_evolution.capture_envtrueWhether to record library versions (scanpy, squidpy, etc.) when saving a template.

Environment Variables

You can override without changing code:
VariableExampleDescription
BIOCORTEX_TEMPLATE_EVOLUTIONfalseDisable template evolution.
BIOCORTEX_TEMPLATE_THRESHOLD0.9Score threshold (0.0–1.0).
BIOCORTEX_TEMPLATE_STORAGE_DIRdata/template_evolutionStorage directory for template JSON files.

Storage Format

Each template is stored as a JSON file under storage_dir, with a filename derived from skill_id and step_name (e.g. anndata_load_and_qc_spatial_data.json). Contents include:
  • skill_id, step_name
  • code — the executed code that passed validation
  • score — Critic score
  • env_snapshot — optional dict of library versions
  • timestamp — when it was saved
Only one template per (skill_id, step_name) is kept; a new run overwrites only if its score is the existing one.

When to Use It

  • Recommended for workflows you run often (e.g. spatial transcriptomics SVG detection, single-cell QC + clustering). After a few successful runs with score ≥ 0.9, those steps will reuse stable code and run faster and more reliably.
  • Optional to disable (e.g. BIOCORTEX_TEMPLATE_EVOLUTION=false) if you want every run to generate fresh code, or while debugging template behavior.

Relation to Other Features

  • Self-Correction — Runs first (reflection + retry). Template evolution only records code after a step has passed validation. If a step keeps failing, Self-Correction keeps trying; once it passes with score ≥ 0.9, the template is saved.
  • SKILL / workflows — The matched skill comes from the task description (same as Planner/Executor). Step names come from the DAG (Planner output). So the same “analysis type” and “step name” consistently map to the same template key.
For the full design and optional extensions (e.g. multi-draft optimizer, evolution-lite), see the internal doc: docs/CODE_OPTIMIZER_PLAN.md.