Home / Skills / indexable-inc/blaze

testing

How blaze tests its own code, the one home for it: few tests, each for hard-to-verify logic behind a stable interface; property tests with Hegel (vendor/hegeltest, imported as `hegel`); deterministic simulation tests (DST) on blaze-core's seeded `sim::Sim` (virtual clock, shuffled completions, failure injection, crashes that tear unsynced writes), moving to Hegel's own `hegel::sim`; an invariant o

skillLicense: Apache-2.0 Source: indexable-inc/blaze

Install

Copy this folder into your Claude Code project (needs Node.js):

npx degit indexable-inc/blaze/.agents/skills/testing .claude/skills/testing

Or install it for every project:

npx degit indexable-inc/blaze/.agents/skills/testing ~/.claude/skills/testing

File content

Made by its original authors and shared under Apache-2.0. We show it unchanged, with credit and a link to the source. Source ↗

---
name: testing
description: "How blaze tests its own code, the one home for it: few tests, each for hard-to-verify logic behind a stable interface; property tests with Hegel (vendor/hegeltest, imported as `hegel`); deterministic simulation tests (DST) on blaze-core's seeded `sim::Sim` (virtual clock, shuffled completions, failure injection, crashes that tear unsynced writes), moving to Hegel's own `hegel::sim`; an invariant oracle with planted-bug controls and seed replay; sans-IO cores as the precondition; round-trip tests for persisted state; testing a causal claim by removing the cause. Proof for concurrent, durable, crash-recovering, billing and data-deletion code is a DST, never a code review. Load before writing or changing a test, before changing anything concurrent, persisted, crash-recovering, billing or data-deleting (the store, the daemon image, the profile history, a lock-free structure, a ring), and before claiming such code is correct."
---

# Testing blaze

<!-- Generated by `bun tools/agents/rules.ts --write` from tools/agents/rules.ts, down to the end marker; edit the rules there. AGENTS.md carries each rule as one line. -->

## test-the-cause (rule 23)

A causal claim (this change caused the regression, this lock causes the convoy, this index makes it
fast) is tested by removing the cause and watching the effect go, not by checking that its premises
hold. Pin a load-bearing one with a test named for the property.

## persisted-state-round-trips (rule 26)

