# The IR

The canonical, versioned intermediate representation of an AgenticMind graph. Document shape, field rules, control edges, canonical serialization, hashing and migrations.

Source: https://glamorous-boar.cloudvent.net/architecture/ir/

---

The **IR** is the durable source of truth for a workflow's architecture: what is validated, planned, executed, exported, diffed and migrated. Models live in `packages/ir`; the published schema is `ir-v1.0.schema.json`, served at `GET /api/ir/schema` and printed by `agenticmind schema`.

## Document shape

<CodeBlock language="json" filename="hello-pipeline (IR v1)" showCopy={true}>

```
{
  "ir_version": "1.0",
  "project": {"id": "prj_1a2b3c", "name": "Hello pipeline", "description": ""},
  "runtime": {"engine": "agenticmind", "profile": "development"},
  "nodes": [
    {"id": "name", "type": "agenticmind.core.manual_input", "version": "1.0.0",
     "config": {"key": "name", "value_type": "String", "default": "world"},
     "ui": {"position": {"x": 0, "y": 80}}},
    {"id": "greet", "type": "agenticmind.core.text_template", "version": "1.0.0",
     "config": {"template": "Hello, {{ text }}!"},
     "execution": {"timeout_seconds": 5}},
    {"id": "result", "type": "agenticmind.core.output", "version": "1.0.0", "config": {"key": "greeting"}}
  ],
  "edges": [
    {"id": "e1", "kind": "data",
     "source": {"node": "name", "port": "value"}, "target": {"node": "greet", "port": "text"}},
    {"id": "e2", "kind": "data",
     "source": {"node": "greet", "port": "text"}, "target": {"node": "result", "port": "value"}}
  ],
  "metadata": {}
}
```

</CodeBlock>

## Field rules

| Field | Rules |
|---|---|
| `ir_version` | `"1.0"`. Older versions are upgraded by registered migrations before validation. |
| `project.id` | `[A-Za-z0-9_-]{1,64}`. The API forces it to match the resource ID. |
| `nodes[].id` | Stable, `^[A-Za-z_][A-Za-z0-9_-]{0,63}$`, unique. |
| `nodes[].type` | Dotted, at least 3 segments, e.g. `agenticmind.core.output`. Resolved in the registry. |
| `nodes[].version` | Semantic version of the node contract. **Exact match required.** |
| `nodes[].config` | Free JSON at the IR level; validated against the node's own config schema. Secret fields hold `{"$secret": "NAME"}` only. |
| `nodes[].execution` | Optional overrides: `timeout_seconds` (0–3600], `max_attempts` 1–10. |
| `nodes[].ui` | Presentation only (`position`, `label`); never affects execution. |
| `edges[].kind` | `data` (output port → input port) or `control` (branch → `trigger`). |
| `metadata` | Free JSON for tooling, e.g. `metadata.api.endpoints` for export and `metadata.mcp.expose`. |

Unknown fields are rejected everywhere (`extra="forbid"`), so typos fail loudly.

## Control edges

Every node has an implicit control input `trigger` and one or more control output branches (`done` for ordinary nodes).

<CodeBlock language="json" filename="A control edge" showCopy={true}>

```
{"id": "c1", "kind": "control",
 "source": {"node": "check", "port": "true"},
 "target": {"node": "notify", "port": "trigger"}}
```

</CodeBlock>

## Canonical serialization and hashing

`to_canonical_json` sorts nodes and edges by ID and keys alphabetically, omits unset optional structural fields, and preserves user-authored config exactly (including explicit `null`). The **content hash** is SHA-256 of the compact canonical form. Revisions and runs record it, so a run can always be traced to the exact architecture it executed, and GitHub diffs show only real changes.

Export: `GET /api/projects/{id}/export` returns the canonical form; re-import with `POST /api/projects` (`document`) or **New → Import IR…**.

## Migrations

<CodeBlock language="python" filename="Registering a migration" showCopy={true}>

```
from agenticmind_ir import register_migration

@register_migration("1.0", "1.1")
def add_subgraphs(doc: dict) -> dict:
    doc.setdefault("subgraphs", [])
    return doc
```

</CodeBlock>

Migrations operate on raw dicts before validation, chain until the current version, and must preserve all user configuration. Each one gets a compatibility test, and the published schema is regenerated (a test fails on drift).

## Node versioning versus IR versioning

The IR version changes rarely (structure of the document). Node versions change whenever a node's contract changes: new ports, new permissions, different behaviour. Old node versions stay registered, which is why a project saved months ago still validates and runs identically. Adding an enum option to a config (for example a new provider) is backward compatible and does not bump the node version.
