> 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/legacy-grammar-loop.md).

# Legacy Grammar Loop

The legacy loop lives in:

```
packages/research/agent_loops/grammar_loop.py
```

It is registered as:

```
grammar
```

## What it is

The legacy loop is a modular agent loop that builds and evaluates structural grammar rules. It predates the V2 `RobotDesignProgram` path.

## Modular loop API

The loop uses local primitives such as:

* `ModularAgentLoop`,
* `Node`,
* `add_edge`,
* `add_router`,
* node insertions.

These make the loop app-testable and configurable without requiring the product API to know its internal graph.

## Typical nodes

The loop can include nodes for:

* query normalization,
* state or checklist construction,
* structural rule generation,
* node-name resolution,
* structural rule compilation,
* evaluation,
* HITL summarization.

## LangGraph behavior

The loop can run without automatic LangGraph invocation unless the relevant environment flag enables it. This keeps unit tests from requiring every LangGraph runtime path.

## Difference from grammar V2

| Area                | Legacy `grammar`          | `grammar_v2`                                                 |
| ------------------- | ------------------------- | ------------------------------------------------------------ |
| Body representation | Structural rules          | `RobotDesignProgram` derivation                              |
| Compiler            | Grammar graph helpers     | `robot_program.py` compiler                                  |
| App path            | Still registered          | Current baseline is V2                                       |
| Determinism         | More agent-rule dependent | Deterministic expansion and lowering after program selection |

Use `grammar_v2` or `creative_qd_v2` for new product-facing generation unless there is a specific reason to test the legacy path.


---

# 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/legacy-grammar-loop.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.