Anything blaze persists (the daemon image, the store, the plane's history) has a test that restores
it and checks the result equals a fresh build byte for byte, and a budget for restart to first
answer. A restore that silently starts empty is a failure the daemon reports, naming why. A format
that is written while blaze runs and read after a crash also gets a DST (`dst-not-review`) that
crashes it at every point and checks the restore.

## dst-not-review (rule 43)

Code that is concurrent (atomics, locks, lock-free rings, two threads or processes sharing state),
durable (anything written to disk that must survive), crash-recovering, billing or data-deleting is
proven by a deterministic simulation test (DST): one Hegel test that draws an operation sequence,
the fault classes to turn on, crash points and schedules, runs the code as a sans-IO core in the
simulation (`blaze_core::sim::Sim` today, `hegel::sim` as it lands), and checks an oracle that
states the invariant ("every row recorded appears exactly once after any crash"), with planted-bug
controls the oracle must catch and a failing seed that replays. A code review is not evidence that
such code is right: it finds candidates, and each one it finds becomes a case the DST must catch
before the fix lands. A change to such code without a DST that covers it is unfinished, whoever
reviewed it.

<!-- End of the generated rules. Hand-written notes follow. -->

This skill is the one home for how blaze tests its own code. Other skills and docs point here:
`build-and-test` says how tests are run (`blaze test`, the central loop, the main agent runs it),
`debugging` says how a cause is found from the profile, and `runtime-code` holds `sans-io-cores`,
which is this skill's precondition.

## Which test, and how many

Write few tests, each where it pays:

- **Hard-to-verify logic behind a stable interface** (billing math, durability ordering, data
  deletion, a key's coverage, a codec): a unit or property test beside the code. If the
  signature is still moving, don't write one yet; prove it end to end and come back.
- **Concurrent, durable, crash-recovering, billing or data-deleting code**: a deterministic
  simulation test (DST), below. This is the requirement of `dst-not-review`, not an option.
- **A bug found in a real crate**: that crate's own tests under `blaze test`
  (`verify-on-real-projects`); a daemon bug: a step in the edit harness
  ([docs/harness.md](../../../docs/harness.md#adding-a-test)).
- **A slow or flaky reproduction**: shrink it (Hegel's shrinker, treereduce, `git bisect`) into a
  test that runs in seconds, named for the property it breaks. That one is always allowed.
- **Parsers of hostile bytes**: fuzzing (`crates/blaze-fuzz`, `fuzz/`).

No big suites, no tests of code whose shape is still changing, no red/green ritual on a dev
host: push and let CI and the harnesses catch breakage.

## Property tests with Hegel

A property test (a rule over generated inputs, the failing case shrunk) uses Hegel, Hypothesis's
engine ported to Rust and linked as a crate (vendor/hegeltest, our fork
[indexable-inc/hegel](https://github.com/indexable-inc/hegel); pin and changes in
[vendor/hegeltest/SOURCE.md](../../../vendor/hegeltest/SOURCE.md)):

- `hegeltest.workspace = true` under `[dev-dependencies]`, imported as `hegel`.
- `#[hegel::test]` on a `fn(tc: hegel::TestCase)` that draws with `tc.draw(..)` from
  `hegel::generators` (`use hegel::generators as gs;`); `#[hegel::composite]` makes a generator
  of a domain value (an `Op`).
- Keep the default case count to seconds per crate (`#[hegel::test(test_cases = N)]`, 256 by
  default from the root `hegel.toml`); run longer with `HEGEL_TEST_CASES`. Hegel applies that
  only to tests that set no count, so a test with its own count that is worth a long run reads
  it itself (`run_cases` in `crates/blaze-cas/src/tests/model_check.rs`, which also turns the
  example database off: failing examples are never written into the tree).
- Never proptest, quickcheck, arbitrary or bolero.

## Deterministic simulation tests (DST)

A DST runs real blaze code (not a model of it) against a simulated machine whose every choice
comes from one seed: when a completion arrives, which effect fails, where the power goes out,
which thread runs next. Hegel draws those choices, so it searches them, shrinks a failure to the
smallest run that still fails, and replays it exactly.

### What exists today: `blaze_core::sim::Sim`

`crates/blaze-core/src/sim.rs` is a seeded, single-threaded `Driver` for blaze-core's effects
(files, syncs, renames, watches, lists, processes, sockets):

- `Sim::new(seed, Faults { .. })`: a virtual clock (`advance`), completions delayed up to
  `max_latency` and delivered in a shuffled order, an effect failed once in `fail_one_in` (a
  failed append writes a random part of its bytes, a failed sync makes nothing durable), clones
  refused (`no_clone`, as on ext4), watch changes lost (`lose_change_one_in`).
- `crash()`: power loss. Everything in flight is gone; each file keeps what was synced plus a
  random prefix of the later changes, the last of which may be torn. A whole-file write or rename
  that completed is kept whole.
- Targeted faults: `fail_syncs`, `hold_syncs`/`release_held` (a commit that hangs), `slow_writes`,
  `hold_lists`/`fail_lists`, `hold_sends`, `lose_changes`, `fill` (a full disk: appends and
  writes under a directory past its room fail with ENOSPC, an append keeping the part that fit),
  and `record` to see the order completions were told in.
- A lock table for `Effect::Lock`/`Effect::Unlock` (flock's semantics, never waiting: a lock that
  another excludes is `Done::Contended`), with `hold_lock`/`release_lock` for another process's
  lock of a file; a crash lets every lock go, another process's too.
- `program` for spawned processes (a memory limit kills, as a cgroup does), `dial`/`peer_*` for a
  peer outside the simulation.

Examples: `crates/blaze-cas/src/tests/model_check.rs` (Hegel draws the store's operation
sequence and the `Sim` seed, checks it against a `BTreeMap`) and the crash tests in
`crates/blaze-cas/src/tests.rs`; `crates/blaze-graph/src/sched/tests.rs` for the scheduler core.

Its limits: it is one thread, so it interleaves a core's steps and the IO completions between
them, not two threads' atomics; it has one process and one version of the code; and its file
system is simpler than a kernel's (a completed whole-file write or rename is durable).

### Where it is going: `hegel::sim`

The harness is moving to Hegel's own DST, `hegel::sim`, in progress on our fork
(indexable-inc/hegel, branch `dst`):

- **Scheduled atomics**: every atomic op is a switch point the scheduler chooses at, so two
  threads' loads, stores and CASes interleave in every order Hegel draws.
- **A simulated kernel** with Linux and macOS semantics for fsync (and `F_FULLFSYNC`), rename (a
  rename durable before the data it names, unless synced), errseq (a writeback error reported
  once, to whoever asks first), ENOSPC, mmap (what a crash keeps of a dirty mapped page) and
  signals.
- **Multi-process** crash and restart, and **version skew**: an old and a new build of the same
  code sharing one directory.
- **A WASI host** that instruments wasm atomics, so code built to wasm runs under the same
  scheduler.

Until it lands, use `blaze_core::sim::Sim` and model what it lacks in the test (draw which actor
steps next; run the old and new codec against one simulated disk). Port a DST to `hegel::sim` once
the capability it needs is on the fork's `main` and pinned in vendor/hegeltest.

### The recipe

1. **Make the code a sans-IO core** on blaze-core's effects (`sans-io-cores`): it reads time from
   the injected clock, hands back effects, and takes completions. A core that opens files,
   spawns threads or sleeps cannot be put in the simulation; making it a core is the first part
   of the change, not optional cleanup.
2. **Write one Hegel test** that draws everything:
   - the operation sequence (`gs::vecs(op())`: record, flush, merge, read, retire, restart, ...);
   - the fault classes to turn on, as a swarm: each run draws a subset (sync failures, short
     writes, latency, ENOSPC, lost watch changes) so a rare class is not drowned by a common one;
   - the crash points (after which op, or after how many completions);
   - the schedule: which actor (thread, process, the daemon or a reader) takes its next step,
     and with `hegel::sim`, the switch at every atomic op;
   - the `Sim` seed (`tc.draw(gs::integers())`), so Hegel shrinks and replays it with the rest.
3. **Run it in the simulation**: drive the core with the `Sim` driver, crash it, reopen it (the
   restart runs recovery exactly as the daemon would), and keep going.
4. **Check an oracle that states the invariant**, against a plain model kept beside the run, at
   every step and after every crash: "every row recorded and flushed appears exactly once after
   any crash", "a reader never sees a file the manifest does not list", "no byte is deleted while
   a pin holds it", "the bill equals the sum of the usage recorded, once". Assert what is
   true, not what the code happens to do.
5. **Planted-bug controls**: for each safeguard (the sync before the rename, the order of commit
   and consume, the second atomic), a test plants its removal (a test-only switch in the core)
   and expects the DST to find a counterexample within its case budget. An oracle that misses a
   plant is too weak; fix the oracle or the draws before trusting a green run. This is
   `test-the-cause` applied to the test itself.
6. **Replay a failing seed**: Hegel prints the shrunk draws (ops, faults, crash point, seed).
   Pin it as a plain `#[test]` named for the property it broke, with those values written out,
   or rerun with `Settings::new().seed(Some(n))`; it replays exactly because nothing in the run
   reads real time, real threads or hash order (`deterministic-output`).

A DST is fast enough for the merge gate at its default case count, and runs long
(`HEGEL_TEST_CASES`) in the release train.

### Worked example: blaze-profile's history

blaze-profile keeps a history on disk (docs/profiling.md): `manifest.json`, rewritten to a hidden
name and renamed over the last under `manifest.lock`, is the one commit point of a flush, a
merge and a retire (`crates/blaze-profile/src/history/manifest.rs`, `retire.rs`, `pin.rs`); and
the flight recorder's postmortem rings are mmapped, so a crashed daemon's last events survive
(`crates/blaze-profile/src/flight/postmortem.rs`). On 2026-11-04 a code review of this code, which
had landed with tests, found eight bug classes. None of its tests could have caught them, and
each maps to a DST capability that does:

| Bug class the review found | What catches it |
|---|---|
| No fsync of the new manifest (and its directory) before the rename: after a crash the name points at a torn or empty file | Crash points plus the kernel's rename-before-data semantics (`hegel::sim`; today's `Sim` keeps a completed whole-file write whole, so write it through appends and syncs to see it there) |
| A crash between the commit and its consumption (files listed, ring cursor not yet advanced) double-counts rows on restart | A crash point at every effect and the exactly-once oracle (today's `Sim::crash`) |
| A flush that fails part way (one table written, the next not) leaves a manifest naming half a flush | Swarm fault classes: `fail_one_in`, `fail_syncs`, short writes, ENOSPC (`hegel::sim`) |
| A DEBUG hold (data kept for a reader) is lost on a crash, and the data is deleted under it | Crash and restart with the oracle "nothing a live hold names is deleted" |
| Recovery runs on the writer's open but not on a read: a reader after a crash sees what a crash left | Draw a reader as its own actor after each crash, with the oracle on what it reads; multi-process crash and restart (`hegel::sim`) |
| Version skew on retired names: an older daemon reads the newer naming of retired files as live, or deletes them | Version skew, two builds' cores on one directory (`hegel::sim`; today, both codecs in one test on one `Sim`) |
| A lost `flushed` update when a ring moves between two atomics: a writer's store lands between the mover's load and its store | Scheduled atomics, a switch point at every atomic op, plus mmap semantics for what a crash keeps of the ring (`hegel::sim` only) |
| Recovery holds `manifest.lock` while it walks the directory, so a daemon that starts during it blocks | Multi-process restart on the virtual clock with a bounded-progress oracle: the second daemon's first answer arrives within its bound (`hold_lists`/`slow_writes` today, a second process in `hegel::sim`) |

Since 2026-11-04 the writer is a sans-IO core (`crates/blaze-profile/src/history/files.rs`,
marked `#![blaze::sans_io]`; `manifest.rs`, `retire.rs`, `compact.rs` and `merge.rs` too) driven
by `history/drive.rs`, and its DST is `history/files/tests.rs`: it draws flushes, build ends
(merges), crashes at a completion count and reopens (recovery), with swarm faults
(`fail_one_in`, `fill` for ENOSPC, `fail_syncs`, a held `manifest.lock`), and its oracle is that
after any crash and reopen the rows in listed files plus the rows recovered equal the rows
recorded, each once, no listed file is missing or torn, and the history always opens. Its
planted bugs (`files::Plant`) are the first two rows of the table: no sync before a rename, and
a flush's consumption marked after its commit.

The full DST for it draws records, flushes, merges, retires, pins, reads and restarts, crashes at any
point, and checks: every row recorded and flushed appears exactly once; no file a live pin's
generation lists is deleted; a reader opened after any crash sees only listed files; a restart
reaches its first answer within its bound. Each row of the table is a planted bug it must catch.

## Persisted state and causes

- `persisted-state-round-trips` (above): a restore equals a fresh build byte for byte; a DST adds
  the crashes.
- `test-the-cause` (above): remove the cause, watch the effect go. The method for finding a cause
  in the first place, from the profile, is the `debugging` skill.