rille

    Decision graphs of MoonBit functions: every test, arm and call in order, from the source.

    luna-flow
    moonbit
    static-analysis
    decision-graph
    Download zip
    Author
    Version
    0.0.2
    License
    Apache-2.0
    Last updated
    2 hours ago
    Downloads
    3

    #rille

    rille draws the decision graph of a MoonBit function: every test the function makes (if, guard, match, arm guard, loop condition), every early exit (return, raise, abort, panic) and every call, in the order the flow meets them, each with its text as written and its file:line:col. Calls that resolve to a function of the same module are followed into the callee, up to a depth limit. It reads the source only (the official moonbitlang/parser); it has no type information.

    It is written in MoonBit and serves the feedback loop of floating, where route reviews start from decision graphs that are drawn by hand today.

    Status: v0.2. rille graph draws a graph as JSON, a text tree, a test list, Mermaid or DOT, and every output names the rille and the source commit that produced it; rille parse checks that a source tree parses. Against two graphs of floating drawn by hand, rille lists every test in the same order at the same line, up to differences of granularity (verification).

    #What rille does and does not do

    It walks one function and every function of the module that a call reaches, to a depth limit, and records each if, else if, guard, match arm and arm guard, while, for and for .. in condition, try handler, return, raise, abort, panic, fail and call, in evaluation order.

    It reads syntax only. A call is followed when the source says which function it calls:

    CallFollowed to
    f(...)the function f of the package, or the one using @p { f } brings in
    @p.f(...)f in the package that @p imports, inside the module
    T::m(...), @p.T::m(...)the method m of T, inherent first, then impl ... for T with m
    x.m(...)as T::m, when the type of x is written: an annotated parameter or let, self of a method, or a record field whose declaration writes its type

    These stay in the graph as unresolved calls, with the reason:

    • a method on a value whose type is not written (untyped_receiver; the package's methods of that name are listed as candidates, none is chosen), including any value of an inferred, optional (T?) or function type;
    • a method of a written type that has no inherent method or implementation of that name in its package: a trait method, or a type of core or of a package outside the module such as Int::max (missing_method);
    • a name with no definition (no_definition), an alias the package does not import (not_imported), a package outside the module (outside_module);
    • a name with two or more definitions for the target (ambiguous), a callee that is an expression (expression_callee), an extern stub (extern_stub).

    Operators (a * b, a == b) are not calls, && and || are part of a test's text, and the bodies of closures and local functions are not walked. A file whose target condition in moon.pkg rille does not understand is read for every target, and calls into it are marked target_unknown.

    #Layers

    LayerPackageResponsibility
    VocabularysourceSourceFile, Span
    Syntaxsyntaxthe only importer of moonbitlang/parser: diagnostics and rille's view of a file
    Configurationconfigmoon.mod and moon.pkg: module name and version, imports, target files
    Modelgraphnode and edge kinds, FnKey, SiteId, NodeId, Graph, GraphBuilder, Producer, JSON form
    Resolutionresolvenames to definitions; unresolved as a value with its reason
    Walkwalkthe view of a body to a graph, through GraphBuilder
    Renderingrendertext tree, test list, Mermaid and DOT, from the graph alone
    Edgecmd/rillearguments, files (the module, its git commit), printing

    Dependencies point downward: source depends on nothing; syntax, config and graph on source at most; resolve on syntax and graph; walk on resolve; render on graph; only cmd/rille reads files or prints.

    #JSON schema

    rille graph --format json prints one document of schema rille.graph/1; the graph API states it in full.

    { "schema": "rille.graph/1", "producer": { "name": "rille", "version": <rille version>, "commit": <hash> | null, "commit_unknown": <reason, when null>, "module": <module name>, "module_commit": <hash> | null, "module_commit_unknown": <reason, when null> }, "module": <module name>, "package": <full package path>, "entry": <name or Type::name>, "depth": <N>, "target": <target>, "root": <node id>, "nodes": [ pre-order: the order the flow meets them { "id": <node id>, path of edges from the root: m::f/body0/then1 "site": <site id>, function and path inside it: m::g#body0/arm1 "kind": fn | if | guard | match | arm | arm_guard | while | for | for_in | try | return | raise | abort | panic | call | closure, ...the texts of the kind, as written: fn: function; if, guard, arm_guard, while, for: cond; match: scrutinee; arm: pattern; for_in: binders, iterable; return, raise: value; abort: args; closure: text; call: callee, resolution, target_unknown (when marked), "span": { "file": <path>, "start": [line, col], "end": [line, col] }, "edges": [ { "kind": body | cond | then | else | arm | guard | call | return, "to": <node id> } ] } ] }

    A call's resolution is {"status": "expanded" | "back_edge" |"depth_limit", "target": <fn key>}, {"status": "local", "name": ...} or {"status": "unresolved", "reason": <code>, "message": ..., "candidates":[...]}. Ids and sites are made from the shape of the tree, never from lines: the same source gives the same ids, and an edit above a function does not change its ids.

    Changes of the schema:

    • rille.graph/1: the guard of an arm (p if g => b, in a match or a catch) is a test of its own, a node of kind arm_guard with cond under the arm's guard edge; the steps inside the guard hang under its cond edge. The arm node no longer has a guard field. So the list of tests, and --format tests, holds every guard in source order. The document names its producer (producer: rille's version and commit, the module and the commit checked out at its root) and the target its files were chosen for.
    • rille.graph/0: the first schema (0.0.1).

    #Command line

    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

    rille help describes each format. The human formats (tests, text, mermaid, dot) cut texts at 80 characters; --width N cuts at N and --no-truncate keeps them whole. JSON is the data and is never cut.

    Every output names what produced it: producer in JSON, and a first comment line in the other formats (# in text and tests, %% in mermaid, // in dot) with rille's version and commit, the module and its commit, the package, entry, depth and target. rille version (or --version) prints the version and the commit rille was built from. A build from a source checkout says commit unknown: not embedded; a published build names its commit, written in by the publish workflow (scripts/stamp-build-commit.sh). The module's commit is the HEAD of its git checkout, read from git's files; a sandboxed run sees it only when its policy allows reading the .git.

    Run it from a checkout with moon run src/cmd/rille -- <arguments> (any target: wasm-gc by default, also --target native, wasm or js).

    #As an agent skill

    skill/SKILL.md tells an agent when to call rille, how to run it sandboxed, and how to read its output, unresolved calls and exit statuses; skill/policy.example.json is the policy it runs under.

    #Exit status

    Output goes to standard output; a failure prints rille: <message> and exits with a status that says which kind it is:

    StatusMeaning
    0success (also help and version)
    1a defect of rille itself
    2usage error: unknown command or option, missing value, bad --depth, --format or --width
    3a file or directory the command needs cannot be read: missing, or denied by a sandbox policy (moon.mod, the entry package's moon.pkg, a directory or file of rille parse)
    4a moon.mod or moon.pkg does not parse or lacks its name, or rille parse found files with parser reports (the report is printed, then the status)
    5the 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

    A package other than the entry's that cannot be read does not fail the run: calls into it stay unresolved (outside_module).

    #On wasm, sandboxed

    The linear-memory wasm build runs under moonrun with a policy that allows only what rille needs: reading the module it draws.

    moon build --target wasm --release src/cmd/rille cat > policy.json <<'JSON' { "fs": { "read": ["/path/to/floating/library"] } } JSON moonrun --policy policy.json _build/wasm/release/build/cmd/rille/rille.wasm \ graph --module /path/to/floating/library --package interval --entry mul_interval_impl --format tests

    A policy turns every other access off (writing, network, environment, processes). When it does not allow reading the module, rille says so: rille: cannot read <root>/moon.mod (under moonrun or moonx, the policy mustallow reading the module). moonx runs a package of the Mooncakes registry on the same runtime, with the same policy file, once rille is published (publishing is the maintainer's):

    moonx --experimental-policy policy.json Luna-Flow/rille/cmd/rille@<version> graph ...

    #Reading and citing a graph

    A graph is evidence for later work, so a document that uses one cites what produced it. Copy the producer line of the output as it is, or, from JSON, producer.version, producer.commit, producer.module_commit and the document's package, entry, depth and target:

    # 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

    The same rille version on the same module commit with the same package, entry, depth and target gives the same bytes: the walk is deterministic and ids come from the shape of the code, not from lines. What the line cannot know, it says:

    • commit unknown: not embedded: rille was built from a source checkout. Use a published version, or cite the commit of the checkout yourself.
    • module ... (commit unknown: ...): no .git at or above the module root, or a sandbox policy that does not allow reading it. Allow the .git (see the skill), or cite the commit yourself. When the module is a git archive of a commit, cite that commit.
    • The module commit is HEAD's: changes in the working tree are not seen. Draw from a clean checkout or an archive when the graph is to be cited.

    A program that reads the JSON checks schema first (rille.graph/1; the changes are listed under JSON schema) and reads condition texts from JSON, never from the cut human formats; --format tests--no-truncate gives the same texts with runs of white space made one space.

    #rille on rille

    rille's own walk and resolver are checked with rille: drawn with --depth 0, none of the 103 functions of the core packages (190 tests) tests the same condition twice; the only texts that occur twice in one function are four for .. in loops that make two passes over the same collection. The graphs of the call-handling functions were read for values tested twice under different texts (each such repeat was removed). Walker::call, which decides what becomes of a resolved call, makes three tests, each once:

    flowchart TD n0("fn Walker::call :405") n1["closure (resolution : @graph.Resolution) =#gt; { self.add( parent, edge, Call(callee=callee... :414"] n2{"T1 match def.body :422"} n3["arm Extern :423"] n4["call call :423"] n5["call ignore :423"] n6["arm Code(body) :424"] n7{"T2 if frame.stack.iter().any(k =#gt; k == def.key) :425"} n8["call frame.stack.iter :425"] n9["closure k =#gt; k == def.key :425"] n10["call frame.stack.iter().any :425"] n11["call call :426"] n12["call ignore :426"] n13{"T3 if frame.depth #gt;= self.max_depth :427"} n14["call call :428"] n15["call ignore :428"] n16["call call :430"] n17["call self.add :431"] n18["call self.walk_body :432"] n0 -->|body| n1 n0 -->|body| n2 n2 -->|arm| n3 n2 -->|arm| n6 n3 -->|body| n4 n3 -->|body| n5 n6 -->|body| n7 n7 -->|cond| n8 n7 -->|cond| n9 n7 -->|cond| n10 n7 -->|then| n11 n7 -->|then| n12 n7 -->|else| n13 n13 -->|then| n14 n13 -->|then| n15 n13 -->|else| n16 n13 -->|else| n17 n13 -->|else| n18

    #Roadmap

    rille grows from floating's real tasks: a capability is built when a task has shown it useful (the direction is set in discussion 14).

    • v0: the static graph from syntax. v0.1: an agent tool (every target, wasm under a sandbox policy, moonx, the skill).
    • v0.2: completeness and reproducibility. Arm guards are tests (schema rille.graph/1); every output names its producer and the module's commit; texts are cut only in the human formats.
    • Next: keep the structure of a decision (site) apart from its occurrence in a calling context (id), sharing structure across call sites without losing per-call information, and filtering such as accessors only in presentation (a design note comes first); then partition tables whose paths carry their evidence (statically possible, dynamically observed, proven unreachable, unobserved and unknown); costs per node later. Each step is measured in the floating rounds that use it.

    #Measuring the benefit

    rille is worth what it saves in real work. A floating round that uses rille records, in its feedback note:

    • the time to the first useful output and the analysis time with rille, against the time of the same analysis by hand (measured, or estimated and marked as such);
    • the agent tokens of the analysis, where the harness reports them;
    • decisions that a reading by hand missed and rille found, with file:line;
    • results of rille that were wrong or incomplete, each with a minimal source, filed as an issue of this repository;
    • what followed: tests, changes to the code, and changes to rille;
    • the producer lines of the graphs the round used.

    These notes decide what rille builds next.

    #Development

    just fmt # format just check # type-check the default target just check-all # type-check every target just test # run the tests just info # regenerate pkg.generated.mbti just ready # format, check all targets, regenerate interfaces, test with coverage

    The manual starts at doc/manual/index.md. See CONTRIBUTING.md for code style, testing, documentation and commit conventions.

    #License

    Apache-2.0. See LICENSE and NOTICE.