Skip to content

feat!: redesign LSpec v3 as a Bool-based, parallel-first, formally verified test framework - #94

Merged
johnchandlerburnham merged 1 commit into
mainfrom
v3-redesign
Nov 30, 2026
Merged

johnchandlerburnham merged 1 commit into
mainfrom
v3-redesign

Conversation

@arthurpaulino

Copy link
Copy Markdown
Member

Full breaking redesign of LSpec (v3.0.0) addressing import-time execution overhead and parallel test scalability observed in large consumers such as Ix, backed by a zero-sorry Lean formalization of the test runner enforced in CI:

  1. Zero import-time overhead & lazy test execution

    • Replace Prop/Testable/Decidable assertions with Bool- and AssertM-based expectations (expect, shouldBe, shouldNotBe, shouldSatisfy, shouldBeLt/Le/Gt/Ge, shouldContain, shouldThrow, shouldReturn, expectSome/Ok/Err, pending/xit) with compile-time source locations via a meta-only pos% macro (zero runtime Lean compiler dependency).
    • Define AssertM as a nominal single-field structure (erased to IO (Except AssertStop α) at runtime) so do-notation cleanly lifts polymorphic IO actions such as IO.ofExcept and scoped resource getters.
    • Defer every leaf test (it, itBool, prop, propWith) under a Unit thunk inside SpecM (StateT (Array TestTree) BaseIO), declare DSL macros using non-reserved symbols (&"...") so downstream identifiers like prop/it are never shadowed, and provide lspec_suite (setting compiler.extract_closed false) so Lean never evaluates or hoists test bodies during module initialization, --list, or filtering.
    • Drop #lspec, SlimCheck, and the plausible dependency so LSpec has zero external dependencies.
  2. Bounded-thread parallel execution with controlled memory usage

    • Introduce a nest-core-inspired TestTree IR, IsTest typeclass, pluggable TestProcessor pipeline (Processors.list, Processors.console), and Hspec-style SpecM DSL (describe, context, it, prop, parallel, sequential, withTag/withTags, withOptions, withResource/beforeAll, beforeEach/afterEach/aroundEach).
    • Compile filtered trees into a shared pure ExecPlan IR consumed by both sequential and parallel execution paths, using ExecutionMode.combine so nested parallel scopes inside sequential remain strictly sequential.
    • Implement a fixed-size WorkerPool (Std.Mutex + Std.Condvar + Std.Queue) parameterized by RunConfig.threads / CLI -j N / --threads N so CI jobs can bound concurrent memory usage while maximizing wall-clock speed.
    • Drop completed test closures immediately upon completion, isolate per-test stdout/stderr via IO.FS.withIsolatedStreams, catch uncaught IO.Error per test, skip acquiring filtered-out resources, and avoid acquiring withResource after --fail-fast triggers.
  3. Deterministic, thread-safe Bool property testing

    • Add pure SplitMix64 Gen/Rng, fuel-bounded Shrinkable, Sampleable, FormatValue, arbitrary-arity PropTestable, precondition guards (==>), and forAll/forAllShrink/forAllNamed.
    • Derive deterministic per-test seeds from (masterSeed, testPath) and always report seeds so failures are 100% reproducible across sequential and parallel runs.
  4. Formally verified runner (LSpecProofs, zero sorry, zero consumer overhead)

    • Convert all pure runner functions in LSpec.Runner (TestFilter.matchesTest, filterTree, listTests, ReportTree.leaves/leafCount/failures/summarize, compilePlan, computeWorkerCount) from partial to total with kernel-checked termination proofs.
    • Provide an opt-in LSpecProofs library (excluded from defaultTargets so downstream consumers incur zero build or runtime overhead) proving:
      • Filtering & resource pruning (LSpecProofs.Filter): direct proofs on the executable runner that filtering is sound and complete with respect to listTests, idempotent, obeys exclusion precedence, and prunes empty scopes so filtered-out withResource blocks never survive to execute.
      • Reporting & deterministic planning (LSpecProofs.Report): direct proofs of exact pass/fail/skip accounting (total = leafCount, failed = failures.size, allPassed ↔ failures.isEmpty), permutation invariance of summary counts across arbitrary parallel completion orders, compilePlan equivalence with listTests, mode-independent deterministic per-test seed assignment, fail-fast skip accounting, and worker pool size bounds. - Parallel scheduler & resource safety (LSpecProofs.Concurrency): small-step operational semantics of the bounded WorkerPool and ExecPlan scheduler proving bounded concurrency (activeWorkers ≤ cap on every reachable trace), sequential exclusion and strict left-to-right ordering, withResource acquire/release bracket safety (no child executes before acquire; no release before all children finish; exact-once release on completion), deadlock freedom (including arbitrarily nested resources at cap = 1) and termination with an exact step bound, and sequential–parallel confluence (every parallel execution produces the identical ReportTree and RunSummary as sequential execution). - Compile-time axiom gate (LSpecProofs.Axioms): #guard_msgs in #print axioms checks on all 31 headline theorems compiled with -DwarningAsError=true, ensuring only Lean's standard axioms (propext, Quot.sound, Classical.choice) are used—never sorryAx or custom axioms.
  5. CI, tests, examples, and documentation

    • Remove all legacy v2 modules (LSpec/LSpec.lean, LSpec/Instances.lean, LSpec/Plausible.lean, LSpec/SlimCheck/, Examples/Testing.lean).
    • Place internal self-tests under LSpecTests/ (LSpecTests/Main.lean, LSpecTests/ImportCheck.lean) so LSpec's build directory never collides with downstream consumers' Tests.* modules on LEAN_PATH, and update Examples/Examples.lean and README.md.
    • Enforce LSpecProofs in both CI jobs (.github/workflows/ci.yml Lean job via lake build LSpecProofs --wfail and Nix job via checks.proofs in flake.nix).
    • Verified with lake build --wfail, lake build LSpecProofs --wfail, lake test (35/35), lake exe tests -j 4 (35/35), Examples/Examples.lean, nix flake check (checks.tests & checks.proofs), and end-to-end migration of Ix (55/55 primary suites passing; --list 18.89s -> 6.93s; full suite 380.82s -> 157.43s at -j 4, 116.61s at -j 8, 101.22s at -j 16).

