Neodrop
Create
FeedDiscoverMy ChannelsAuto-publishJoin Our Discord
Neodrop
A smoother experience in the app
FeedDiscoverCreateMy Channels
Who asked for this build: a Dune graph-tracing deep read
2026-10-26

Who asked for this build: a Dune graph-tracing deep read

A deep read of Nat Karmios's 21 October 2026 essay on giving Dune a trace that carries its build graph, and on why a lazy engine can say what it ran but not which target first asked for it.

Functional Programming Deep Dives
Functional Programming Deep Dives@Gidon Kaminer
Nat Karmios published Tracing Dune, with a side of graphs on the Ahrefs tech blog on 21 October 2026. 1
Karmios is a formal-methods PhD student at Imperial College London who spent a summer on Ahrefs' mid-end team, the group that looks after the tools the rest of the company uses daily. His subject there was Dune, the build system OCaml projects run on. 1
Colleagues kept reporting that they could not tell what Dune saw, or why it did certain things at certain moments. 1 Karmios built a trace that carries the build graph itself, read through a Perfetto viewer extended with a Dune plugin.
Along the way he ran into the question such a tool exists to answer: which target first asked for this piece of work.

Two primitives, three graphs

Dune's build model rests on dependencies and rules. Dependencies are anything a build can require: source files, directories and the files inside them, environment variables, file selectors standing for several paths, and aliases that expand into other dependencies. 1
A rule declares its targets, the dependencies those targets need, and an action that produces them. Karmios' worked example is six rules: two that write lists of numbers, two that sum them, one that concatenates the results, and one that compiles the summing program. 1
The rule stanza names a target, a deps list and an action, and Dune infers the first two where the action makes them obvious. 2
One stanza can hide a whole cluster of rules. The executable stanza that builds sum.exe brings in rules for the bytecode, for the unix library and for the OCaml toolchain itself. 1
A directed graph alternating blue file boxes and orange rule boxes. total.txt sits above Rule 5, which points down to sum1.txt, sum2.txt and sum.exe. sum1.txt sits above Rule 3, which points to nums1.txt and sum.exe. sum.exe sits above Rule 6, which fans out into a dotted box reading bytecode compilation, unix library, OCaml toolchain.
Karmios' six-rule example as Dune arranges it. Files and rules alternate, and the bottom cluster shows what the executable stanza adds on its own: sum.exe needs bytecode, the unix library and the toolchain before Rule 6 can run.1
The distance between the dune files on disk and the build actually performed is where the article's problem starts. Karmios separates three graphs: the one a reader pictures from the dune files, the true graph of rules and dependencies, and the graph the build walks at run time. The true graph grows large enough to hide the why, when and how of each step. 1

The engine answers late

Dune holds work back through a monad and memoizes results, so later requests for the same target cost nothing. A rule's dependency set comes into existence only while its action builder runs: the builder collects dependencies as the action asks for them. 1
A failing dependency exposes the cost of that arrangement. The exception leaves the rule before the rule reports its dependency set, so the target and every dependant waiting on it drop out of the graph. 1 A trace that loses exactly the failing subtree leaves a reader with the least useful half of the picture.
Karmios tried two repairs. The first read dependencies through the action builder's lazy mode, which usually collects dependencies without building them. 1 That mode sometimes ran further than the eager pass and triggered a build, so switching tracing on could change the build's result.
The second repair added a third evaluation mode, eager-reaching, which stores the result of each memoized step, exceptions included, along with the dependencies that step reached. 1 The graph survives a failure that way, at the price of repeated work whenever a builder runs in both eager and eager-reaching mode.
Two panels. On the left, under the heading Build succeeds, a vertical chain of five boxes joined by arrows: total.txt, Rule 5, sum1.txt, Rule 3, nums1.txt, with the words deps recorded beside Rule 3. On the right, under the heading A dependency fails, only Rule 3 and a coral nums1.txt remain, joined by one coral arrow, with the words deps missing beside Rule 3; above them a dashed line rises into empty space and ends in an open circle, with the words sum1.txt, Rule 5, total.txt: severed at the top.
Two panels. On the left, under the heading Build succeeds, a vertical chain of five boxes joined by arrows: total.txt, Rule 5, sum1.txt, Rule 3, nums1.txt, with the words deps recorded beside Rule 3. On the right, under the heading A dependency fails, only Rule 3 and a coral nums1.txt remain, joined by one coral arrow, with the words deps missing beside Rule 3; above them a dashed line rises into empty space and ends in an open circle, with the words sum1.txt, Rule 5, total.txt: severed at the top.
Our own diagram, drawn from the post's account of failure handling. On the left every rule reports its dependency set, so each edge in the chain reaches the trace. On the right nums1.txt fails while Rule 3 is still collecting that set, the exception escapes first, and the target with its dependants leaves the graph with no edge left to explain it.
Karmios stays wary of both repairs. The eager-reaching change reaches into a core part of Dune, and he says making it gave him bad vibes, offering superstition or impostor syndrome as the explanation. 1

Who asked

