> 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/research/grammar-v2.md).

# Grammar V2 Loop

`grammar_v2` lives in:

```
packages/research/agent_loops/grammar_v2_loop.py
```

It is the current baseline loop for robot body generation.

## What it emits

The loop emits a compact `RobotDesignProgram`, then uses the pipeline compiler to produce:

* expanded robot graph,
* validation messages,
* component program,
* HITL payload,
* debug state.

## Default grammar catalog

`build_default_grammar_catalog_v2()` defines a small derivation grammar with symbols such as:

* `S`,
* `body`,
* `body_chain`,
* `segment`,
* `body_joint`,
* `limb_mount`,
* `limb_pair`,
* `limb`,
* `upper_limb`,
* `limb_joint`,
* `lower_limb`,
* `contact_surface`.

The default rule set includes patterns for:

* worm or snake-like chained bodies,
* quadruped bodies,
* bilateral limb pairs,
* two-link limbs with contact surfaces.

## Default component catalog

`build_default_component_catalog_v2()` maps graph symbols into component primitives such as:

* connectors,
* capsules,
* joints,
* contact surfaces.

This is the bridge from grammar symbols to component-level body structure.

## Deterministic fallback

If no program-building model is configured, the loop uses a fallback program:

* prompts containing words like worm, snake, serpentine, or crawl become a chained body,
* other prompts generally become a quadruped-style program.

This fallback is deliberate. It keeps local tests and demos usable without a live LLM.

## Loop steps

```mermaid
flowchart TD
    A["Prompt"] --> B["Build or receive program"]
    B --> C["Validate selected rules and targets"]
    C --> D["Expand derivation graph"]
    D --> E["Validate graph topology"]
    E --> F["Lower graph to component program"]
    F --> G["Create HITL payload"]
    G --> H["Return AgentLoopResult"]
```

## HITL content

The HITL payload can include:

* summary,
* compile-safe flag,
* graph node and edge counts,
* symbol counts,
* component summary,
* selected `RobotDesignProgram`,
* validation messages,
* candidate metadata.

The product route reads this payload to create frontend candidates.

## Why this loop matters

`grammar_v2` is the cleanest current answer to "how robot bodies are generated." It does not ask the frontend to invent geometry. It produces a structured derivation that the pipeline can expand, validate, and lower deterministically.


---

# 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/research/grammar-v2.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.
