;  The release version has one source, dune-project's (version …): this rule
;  reads it into Build_info at build time, and package.nix reads the same line,
;  so the banner, the package and the changelog cannot disagree by hand
;  (nib-0.1-soundness, release step 4).
(rule
 (targets build_info.ml)
 (deps (file ../dune-project))
 (action
  (with-stdout-to build_info.ml
   (progn
    (run sed -n "s/^(version \\(.*\\))$/let version = \"\\1\"/p" ../dune-project)
    ;  The release label a downloadable binary carries in its version line
    ;  (`build macos-arm64`), set by release.nix through package.nix. Empty in
    ;  every other build, which then prints the store path or `dev` as before.
    (echo "\nlet release = \"%{env:NIB_RELEASE=}\"\n")))))

;  nibrt, the runtime a counted module links (lib/rt_link.ml): the Rust crate
;  system/tools/nibrt compiled to two relocatable wasm objects, committed here
;  by its build.sh so this build needs no Rust toolchain.
(rule
 (targets nibrt_obj.ml)
 (deps (:gen embed_obj.ocaml) (:plain nibrt.o) (:check nibrt-check.o))
 (action (with-stdout-to %{targets} (run ocaml %{gen} %{plain} %{check}))))

;  The native backend's runtime (lib/native.ml): nibrt's object and bitcode for each
;  target this tree carries (system/tools/nibrt/build.sh writes them), and the
;  C driver a native build links.
(rule
 (targets nibrt_native.ml)
 (deps (:gen embed_native.ocaml) (glob_files nibrt-*-*.o) (glob_files nibrt-*-*.bc)
  nibrt-native.llvm native_driver.c)
 (action (with-stdout-to %{targets} (run ocaml %{gen}))))

(library
 (name nib)
 (modules
   build_info
   json
   sha256
   diagnostics
   collections
   types
   footprint
   mint
   handles
   chan
   arena
   claims
   frame
   backtrace
   seam
   park
   effect_trace
   fixtures
   scratch_dir
   bench_effectors
   serve_host
   builtins
   manifest
   journal
   world
   remote_data
   remote
   parfor
   ast
   errors
   names
   lexer
   parser
   parser_messages
   interp
   typecheck
   interface_check
   eval
   hot
   pretty
   fmt
   escape
   loader
   xfer
   hoist
   driver
   io_backend reactor
   serve
   nibd
   wasm_park
   wasm_bin
   nibrt_obj
   rt_link
   aot
   nibrt_native
   native_prep
   cell_remove
   native
   inspect
   svm
   scodec
   shandshake
   shost)
 ;  #441: SO_PEERCRED. OCaml's stdlib has no binding for it, and the daemon
 ;  identifies its peer before reading a byte — so the one stub in the tree has
 ;  a sibling for the same reason and in the same place.
 (foreign_stubs (language c) (names nibd_stubs nib_io_stubs)
                (flags (:standard -D_GNU_SOURCE)))
 (libraries menhirLib unix)
 (flags -w -27-26-11@8))

(executable
 (name nibc)
 (modules nibc)
 (libraries nib unix))

;  The documentation binary (§4 of specs/nib-diagnostics/INTENT.md): a thin
;  wrapper over Diagnostics.doc_main, which `nibc --explain` shares.
(executable
 (name nibdoc)
 (modules nibdoc)
 (libraries nib))

