diff --git a/docs/testing.md b/docs/testing.md index 93d8821b..bc962193 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -4233,6 +4233,54 @@ was granted to, once). `fuzz_op_sequence::Op` gained the invoice, bill, payment, settlement, write-off, void and draft-state ops and a control-check invariant (control accounts equal their documents). +### Running P12 step 2 + +One night runs, in order: every fuzz target side by side (one build first, +then each through `just fuzz `), `just prove-all`, +`just oracle` (skipped as failed when Docker is unavailable), the property +tests at the maxima below, then one mutation recipe per crate (`mutants`, +`mutants-store`, `mutants-import`, `mutants-config`, `mutants-mcp`, +`mutants-host`, `mutants-package`, `mutants-vault`, `mutants-cli`), one at +a time. A crash or a failed step does not stop the night; it is recorded +and the next step runs. At the deadline the running step's process group +is stopped and marked partial, so the machine is free in the morning. + +```sh +just p12-night # start detached; prints the summary and log paths +just p12-status # the summary and whether a runner is live +just p12-stop # stop a live runner the way the deadline does +``` + +State is in `${XDG_CACHE_HOME:-$HOME/.cache}/wanderbooks/p12/`: +`summary.md` (rewritten after every step: status, start, end, duration, +result), `logs/.log` (and `logs/fuzz-.log`), `done/` +markers, and `mutants//` with each run's `mutants.out` (the core) or +`mutants-` output. A step with a done marker is skipped, so a night +resumes where the last one stopped; a partial or failed step has no marker +and reruns next night. Mutation runs start over after a cut-off: the +recipes as written do not pass cargo-mutants' `--iterate`, so a half-run +crate is redone rather than mixed with the old output. + +The knobs: `P12_FUZZ_SECONDS` (per target, default 3600), `P12_STOP` +(local time, default 07:00, the next occurrence after the start), +`P12_STEPS` (a space-separated list, e.g. `"fuzz props-max"`), +`P12_DRY_RUN=1` (every step becomes a sleep and a fake result, no cargo), +and `P12_DRY_SLEEP` (that sleep, default 2 seconds). + +The property maxima step is the documented ones, with their tests: +`STORE_CASES=5000` on `ledger-store` `tests/store.rs` +`refused_commands_never_reach_the_journal`; `WB_ORDERS_DEPTH=6` on +`tests/asset_orders.rs` +`every_order_of_runs_reversals_disposals_and_closes_keeps_the_books_right` +(plan 8.2 P12; `plan/STATUS.md` says six is P12's); `CF_CASES=5000` on +`ledger-core` `tests/spec_cash_flow.rs`; `PROPTEST_CASES=5000` on +`tests/spec_party_totals.rs`; `PROPTEST_CASES=2000` on `ledger-store` +`tests/settings.rs` `settings_replay_is_pure`. One documented maximum has +no knob: `tests/form1099.rs`'s `party_totals_are_the_1099s_buckets` is +described above as `PROPTEST_CASES` with 2,000 clean, but the test +hard-codes 256; P12 must either give it the knob or record the smaller +count. + ### ledger-core - [P2 slices 3 to 8] `settings.rs` from scratch. diff --git a/justfile b/justfile index 9d4b3754..81e6c5fb 100644 --- a/justfile +++ b/justfile @@ -241,6 +241,18 @@ mutants-cli shard="0/1": sweep-test-tmp PROPTEST_MAX_SHRINK_ITERS=0 TMPDIR={{mutants_tmp}} cargo mutants -p ledger-cli --test-package ledger-cli --no-default-features -j 4 --timeout-multiplier 3 --minimum-test-timeout 600 --shard {{shard}} -o mutants-cli @just sweep-test-tmp +# P12 step 2: start the resumable overnight run, detached (D-66, docs/testing.md) +p12-night: + @scripts/p12-night.sh start + +# P12 step 2: print the summary and whether a runner is live +p12-status: + @scripts/p12-night.sh status + +# P12 step 2: stop a live runner the way the 07:00 deadline does +p12-stop: + @scripts/p12-night.sh stop + # No mutation runs on the viewer (owner, 2026-10-18: they crushed the # machine, and for the GUI it's "not as critical"). Its two musts are # tested directly instead: it never writes the books (tests/read_only.rs) diff --git a/scripts/p12-night.sh b/scripts/p12-night.sh new file mode 100755 index 00000000..3ac63395 --- /dev/null +++ b/scripts/p12-night.sh @@ -0,0 +1,619 @@ +#!/usr/bin/env bash +# P12 step 2: the resumable overnight test run (D-66, docs/testing.md). +# +# `just p12-night` starts this detached. It walks one queue of steps, skips +# those whose done marker exists, rewrites the summary after each step, and +# stops at P12_STOP so the machine is free in the morning. Every step's +# log and status live under ${XDG_CACHE_HOME:-$HOME/.cache}/wanderbooks/p12. +set -euo pipefail + +self=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}") +repo=$(cd "$(dirname "$self")/.." && pwd) +state=${XDG_CACHE_HOME:-$HOME/.cache}/wanderbooks/p12 +logs=$state/logs +steps_dir=$state/steps +done_dir=$state/done +pidfile=$state/pid +step_pidfile=$state/step.pid + +fuzz_seconds=${P12_FUZZ_SECONDS:-3600} +dry_sleep=${P12_DRY_SLEEP:-2} + +queue=( + fuzz + prove-all + oracle + props-max + mutants + mutants-store + mutants-import + mutants-config + mutants-mcp + mutants-host + mutants-package + mutants-vault + mutants-cli +) + +# The mutation recipes and the output directory each writes (the core +# recipe uses cargo-mutants' default; the rest pass -o ). +mutation_out() { + case "$1" in + mutants) echo mutants.out ;; + *) echo "$1" ;; + esac +} + +iso() { date '+%Y-%m-%d %H:%M:%S'; } + +ensure_dirs() { + mkdir -p "$logs" "$steps_dir" "$done_dir" "$state/mutants" +} + +read_field() { # file key + sed -n "s/^$2=//p" "$1" 2>/dev/null | head -1 +} + +record_result() { # step text + printf '%s\n' "$2" > "$steps_dir/$1.result" +} + +record_details() { # step (stdin) + cat > "$steps_dir/$1.details" +} + +write_status() { # step status start end secs result + { + printf 'status=%s\n' "$2" + printf 'start=%s\n' "$3" + printf 'end=%s\n' "$4" + printf 'secs=%s\n' "$5" + printf 'result=%s\n' "$6" + } > "$steps_dir/$1.status" +} + +runner_alive() { + [ -f "$pidfile" ] || return 1 + local pid args + pid=$(cat "$pidfile" 2>/dev/null || true) + [ -n "$pid" ] || return 1 + kill -0 "$pid" 2>/dev/null || return 1 + args=$(ps -o args= -p "$pid" 2>/dev/null || true) + [[ $args == *p12-night.sh* ]] || return 1 + return 0 +} + +validate_selection() { + if [ -z "${P12_STEPS:-}" ]; then + return 0 + fi + local step + for step in ${P12_STEPS}; do + case " ${queue[*]} " in + *" $step "*) ;; + *) + echo "P12: unknown step '$step' (P12_STEPS); known: ${queue[*]}" >&2 + return 2 + ;; + esac + done +} + +selected_steps() { + if [ -n "${P12_STEPS:-}" ]; then + # Word splitting is the hook's contract: a space-separated list. + # shellcheck disable=SC2206 + local picked=(${P12_STEPS}) + printf '%s\n' "${picked[@]}" + else + printf '%s\n' "${queue[@]}" + fi +} + +compute_deadline() { + local stop=${P12_STOP:-07:00} + local target now + now=$(date +%s) + if ! target=$(date -d "today $stop" +%s 2>/dev/null); then + echo "P12: P12_STOP '$stop' is not a time this date understands" >&2 + return 2 + fi + if [ "$target" -le "$now" ]; then + target=$(date -d "tomorrow $stop" +%s) + fi + printf '%s\n' "$target" +} + +write_summary() { + local state_word started deadline step status start end secs result + state_word=$(cat "$state/run.state" 2>/dev/null || echo "not started") + started=$(cat "$state/started" 2>/dev/null || echo "-") + deadline=$(cat "$state/deadline.iso" 2>/dev/null || echo "-") + { + echo "# P12 night" + echo + echo "- state: $state_word" + echo "- started: $started" + echo "- deadline: $deadline" + echo "- state dir: \`$state\`" + echo + echo "| step | status | start | end | duration | result |" + echo "|---|---|---|---|---|---|" + for step in "${queue[@]}"; do + if [ -f "$done_dir/$step" ]; then + status="done" + elif [ -f "$steps_dir/$step.status" ]; then + status=$(read_field "$steps_dir/$step.status" status) + else + status="not started" + fi + if [ -f "$steps_dir/$step.status" ]; then + start=$(read_field "$steps_dir/$step.status" start) + end=$(read_field "$steps_dir/$step.status" end) + secs=$(read_field "$steps_dir/$step.status" secs) + result=$(read_field "$steps_dir/$step.status" result) + else + start="-" + end="-" + secs="" + result="" + fi + [ -n "$start" ] || start="-" + [ -n "$end" ] || end="-" + if [ -n "$secs" ]; then + secs="${secs}s" + else + secs="-" + fi + result=${result//|/\\|} + printf '| %s | %s | %s | %s | %s | %s |\n' \ + "$step" "$status" "$start" "$end" "$secs" "$result" + done + if [ -f "$steps_dir/fuzz.details" ]; then + echo + echo "## Fuzzing, per target" + echo + cat "$steps_dir/fuzz.details" + fi + if [ -f "$steps_dir/props-max.details" ]; then + echo + echo "## Property maxima" + echo + cat "$steps_dir/props-max.details" + fi + } > "$state/summary.md.tmp" + mv "$state/summary.md.tmp" "$state/summary.md" +} + +# ---------------------------------------------------------------- step bodies + +dry_result() { + local step=$1 target + case "$step" in + fuzz) + { + for f in "$repo"/fuzz/fuzz_targets/*.rs; do + target=$(basename "$f" .rs) + echo "- $target: dry run, 0 executions, no crashes" + done + } | record_details fuzz + record_result fuzz "dry run: every target ran for a fake moment" + ;; + mutants|mutants-store|mutants-import|mutants-config|mutants-mcp|mutants-host|mutants-package|mutants-vault|mutants-cli) + record_result "$step" "dry run: caught 0, missed 0, timeout 0, unviable 0" + ;; + props-max) + echo "- dry run: the property commands were not run" | record_details props-max + record_result props-max "dry run: property maxima" + ;; + *) + record_result "$step" "dry run: $step ok" + ;; + esac +} + +run_fuzz() { + local build_log=$logs/fuzz-build.log + local fuzz_bin target + if ! fuzz_bin=$(mise which cargo-fuzz 2>>"$build_log"); then + record_result fuzz "failed: cargo-fuzz not found (see logs/fuzz-build.log)" + return 1 + fi + echo "building every fuzz target (one build)" >&2 + if ! (cd "$repo/fuzz" && RUSTUP_TOOLCHAIN=nightly-2026-10-01 "$fuzz_bin" build) >>"$build_log" 2>&1; then + record_result fuzz "failed: the fuzz-target build failed (see logs/fuzz-build.log)" + return 1 + fi + + local -a names=() pids=() + local f + for f in "$repo"/fuzz/fuzz_targets/*.rs; do + target=$(basename "$f" .rs) + names+=("$target") + # Each target through the existing recipe, all side by side. + just --justfile "$repo/justfile" fuzz "$target" "$fuzz_seconds" \ + > "$logs/fuzz-$target.log" 2>&1 & + pids+=("$!") + done + if [ "${#names[@]}" -eq 0 ]; then + record_result fuzz "failed: no fuzz targets found" + return 1 + fi + + local i rc runs crash_list crash_count total=0 crashes=0 failed=0 + local details=$state/steps/fuzz.details.tmp + : > "$details" + for i in "${!pids[@]}"; do + target=${names[$i]} + rc=0 + wait "${pids[$i]}" || rc=$? + runs=$(grep -oE 'stat::number_of_executed_units: *[0-9]+' "$logs/fuzz-$target.log" | + tail -1 | grep -oE '[0-9]+' || true) + if [ -z "$runs" ]; then + runs=$(grep -oE 'Done [0-9]+ runs' "$logs/fuzz-$target.log" | + tail -1 | grep -oE '[0-9]+' || true) + fi + runs=${runs:-0} + total=$((total + runs)) + crash_list=$(find "$repo/fuzz/artifacts/$target" -mindepth 1 -maxdepth 1 -type f 2>/dev/null | + sort || true) + crash_count=$(printf '%s\n' "$crash_list" | grep -c . || true) + crashes=$((crashes + crash_count)) + { + echo "- $target: $runs executions" + if [ "$crash_count" -gt 0 ]; then + echo " - crash files:" + while IFS= read -r crash; do + [ -n "$crash" ] || continue + echo " - \`$crash\`" + done <<<"$crash_list" + fi + } >> "$details" + if [ "$rc" -ne 0 ] || [ "$crash_count" -gt 0 ]; then + failed=1 + fi + done + mv "$details" "$state/steps/fuzz.details" + + if [ "$failed" -eq 0 ]; then + record_result fuzz "${#names[@]} targets, $total executions, no crashes" + return 0 + fi + record_result fuzz "${#names[@]} targets, $total executions, $crashes crash files (see the details below)" + return 1 +} + +run_oracle() { + if ! docker info >/dev/null 2>&1; then + record_result oracle "failed: docker info failed; Docker is not available to this user" + return 1 + fi + if ! just --justfile "$repo/justfile" oracle; then + record_result oracle "failed: just oracle failed (see the log)" + return 1 + fi + record_result oracle "just oracle passed" +} + +run_props_max() { + # The maxima docs/testing.md names, with the tests those maxima name. + # (The form1099 property's documented 2,000 has no env knob: the test + # hard-codes 256, so it is recorded below rather than guessed at.) + local -a cmds=( + "STORE_CASES=5000 cargo test -p ledger-store --test store refused_commands_never_reach_the_journal" + "WB_ORDERS_DEPTH=6 cargo test -p ledger-store --test asset_orders every_order_of_runs_reversals_disposals_and_closes_keeps_the_books_right" + "CF_CASES=5000 cargo test -p ledger-core --test spec_cash_flow" + "PROPTEST_CASES=5000 cargo test -p ledger-core --test spec_party_totals" + "PROPTEST_CASES=2000 cargo test -p ledger-store --test settings settings_replay_is_pure" + ) + local details=$state/steps/props-max.details.tmp + local cmd rc failed=0 + : > "$details" + for cmd in "${cmds[@]}"; do + echo "\$ $cmd" >> "$details" + rc=0 + eval "$cmd" || rc=$? + if [ "$rc" -eq 0 ]; then + echo " ok" >> "$details" + else + echo " FAILED" >> "$details" + failed=1 + fi + done + echo "note: docs/testing.md's form1099 party-totals property says 2,000, but the test hard-codes 256; not run at 2,000 here." >> "$details" + mv "$details" "$state/steps/props-max.details" + if [ "$failed" -eq 0 ]; then + record_result props-max "property tests at their documented maxima passed" + return 0 + fi + record_result props-max "failed: a property-maximum command failed (see the details below)" + return 1 +} + +run_mutants() { + local step=$1 out dir dest rc=0 + out=$(mutation_out "$step") + dir=$repo/$out + dest=$state/mutants/$step + # No resume: the recipes do not pass cargo-mutants' --iterate, so a + # cut-off crate starts over rather than mixing a half-finished run. + rm -rf "$dir" + just --justfile "$repo/justfile" "$step" || rc=$? + rm -rf "$dest" + mkdir -p "$dest" + if [ -d "$dir" ]; then + cp -a "$dir/." "$dest/" + fi + local caught missed timeout unviable + count_file() { if [ -f "$1" ]; then wc -l < "$1" | tr -d ' '; else echo 0; fi; } + caught=$(count_file "$dest/caught.txt") + missed=$(count_file "$dest/missed.txt") + timeout=$(count_file "$dest/timeout.txt") + unviable=$(count_file "$dest/unviable.txt") + if [ "$rc" -eq 0 ]; then + record_result "$step" "caught $caught, missed $missed, timeout $timeout, unviable $unviable" + return 0 + fi + record_result "$step" "failed: caught $caught, missed $missed, timeout $timeout, unviable $unviable (see the log)" + return 1 +} + +run_one_step() { + local step=$1 + ensure_dirs + cd "$repo" + if [ "${P12_DRY_RUN:-0}" = 1 ]; then + sleep "$dry_sleep" + dry_result "$step" + return 0 + fi + case "$step" in + fuzz) run_fuzz ;; + prove-all) + if just --justfile "$repo/justfile" prove-all; then + record_result prove-all "just prove-all passed" + else + record_result prove-all "failed: just prove-all failed (see the log)" + return 1 + fi + ;; + oracle) run_oracle ;; + props-max) run_props_max ;; + mutants|mutants-store|mutants-import|mutants-config|mutants-mcp|mutants-host|mutants-package|mutants-vault|mutants-cli) + run_mutants "$step" + ;; + *) + record_result "$step" "failed: unknown step" + return 1 + ;; + esac +} + +# ------------------------------------------------------------------ the runner + +current_step="" +step_pid="" +watchdog_pid="" +step_start_iso="" +step_start_epoch=0 +deadline_epoch=0 + +cleanup() { + local now now_epoch + now=$(iso) + now_epoch=$(date +%s) + if [ -n "$watchdog_pid" ]; then + kill "$watchdog_pid" 2>/dev/null || true + fi + if [ -n "$current_step" ] && [ -n "$step_pid" ] && kill -0 "$step_pid" 2>/dev/null; then + kill -TERM -- "-$step_pid" 2>/dev/null || true + sleep 2 + kill -KILL -- "-$step_pid" 2>/dev/null || true + write_status "$current_step" partial "$step_start_iso" "$now" \ + "$((now_epoch - step_start_epoch))" "stopped by p12-stop" + fi + echo "stopped" > "$state/run.state" + write_summary + rm -f "$pidfile" "$step_pidfile" + echo "P12: stopped." >&2 + exit 143 +} + +run_step() { + local step=$1 + local log=$logs/$step.log + current_step=$step + step_start_iso=$(iso) + step_start_epoch=$(date +%s) + : > "$log" + rm -f "$steps_dir/$step.details" + write_status "$step" running "$step_start_iso" "" "" "running" + write_summary + + setsid "$self" --run-step "$step" >>"$log" 2>&1 "$step_pidfile" + + ( + while kill -0 "$step_pid" 2>/dev/null; do + if [ "$(date +%s)" -ge "$deadline_epoch" ]; then + touch "$state/deadline.reached" + kill -TERM -- "-$step_pid" 2>/dev/null || true + sleep 3 + kill -KILL -- "-$step_pid" 2>/dev/null || true + exit 0 + fi + sleep 2 + done + ) & + watchdog_pid=$! + + local rc=0 + wait "$step_pid" || rc=$? + kill "$watchdog_pid" 2>/dev/null || true + wait "$watchdog_pid" 2>/dev/null || true + watchdog_pid="" + + local end end_epoch secs result + end=$(iso) + end_epoch=$(date +%s) + secs=$((end_epoch - step_start_epoch)) + result=$(cat "$steps_dir/$step.result" 2>/dev/null || echo "no result recorded") + + if [ "$rc" -eq 0 ]; then + : > "$done_dir/$step" + write_status "$step" "done" "$step_start_iso" "$end" "$secs" "$result" + elif [ -f "$state/deadline.reached" ]; then + write_status "$step" partial "$step_start_iso" "$end" "$secs" \ + "stopped at the deadline (${P12_STOP:-07:00}); reruns next night" + else + write_status "$step" failed "$step_start_iso" "$end" "$secs" "$result" + fi + current_step="" + step_pid="" + return 0 +} + +run_runner() { + ensure_dirs + cd "$repo" + validate_selection + # A runner killed with KILL leaves its step marked running. No runner is + # live now, so record that step as partial (it has no done marker and + # reruns below). + local old_step old_start + for old_step in "${queue[@]}"; do + if [ ! -f "$done_dir/$old_step" ] && + [ "$(read_field "$steps_dir/$old_step.status" status)" = running ]; then + old_start=$(read_field "$steps_dir/$old_step.status" start) + write_status "$old_step" partial "${old_start:--}" "$(iso)" 0 \ + "a previous runner stopped without finishing; reruns now" + fi + done + printf '%s\n' "$$" > "$pidfile" + rm -f "$state/deadline.reached" "$step_pidfile" + local started_iso + started_iso=$(iso) + printf '%s\n' "$started_iso" > "$state/started" + if ! deadline_epoch=$(compute_deadline); then + rm -f "$pidfile" + exit 2 + fi + date -d "@$deadline_epoch" '+%Y-%m-%d %H:%M:%S' > "$state/deadline.iso" + echo "running" > "$state/run.state" + write_summary + trap cleanup TERM INT + + local step + while IFS= read -r step; do + [ -n "$step" ] || continue + if [ -f "$done_dir/$step" ]; then + continue + fi + if [ "$(date +%s)" -ge "$deadline_epoch" ]; then + echo "deadline" > "$state/run.state" + write_summary + rm -f "$pidfile" "$step_pidfile" + return 0 + fi + run_step "$step" + write_summary + if [ -f "$state/deadline.reached" ]; then + echo "deadline" > "$state/run.state" + write_summary + rm -f "$pidfile" "$step_pidfile" + return 0 + fi + done < <(selected_steps) + + echo "finished" > "$state/run.state" + write_summary + rm -f "$pidfile" "$step_pidfile" +} + +start_runner() { + ensure_dirs + validate_selection + if runner_alive; then + echo "P12: a runner is already live (pid $(cat "$pidfile")); not starting another." + echo "Summary: $state/summary.md" + return 0 + fi + rm -f "$pidfile" "$step_pidfile" + setsid nohup "$self" run >>"$logs/runner.log" 2>&1 /dev/null || true + for i in $(seq 1 50); do + kill -0 "$pid" 2>/dev/null || break + sleep 0.1 + done + if kill -0 "$pid" 2>/dev/null; then + kill -KILL "$pid" 2>/dev/null || true + fi + if [ -f "$step_pidfile" ]; then + spid=$(cat "$step_pidfile" 2>/dev/null || true) + if [ -n "$spid" ] && kill -0 "$spid" 2>/dev/null; then + kill -TERM -- "-$spid" 2>/dev/null || true + sleep 1 + kill -KILL -- "-$spid" 2>/dev/null || true + fi + fi + rm -f "$pidfile" "$step_pidfile" + echo "P12: stopped." +} + +usage() { + cat >&2 <<'EOF' +usage: scripts/p12-night.sh start|status|stop + scripts/p12-night.sh --run-step STEP (internal) +EOF + exit 2 +} + +case "${1:-}" in + start) start_runner ;; + status) show_status ;; + stop) stop_runner ;; + run) run_runner ;; + --run-step) + [ -n "${2:-}" ] || usage + run_one_step "$2" + ;; + *) usage ;; +esac