Hew packages

hew.dag

Fail-closed runtime DAG parsing, topological sorting, and dependency-ordered value routing for Hew

Use version 0.4.1

hew add hew.dag --version 0.4.1
Declared Hew requirement
>=0.6.1, <0.8.0
Licence
MIT OR Apache-2.0
Published
2026-10-22T10:01:12.540Z

Dependencies

No package dependencies.

Package README

# hew.dag

Runtime-loaded, fail-closed directed acyclic graph parsing and value routing.
Malformed specs, unknown operations, bad node references, and cycles are typed
`DagError` values.

```hew
import hew.dag;

fn main() {
    match dag.run_text("add:1 multiply:10 | 0->1", 3) {
        .Ok(report) => println(f"sink = {dag.sink_output(report)}"),
        .Err(failure) => println(dag.describe(dag.error_of(failure))),
    }
}
```

Run `hew run dag/examples/linear_pipeline.hew` from the ecosystem repository
root for a complete example.

## API surface

**Types**

- `NodeOp` — `Add | Multiply | Square | Identity`, the operation a node applies.
- `NodeSpec { id, op_code, operand }` — one node's compiled operation.
- `Edge { src, dst }` — a directed dependency (`dst` runs after `src`).
- `DagSpec { num_nodes, nodes, edges }` — a compiled graph.
- `RunReport { outputs, order, num_nodes }` — the outcome of a run.
- `RunFailure { err, stopped_at }` — a run failure with the node it stopped at.
- `DagError` — `Empty | Cyclic | UnknownNode(i64) | Malformed(i64) | UnknownOperation(i64) | Dispatch(i64)`.
- `LifecycleState` — the spec's readiness (`lifecycle_for`/`state_name`).

**Building and parsing specs**

- `new_spec() -> DagSpec` / `add_node(spec, op, operand) -> DagSpec` /
  `add_edge(spec, src, dst) -> Result<DagSpec, DagError>` — build a spec
  programmatically.
- `parse_spec(text: string) -> Result<DagSpec, DagError>` — parse the
  `op:operand ... | src->dst ; ...` text grammar (see the module doc comment
  in `dag.hew`).

**Routing**

- `run_text(text, initial) -> Result<RunReport, RunFailure>` — parse and
  route in one call, using the library's own `apply_op` kernel.
- `route_with(spec, initial, dispatch: fn[suspends](i64, i64) -> i64 fails DagError) -> Result<RunReport, RunFailure>` —
  route through a caller-supplied per-node dispatch, called once per node in
  topological order. The dispatch may suspend, so it can ask an actor, and it
  fails with `DagError.Dispatch(node)` when a node cannot produce an output;
  the run then stops with `stopped_at` set to that node. `route_with` always
  suspends, so a caller that cannot suspend uses `run_text` instead.
- `apply_op(op, x, operand) -> i64` — the per-node operation kernel
  (`add`/`multiply`/`square`/`identity`).
- `topo_order(spec) -> Result<Vec<i64>, DagError>` / `validate(spec) -> Result<i64, DagError>` /
  `is_acyclic(spec) -> Result<bool, DagError>` — topological sort and
  validation, refusing cyclic specs.

**Inspecting results**

- `node_count`, `edge_count`, `edge_src`, `edge_dst`, `node_at` — read a
  `DagSpec`.
- `output_at(report, id)`, `sink_output(report)` — read a `RunReport`.
- `error_of(failure) -> DagError`, `describe(error) -> string`,
  `is_cyclic(error) -> bool` — inspect and render a `DagError`.
- `lifecycle_for(spec) -> LifecycleState`, `state_name(state) -> string` —
  a spec's lifecycle state before routing.

`dag/examples/actor_pipeline.hew` routes through one live actor per node: its
`route_with` dispatch asks the node's actor and returns the reply, or fails
with `DagError.Dispatch(node)` when the actor does not answer.