;  The diagnostics acceptance tests (specs/nib-diagnostics/INTENT.md §5.2-§5.15).
;  Its fixtures are files — one .nib per code in the table, plus the fourteen
;  snapshots — so it is its own executable, like test_packages. The two
;  binaries are passed on argv: every claim in §5.6/§5.11/§5.14 is about which
;  *stream* the bytes reach, and that is only observable from outside the
;  process.
(test
 (name test_diagnostics)
 (modules test_diagnostics)
 (deps (glob_files ../test/diagnostics/fixtures/*.nib)
       (glob_files ../test/diagnostics/*.txt)
       (file ../dune-project)
       (glob_files ../CHANGELOG.md)
       %{exe:nibc.exe} %{exe:nibdoc.exe})
 (action (run %{test} %{exe:nibc.exe} %{exe:nibdoc.exe}))
 (libraries nib unix))

;  The journal's acceptance tests — slice one of `specs/nib-replay/INTENT.md`
;  (AC1-AC5, G5). Its own executable because every claim is about bytes on disk
;  and an exit code, observed from outside the process that wrote them: the
;  suite writes a journal, replays it against a moved host clock, and edits the
;  file to prove each refusal fires. The `deps` are what stage the `.nib`
;  fixtures in the build directory, where `../test/journal` finds them.
(test
 (name test_journal)
 (modules test_journal)
 (deps (glob_files ../test/journal/*.nib)
       %{exe:nibc.exe})
 (action (run %{test} %{exe:nibc.exe}))
 (libraries nib unix))

;  §5.15: the two binaries must print the same version line, and it must be the
;  pinned one — so the pin moves only when the table's prose does. The build
;  field names the artifact (a store path, a release label, or `dev`), so it is
;  read as `dev` here: the golden pins the version and the digest, which are
;  the same in every build of one tree.
(rule
 (alias runtest)
 (deps %{exe:nibc.exe} %{exe:nibdoc.exe} ../test/diagnostics/version.expected.txt)
 (targets version-nibc.txt version-nibdoc.txt)
 (action
  (progn
   (with-stdout-to version-nibc.txt
    (pipe-stdout (run %{exe:nibc.exe} --version)
     (run sed "s/, build [^)]*)$/, build dev)/")))
   (with-stdout-to version-nibdoc.txt
    (pipe-stdout (run %{exe:nibdoc.exe} --version)
     (run sed "s/, build [^)]*)$/, build dev)/")))
   (diff ../test/diagnostics/version.expected.txt version-nibc.txt)
   (diff version-nibc.txt version-nibdoc.txt))))

(ocamllex lexer)
(menhir
 (modules parser)
 (flags --table))

; The Phase 3b sentences live in parser.messages, in menhir's own format, and are
; compiled by menhir into a plain lookup module. It is a hand-written rule rather
; than a `--compile-errors` flag on the `menhir` stanza because dune 3.21 runs that
; stanza in a sandbox that contains only its own inputs, and the stanza has no
; `(deps ...)` field: the flag reaches menhir but the file does not (measured: three
; ways — bare flag, %{dep:...}, (deps ...) is a hard error). Here the deps are ours.
(rule
 (targets parser_messages.ml)
 (deps parser.messages parser.mly)
 (action
  (with-stdout-to
   parser_messages.ml
   (run menhir --compile-errors %{dep:parser.messages} %{dep:parser.mly}))))
; L0-1 tests. Dependency-free by design (see lib/test_nib.ml).
; `../std/*.nib` is a dependency because the tool's std directory is *beside the
; binary* (_build/default/std/io.nib) and a test that says `use "std/io.nib"` has
; to find exactly the file a deployed tool would find — copying it into the test
; would test a copy.
(test
 (name test_nib)
 (modules test_nib test_manifest)
 (deps (glob_files ../examples/*.nib) (glob_files ../examples/multi/*.nib)
       ;  `test_fmt` walks `../examples` whole and asserts a floor on what it
       ;  finds, so the mirror it reads must be the *family*, not the subset the
       ;  globs above happen to name: with globs alone the walk is a step
       ;  function on how much of `examples/` some rule declared, and an example
       ;  added to a directory no glob names would be invisible to the guard
       ;  while it kept passing. `source_tree` makes the shipped corpus complete
       ;  in every shape, the nix sandbox included (`examples/` is derivation
       ;  source; `docs/` is not, which is why only this tree is declared).
       (source_tree ../examples)
       (glob_files ../test/m5/*.nib) (glob_files ../test/m512/*.nib) (glob_files ../test/m513/*.nib) (glob_files ../test/m6/*.nib) (glob_files ../test/m7/*.nib) (glob_files ../test/m8/*.nib) (glob_files ../test/m9/*.nib)
       (glob_files ../test/boundary/*.nib)
       (glob_files ../test/fuel/*.nib)
       (glob_files ../test/park/*.nib)
       (glob_files ../test/park/*.frame)
       (glob_files ../test/fold/*.nib)
       ;  `specs/nib-iterators` stage 4: the bridges' fixtures. One of them
       ;  (`of_source.nib`) has two wires, so its checks name the wire.
       (glob_files ../test/iter/*.nib)
       ;  `test_http_framing`: the framing fixture (`Http.wanted` /
       ;  `render_with` / `wants_close`), one file with sixteen wires.
       (glob_files ../test/http/*.nib)
       (glob_files ../test/iter/*.expected)
       (glob_files ../std/*.nib)
       ;  `test_signature_dump` (#403): the frozen `nibc FILE` bytes. The goldens
       ;  are files, and one of them is a *package* — a contract plus the body
       ;  directory beside it, which the loader reads off the filesystem — so the
       ;  dependency is the whole tree, not a glob of the top level.
       (source_tree ../test/dump)
       (glob_files ../docs/*.md)
       ;  The language reference's pages and its example files, for
       ;  `test_language_docs`: it compares every ```nib block with the example
       ;  file it claims to be. Recursive because a package example is a tree.
       ;  Absent from the nix sandbox by design (`package.nix` drops `docs/`), and
       ;  the check is guarded on that absence.
       (glob_files_rec ../docs/language/*))
 (libraries nib unix))


;  `specs/nib-pure-ir/CONTRACTS.md` §1-§4: the effect trace across resources, the
;  demand counts, activation identity, and fuel-as-a-bound. Its own executable
;  because each claim is a *run* of a fixture file, and because the M4 cut edits
;  test_nib.ml — the two diffs must not collide.
;  `--dump` prints what each fixture does (trace, outcome, transitions).
(test
 (name test_pure_ir)
 (modules test_pure_ir)
 (deps (glob_files ../test/pure-ir/*.nib) (glob_files ../std/*.nib))
 (libraries nib unix))

;  `specs/nib-pure-ir/INTENT.md` §M5, `Par` (splittable collections): the chunk
;  arithmetic, the six admissions (five of them refusals with an exact sentence),
;  and the claim that worker count is unobservable — the same program answers the
;  same string at 1, 2, 3 and 4 workers. No timing assertion: that belongs in
;  lib/parfor_probe.ml and specs/nib-pure-ir/EVIDENCE-parfor.md, not in a test
;  that would then be a test of the box's load average.
(test
 (name test_parfor)
 (modules test_parfor)
 (deps (glob_files ../test/parfor/*.nib) (glob_files ../std/*.nib))
 (libraries nib unix))

;  The packages intent (specs/nib-packages/INTENT.md) has its own executable
;  because its fixtures are *directory trees*: a package with `from` is a
;  contract file plus the .nib files beside it, and each claim in the intent
;  table is a tree in test/packages/. Keeping them out of test_nib also keeps
;  the two units' diffs from colliding in one file.
(test
 (name test_packages)
 (modules test_packages)
 (deps (glob_files ../test/packages/*.nib)
       (glob_files ../test/packages/*/*.nib)
       (glob_files ../std/*.nib))
 (libraries nib unix))

;  The lexical-names gate (specs/nib-lexical-names/INTENT.md §4, §9 rows 5-8
;  and 11): an enclosing path is not spellable from inside. Its own executable
;  for the same reason as test_packages — the claim is about where a *file*
;  stands, so every fixture is a directory tree (a contract plus the parts
;  beside it) written at run time, and the trees are the site under test, not
;  a detail of it. Keeping it out of test_nib also keeps the two units' diffs
;  from colliding in one file.
(test
 (name test_gate)
 (modules test_gate)
 (libraries nib unix))

; M0/M1 acceptance: `--run` writes exactly the program's output to stdout and
; nothing else, so the golden file is a byte-for-byte diff, not a transcript.
(rule
 (targets page.out.html)
 (deps ../examples/page.nib)
 (action
  (with-stdout-to page.out.html
   (run ./nibc.exe --run ../examples/page.nib --wire Prod --arg user=Jahfer))))

(rule
 (alias runtest)
 (deps ../test/page.expected.html page.out.html)
 (action (diff ../test/page.expected.html page.out.html)))

; nib-seam-scope §11 ship 2: the demonstration program is executable, and both
; wires are pinned. `Prod` shows one default from each scope (a package's own
; choice standing beside a wire default that fills only what is still asked
; for); `Sole` shows one written key overruling both. The expected files are
; written by hand — a golden copied from the program's own output pins nothing.
; The body is listed file by file: dune sandboxes the rule, and the compiler
; reads the whole body of `App` from `app/`.
(rule
 (targets greeting-prod.out)
 (deps ../examples/greeting.nib ../examples/app.nib ../examples/other.nib
       (glob_files_rec ../examples/app/*.nib))
 (action
  (with-stdout-to greeting-prod.out
   (run ./nibc.exe --run ../examples/greeting.nib --wire Prod))))

(rule
 (alias runtest)
 (deps ../test/greeting-prod.expected.txt greeting-prod.out)
 (action (diff ../test/greeting-prod.expected.txt greeting-prod.out)))

(rule
 (targets greeting-sole.out)
 (deps ../examples/greeting.nib ../examples/app.nib ../examples/other.nib
       (glob_files_rec ../examples/app/*.nib))
 (action
  (with-stdout-to greeting-sole.out
   (run ./nibc.exe --run ../examples/greeting.nib --wire Sole))))

(rule
 (alias runtest)
 (deps ../test/greeting-sole.expected.txt greeting-sole.out)
 (action (diff ../test/greeting-sole.expected.txt greeting-sole.out)))

; M2 acceptance: a list of records in, one <li> per row out; an empty list in,
; nothing out. Both wires are the same function, so the diff pins the *rule*
; ("no rows, no output") and not a special case. Written by hand, not copied
; from the program's output — a golden that is generated from the code under
; test proves nothing.
(rule
 (targets rows3.out.html)
 (deps ../examples/rows.nib)
 (action
  (with-stdout-to rows3.out.html
   (run ./nibc.exe --run ../examples/rows.nib --wire Three))))

(rule
 (alias runtest)
 (deps ../test/rows3.expected.html rows3.out.html)
 (action (diff ../test/rows3.expected.html rows3.out.html)))

(rule
 (targets rows0.out.html)
 (deps ../examples/rows.nib)
 (action
  (with-stdout-to rows0.out.html
   (run ./nibc.exe --run ../examples/rows.nib --wire Empty))))

(rule
 (alias runtest)
 (deps ../test/rows0.expected.html rows0.out.html)
 (action (diff ../test/rows0.expected.html rows0.out.html)))

; M2.5 acceptance: the same three rows carrying fields `item` never reads. The
; expected bytes are `rows3.expected.html` itself, not a second copy of it, so
; the diff states the width claim directly: extra fields change nothing.
(rule
 (targets rowswide.out.html)
 (deps ../examples/rows.nib)
 (action
  (with-stdout-to rowswide.out.html
   (run ./nibc.exe --run ../examples/rows.nib --wire Wide))))

(rule
 (alias runtest)
 (deps ../test/rows3.expected.html rowswide.out.html)
 (action (diff ../test/rows3.expected.html rowswide.out.html)))

; M3.5 acceptance: the same page, written the other way round. `rows.nib`
; reads `row.title` / `row.note`; `rows-destructure.nib` unpacks the row with
; `let {title, note} = row;` and is otherwise the same program. Both are diffed
; against `rows3.expected.html` -- the *same* 89 bytes, the wide wire included,
; so "extra labels tolerated" is asserted for the destructured form too and
; byte-equality between the two spellings is the artifact.
(rule
 (targets rows-destructure-wide.out.html)
 (deps ../examples/rows-destructure.nib)
 (action
  (with-stdout-to rows-destructure-wide.out.html
   (run ./nibc.exe --run ../examples/rows-destructure.nib --wire Wide))))

(rule
 (alias runtest)
 (deps ../test/rows3.expected.html rows-destructure-wide.out.html)
 (action (diff ../test/rows3.expected.html rows-destructure-wide.out.html)))

(rule
 (targets rows-destructure-three.out.html)
 (deps ../examples/rows-destructure.nib)
 (action
  (with-stdout-to rows-destructure-three.out.html
   (run ./nibc.exe --run ../examples/rows-destructure.nib --wire Three))))

(rule
 (alias runtest)
 (deps ../test/rows3.expected.html rows-destructure-three.out.html)
 (action (diff ../test/rows3.expected.html rows-destructure-three.out.html)))

; M3 acceptance: "edit a JSON file, re-run, the page changes". One program, two
; data files, two hand-written expected pages. The goldens are written from the
; data, not copied from the program's output, so the diff states the claim the
; milestone makes: the page follows the file.
(rule
 (targets page-data-a.out.html)
 (deps ../examples/page-data.nib ../examples/data/rows-a.json)
 (action
  (with-stdout-to page-data-a.out.html
   (run ./nibc.exe --run ../examples/page-data.nib --wire Prod
        --arg source=../examples/data/rows-a.json))))

(rule
 (alias runtest)
 (deps ../test/page-data-a.expected.html page-data-a.out.html)
 (action (diff ../test/page-data-a.expected.html page-data-a.out.html)))

(rule
 (targets page-data-b.out.html)
 (deps ../examples/page-data.nib ../examples/data/rows-b.json)
 (action
  (with-stdout-to page-data-b.out.html
   (run ./nibc.exe --run ../examples/page-data.nib --wire Prod
        --arg source=../examples/data/rows-b.json))))

(rule
 (alias runtest)
 (deps ../test/page-data-b.expected.html page-data-b.out.html)
 (action (diff ../test/page-data-b.expected.html page-data-b.out.html)))

; The failure side is part of the acceptance, not an afterthought: a malformed
; document must be a *nib* error — a `raise` the program could have rescued,
; printed with its own name and byte offset, exiting 1 — and never an OCaml
; exception trace. Both halves are asserted: the exit code by the action, the
; absence of a trace by the check below.
(rule
 (targets page-data-broken.out page-data-broken.err)
 (deps ../examples/page-data.nib ../examples/data/broken.json)
 (action
  (with-stdout-to page-data-broken.out
   (with-stderr-to page-data-broken.err
    (with-accepted-exit-codes 1
     (run ./nibc.exe --run ../examples/page-data.nib --wire Prod
          --arg source=../examples/data/broken.json))))))

(rule
 (alias runtest)
 (deps page-data-broken.err page-data-broken.out)
 (action
  (bash
   "grep -q 'InvalidJson(\"byte ' page-data-broken.err \
    && ! grep -q 'Fatal error\\|Exception\\|Raised_\\|Stack backtrace' page-data-broken.err \
    && test ! -s page-data-broken.out")))

; M5.1 acceptance: the *same* page, split across two files, is byte-identical to
; the single-file program's -- both are diffed against `page-data-a.expected.html`
; itself, so the claim is structural change with no output change, and the two
; spellings (qualified across files, bare within one) are held to the same bytes.
(rule
 (targets multi-page.out.html)
 (deps ../examples/multi/app.nib ../examples/multi/render.nib
       ../examples/data/rows-a.json)
 (action
  (with-stdout-to multi-page.out.html
   (run ./nibc.exe --run ../examples/multi/app.nib --wire Prod
        --arg source=../examples/data/rows-a.json))))

(rule
 (alias runtest)
 (deps ../test/page-data-a.expected.html multi-page.out.html)
 (action (diff ../test/page-data-a.expected.html multi-page.out.html)))

(rule
 (targets multi-page-single.out.html)
 (deps ../examples/multi/app-single.nib ../examples/data/rows-a.json)
 (action
  (with-stdout-to multi-page-single.out.html
   (run ./nibc.exe --run ../examples/multi/app-single.nib --wire Prod
        --arg source=../examples/data/rows-a.json))))

(rule
 (alias runtest)
 (deps ../test/page-data-a.expected.html multi-page-single.out.html)
 (action (diff ../test/page-data-a.expected.html multi-page-single.out.html)))

;  Part IV stage (iii) item 4: fuel. The diverging program is a *separate
;  process*, so what is asserted is the command line's behaviour: exit code 3,
;  an empty stdout (a stopped run must not look like a short answer), and a
;  diagnostic that names the count and the flag.
(rule
 (targets fuel-diverges.out fuel-diverges.err)
 (deps ../test/fuel/diverges.nib)
 (action
  (with-stdout-to fuel-diverges.out
   (with-stderr-to fuel-diverges.err
    (with-accepted-exit-codes 3
     (run ./nibc.exe --run --steps 5000 ../test/fuel/diverges.nib))))))

(rule
 (alias runtest)
 (deps fuel-diverges.out fuel-diverges.err)
 (action
  (bash
   "grep -q 'step limit of 5000 transitions' fuel-diverges.err \
    && grep -q -- '--steps' fuel-diverges.err \
    && test ! -s fuel-diverges.out")))

;  The same program with *no* `--steps`, because the flag was never the thing
;  that bounded it: `--run` is the mode with nobody watching, and the default
;  cap is what a turn gets there. This rule is the one that would have hung
;  before the per-turn reading — a `--run` used to lift the default to "no
;  limit", so the rule below had nothing to observe for ~6 s and then forever.
;  Named explicitly, so the day the default moves this says so instead of
;  quietly passing.
(rule
 (targets fuel-default.out fuel-default.err)
 (deps ../test/fuel/diverges.nib)
 (action
  (with-stdout-to fuel-default.out
   (with-stderr-to fuel-default.err
    (with-accepted-exit-codes 3
     (run ./nibc.exe --run ../test/fuel/diverges.nib))))))

(rule
 (alias runtest)
 (deps fuel-default.out fuel-default.err)
 (action
  (bash
   "grep -q 'step limit of 250000000 transitions' fuel-default.err \
    && grep -q -- '--steps' fuel-default.err \
    && test ! -s fuel-default.out")))

;  A resident loop that performs a native operation on every turn resets its
;  drive's counter at `native_step`, so 300 turns (~8,700 total transitions)
;  finish under `--steps 2000` with exit 0, `300` on stdout, and an empty
;  stderr.
(rule
 (targets fuel-resident.out fuel-resident.err)
 (deps ../test/fuel/resident.nib (glob_files ../std/*.nib))
 (action
  (with-stdout-to fuel-resident.out
   (with-stderr-to fuel-resident.err
    (run ./nibc.exe --run --wire W --steps 2000 ../test/fuel/resident.nib)))))

(rule
 (alias runtest)
 (deps fuel-resident.out fuel-resident.err)
 (action
  (bash
   "grep -qx '300' fuel-resident.out && ! grep -q 'nib-c000' fuel-resident.err")))

; The trace under a runtime failure (§5) — through the *binary*, because that is
;  where a reader sees it: the diagnostic line, then the frames still owed when
;  it failed, innermost first. Two deep, so the order is visible; the second
;  check is the one that keeps the block *under* the line rather than inside it
;  (`rendered` is frozen in the doc, so the line must be byte-for-byte the old
;  one), and the third is silence: an empty gap prints nothing at all.
(rule
 (targets backtrace-nested.out backtrace-nested.err)
 (deps ../test/backtrace/nested.nib)
 (action
  (with-stdout-to backtrace-nested.out
   (with-stderr-to backtrace-nested.err
    (with-accepted-exit-codes 1
     (run ./nibc.exe --run ../test/backtrace/nested.nib))))))

(rule
 (alias runtest)
 (deps backtrace-nested.out backtrace-nested.err)
 (action
  (bash
   "grep -q '^Runtime error at .*:6:50: DivisionByZero[(].Division by zero.[)] [[]nib-r000[]]$' backtrace-nested.err \
    && grep -q '^  the work still owed when it failed, innermost first [(]2 frames[)]:$' backtrace-nested.err \
    && grep -A2 'innermost first' backtrace-nested.err \
       | grep -q 'operand of .*:7:50$' \
    && test ! -s backtrace-nested.out")))

; Part IV, stage (iii) acceptance — the command surface is retired -----------
;  This block parked a run in one process (`--run --park park-frame.json`),
;  continued it in another (`--run --resume`), diffed the answer against the run
;  that never stopped, diffed the re-binding measurement, and pinned the frame
;  file's head verbatim. `specs/nib-pure-ir/INTENT.md` §"Accepted losses" deleted
;  both flags and named the loss **permanent** ("cross-process park/restart —
;  `--park`/`--resume` deleted"); D7 is the surface those flags were the
;  instrument of, and removing them removed the only boundary a continuation
;  would have had to cross. With the flags gone these five rules have no command
;  to run, so they went with them, and `test/park/{park,park-rebind}.nib` +
;  `park-frame.expected.head` went with the rules.
;
;  What stayed is the *foundation*, not the flags: `lib/park.ml` (the frame
;  encoder and reader), `Eval.park_hook` and `Eval.resume_run` are compiled and
;  linked, and `lib/test_nib.ml` drives them directly — the encoder's refusals by
;  kind, the reader's refusals by tag, the round trip, a `data` record and a live
;  walk park and resume (AC4.4), a fold frame written and read back (AC2.2/AC2.3),
;  and the encoder's refusal of a captured `var` (AC2.4). The four fixtures under
;  `test/park/` are kept as *data* for the same reason: `refuse-symbol.nib` is
;  read by the P5 group, and `refuse-builtin.nib`, `resume-arm.nib` and
;  `resume-twice.frame` are the shapes the next slice's park tests need — a
;  fixture is one `git` line to use and cannot be reconstructed from prose.
;  `resume-twice.frame` is a **v2** document and the reader is v3 since Phase 6
;  step 3.1, so it is refused by version today: regenerate it from
;  `resume-arm.nib` rather than trusting it, and do not read the refusal as a
;  bug in the reader.
;  `park-fold.nib` is additionally run *whole* by AC2.2's control.

;  ─── The transcript's frozen bytes ───
;  `   error: <body> [nib-r000]` and `   fail: <body>` are printed by
;  `Printf.printf` straight to the process's stdout, so a test sharing that
;  process cannot read them back; `Eval.transcript_error_line` pins the line's
;  *text*, but only the process's own output can pin the bytes around it (the
;  3-space indent of §2.6, the blank lines, the `── Test: … ──` header) and the
;  exit codes that go with them. Two goldens, one per line kind: the `fail:`
;  line carries *no* code on purpose — a failed assertion is a verdict about the
;  program, not a diagnostic about the source, and §2.6 gives a code only to the
;  `error:` line. A change to either line is a change to a frozen shape and must
;  show up as a diff here, not as a quietly updated `contains` pin. The exit code
;  is pinned the same way: both programs fail, so both rules accept exactly 1 —
;  `(or 0 1)` would let a regression to "the error no longer counts" pass as a
;  matching golden.
(rule
 (targets transcript-error.out)
 (deps ../test/transcript/error.nib)
 (action
  (with-stdout-to
   transcript-error.out
   (with-accepted-exit-codes 1
    (run ./nibc.exe --transcript %{dep:../test/transcript/error.nib})))))

(rule
 (alias runtest)
 (deps transcript-error.out)
 (action (diff ../test/transcript/error.out transcript-error.out)))

(rule
 (targets transcript-fail.out)
 (deps ../test/transcript/fail.nib)
 (action
  (with-stdout-to
   transcript-fail.out
   (with-accepted-exit-codes 1
    (run ./nibc.exe --transcript %{dep:../test/transcript/fail.nib})))))

(rule
 (alias runtest)
 (deps transcript-fail.out)
 (action (diff ../test/transcript/fail.out transcript-fail.out)))

; ---- fibres: the scheduler's traces — retired with the surface ------------
;  Two `--conc-log` goldens used to sit here: `examples/fibres.nib` under the
;  serial default and `examples/fibres-cancel.nib` under `Conc.Sched = Conc.Fifo`.
;  `specs/nib-pure-ir/INTENT.md` D7 retires `Scope`/`Conc`/`Fibre`/`sw.*` and the
;  user-visible scheduler policy with them, so both programs are deleted (with
;  `std/conc.nib`, `std/scope.nib`, `std/park.nib`) and the traces they pinned
;  have no subject. The machine those traces exercised is now deleted too (the
;  second cut, 2026-11-06), so nothing in the tree replays
;  `test/nib-fibres/*.itf.json` any more: the conformance test that fed each
;  trace to `Conc`'s state machine went with it. The twenty traces are kept
;  because they are the *model's* output, cited by
;  `specs/nib-fibres/CONFORMANCE.md`, and regenerable from `specs/nib-fibres/*.qnt`
;  with quint; they are no longer a build dependency of anything.

;  M2 of specs/nib-pure-ir/PLAN.md: the boxed↔unboxed conversion measurement.
;  Additive by design — it reads the library, it changes nothing in it.
(executable
 (name kernel_probe)
 (modules kernel_probe)
 (libraries nib unix))

;  M5 of specs/nib-pure-ir/PLAN.md: what a `Par` loop buys end to end, with the
;  input construction and the conversion inside the number. Additive like M2's
;  probe — its own executable, nothing in the library changes for it.
(executable
 (name parfor_probe)
 (modules parfor_probe)
 (libraries nib unix))

;  `specs/nib-pure-ir/PLAN-cap-and-bench.md` job 2: what `--parallel` buys, at
;  which size, against all three arms (the default path, the builtin fold at one
;  lane, and the kernel at k lanes). Its own executable like the two probes
;  above, and deliberately not a test: a timing assertion would be an assertion
;  about the box's load average.
(executable
 (name parfor_bench)
 (modules parfor_bench)
 (libraries nib unix))

;  `specs/nib-pure-ir/CONTRACTS.md` §1, the half that could not fail before:
;  a write and a send that fail *after* the bytes start moving. Each check
;  injects the failure (a child under `RLIMIT_FSIZE` for the disk, a
;  socketpair that hangs up mid-payload for the socket) and asserts that the
;  trace and the operating system agree — including about a partial result.
(test
 (name test_fx_sound)
 (modules test_fx_sound)
 (deps (glob_files ../std/*.nib) nibc.exe)
 ;  a1 sets RLIMIT_FSIZE on its own forked child before exec'ing nibc; OCaml's
 ;  Unix module has no setrlimit, so this is the one stub in the tree.
 (foreign_stubs (language c) (names fx_rlimit_stubs))
 (libraries nib unix))

;  `specs/nib-hoist/INTENT.md` §4A-§4I, §7: capture at a boundary and adoption
;  of the remainder. Its own executable because every claim is a *run* of a
;  fixture — three effector calls that leave a file behind, and a second runtime
;  that continues them — and because the evidence is the file, not a message.
;  Read by plain name, so copied in as real targets: a `(deps ...)` entry is
;  materialised only inside the sandboxed runtest action, and a reader running
;  `dune exec lib/test_hoist.exe` would not find it.
(rule
 (target hoist-prefix.nib)
 (deps ../test/hoist/prefix.nib)
 (action (copy %{dep:../test/hoist/prefix.nib} %{target})))
(rule
 (target hoist-prefix-far.nib)
 (deps ../test/hoist/prefix-far.nib)
 (action (copy %{dep:../test/hoist/prefix-far.nib} %{target})))
(rule
 (target hoist-nested.nib)
 (deps ../test/hoist/nested.nib)
 (action (copy %{dep:../test/hoist/nested.nib} %{target})))
(rule
 (target hoist-prefix-other.nib)
 (deps ../test/hoist/prefix-other.nib)
 (action (copy %{dep:../test/hoist/prefix-other.nib} %{target})))
;  `specs/nib-dispatch/INTENT.md` §7's four units, each a script that runs the
;  real binary as real processes. They live in `@runtest` for the same reason
;  `handoff.sh` does: a claim the suite does not run is a claim nothing defends.
;  Each takes `nibc` as its first argument and makes its own scratch directory.
(rule
 (alias runtest)
 (deps ../test/hoist/signal-capture.sh ../test/hoist/slow.nib
       (glob_files ../std/*.nib) %{exe:nibc.exe})
 (action (run sh %{dep:../test/hoist/signal-capture.sh} %{exe:nibc.exe})))
(rule
 (alias runtest)
 (deps ../test/hoist/shed-and-take.sh ../test/hoist/slow.nib
       (glob_files ../std/*.nib) %{exe:nibc.exe})
 (action (run sh %{dep:../test/hoist/shed-and-take.sh} %{exe:nibc.exe})))
(rule
 (alias runtest)
 (deps ../test/hoist/twice.sh ../test/hoist/prefix.nib
       (glob_files ../std/*.nib) %{exe:nibc.exe})
 (action (run sh %{dep:../test/hoist/twice.sh} %{exe:nibc.exe})))
(rule
 (alias runtest)
 (deps ../test/hoist/kill-mid-publish.sh (glob_files ../std/*.nib) %{exe:nibc.exe})
 (action (run sh %{dep:../test/hoist/kill-mid-publish.sh} %{exe:nibc.exe})))

;  `bin/nibc`, the wrapper that resolves the *published* compiler from the
;  deployment record, is the binary the docs now name — so it is gated like one.
;  The test plants a copy of the real wrapper in a fake repo and a realised
;  stand-in for a store output, and asserts resolution, refusal and exec.
(rule
 (alias runtest)
 (deps ../bin/nibc ../test/wrapper/nibc-wrapper.sh)
 (action (run sh %{dep:../test/wrapper/nibc-wrapper.sh} %{dep:../bin/nibc})))

;  The process-level half of the hoist suite: two real `nibc` processes, one
;  continuation, real files as the evidence (§7 checks 1-7). It lives in
;  `@runtest` because a claim about processes that only a hand run checks is a
;  claim the suite does not make. `test_hoist` is the in-process half beside it.
(rule
 (alias runtest)
 (deps ../test/hoist/handoff.sh ../test/hoist/prefix.nib ../test/hoist/prefix-far.nib
       ../test/hoist/prefix-other.nib ../test/hoist/prefix-extra.nib
       ../test/hoist/prefix-w2.nib ../test/hoist/nested.nib ../test/hoist/slow.nib
       (glob_files ../std/*.nib) %{exe:nibc.exe})
 (action (run sh %{dep:../test/hoist/handoff.sh} %{exe:nibc.exe})))

;  The two dumps, `--inspect-journal` and `--inspect-frame`. Its own test
;  executable for the reason `test_journal` has one: the fixture of a dump is a
;  file the suite caused `nibc` to write (a journal from `--journal-out`, a
;  frame from `--capture`) and the evidence is the text it printed and the
;  status it answered — neither is visible from inside the process. The `.nib`
;  files are `deps` because the runs that write those artifacts need them.
(test
 (name test_inspect)
 (modules test_inspect)
 (deps (glob_files ../test/journal/*.nib) hoist-prefix.nib
       (glob_files ../std/*.nib) %{exe:nibc.exe})
 (action (run %{test} %{exe:nibc.exe}))
 (libraries nib unix))

(test
 (name test_hoist)
 (modules test_hoist)
 (deps hoist-prefix.nib hoist-prefix-far.nib hoist-prefix-other.nib hoist-nested.nib
       (glob_files ../std/*.nib))
 (libraries nib unix))

(test
 (name test_svm)
 (modules test_svm)
 (libraries nib unix))

;  #441: the native serve daemon, over real sockets. Its own executable because
;  every claim is about something outside the daemon — a directory mode, a byte
;  on a socket, a count of answered requests — and because the daemon runs in a
;  thread while the test is the client.
;  ─── §2 item 6: the domain half ───
;  `specs/nib-connection-claims`: the registry has no lock, and the reason is
;  that a cell is reachable from one domain only. Its own executable because it
;  spawns a domain, and OCaml refuses `Unix.fork` after that — `test_nib` forks.
(test
 (name test_claims_domain)
 (modules test_claims_domain)
 (libraries nib unix))

;  `specs/nib-multi-domain` §2 items 5, 6, 7 (and 3, as it lands): the owner
;  identity is per-domain, and no domain may adopt a row it did not claim. Its
;  own executable for the same reason as the file above — it spawns domains,
;  and `test_nib` forks. Two lanes on one barrier, because a single-threaded
;  test cannot see a process-wide cell being written by somebody else.
(test
 (name test_multi_domain)
 (modules test_multi_domain)
 (libraries nib unix))

;  `specs/nib-serve-reactor` §2 item 3: the readiness seam runs its own
;  conformance file once per backend, so "the same checks on both" is a
;  property of the build and not something the report has to be believed about.
;  `specs/nib-serve-reactor` §2 items 1, 2, 4, 5 and 7: the loop's own numbers
;  — idle CPU, arrivals inside a millisecond, no per-connection work in a
;  round, deadlines woken once, one turn per connection per round, one scratch
;  buffer. A stub body, one file, run once per backend like the seam's
;  conformance file: "the same checks on both" is a property of the build.
;  An `executable`, not a `test`: a `test` stanza runs the file itself under
;  `@runtest` with the default backend, so epoll would be run twice and the
;  suite's own total would be wrong by one pass.
(executable
 (name test_reactor)
 (modules test_reactor)
 (libraries nib unix threads.posix))

(rule
 (alias runtest)
 (deps test_reactor.exe)
 ;  epoll is Linux's; elsewhere the select run below is the whole check.
 (enabled_if (= %{system} linux))
 (action (setenv NIB_IO_BACKEND epoll (run ./test_reactor.exe))))

(rule
 (alias runtest)
 (deps test_reactor.exe)
 (action (setenv NIB_IO_BACKEND select (run ./test_reactor.exe))))

;  Same shape as `test_reactor`: the two rules below are the only runs.
(executable
 (name test_io_backend)
 (modules test_io_backend)
 (libraries nib unix))

(rule
 (alias runtest)
 (deps test_io_backend.exe)
 ;  epoll is Linux's; elsewhere the select run below is the whole check.
 (enabled_if (= %{system} linux))
 (action (setenv NIB_IO_BACKEND epoll (run ./test_io_backend.exe))))

(rule
 (alias runtest)
 (deps test_io_backend.exe)
 (action (setenv NIB_IO_BACKEND select (run ./test_io_backend.exe))))

(test
 (name test_nibd)
 (modules test_nibd)
 ;  #442: no `NIB_BENCH_EFFECTORS` here on purpose — this file is the proof
 ;  that the daemon serves a program with no benchmark gate set.
 (action (setenv NIBC_STD %{project_root}/std (run %{test})))
 (libraries nib unix threads.posix))

;  #433 part 1: the flat arena. Its own executable because the claims are about
;  *bytes* — the layout is a format, and the format is what is asserted: the
;  same value lays out to the same bytes, the bytes read back to the same value,
;  a forged or truncated buffer is an `Error` and not an exception, and the
;  non-allocating scan agrees with the allocating read. Nothing here measures
;  time; the numbers live in `specs/nib-serve/433-ARENA.md`.
(test
 (name test_arena)
 (modules test_arena)
 (libraries nib unix))

;  #433 part 3: `Park` as a host import. The claims are about the seam, not
;  about bytes or time: an import list is the whole permission to suspend, and a
;  real `Handles.Park` raised inside `native_step` leaves the machine
;  re-enterable. Both are observable from outside the library, which is why this
;  is a test and `arena_probe` is not.
;  #435–#438: the socket's frames, handshake and host, over a real socketpair.
;  Run through a rule so that `shost.ml` is a *declared* dependency: the line
;  budget in #435 is a measurement of that file, and a test that measured a path
;  which happened not to exist would report a flattering zero.
(executable
 (name test_sock)
 (modules test_sock)
 (libraries nib unix threads.posix))

(rule
 (alias runtest)
 (deps test_sock.exe shost.ml)
 (action (run ./test_sock.exe shost.ml)))

(test
 (name test_wasm_park)
 (modules test_wasm_park)
 (libraries nib unix))

(test
 (name test_aot)
 (modules test_aot)
 (libraries nib unix))

;  #433's benchmark, additive like `bench/`: it reads the library, changes
;  nothing in it, and is deliberately not a test (a timing assertion would be an
;  assertion about the box's load average).
(executable
 (name flatvec_probe)
 (modules flatvec_probe)
 (libraries nib unix))

;  The flat-`Vec` assertions (`specs/nib-flat-vec`). An `executable` plus an
;  explicit `runtest` rule, same shape as `test_reactor`: a `(test …)` stanza
;  would also run it, but the rule states which alias runs it and keeps the
;  suite's own total honest.
(executable
 (name test_flatvec)
 (modules test_flatvec)
 (libraries nib unix))

(rule
 (alias runtest)
 (deps test_flatvec.exe)
 (action (run ./test_flatvec.exe)))

(executable
 (name arena_probe)
 (modules arena_probe)
 (libraries nib unix))



;  The builtin namespaces and native instances, printed as signatures for the
;  site's stdlib reference (areas/utilities/nib-site/gen/build_docs.py). Not
;  installed: package.nix copies nibc alone.
(executable
 (name stddump)
 (modules stddump)
 (libraries nib))
