test(fuzz): add deterministic fuzz harness for parser, kitchen, analyzer and formatter #196

Merged
abiwab merged 3 commits from test/fuzz-harness into main 2026-11-21 19:35:44 +01:00
Owner

Adds fuzz/, a private workspace (no new dependency) that feeds mutated versions of every .gram snippet in the repo to the parser, the compiler, the analyzer and the formatter, and checks invariants on each input. First topic of the 1.6 cycle (step 0: a safety net on main before the multi-editor launch exposes the parser to more users). No changeset: private tooling only.

What it does

  • Corpus: the audit's extractors, refactored into one shared collectCorpus() (audit/src/corpus.ts). bun run audit output is byte-identical before and after.
  • Invariants (fuzz/src/invariants.ts): getAST only throws GramParseError with a valid offset/expected; compile, analyze (playground example database) and formatGram never throw; the formatter is idempotent and keeps the meaning of a recipe that parses; parse + compile + format stay within a time budget.
  • Meaning comparison (fuzz/src/normalize.ts): compiled JSON compared up to positions and deliberate canonicalizations, one commented rule each, calibrated on the unmutated corpus. Formatter rules that repair a mistake by design (composite <, ->&x {}, ##Title) are exempted.
  • Determinism: seeded PRNG, each input a pure function of (seed, index); a failure prints its replay command (bun run fuzz --seed … --index …) and a minimized input.
  • Growth (fuzz/tests/pathological.test.ts): worst-case generators, judged by the growth exponent per stage between 3 125 and 50 000 characters (linear measures 0.90–1.06, threshold 1.15).

Ratchet

fuzz/known-failures.json lists the violations found so far, each tied to an issue; KNOWN_SLOW does the same for super-linear cases (it.failing). The test fails on any new violation, and on a listed one that no longer reproduces: once fixed, drop the entry and add the minimized input to fuzz/regressions/.

Budget

bun test fuzz runs in about 10 s locally (fixed seed, 2 000 mutated inputs + the unmutated corpus + growth tests). A 200 000-input local session found no cause beyond those already caught by the CI run.

Merge order

Merge this PR first, then the parser fix for the quadratic sum (its PR removes the matching KNOWN_SLOW entry).

Adds `fuzz/`, a private workspace (no new dependency) that feeds mutated versions of every `.gram` snippet in the repo to the parser, the compiler, the analyzer and the formatter, and checks invariants on each input. First topic of the 1.6 cycle (step 0: a safety net on `main` before the multi-editor launch exposes the parser to more users). No changeset: private tooling only. ## What it does - **Corpus**: the audit's extractors, refactored into one shared `collectCorpus()` (`audit/src/corpus.ts`). `bun run audit` output is byte-identical before and after. - **Invariants** (`fuzz/src/invariants.ts`): `getAST` only throws `GramParseError` with a valid `offset`/`expected`; `compile`, `analyze` (playground example database) and `formatGram` never throw; the formatter is idempotent and keeps the meaning of a recipe that parses; parse + compile + format stay within a time budget. - **Meaning comparison** (`fuzz/src/normalize.ts`): compiled JSON compared up to positions and deliberate canonicalizations, one commented rule each, calibrated on the unmutated corpus. Formatter rules that repair a mistake by design (composite `<`, `->&x {}`, `##Title`) are exempted. - **Determinism**: seeded PRNG, each input a pure function of (seed, index); a failure prints its replay command (`bun run fuzz --seed … --index …`) and a minimized input. - **Growth** (`fuzz/tests/pathological.test.ts`): worst-case generators, judged by the growth exponent per stage between 3 125 and 50 000 characters (linear measures 0.90–1.06, threshold 1.15). ## Ratchet `fuzz/known-failures.json` lists the violations found so far, each tied to an issue; `KNOWN_SLOW` does the same for super-linear cases (`it.failing`). The test fails on any new violation, and on a listed one that no longer reproduces: once fixed, drop the entry and add the minimized input to `fuzz/regressions/`. ## Budget `bun test fuzz` runs in about 10 s locally (fixed seed, 2 000 mutated inputs + the unmutated corpus + growth tests). A 200 000-input local session found no cause beyond those already caught by the CI run. ## Merge order Merge this PR first, then the parser fix for the quadratic sum (its PR removes the matching `KNOWN_SLOW` entry).
test(fuzz): link known failures to their issues
All checks were successful
CI / Build & Test (pull_request) Successful in 5m12s
471d798dec
abiwab merged commit c4c9569273 into main 2026-11-21 19:35:44 +01:00
abiwab deleted branch test/fuzz-harness 2026-11-21 19:35:45 +01:00
Sign in to join this conversation.
No description provided.