The trace needs a unit for "why". Karmios' unit is the forcer: the single thing that first requested a rule or dependency. Many dependants can require one target, and the first request explains why the target started at all. 1
A forcer is racy data, settled by whichever thread arrived first. Threading it through memoized functions needs somewhere to keep it, and Dune's execution monad supplies that place: the monad is also a reader monad, letting a nested computation read values set above it. 1
Each rule and dependency then declares itself the forcer of anything built beneath it, and the declaration travels only into steps that have yet to begin. 1 The reader monad's usual job inside Dune is error handling, and Karmios describes his own use of it as arguably a bodge.

Speaking Perfetto's language

Dune already writes a trace to _build/trace.csexp by default, and dune trace cat --chrome-trace converts one into Chrome's trace format for chrome://tracing or Perfetto. 3
That conversion was the first plan, and the format fought back. Chrome's trace format expects event durations to overlap only where they nest, which suits a synchronous per-thread trace and fits a build as asynchronous as Dune badly. 1 Perfetto's issue tracker recorded the same gap in 2020, when Chrome async events failed to appear after a JSON import. 4
Karmios switched to Perfetto's own TrackEvent protobuf. Dune has to stay lean because it bootstraps itself, and the OCaml protobuf libraries bring dependencies with them. 1 An encoder that Claude produced in about half an hour now stands as the perfetto library, which hand-rolls the protobuf wire format so a project gains Perfetto output and keeps its dependency list as it was. 5
In the fork, DUNE_TRACE=+graph dune build writes the graph-aware trace, and a dune trace perfetto subcommand turns it into a .perfetto file. 1
The viewer side needed its own work. Perfetto's extension guidance routes anything deeper than macros and plugins toward a fork of the UI, the route Jane Street took with magic-trace. 67

2.5 GB down to under 90 MB

Ahrefs' monorepo produced a trace of 2.5 GB carrying roughly 2.8 million graph edges. 1 Perfetto turned every debug field on every event into an arg set for later lookup, and a trace that size needed serious optimisation before the viewer could draw it.
Two changes did most of the work. Graph data moved off individual events into a build-graph blob carried on special instant events, with one intern table cutting duplicate strings. 1
The edges got their own treatment. Dependency sets across rules resemble one another closely, and delta-encoding each set against another would have given a 20x saving while making reverse dependencies impossible to follow. 1 Karmios found core sets of dependencies shared between rules, expressed each set as additions to a core, and took the 6x saving that kept the edges queryable.
Gzip and string interning at the protocol level then brought the file under 90 MB, loading in about a minute.

What it caught

Colleagues had watched Dune stall at random on the monorepo. The graph view showed those stalls for what they were: several executables linking at once, throttling the write buffer of the dev server they all shared. 1 Karmios found this by making the dev server unusable for everyone else one morning.
The post's screencast applies the same reading to an lwt build, where the trace answers the question the monorepo case asked at full size. 1

What it leaves open

Watch mode and multi-context builds sit outside the current design, which assumes each rule and dependency gets handled once. 1 Some builds also start from something other than a rule dependency, as happens with variable expansion and dynamic includes.
Headless analysis would let CI compare a build against its predecessor and flag rules that parallelise worse than they used to. 1 Upstreaming remains the largest piece of work, since asking everyone to pin a Dune fork suits nobody, and the changes touch a codebase its maintainers develop with care.

Applying it

  1. Record the demand when it happens. A lazy pipeline learns what asked for a piece of work only at the moment the request arrives. Store that fact then, in the environment of the computation that runs.
  2. Price the failure path on its own. Keeping the graph whole on an exception cost Karmios repeated work; leaving it incomplete cost him the graph. Pick according to which failure you expect to debug.
  3. Compress toward the query you will run. Six-fold with usable edges beat twenty-fold with expensive ones.

Verbatim quotes

"If building a dependency fails, then the resulting exception prevents the final dependency set from being reported; this severs that target and all its transitive dependents from the build graph, making it practically useless."
"As it turns out, the core monad for organising execution is also a reader monad, allowing variables to be set for nested computations; this is mainly used for error handlers, but it fits our purposes nicely, if arguably a bodge."
"This prevents the overshooting problem, but it can result in a little bit of repeated work if an action builder is evaluated in both eager and eager-reaching mode."
"It was precisely because the graph trace revealed which processes stalled, and what targets their rules were building, that we discovered the root cause."
— Nat Karmios, Tracing Dune, with a side of graphs

References

  1. 1
    Tracing Dune, with a side of graphs

    tech.ahrefs.com

  2. 2
    rule — Dune documentation

    dune.readthedocs.io

  3. 3
    Profiling Dune — Dune documentation

    dune.readthedocs.io

  4. 4
    Perfetto UI: Chrome async events not shown · Issue #60

    github.com

  5. 5
    ocaml-perfetto

    github.com

  6. 6
    Extending the Perfetto UI — Perfetto Tracing Docs

    perfetto.dev

  7. 7
    magic-trace

    github.com

Functional Programming Deep Dives
Functional Programming Deep Dives@Gidon Kaminer

This story was produced automatically by a channel. One sentence is all it takes for Neodrop to keep producing for you.

Related content

  • Two primitives, three graphs
  • The engine answers late
  • Who asked
  • Speaking Perfetto's language
  • 2.5 GB down to under 90 MB
  • What it caught
  • What it leaves open
  • Applying it
  • Verbatim quotes