# ROM Farmer [![CI](https://github.com/alewman/rom-farmer/actions/workflows/ci.yml/badge.svg)](https://github.com/alewman/rom-farmer/actions/workflows/ci.yml) ![Python 3.10+](https://img.shields.io/badge/python-3.10%2B-blue) ![License: MIT](https://img.shields.io/badge/license-MIT-green) > **🚧 Heavy refactor in progress.** The compiler core (`ir`/`engine`/`planner`/`analysis`/`targets`/`driver`) is stable and strict-typed, but the operator layer is being actively cut down: the old SSH deployment operator ("Farm-Hand") and its LangGraph budget-critic optimizer have just been removed in favor of a leaner, intent-driven MCP surface (`romfarmer.intent`). Expect churn in `cli/`, `mcp/`, and `config/` β€” and stale references in older docs β€” for a while yet. **A content-addressed build compiler for ROM collections, designed to be driven by an AI agent.** You describe a collection β€” platforms, 1G1R rules, formats, a storage budget, a target device. ROM Farmer compiles that description into a deterministic plan, executes only the work that hasn't been done before, and emits a ready-to-play tree for Batocera, RetroBat, ROCKNIX, or any frontend you define. ``` You: "Build a 1G1R English PSX set within 60 GB, prioritize RPGs" resolve β†’ catalog β†’ plan (pure passes) β†’ execute (cached) β†’ emit ``` > ROM Farmer exposes **25 [MCP](https://modelcontextprotocol.io) tools**. Any MCP client β€” VS Code Copilot, Claude Desktop, Cursor, Cline, Windsurf β€” can discover them and operate the whole system. A full CLI exists too; the agent is simply the primary operator. --- ## Why a compiler? Every ROM manager today is a pipeline of steps that mutate files in place. ROM Farmer treats a collection as a *program* and a build as *compiling it for a target*: - **The YAML is the source language.** Platforms, recipes, and target profiles compose declaratively. - **The CAS is the object store.** Every artifact β€” extracted ROM, CHD, 7z, M3U β€” is stored once by SHA-256 and hardlinked into output trees. Build a second target and shared artifacts cost zero bytes. - **SQLite is the action cache.** Each transform is keyed by `sha256(tool, tool_version, params, input hashes)`. Re-running a build re-derives the plan and skips every action already in the cache β€” resume is free, not a feature. - **The plan is data.** `romfarmer plan run --explain` shows why every game was kept or dropped, pass by pass, before a single file is touched. Five phases, each with one frozen input type and one frozen output type: ``` RESOLVE YAML configs β†’ BuildManifest CATALOG manifest + sources + DATs + DB β†’ Catalog (GameUnits: multi-disc atomic, region, rating, generation) PLAN catalog β†’ pure passes β†’ lowering β†’ BuildPlan (Action DAG) EXECUTE plan + CAS + action cache β†’ materialized artifacts in CAS EMIT artifacts + TargetProfile β†’ output tree + gamelist.xml ``` Phases 1–3 never touch the filesystem. Only the executor and the emitter do. | Concept | Lives in | What it is | |---|---|---| | **Pass** | `planner/passes/` | Pure `(Catalog, Manifest, CostModel) β†’ Catalog`. DAT dedup, 1G1R, region, rating, curated lists, cross-generation dedup, budget knapsack. | | **Lowering** | `planner/lowering/` | Per-platform rules that turn a `GameUnit` into an action chain: cartridge, disc, RVZ, WUX, XISOβ†’SquashFS, arcade. (PS3 rule is a stub; see status below.) | | **Transform** | `engine/transforms/` | Cache-unaware tool adapters: `chdman`, `7z`, `mksquashfs`, `extract-xiso`, `dolphin-tool`, `ps3dec`, M3U. | | **Emitter** | `targets/emitters/` | Hardlink materializer + frontend dialects (EmulationStation `gamelist.xml`, extras). | | **TargetProfile** | `config/targets/*.yaml` | Declarative constraints: folder names, extensions, format preferences, media policy. Adding a device is YAML, not code. | The compiler core (`ir/`, `engine/`, `planner/`, `analysis/`, `targets/`) is `mypy --strict` clean, guarded by import-linter contracts, and covered by invariant tests: *build twice β‡’ zero transforms run*; *plan is a pure function of (manifest, catalog)*; *no CAS hash ever leaks into an output filename*. External tools run under a pinned environment with reproducibility flags; `romfarmer doctor --determinism` runs each one twice and diffs the bytes, and CI does the same. --- ## The intent layer ROM Farmer separates **deterministic** work from **judgment** work, and keeps the boundary explicit. An agent talks to `romfarmer.intent` β€” a small, read-only MCP surface (`capabilities`, `inventory`, `validate_spec`, `dry_run`, `write_curated_list`) β€” to negotiate a Spec against a target's real capabilities and a scanned inventory, iterating on rating/budget thresholds until a `dry_run` reports the build fits. The agent is the critic; there's no LLM or LangGraph loop inside the compiler. Once the spec is settled, `romfarmer spec run` hands it to the deterministic RESOLVE β†’ CATALOG β†’ PLAN β†’ EXECUTE β†’ EMIT pipeline above. This replaces an earlier design (SSH deployment operator + LangGraph budget-critic optimizer, both now removed) that mixed judgment and execution in one process β€” see the refactor note at the top of this file. --- ## Quick start ### Install ```bash git clone https://github.com/alewman/rom-farmer.git cd rom-farmer pip install -e ".[dev]" romfarmer init # scaffold config/, dats/, source/, output/ ``` External tools are optional and only needed for the formats you use: `chdman` (CHD), `7z`, `mksquashfs`, `extract-xiso`, `dolphin-tool` (RVZ), `wit`/`wux`, `ps3dec`. See `install-tools.sh`. Shipped configs locate everything through three variables (defaults in parentheses, override in `.env`): `ROMFARMER_SOURCE_ROOT` (`source/`, Myrient layout), `ROMFARMER_ARCHIVE_ROOT` (`source/archive.org`), `ROMFARMER_OUTPUT_ROOT` (`output/`). Point the first at your Myrient mirror and every `config/builds/*.yaml` runs unedited. ### Connect an AI client ```json { "mcpServers": { "romfarmer": { "command": "python3", "args": ["-m", "romfarmer.mcp_server"], "cwd": "/path/to/rom-farmer" } } } ``` The agent reads `AI_TOOLBOX.md` for domain context and discovers the tools on its own. ``` "What platforms do I have in my collection?" "Build a 1G1R English PSX set compressed to CHD" "Connect to my Batocera at batocera.local and scan what's there" "How much space would Saturn + Dreamcast + PSX take?" "Deploy the top-rated PS2 games within 120 GB to my device" ``` ### Or use the CLI ```bash # Plan first β€” pure, no files touched romfarmer plan run nointro-1g1r-eng-7z-retrobat --explain # or a path to a build YAML # Build (dry-run prints the action DAG; resume is implicit via the action cache) romfarmer build run --name nointro-1g1r-eng-7z-retrobat --dry-run romfarmer build run --name nointro-1g1r-eng-7z-retrobat --target rocknix-r36s --storage-budget 512gb # DATs, collection, metadata romfarmer dat import romfarmer dat generate -n "Name" -r reference.dat -o out.dat romfarmer scan directory romfarmer metadata import-arrm # Spec β€” the EXECUTE door for agent-authored intent romfarmer spec validate romfarmer spec run ``` --- ## Configuration A build composes **recipes** (how to process a family of platforms) with a **target** (where the output goes): ```yaml # config/builds/nointro-1g1r-eng-7z-retrobat.yaml name: nointro-1g1r-eng-7z-retrobat target: retrobat-pc recipes: - nointro-7z # cartridge platforms β†’ 7z - nointro-none # platforms that need raw files (3DS, …) output_base: /path/to/output/roms-retrobat post_build: - name: genre-organization type: genre_organize options: { mode: hardlink, merge_small: 3 } ``` | Directory | Contents | |---|---| | `config/platforms/` | ~70 platform definitions (DAT patterns, extensions, disc/cart type) | | `config/recipes/` | Processing recipes: `redump-chd`, `nointro-7z`, `nintendo-disc-rvz`, `xbox-xiso`, `ps3-jb`, `arcade-*`, … | | `config/targets/` | Target profiles: `batocera-pc`, `batocera-steamdeck`, `retrobat-pc`, `rocknix-r36s` | | `config/builds/` | Build specs that compose the above | | `config/curations/` | Curated lists and AI-generated tiers/rescue lists | The cost model learns real compression ratios from every build, so budget predictions improve over time. --- ## MCP tools (25) | Category | Tools | |---|---| | **Collection** | `list_platforms`, `list_builds`, `get_build_status`, `get_platform_stats`, `query_collection`, `calculate_budget`, `get_compression_ratio` | | **DAT files** | `dat_hardware_list`, `dat_hardware_games`, `dat_game_variants`, `dat_search` | | **Clone lists** | `clonelist_diff`, `clonelist_validate`, `clonelist_patch`, `clonelist_metadata_generate` β€” Retool clone-list maintenance when DATs update | | **Metadata** | `scraper_search`, `scraper_game_info`, `scraper_platforms`, `scraper_genres`, `scraper_top_rated` | | **Wikipedia** | `wiki_search`, `wiki_game_info`, `wiki_get_section`, `wiki_stats`, `wiki_find_game` | Plus a separate read-only intent surface (`inventory`, `capabilities`, `validate_spec`, `dry_run`, `write_curated_list`) and MCP resources (platform, build, and DAT configs) and prompts (`build_rom_collection`, `analyze_collection`, `recommend_games`). --- ## Supported platforms | Family | Platforms | |---|---| | **Nintendo** | NES/FDS, SNES, N64, GameCube, Wii/WiiWare, Wii U, GB/GBC/GBA, DS, 3DS, Virtual Boy, PokΓ©mon Mini | | **Sony** | PSX, PS2, PSP, PSP Minis; PS3 JB folders via the legacy pipeline only (see status) | | **Sega** | SG-1000, Master System, Genesis, Mega CD, 32X, Saturn, Dreamcast, Game Gear | | **Microsoft** | Xbox, Xbox 360 (XISO β†’ SquashFS) | | **Arcade** | MAME/HBMAME, FBNeo, Naomi/Naomi 2, Atomiswave, Model 2/3, Chihiro, Triforce, Lindbergh, Hikaru, Namco 246, TeknoParrot | | **Other** | 3DO, PC Engine/SuperGrafx/CD, Neo Geo/CD/Pocket, WonderSwan, MSX, ColecoVision, Intellivision, Vectrex, Atari 2600/5200/7800/Jaguar/Lynx | Sources follow No-Intro and Redump naming; Retool 1G1R DATs are supported directly. --- ## Project status ROM Farmer is a working system that builds and deploys multi-terabyte collections daily, but it is a **single-maintainer project in active development**. What that means concretely: - **Compiler core** (`ir`, `engine`, `planner`, `analysis`, `targets`): strict-typed, invariant-tested, stable contracts. `ActionKey` canonical form is frozen. - **Validated end-to-end on real collections:** cartridge β†’ 7z (full NES set), disc β†’ CHD (single and multi-disc with M3U), arcade passthrough (Neo Geo: identical set to the previous pipeline), GameCube RVZ, Xbox XISO, Wii U WUX. **Not yet exercised through the compiler:** Xbox SquashFS (Batocera). - **PS3 is not in the compiler yet.** Decrypt β†’ JB folder β†’ PSN update/DLC is a multi-input, folder-valued chain that the previous pipeline handled in a 750-line stage; the compiler has the pieces (`ps3dec` transform, `cas/tree.py` Merkle-style `TreeManifest` for folder outputs) but no lowering rule joins them. `romfarmer doctor --build ps3-jb-retrobat` reports this honestly. Existing PS3 libraries built by the previous pipeline are unaffected. - **Operator layer** (`cli`, `mcp`, `metadata`, `config`): broader, older, and less strictly typed. It works; it is being tightened incrementally. - **Not yet done:** CAS garbage collection, a web UI, multi-file CUE/BIN passthrough (CHD is unaffected). - Before a long build: `romfarmer doctor --build ` (seconds) then `romfarmer plan run --explain`. - Architecture decisions and their reasoning are recorded in [`docs/compiler-refactor/`](docs/compiler-refactor/), including what was considered and deliberately *not* built. | | | |---|---| | Python modules | 184 (~43K lines) | | Tests | 677 | | MCP tools | 25 | | Config | 150 YAML files | | Platforms | ~70 definitions | --- ## Development ```bash pip install -e ".[dev]" make check # what CI runs: pytest, ruff, ruff format, import-linter, mypy --strict on the core make test # just the tests make lint-fix # auto-fix lint ``` Tests are hermetic β€” no ROMs, DATs, or external tools required. CI runs on Python 3.10, 3.12, and 3.13. --- ## How it compares | | ROM Farmer | RomM | Igir | |---|---|---|---| | **Interface** | AI agent (MCP) + CLI | Web UI | CLI | | **Model** | Content-addressed build compiler | Library manager | File copier/sorter | | **Incremental** | Action cache keyed by input hashes | β€” | β€” | | **Format transforms** | CHD, RVZ, XISO, SquashFS, 7z, WUX | β€” | Archive only | | **Storage budget** | Rating-priority knapsack + learned cost model | β€” | β€” | | **Metadata** | ScreenScraper, ARRM import, Wikipedia, MobyGames scores | IGDB, MobyGames | β€” | --- ## License MIT ## Acknowledgments - **[No-Intro](https://no-intro.org)**, **[Redump](http://redump.org)**, and **[Retool](https://github.com/unexpectedpanda/retool)** β€” preservation DATs and 1G1R clone lists - **[Model Context Protocol](https://modelcontextprotocol.io)** β€” the universal AI tool interface - **[Bazel](https://bazel.build)** and **[Nix](https://nixos.org)** β€” the build-system ideas this borrows from, and the ones it deliberately doesn't - **[Igir](https://github.com/smart-retro/igir)** β€” inspiration for ROM management workflows - **[Voyager](https://voyager.minedojo.org)** β€” inspiration for auto-captured skill libraries