> For the complete documentation index, see [llms.txt](https://robodex.gitbook.io/robodex-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://robodex.gitbook.io/robodex-docs/pipeline/robot-design-program.md).

# Robot Design Program V2

The core body-generation compiler lives in:

```
packages/pipeline/robot_program.py
```

This is one of the most important files in the repo.

## Core idea

The system does not directly hallucinate a final mesh. It creates a compact, auditable robot program:

```
RobotDesignProgram
  -> ExpandedRobotGraph
  -> ComponentProgram
  -> RobotDesignHitlV2
```

Each step is deterministic after the program is selected.

## Main dataclasses

| Dataclass            | Role                                                                   |
| -------------------- | ---------------------------------------------------------------------- |
| `RobotDesignProgram` | Selected grammar, start symbol, derivation steps, metadata             |
| `DerivationStep`     | One grammar rule application against a target node                     |
| `GrammarCatalog`     | Available grammar symbols and rules                                    |
| `GrammarRule`        | Rule expansion definition                                              |
| `ExpandedRobotGraph` | Expanded graph of generated robot nodes and edges                      |
| `ComponentProgram`   | Lowered component-level body representation                            |
| `RobotDesignHitlV2`  | Reviewable backend-facing payload                                      |
| `ProgramCompilation` | Full compiler result with program, graph, components, errors, warnings |

## Compiler stages

```mermaid
flowchart TD
    A["RobotDesignProgram"] --> B["validate_design_program"]
    B --> C["expand_design_program"]
    C --> D["validate_expanded_graph"]
    D --> E["lower_components"]
    E --> F["validate_component_program"]
    F --> G["make_robot_design_hitl_v2"]
    G --> H["ProgramCompilation"]
```

## Program validation

`validate_design_program()` checks that the program uses known symbols, known rules, valid targets, and valid repeat settings.

This prevents a loop from returning a payload that looks plausible but cannot be expanded against the grammar catalog.

## Graph expansion

`expand_design_program()` applies derivation rules to build an `ExpandedRobotGraph`.

It handles:

* selected target nodes,
* repeated expansions,
* metadata propagation,
* edges between generated nodes,
* stable graph construction.

## Graph validation

`validate_expanded_graph()` catches topology problems such as:

* open nonterminals,
* disconnected graph regions,
* orphan limb-like structures,
* missing contact surfaces,
* invalid or inconsistent generated nodes.

This is where a body becomes more than a text description. It must pass structural checks.

## Component lowering

`lower_components()` maps graph nodes to component primitives using the component catalog.

Examples:

* body segments become capsules or connectors,
* limb links become capsules,
* joints become component joints,
* feet or contact points become contact surfaces.

The result is a lower-level body program that can be materialized or rendered more concretely.

## HITL payload

`make_robot_design_hitl_v2()` produces a human-in-the-loop payload with summary fields the backend can consume.

It can include:

* selected program,
* graph summary,
* symbol counts,
* component summary,
* compile-safe status,
* validation messages,
* candidate metadata.

The product design route reads this HITL payload to build flat frontend candidates.

## Why this file is the body-generation center

Agent loops decide what program to try. `robot_program.py` decides whether that program is structurally meaningful and how it expands into a generated body.

When debugging generated bodies, start here after confirming which loop produced the program.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://robodex.gitbook.io/robodex-docs/pipeline/robot-design-program.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
