> ## Documentation Index
> Fetch the complete documentation index at: https://docs.canary.effectiveai.app/llms.txt
> Use this file to discover all available pages before exploring further.

# fragment and module nodes

> Split a procedure across files with a fragment, or reuse a typed block of steps with its own inputs and outputs as a module.

One procedure document can include others. A `fragment` node pastes another file's sections in
place: it is only a way to split a long document. A `module` node runs a reusable block of steps
with its own declared inputs and outputs and its own private names, inside the same run. To start
a separate run of another published procedure, use [`invoke`](https://docs.canary.effectiveai.app/procedures/reference/nodes/invoke).

| You want to | Use | Boundary |
| - | - | - |
| Split one long document into files, for readability | `fragment` | None: it behaves as if written in place |
| Reuse the same steps in several places with a clear contract | `module` | Declared inputs and outputs; private names |
| Run another published procedure as its own run | `invoke` | A separate run with its own version and lifecycle |

## Syntax

```yaml fragment id=intake-checks theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
fragment: './shared/intake-checks.procedure.fragment.md'
```

```yaml module id=underwriting-gate theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
module: './modules/approval-gate.procedure.module.md'
input:
  group: group:underwriting
  summary: $intake.summary
outputs:
  decision: underwriting_decision
```

## Fields

### fragment

| Field | Type | Required | Default | What it does |
| - | - | - | - | - |
| `fragment` | quoted path | Yes | None | A relative path to a file ending in `.procedure.fragment.md` |

### module

| Field | Type | Required | Default | What it does |
| - | - | - | - | - |
| `module` | quoted path | Yes | None | A relative path to a file ending in `.procedure.module.md` |
| `input` | mapping | For each module input without a default | None | A value for each of the module's inputs: a reference or a built value |
| `outputs` | mapping | Yes, one entry per module output | None | Maps each module output to a new name in the including document |

Both fences take no attributes after `id`, and their bodies take no other fields.

Paths are static and relative to the including file. They may go up with `../`, but must stay
inside the procedure's source folder. Every included file must exist when you compile. When you
publish or start a draft, send the included files with the root source, in the request's
`fragments` and `documents` fields. See
[Publish a version](https://docs.canary.effectiveai.app/guides/procedures.md#publish-a-version).

## Fragments

A fragment file starts with front matter that holds only its kind:

````markdown shared/intake-checks.procedure.fragment.md theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
---
kind: procedure-fragment@v1
---

# Intake checks

## Summarize the request

Build the one-line summary both approval gates show.

```yaml value id=summarize-request
schema: intake-summary
output: intake
value:
  summary: 'Endorsement {{ $inputs.endorsement.endorsementId }} on {{ $inputs.endorsement.policyNumber }}'
```

## Intake resources

```yaml schema id=endorsement
type: object
additionalProperties: false
required: [endorsementId, policyNumber]
properties:
  endorsementId: { type: string }
  policyNumber: { type: string }
```

```yaml schema id=intake-summary
type: object
additionalProperties: false
required: [summary]
properties:
  summary: { type: string }
```
````

A fragment has no inputs, outputs, ID or version. When the document is compiled:

* The fragment's H2 to H6 sections are placed under the heading that holds the `fragment` fence,
  one level deeper. Its H1 makes the file readable on its own and isn't included.
* Everything behaves as if written in the including document. Schemas and functions declared in
  either file can be used from the other. `$inputs` and every name mean the same thing.
* Fence IDs and heading addresses must be unique across the combined document.
* Under the heading that holds the `fragment` fence, other executable fences may come before it
  but not after it. Put later steps under a child heading or the next heading.
* A fragment can include fragments and modules.
* A fragment path can be included only once in a document. Cycles and missing files fail
  compilation.
* Errors point at the line in the fragment file, together with the chain of fences that included
  it.

On a run page, the `fragment` fence shows the file's path and opens the fragment as its own
document. A fragment adds nothing at run time: the run behaves exactly as if the sections were
written in place.

## Modules

A module file declares typed inputs and outputs in its front matter:

````markdown modules/approval-gate.procedure.module.md theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
---
kind: procedure-module@v0
inputs:
  group:
    schema: selector
  summary:
    schema: summary-text
outputs:
  decision:
    schema: gate-decision
---

# Approval gate

Ask one group to approve. The gate returns the decision; the caller decides
what a rejection means.

## Ask for approval

The task sees only what the caller passed in `$inputs`.

```yaml task id=approve
assignees:
  - $inputs.group
title: 'Approve: {{ $inputs.summary }}'
instructions: Approve or reject this endorsement.
result:
  schema: gate-decision
output: approval
```

## Record the decision

```yaml value id=record
schema: gate-decision
output: decision
value: $approval.value
```

## Resources

```yaml schema id=selector
type: string
```

```yaml schema id=summary-text
type: string
```

```yaml schema id=gate-decision
type: object
additionalProperties: false
required: [approved]
properties:
  approved: { type: boolean }
  note: { type: string }
```
````

| Front matter | Required | What it does |
| - | - | - |
| `kind` | Yes | `procedure-module@v0` |
| `inputs` | No | The module's inputs, each with a `schema`. An input with a `default` may be left out by the caller |
| `outputs` | No | The module's outputs, each with a `schema` |

A module has no ID, version, triggers, permissions or lifecycle of its own. The root document's
version pins the exact module file.

### Scope

* Inside the module, `$inputs` is the module's own input, set from the fence's `input`. Values
  from the including document aren't visible unless passed in. `$run` is available as usual.
* The module's step IDs, heading addresses, values, schemas, functions, effects and secrets are
  private to each inclusion. Two inclusions of the same module never share state or names.
* Every path that finishes the module must bind each declared output. The fence's `outputs` maps
  each one to a new name in the including document, such as `$underwriting_decision`. Nothing else
  comes out.
* A module can't end the run. `complete`, including a choice case that ends the run, isn't
  allowed inside it. Return data and let the including document decide.
* A module can include other modules and fragments. A fragment included by a module becomes part
  of that module and private to it.

Unlike a fragment, the same module can be included in several places, as the example below does.
A module can also be included in a body of a `choice`, `repeat`, `for-each` or `parallel`.

## Example

An endorsement request passes two approval gates that reuse one module. A fragment holds the
shared intake step and its schemas. The three files sit in one folder: the fragment and module
files above, and this root document.

````markdown endorsement-review.procedure.md theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
---
kind: procedure@v0
id: endorsement-review
inputs:
  endorsement: { schema: endorsement }
outputs:
  result: { schema: endorsement-result }
---

# Endorsement review

Check an endorsement request, then pass it through two approval gates.

## Check the request

The intake checks live in a shared fragment so several procedures can use the
same steps.

```yaml fragment id=intake-checks
fragment: './shared/intake-checks.procedure.fragment.md'
```

## Underwriting gate

```yaml module id=underwriting-gate
module: './modules/approval-gate.procedure.module.md'
input:
  group: group:underwriting
  summary: $intake.summary
outputs:
  decision: underwriting_decision
```

## Billing gate

```yaml module id=billing-gate
module: './modules/approval-gate.procedure.module.md'
input:
  group: group:billing
  summary: $intake.summary
outputs:
  decision: billing_decision
```

## Finish

```yaml complete id=finish
value:
  underwritingApproved: $underwriting_decision.approved
  billingApproved: $billing_decision.approved
```

## Resources

```yaml schema id=endorsement-result
type: object
additionalProperties: false
required: [underwritingApproved, billingApproved]
properties:
  underwritingApproved: { type: boolean }
  billingApproved: { type: boolean }
```
````

The root uses the `endorsement` schema and the `$intake` value from the fragment as if they were
its own. Each gate creates its own task. The module's `approve` step is identified as
`underwriting-gate:approve` in the first gate and `billing-gate:approve` in the second.

## On retry and resume

* **Fragments don't exist at run time.** They are combined into the document when it compiles.
* **Module input is fixed once.** The fence's `input` is evaluated once, when the run enters the
  module.
* **Outputs appear together.** When the module finishes, its outputs are checked against their
  schemas and appear in the including document at once. If the module fails, no output appears.
* **Step identity follows the inclusion ID.** Steps inside a module are identified by the
  inclusion's fence ID and their own ID, such as `underwriting-gate:approve`. Moving the module
  file doesn't change that identity; renaming the inclusion's `id` does. Tests also address
  module steps by these names.

## Common errors

| Code | Cause | Fix |
| - | - | - |
| `PROC-FRAGMENT-101` | A bad `fragment` field or path (`fragment must be a static relative path ending in .procedure.fragment.md`) | Use a quoted relative path to a `.procedure.fragment.md` file |
| `PROC-FRAGMENT-103` | The fragment's front matter isn't valid | Use only `kind: procedure-fragment@v1` |
| `PROC-FRAGMENT-104` | A missing file, a cycle, the same fragment included twice, a path outside the source folder, a duplicate ID, or an executable fence after the `fragment` fence under the same heading (`Executable fences may not follow a transparent fragment directly under the same heading.`) | Fix the path or IDs; move later steps under another heading |
| `PROC-MODULE-001` | A bad `module` field, path, input value or output mapping | Use the fields above |
| `PROC-MODULE-003` | The module's front matter isn't valid, or the module can end the run (`Modules cannot complete the parent run; bind declared outputs instead.`) | Remove `complete` from the module and bind its outputs |
| `PROC-MODULE-004` | A missing module file, a cycle, or a path outside the source folder | Fix the path |
| `PROC-MODULE-005` | An input is missing or not declared, or an output isn't mapped or isn't declared (`Module output "decision" must be mapped.`) | Pass every required input and map every declared output |

## See also

* [invoke node](https://docs.canary.effectiveai.app/procedures/reference/nodes/invoke.md): start a separate run of a published procedure
* [Procedure document](https://docs.canary.effectiveai.app/procedures/reference/document.md)
* [parallel node](https://docs.canary.effectiveai.app/procedures/reference/nodes/parallel.md), [for-each node](https://docs.canary.effectiveai.app/procedures/reference/nodes/for-each.md), [repeat node](https://docs.canary.effectiveai.app/procedures/reference/nodes/repeat.md) and [choice node](https://docs.canary.effectiveai.app/procedures/reference/nodes/choice.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.