AlgoTune Agent
Command-driven algorithm optimizer with evaluation tools, profiling, multi-file edits, and best-snapshot restore.
# AlgoTune Agent
This is a Galapagos-native port of the command agent in the original AlgoTune repository. It keeps
the upstream behavior that matters to the search: one command per model turn, edit-time syntax and
tampering checks, evaluation after edits, reference/input/profile tools, a mutable workspace
separate from the best valid snapshot, explicit `revert`, the $1 model-spend stop, and five full
recent messages per role with older messages shortened.
Galapagos tasks expose `run_solver(problem)` rather than upstream's standalone
`class Solver.solve`. The virtual `solver.py` therefore maps to the complete Galapagos candidate
and retains its required `run_solver` entrypoint. Helper `.py` files, `setup.py` or
`pyproject.toml`, and `.pyx`/`.pxd` files are bundled into that single candidate artifact and
materialized at import time. Cython, Pythran, and DaCe preparation therefore happens before the
timed solver call while task-owned evaluation remains unchanged. The whole loop runs in the
selected task image by default, ensuring the agent's diagnostics and trusted scorer see the same
packages.
The source used for this port is [oripress/AlgoTune](https://github.com/oripress/AlgoTune), local
reference commit `dff9914`. In particular, the behavior was traced through
`AlgoTuner/interfaces/llm_interface.py`, `interfaces/commands`, `editor/editor_functions.py`,
`interfaces/core/message_handler.py`, and `messages/initial_system_message.txt`.
## Galapagos component mapping
| Galapagos role | AlgoTune behavior |
|---|---|
| Population | Keep every evaluator-valid measured workspace and return the best speedup snapshot; invalid command results remain trajectory-only. |
| SelectionPolicy | Continue from the mutable current workspace, independently of the snapshot. |
| PromptBuilder | Initial AlgoTune command instructions, task description, reference `solve`, validator, and budget. |
| Proposer | Parse and execute exactly one `edit`, `delete`, `ls`, `view_file`, `revert`, `reference`, `eval_input`, `eval`, `profile`, or `profile_lines` command. |
| Evaluator | Use the selected `algotune_*` task's scorer without replacing or wrapping its score. |
| Memory | Role-preserving command transcript; keep five recent full messages per role and shorten older messages. |
The port intentionally adapts three storage/harness boundaries, not the agent policy:
- Galapagos' required candidate entrypoint is `run_solver(problem)`, while the standalone upstream
workspace used `class Solver.solve`. The seed already maps the original task class to `run_solver`.
- A Galapagos genome is one text artifact. Virtual helper files are encoded into it and materialized
before import; Cython/pyproject, Pythran, and DaCe preparation happens there, outside the timed
solver call.
- Upstream selects on a development split and restores the snapshot for a held-out split. The port
selects the best genome using the task-owned Galapagos AlgoTune evaluator, whose fresh randomized
instances supply the anti-precomputation boundary. The winning genome is restored logically as
`RunResult.best` and the normal Galapagos `best/` artifact.
Run an experiment:
```bash
export OPENROUTER_API_KEY=...
galapagos run \
--scaffold algotune_agent \
--task algotune_svm \
--set proposer.model_name=openai/o4-mini \
--set proposer.api_base=openrouter \
--set general.max_iterations=9999 \
--output-dir runs/algotune_svm
```
The controller normally stops once provider-reported model cost reaches `$1.00`. Keep an explicit
iteration cap because some local/OpenAI-compatible endpoints report zero cost. The final Galapagos
winner is always the best measured valid snapshot, even when the last edited workspace is broken.
For a host-only smoke run (using the host's installed packages), add
`--set general.eval_mode=local`. The default Docker path is the faithful environment path; its first
run may build the large AlgoTune scientific-toolbox image, while later runs reuse the cached image.
Programmatic construction uses the same registered card:
```python
import galapagos as gx
model = gx.GalapagosModel.from_card("openai/o4-mini", host="openrouter")
config = gx.GalapagosConfig.from_config("algotune_agent", general__max_iterations=9999)
task = gx.GalapagosTask.from_card("algotune_svm")
scaffold = gx.AutoScaffold.from_card("algotune_agent", config=config, model=model)
result = scaffold.run(task=task, run_dir="runs/algotune_svm")
print(result.best_score, result.best.content)
```