---
name: rille
description: "Draw the decision graph of a MoonBit function: every test (if, guard, match, arm guard, loop), early exit and call in the order the flow meets them, with condition text and file:line, following calls into the module. Use it before reviewing, rewriting or testing a function's branches."
---

# rille

Use this skill when you need to know which tests a MoonBit function makes and
in what order: reviewing a route, planning a rewrite, choosing test cases per
branch, or checking a hand-drawn branch diagram. rille reads the source only
(no types, no running): what it cannot resolve from the source it marks
`unresolved` instead of guessing.

Do not use it to evaluate code, to find what a value is at run time, or for
calls whose meaning depends on inferred types or traits: those stay
unresolved by design.

## Run

The executable runs on the linear-memory wasm backend under a sandbox
policy that allows reading the module and nothing else. Copy
`policy.example.json`, put the absolute path of the module root (the
directory with `moon.mod`) in `fs.read`, then:

```sh
# once rille is published (versions are the maintainer's):
moonx --experimental-policy policy.json Luna-Flow/rille/cmd/rille@<version> \
  graph --module <root> --package <path> --entry <name> --depth 1 --format tests

# until then, from a checkout of Luna-Flow/rille:
moon build --target wasm --release src/cmd/rille
moonrun --policy policy.json _build/wasm/release/build/cmd/rille/rille.wasm \
  graph --module <root> --package <path> --entry <name> --depth 1 --format tests
```

## Commands

```text
rille graph --module <root> --package <path> --entry <name>
            [--depth N] [--format json|mermaid|dot|text|tests] [--target T]
            [--width N | --no-truncate]
rille parse <dir>...
rille version
rille help
```

- `--module`: the module root. `--package`: the package path below the
  module's source directory (`interval` for `src/interval` when `moon.mod`
  says `source = "src"`; `""` or omitted for the root package).
- `--entry`: a function `name` or a method `Type::name`. A bare name that
  only methods have fails and names them (`did you mean `Binary::add_ctx`?`).
- `--depth`: how many call levels below the entry to expand (default 1).
  Start with 0 or 1; depth 2 of a large function has hundreds of nodes.
- `--format`: `tests` (one line per test, best first read), `text` (indented
  tree with every call), `json` (the full graph, below), `mermaid`, `dot`.
- `--target`: which target-specific files to read (default `native`).
- `--width N`, `--no-truncate`: the human formats cut texts at 80
  characters; `--no-truncate` keeps them whole (use it to diff a list
  against a hand graph). JSON is never cut.
- `parse`: checks that every `.mbt` file below the directories parses.
- `version` (or `--version`): rille's version and the commit it was built
  from.

## Reading the output

Every output starts by naming its producer, so a graph document can cite
it: in JSON the `producer` object, in the other formats one comment line
(`#` in `tests` and `text`, `%%` in `mermaid`, `//` in `dot`):

```text
# rille 0.0.2 (commit <hash>); module Luna-Flow/floating (commit <hash>); package Luna-Flow/floating/binary; entry Binary::div_ctx; depth 1; target native
```

Paste that line as it is into any document that uses the graph: the same
rille version, module commit, package, entry, depth and target give the
same output. A commit that cannot be known says why (`commit unknown:
...`):

- rille's own commit is unknown in a build from a source checkout; a
  published version names it.
- The module's commit is read from the checkout's `.git`; under a policy
  that allows only the module root it is `unknown`. To get it, add the
  checkout's `.git` to `fs.read` (for a git worktree: the worktree's `.git`
  file and the main repository's `.git` directory). For a `git archive`,
  cite the archived commit yourself.
- It is `HEAD`'s commit: uncommitted changes are not seen. Draw from a clean
  tree when the graph is to be cited.

`--format tests` prints one line per test after that line, tab-separated:

```text
T3	if	src/interval/interval.mbt:762	a_nonnegative && b_nonnegative	multiplication_bounds
```

number in flow order, kind (`if`, `guard`, `match`, `arm_guard`, `while`,
`for`, `for_in`, `try`), `file:line`, condition as written, and the function it is
in (an expanded callee shows its own name). An `arm_guard` is the guard of a
`match` or `catch` arm (`p if g => ...`): a test made after the pattern
matched; an arm without a guard is not a test. Conditions are cut at 80
characters; `--no-truncate` keeps them whole (use it to diff against a hand
graph), and the JSON form never cuts.

A callee called from two places is expanded at each call, so its tests
appear twice with different numbers. The JSON `site` of a node names the
function and the place in it independently of the call: two runs of tests
with the same sites are the same tests of the same function. Pure accessors
(such as one `match self.0`) are listed like any test; nothing is filtered.

`--format json` prints one document of schema `rille.graph/1`:

```text
{ "schema": "rille.graph/1",
  "producer": { "name", "version", "commit" | null, "commit_unknown",
                "module", "module_commit" | null, "module_commit_unknown" },
  "module", "package", "entry", "depth", "target", "root",
  "nodes": [                                  pre-order: the order the flow meets them
    { "id",                                   path of edges from the root
      "site",                                 function and path inside it: stable across callers
      "kind": fn | if | guard | match | arm | arm_guard | while | for | for_in
            | try | return | raise | abort | panic | call | closure,
      kind texts: function | cond | scrutinee | pattern | binders, iterable
                  | value | args | callee, resolution, target_unknown | text,
      "span": { "file", "start": [line, col], "end": [line, col] },
      "edges": [ { "kind": body | cond | then | else | arm | guard | call | return,
                   "to": <id> } ] } ] }
```

A call's `resolution`:

| `status` | Meaning | What to do |
| --- | --- | --- |
| `expanded` | the callee (`target`) is followed; its body is the child under the `call` edge | read it as part of the flow |
| `depth_limit` | resolved to `target`, not followed | raise `--depth`, or draw `target` as its own entry |
| `back_edge` | `target` is already on the path (recursion) | read the earlier expansion |
| `local` | a local function or closure | its body is not walked |
| `unresolved` | not resolved; `reason`, `message`, maybe `candidates` | see below |

`unresolved` reasons: `untyped_receiver` (a method on a value whose type is
not written; `candidates` lists the package's methods of that name, none
chosen: decide from the code which one, or draw it as its own entry),
`missing_method` (a trait method or a type outside the module, such as
`Int::max`), `no_definition`, `not_imported`, `outside_module` (a package
that is outside the module or cannot be read), `ambiguous` (two or more
definitions for the target), `expression_callee`, `extern_stub`. Operators
(`a * b`) are not calls, and `&&` / `||` stay inside a test's text.

`target_unknown: <file>` on a call: the callee's file has a target condition
in `moon.pkg` that rille does not understand, so the file was read for every
target and the resolution may be wrong or ambiguous because of it. Check the
file's condition by hand.

## Exit status

| Status | Meaning |
| --- | --- |
| 0 | success |
| 1 | a defect of rille |
| 2 | usage error |
| 3 | a needed file cannot be read: missing, or the policy does not allow reading it (widen `fs.read`) |
| 4 | a broken `moon.mod` or `moon.pkg`, or `rille parse` found files that do not parse |
| 5 | the entry cannot be drawn: no definition (a bare name that only methods have names them: `did you mean `Binary::add_ctx`?`), two or more, an `extern` stub |

Failures print `rille: <message>` on standard output.

## After the work

When rille served a task, record in the task's notes what the
[README](../README.md#measuring-the-benefit) lists: time with rille against
time by hand, agent tokens where known, decisions rille found that a
reading by hand missed, results of rille that were wrong or incomplete
(file each as an issue of Luna-Flow/rille with a minimal source), what
followed, and the producer lines of the graphs used.