…rified test framework

Full breaking redesign of LSpec (v3.0.0) addressing import-time execution overhead and parallel test scalability observed in large consumers such as Ix, backed by a zero-sorry Lean formalization of the test runner enforced in CI:

1. **Zero import-time overhead & lazy test execution**
   - Replace Prop/Testable/Decidable assertions with Bool- and AssertM-based expectations (expect, shouldBe, shouldNotBe, shouldSatisfy, shouldBeLt/Le/Gt/Ge, shouldContain, shouldThrow, shouldReturn, expectSome/Ok/Err, pending/xit) with compile-time source locations via a meta-only pos% macro (zero runtime Lean compiler dependency).
   - Define AssertM as a nominal single-field structure (erased to IO (Except AssertStop α) at runtime) so do-notation cleanly lifts polymorphic IO actions such as IO.ofExcept and scoped resource getters.
   - Defer every leaf test (it, itBool, prop, propWith) under a Unit thunk inside SpecM (StateT (Array TestTree) BaseIO) and declare DSL macros using non-reserved symbols (&"...") so downstream identifiers like prop/it are never shadowed; regular `def mySuite : Spec := do ...` functions work for both top-level and parameterized suites with zero import-time test execution (and test libraries with heavy closed pure expressions can set `-Dcompiler.extract_closed=false` to prevent closed-term hoisting).
   - Drop #lspec, SlimCheck, and the plausible dependency so LSpec has zero external dependencies.

2. **Bounded-thread parallel execution with controlled memory usage**
   - Introduce a nest-core-inspired TestTree IR, IsTest typeclass, pluggable TestProcessor pipeline (Processors.list, Processors.console), and Hspec-style SpecM DSL (describe, context, it, prop, parallel, sequential, withTag/withTags, withOptions, withResource/beforeAll, beforeEach/afterEach/aroundEach).
   - Compile filtered trees into a shared pure ExecPlan IR consumed by both sequential and parallel execution paths, using ExecutionMode.combine so nested parallel scopes inside sequential remain strictly sequential.
   - Implement a fixed-size WorkerPool (Std.Mutex + Std.Condvar + Std.Queue) parameterized by RunConfig.threads / CLI -j N / --threads N so CI jobs can bound concurrent memory usage while maximizing wall-clock speed.
   - Drop completed test closures immediately upon completion, isolate per-test stdout/stderr via IO.FS.withIsolatedStreams, catch uncaught IO.Error per test, skip acquiring filtered-out resources, and avoid acquiring withResource after --fail-fast triggers.

