typescript · dandori

jgeschwendt/typescript · @jlg/dandori

One checker for every change: it runs each gate once per tree and remembers the pass.

mise run check       # verify: never writes the tree
mise run check:fix   # fix: oxlint --fix, oxfmt, then verify

dandori

The dandori bin keys the working tree, skips every gate whose pass is logged at that key, runs the rest cheapest-expected-first and stops at the first failure. The engine knows no tool: the repo's dandori.config.ts exports the fixers, the gates (stele with stamp: false) and the inputs the key hashes beside the tree (.env). The name is Japanese 段取り, the plans and procedure for a job: its steps arranged in order. Two callers: the agent's Stop hook runs fix mode at the end of every turn and hands a failure back as the reason to keep working; hk's pre-push runs verify mode, which on a tree the agent loop already passed is a lookup. A third hook formats each file the agent edits.

agent loop · fix mode at Stop ① edit oxfmt <file> ② fixers oxlint --fix → oxfmt ③ key tree · inputs · platform ④ gates un-stamped, by c/p ⑤ stamp passes only pre-push verify mode git push mise run check lookup stele still runs log dandori/log.tsv
Both modes reach the same key for the same tree, so the passes stamped at the end of a turn are what pre-push finds. The log lives under the Git common dir, shared by every worktree and never in a tree.
run measured
oxfmt --check 0.1 s
tsc · config, dandori, verax 0.2 s
stele check && stele emit --check 0.2 s
oxlint 2.3 s
bun test --isolate . ./.changeset 30.5 s
tree key 0.03 s
mise run check, cold 33.4 s
mise run check, every gate stamped 0.30 s
Stop hook, every gate stamped 0.27 s
per-edit hook 0.10 s

Wall times on an Apple-silicon Mac, this repo at 7 nodes and 118 formatted files. A stamped run is the key, a log read and stele.

Soundness

Proposition 1. Treat a gate as a function g : Tree → {pass, fail}. If g is deterministic and reads nothing outside the tree, or the key covers what it reads, then hash(t₁) = hash(t₂) ⇒ g(t₁) = g(t₂) up to a hash collision, so a recorded pass may be reused.

Proof. Git's tree hash is a hash of every path, mode and blob in the tree, so equal hashes mean equal trees unless two trees collide. A deterministic g that reads only the tree and the key's other inputs gives equal results on equal inputs. ∎

This is the verifying trace of Mokhov, Mitchell and Peyton Jones, Build Systems à la Carte (ICFP 2018, §4.2.2), which records the hashes of a task's dependencies and rebuilds when one changes. Here the dependency set of every gate is the whole tree: an over-approximation, so sound but not minimal. An edit to a README re-runs the tests.

Corollary. The key's tree is git write-tree after git add -A into a copy of the index. Committing that state with git add -A writes the same tree object, so the dirty tree's key equals HEAD^{tree} after the commit and the pass earned in the agent loop is the stamp pre-push finds. The real index is never written.

Order

Proposition 2. With stop-on-first-failure, cost cᵢ and failure probability pᵢ, gate i goes before gate j iff cᵢ/pᵢ < cⱼ/pⱼ.

i then j cᵢ cⱼ · runs if i passes, 1−pᵢ cᵢ + (1−pᵢ)cⱼ j then i cⱼ cᵢ · 1−pⱼ cⱼ + (1−pⱼ)cᵢ difference pⱼcᵢ − pᵢcⱼ
Dashed boxes run only when the gate before them passes. The first order is cheaper exactly when the difference is negative, that is when cᵢ/pᵢ < cⱼ/pⱼ.

Proof. For an order σ of independent gates the expected cost is E(σ) = Σₖ c_σₖ · Πₗ<ₖ (1 − p_σₗ). Swap the adjacent gates i, j at positions k, k+1. Every term before k is unchanged; every term after k+1 carries the factor (1−pᵢ)(1−pⱼ) in either order, so it is unchanged too. With P the probability the prefix passes, only P[cᵢ + (1−pᵢ)cⱼ] becomes P[cⱼ + (1−pⱼ)cᵢ], and the first minus the second is P(pⱼcᵢ − pᵢcⱼ). So an adjacent pair out of ascending c/p order can be swapped without raising the cost. Any order reaches the sorted one through such swaps (each removes one inversion), so the sorted order is optimal. ∎

The pairwise step still holds for dependent gates if pᵢ and pⱼ are read as failure probabilities conditional on the prefix passing, since the suffix factor is the probability that both pass in either order. The global sort assumes independence: when one gate's failure predicts another's, the conditional probabilities change with the prefix and no single key orders them. This is Smith's ratio rule for one machine, W. E. Smith, Various optimizers for single-stage production (Naval Research Logistics Quarterly, 1956), proved by the same exchange.

dandori estimates c as the mean logged duration, the declared cost counted as one run, and p as (fails + 1)/(runs + 2). With no history every p is ½, so the declared costs decide. With the measured costs, tsc at 0.2 s runs before the tests at 30.5 s unless the tests fail more than about 150× as often. The fixers are a precedence constraint outside the sort: they rewrite the tree the gates read, so they run first, oxlint's fixes before the formatter.

Once

Proposition 3. One validation pass is enough. The tests write no source, so the tree is the same before and after them and, by Proposition 1, every static gate returns what it returned on the first pass; a second pass is redundant.

The fixers need a fixpoint F(t) = t for F = oxfmt ∘ oxlint --fix, or a verified tree could still be rewritten. That is not assumed: verify mode runs oxlint and oxfmt --check on the tree the fixers leave, and fails if either would change it. A fixer is stamped only when it left the tree as it found it.

Gate spans

Every run of mise run check is one OpenTelemetry trace through floodgate span: a check span with each gate that actually ran nested under it (bun.test, oxfmt, oxlint, stele, tsc), so floodgate's page shows which gate costs the time and memory. dandori.config.ts wraps each gate's command as mise run span -- <name> -- sh -c '<command>' (.mise/tasks/span), which execs floodgate span when floodgate is on PATH and else the command itself.

A stamped gate is a lookup, never run, so it has no span: on an unchanged tree the trace holds check and the unstamped stele gate only, which is what the 1.3 s covers (dandori's start, the stamp lookups, then stele check and stele emit --check). The gates cost their cold time only after an edit.

Assumptions

input held by fails when
tracked and untracked files the key's tree hash never: ignored files are outside the tree, as they are outside a commit
.env, loaded by mise the key's input hash never
operating system the key's platform and arch an OS upgrade changes a result: the key holds no OS version
node_modules assumed installed from bun.lock an install drifts from the lockfile; stamps are shared across worktrees
bun, node, hk pinned in mise.toml, inside the tree a gate calls a global binary instead of the pinned one
determinism assumed a flaky test: a lucky pass stays stamped until the tree changes
Git history stele is stamp: false a new gate reads history and is stamped

Only passes are stamped. A failure can come from flakiness or a broken environment as well as from the tree, and a stamped failure would hold until the tree changed; re-running it costs one gate, while trusting it could block a good tree. stele checks that each claim's anchor is unchanged since the commit it was stamped at, so its verdict depends on history the tree does not carry; it runs every time, at 0.2 s. Pre-push verifies the working tree, which is the pushed tree when everything is committed; a pre-commit hook that reformats on commit only costs a re-run, since dandori keys what it sees.