3. **Deterministic, thread-safe Bool property testing**
   - Add pure SplitMix64 Gen/Rng, fuel-bounded Shrinkable, Sampleable, FormatValue, arbitrary-arity PropTestable, precondition guards (==>), and forAll/forAllShrink/forAllNamed.
   - Derive deterministic per-test seeds from (masterSeed, testPath) and always report seeds so failures are 100% reproducible across sequential and parallel runs.

4. **Formally verified runner (`LSpecProofs`, zero `sorry`, zero consumer overhead)**
   - Convert all pure runner functions in LSpec.Runner (TestFilter.matchesTest, filterTree, listTests, ReportTree.leaves/leafCount/failures/summarize, compilePlan, computeWorkerCount) from partial to total with kernel-checked termination proofs.
   - Provide an opt-in LSpecProofs library (excluded from defaultTargets so downstream consumers incur zero build or runtime overhead) proving:
     - **Filtering & resource pruning (`LSpecProofs.Filter`)**: direct proofs on the executable runner that filtering is sound and complete with respect to listTests, idempotent, obeys exclusion precedence, and prunes empty scopes so filtered-out withResource blocks never survive to execute.
     - **Reporting & deterministic planning (`LSpecProofs.Report`)**: direct proofs of exact pass/fail/skip accounting (total = leafCount, failed = failures.size, allPassed ↔ failures.isEmpty), permutation invariance of summary counts across arbitrary parallel completion orders, compilePlan equivalence with listTests, mode-independent deterministic per-test seed assignment, fail-fast skip accounting, and worker pool size bounds.
     - **Parallel scheduler & resource safety (`LSpecProofs.Concurrency`)**: small-step operational semantics of the bounded WorkerPool and ExecPlan scheduler proving bounded concurrency (activeWorkers ≤ cap on every reachable trace), sequential exclusion and strict left-to-right ordering, withResource acquire/release bracket safety (no child executes before acquire; no release before all children finish; exact-once release on completion), deadlock freedom (including arbitrarily nested resources at cap = 1) and termination with an exact step bound, and sequential–parallel confluence (every parallel execution produces the identical ReportTree and RunSummary as sequential execution).
     - **Compile-time axiom gate (`LSpecProofs.Axioms`)**: #guard_msgs in #print axioms checks on all 31 headline theorems compiled with -DwarningAsError=true, ensuring only Lean's standard axioms (propext, Quot.sound, Classical.choice) are used—never sorryAx or custom axioms.

5. **CI, tests, examples, and documentation**
   - Remove all legacy v2 modules (LSpec/LSpec.lean, LSpec/Instances.lean, LSpec/Plausible.lean, LSpec/SlimCheck/, Examples/Testing.lean).
   - Place internal self-tests under LSpecTests/ (LSpecTests/Main.lean, LSpecTests/ImportCheck.lean) so LSpec's build directory never collides with downstream consumers' Tests.* modules on LEAN_PATH, and update Examples/Examples.lean and README.md.
   - Enforce LSpecProofs in both CI jobs (.github/workflows/ci.yml Lean job via lake build LSpecProofs --wfail and Nix job via checks.proofs in flake.nix).
   - Verified with lake build --wfail, lake build LSpecProofs --wfail, lake test (35/35), lake exe tests -j 4 (35/35), Examples/Examples.lean, nix flake check (checks.tests & checks.proofs), and end-to-end migration of Ix (55/55 primary suites passing; --list 18.89s -> 6.93s; full suite 380.82s -> 157.43s at -j 4, 116.30s at -j 8, 101.22s at -j 16).
@johnchandlerburnham
johnchandlerburnham merged commit b12338b into main Nov 30, 2026
3 checks passed
@johnchandlerburnham
johnchandlerburnham deleted the v3-redesign branch November 30, 2026 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants