From 4caffa4c1865ebdfded6013ce5d48396458be5da Mon Sep 17 00:00:00 2001 From: Esau Rodriguez Date: Wed, 16 Sep 2026 14:01:06 +0100 Subject: [PATCH 1/4] bump libhegel --- shared/src/test/java/dev/hegel/LabelTest.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 shared/src/test/java/dev/hegel/LabelTest.java diff --git a/shared/src/test/java/dev/hegel/LabelTest.java b/shared/src/test/java/dev/hegel/LabelTest.java new file mode 100644 index 0000000..4614a4b --- /dev/null +++ b/shared/src/test/java/dev/hegel/LabelTest.java @@ -0,0 +1,48 @@ +package dev.hegel; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import org.junit.jupiter.api.Test; + +/** + * {@link Label#of} and {@link Label#combine} must agree with libhegel's {@code + * hegel_label_from_name} / {@code hegel_label_combine}: the 64-bit FNV-1a hash of the name's UTF-8 + * bytes, and FNV-1a over the little-endian bytes of each label in turn. The expected values were + * computed independently from that definition. + */ +class LabelTest { + private static final long LIST = 0x588C222984E69C23L; + private static final long INTEGER = -0x57664BBC5FB7D3A5L; + + @Test + void ofIsTheFnv1aHashOfTheName() { + assertEquals(LIST, Label.of("dev.hegel.list")); + assertEquals(INTEGER, Label.of("dev.hegel.integer")); + assertEquals(LIST, Label.LIST); + } + + @Test + void combineHashesTheLabelsInOrder() { + assertEquals(-0x6BA08FBF26A1271AL, Label.combine(LIST, INTEGER)); + assertEquals(-0x717EA0EE91B61276L, Label.combine(INTEGER, LIST)); + assertEquals(0x47D7283BA12CCBC7L, Label.combine(LIST)); + assertEquals(-0x340D631B7BDDDCDBL, Label.combine()); + // A single label does not combine to itself, so lists(x) never collides with x. + assertNotEquals(LIST, Label.combine(LIST)); + } + + @Test + void frontendConstantsAreDistinct() { + long[] all = { + Label.LIST, Label.LIST_ELEMENT, Label.SET, Label.SET_ELEMENT, Label.MAP, Label.MAP_ENTRY, + Label.TUPLE, Label.ONE_OF, Label.OPTIONAL, Label.FIXED_DICT, Label.FLAT_MAP, Label.FILTER, + Label.MAPPED, Label.SAMPLED_FROM, Label.ENUM_VARIANT, Label.STATEFUL_RULE, Label.COMPOSITE, + }; + for (int i = 0; i < all.length; i++) { + for (int j = i + 1; j < all.length; j++) { + assertNotEquals(all[i], all[j], i + " vs " + j); + } + } + } +} From 2f0c59ee38fc467dc07cc67dbb53c07d5654ca7b Mon Sep 17 00:00:00 2001 From: Esau Rodriguez Date: Wed, 16 Sep 2026 14:01:29 +0100 Subject: [PATCH 2/4] bump libhegel --- .claude/CLAUDE.md | 15 ++- .../src/main/java/dev/hegel/JnaLibhegel.java | 3 + .../test/java/dev/hegel/JnaLibhegelTest.java | 11 ++- .../src/main/java/dev/hegel/lowlevel/Abi.java | 32 ++----- .../java/dev/hegel/lowlevel/Libhegel.java | 4 +- .../src/main/java/dev/hegel/RealLibhegel.java | 4 +- .../test/java/dev/hegel/RealLibhegelTest.java | 11 ++- pom.xml | 2 +- scripts/fetch_natives.py | 18 +++- shared/src/main/java/dev/hegel/Backend.java | 16 ++-- .../src/main/java/dev/hegel/DataSource.java | 3 +- shared/src/main/java/dev/hegel/HegelTest.java | 4 +- shared/src/main/java/dev/hegel/Label.java | 91 +++++++++++++------ .../main/java/dev/hegel/LiveDataSource.java | 6 +- shared/src/main/java/dev/hegel/Runner.java | 4 +- shared/src/main/java/dev/hegel/Settings.java | 4 +- shared/src/main/java/dev/hegel/Stateful.java | 38 +++++++- shared/src/main/java/dev/hegel/TestCase.java | 5 +- .../hegel/generators/FilteredGenerator.java | 4 +- .../hegel/generators/FlatMappedGenerator.java | 4 +- .../hegel/generators/IpAddressGenerator.java | 4 +- .../dev/hegel/generators/ListGenerator.java | 4 +- .../dev/hegel/generators/MapGenerator.java | 4 +- .../dev/hegel/generators/MappedGenerator.java | 4 +- .../dev/hegel/generators/OneOfGenerator.java | 4 +- .../dev/hegel/generators/RecordGenerator.java | 4 +- .../dev/hegel/generators/SetGenerator.java | 4 +- .../dev/hegel/generators/TupleGenerator.java | 4 +- .../java/dev/hegel/BindingErrorPathsTest.java | 12 +-- .../src/test/java/dev/hegel/CoverageTest.java | 16 ++++ .../src/test/java/dev/hegel/FakeLibhegel.java | 3 + .../src/test/java/dev/hegel/ReporterTest.java | 33 ++++--- .../src/test/java/dev/hegel/RunnerTest.java | 10 ++ 33 files changed, 253 insertions(+), 132 deletions(-) diff --git a/.claude/CLAUDE.md b/.claude/CLAUDE.md index f427d07..63c370c 100644 --- a/.claude/CLAUDE.md +++ b/.claude/CLAUDE.md @@ -71,7 +71,8 @@ native bundled in the jar for the host OS/arch (unpacked to a per-user cache; th best-effort — if it cannot be read or written, e.g. under a sandbox that denies writes to the user cache dir, the native is extracted to a fresh directory under the system temp dir instead). The bundled libraries are fetched at build time by `scripts/fetch_natives.py` (wired into Maven's `generate-resources` phase), which discovers whatever -shared objects the pinned `` release publishes — no runtime download. The fetch is **strict** — +shared objects the pinned `` release (tagged `libhegel-v` in hegel-rust; +the plain `v` tags belong to the `hegeltest` crate) publishes — no runtime download. The fetch is **strict** — because the bundled native is the only way end users get the engine (no runtime-download fallback), it fails the build rather than silently producing a jar without natives. For local engine work, build a `libhegel` and point `$HEGEL_LIBHEGEL_PATH` at it (`just build-libhegel` builds @@ -104,8 +105,10 @@ public `Generator`/`TestCase`/`Generators`/`Hegel`/`Stateful` surface stays in ` output callback (`hegel_output_callback_t`) to a `Consumer` — an FFM upcall stub whose arena lives until `runFree`, or a JNA `Callback` strongly referenced until `runFree`. In the JNA binding, C `bool` crosses as `byte` (JNA's default boolean mapping is a 32-bit int) and - `size_t` as `long` (the bundled natives are all 64-bit). `Abi` (lowlevel) holds the C constants; `Label` (shared) holds the frontend's span labels and - the FNV-1a hash for minting new ones. + `size_t` as `long` (the bundled natives are all 64-bit). `Abi` (lowlevel) holds the C constants; `Label` (shared) holds the frontend's span labels — + opaque `u64`s derived from `dev.hegel.` names with the same FNV-1a hash as the engine's + `hegel_label_from_name` (`Label.of`) and `hegel_label_combine` (`Label.combine`); the engine has + no predefined label enum since 0.39. - **Per-case primitives** — `DataSource` is the abstraction generators draw against; `LiveDataSource` wraps the engine, translating return codes (`StopTest` → OVERRUN, `AssumeRejected` → INVALID, `INVALID_ARG` → `IllegalArgumentException` with the engine's @@ -124,7 +127,7 @@ public `Generator`/`TestCase`/`Generators`/`Hegel`/`Stateful` surface stays in ` failure as-is (checked exceptions included), aggregates several into an `AssertionError`, and maps ERROR to `HealthCheckFailure`/`HegelException`. `HegelException` (binding/engine errors) is always thrown, never reported. `Settings` is the immutable config; its closed-state setting types live - alongside it — `Backend` (auto / default / urandom for Antithesis), `Database`, `OptBoolean` — + alongside it — `Backend` (auto = leave it to the engine's profile, which picks urandom inside Antithesis / default / urandom), `Database`, `OptBoolean` — plus `printBlob` (print a copy-pasteable reproducer per failure) and `reproduceFailure` (replay a stored blob instead of running the property). There is no single-test-case mode (the engine dropped it in 0.35); `testCases(1)` is the one-case configuration. @@ -144,7 +147,9 @@ public `Generator`/`TestCase`/`Generators`/`Hegel`/`Stateful` surface stays in ` the engine loop and invokes the user method per case). - **Stateful testing** — `Stateful.run(machine, tc)` reflects `@Rule`/`@Invariant` methods (sorted by name for determinism) and registers them via `hegel_new_state_machine` as a *sequential* - machine (every rule in group 0, concurrency fixed at `1, 1`, worker index 0). It then drives the + machine (every rule in group 0, concurrency fixed at `1, 1`, worker index 0, and a per-machine + `step_count` — `Stateful.DEFAULT_STEP_COUNT` = 50, or the `run(machine, tc, stepCount)` overload; + the engine has no default). It then drives the engine's round protocol: `hegel_state_machine_next_group` opens each round (or reports `HEGEL_STATE_MACHINE_DONE`, which is `INT64_MIN`), `hegel_state_machine_next_rule` hands out the round's rules until the join point, a rule that fails its own assumption is reported with diff --git a/hegel-jna/src/main/java/dev/hegel/JnaLibhegel.java b/hegel-jna/src/main/java/dev/hegel/JnaLibhegel.java index 265d79c..1a6cc57 100644 --- a/hegel-jna/src/main/java/dev/hegel/JnaLibhegel.java +++ b/hegel-jna/src/main/java/dev/hegel/JnaLibhegel.java @@ -196,6 +196,7 @@ int hegel_new_state_machine( long invariantNamesLen, long minConcurrency, long maxConcurrency, + long stepCount, LongByReference outStateMachine, LongByReference outConcurrency); @@ -808,6 +809,7 @@ public int newStateMachine( boolean[] invariantAlwaysCheck, long minConcurrency, long maxConcurrency, + long stepCount, long[] outId, long[] outConcurrency) { Memory groups = new Memory(8L * Math.max(ruleGroups.length, 1)); @@ -831,6 +833,7 @@ public int newStateMachine( invariantNames.size(), minConcurrency, maxConcurrency, + stepCount, id, concurrency); if (code == Abi.OK) { diff --git a/hegel-jna/src/test/java/dev/hegel/JnaLibhegelTest.java b/hegel-jna/src/test/java/dev/hegel/JnaLibhegelTest.java index 02271a8..ccb40b8 100644 --- a/hegel-jna/src/test/java/dev/hegel/JnaLibhegelTest.java +++ b/hegel-jna/src/test/java/dev/hegel/JnaLibhegelTest.java @@ -146,7 +146,16 @@ void stateMachineCallsReportNullHandle() { assertEquals( Abi.E_INVALID_HANDLE, lib.newStateMachine( - 0, List.of("r"), new long[] {0}, List.of("i"), new boolean[] {true}, 1, 1, out, concurrency)); + 0, + List.of("r"), + new long[] {0}, + List.of("i"), + new boolean[] {true}, + 1, + 1, + 50, + out, + concurrency)); assertEquals(7, out[0]); assertEquals(9, concurrency[0]); assertEquals(Abi.E_INVALID_HANDLE, lib.stateMachineNextGroup(0, 0, out)); diff --git a/hegel-lowlevel/src/main/java/dev/hegel/lowlevel/Abi.java b/hegel-lowlevel/src/main/java/dev/hegel/lowlevel/Abi.java index 3e04400..dc97813 100644 --- a/hegel-lowlevel/src/main/java/dev/hegel/lowlevel/Abi.java +++ b/hegel-lowlevel/src/main/java/dev/hegel/lowlevel/Abi.java @@ -2,8 +2,8 @@ /** * Constants from the libhegel C ABI ({@code hegel-c/include/hegel.h}): return codes, run and case - * statuses, phase and health-check bit masks, the engine's reserved span labels, and the sentinels - * the structured primitives use. Kept in sync with the engine header. + * statuses, phase and health-check bit masks, and the sentinels the structured primitives use. + * Kept in sync with the engine header. */ public final class Abi { private Abi() {} @@ -41,33 +41,15 @@ private Abi() {} public static final int HC_TEST_CASES_TOO_LARGE = 1 << 2; public static final int HC_LARGE_INITIAL_TEST_CASE = 1 << 3; - // Span labels reserved by the engine (hegel_label_t; argument to hegel_start_span). - public static final long LABEL_LIST = 1; - public static final long LABEL_LIST_ELEMENT = 2; - public static final long LABEL_SET = 3; - public static final long LABEL_SET_ELEMENT = 4; - public static final long LABEL_MAP = 5; - public static final long LABEL_MAP_ENTRY = 6; - public static final long LABEL_TUPLE = 7; - public static final long LABEL_ONE_OF = 8; - public static final long LABEL_OPTIONAL = 9; - public static final long LABEL_FIXED_DICT = 10; - public static final long LABEL_FLAT_MAP = 11; - public static final long LABEL_FILTER = 12; - public static final long LABEL_MAPPED = 13; - public static final long LABEL_SAMPLED_FROM = 14; - public static final long LABEL_ENUM_VARIANT = 15; - public static final long LABEL_STATEFUL_RULE = 31; - // The label space is open beyond the reserved values: any stable u64 works. - - // hegel_backend_t. - public static final int BACKEND_AUTO = 0; + // hegel_backend_t. There is no automatic value: leaving the backend unset lets the engine's + // settings profile choose (the shipped `workload` profile, selected inside Antithesis, uses + // urandom). public static final int BACKEND_DEFAULT = 1; public static final int BACKEND_URANDOM = 2; // hegel_verbosity_t. - public static final int VERBOSITY_QUIET = 0; - public static final int VERBOSITY_NORMAL = 1; + public static final int VERBOSITY_NORMAL = 0; + public static final int VERBOSITY_QUIET = 1; public static final int VERBOSITY_VERBOSE = 2; public static final int VERBOSITY_DEBUG = 3; diff --git a/hegel-lowlevel/src/main/java/dev/hegel/lowlevel/Libhegel.java b/hegel-lowlevel/src/main/java/dev/hegel/lowlevel/Libhegel.java index f3e52cf..89e1299 100644 --- a/hegel-lowlevel/src/main/java/dev/hegel/lowlevel/Libhegel.java +++ b/hegel-lowlevel/src/main/java/dev/hegel/lowlevel/Libhegel.java @@ -227,7 +227,8 @@ int stringGeneratorText( * value but {@link Abi#STATE_MACHINE_DONE}), {@code invariantAlwaysCheck} parallel to {@code * invariantNames}. The engine draws the concurrency level in {@code [minConcurrency, * maxConcurrency]} and writes it to {@code outConcurrency}; pass {@code 1, 1} for a sequential - * machine. + * machine. {@code stepCount} is the target number of counted rounds per test case (at least 1; + * the engine has no default, and 50 is the conventional choice). */ int newStateMachine( long tc, @@ -237,6 +238,7 @@ int newStateMachine( boolean[] invariantAlwaysCheck, long minConcurrency, long maxConcurrency, + long stepCount, long[] outId, long[] outConcurrency); diff --git a/hegel/src/main/java/dev/hegel/RealLibhegel.java b/hegel/src/main/java/dev/hegel/RealLibhegel.java index 586df3b..0be7315 100644 --- a/hegel/src/main/java/dev/hegel/RealLibhegel.java +++ b/hegel/src/main/java/dev/hegel/RealLibhegel.java @@ -370,7 +370,7 @@ private static MethodHandle findEmitLine() { "hegel_new_state_machine", FunctionDescriptor.of( JAVA_INT, ADDRESS, ADDRESS, ADDRESS, ADDRESS, JAVA_LONG, ADDRESS, ADDRESS, JAVA_LONG, JAVA_LONG, - JAVA_LONG, ADDRESS, ADDRESS)); + JAVA_LONG, JAVA_LONG, ADDRESS, ADDRESS)); this.stateMachineNextGroup = h( linker, lookup, @@ -1006,6 +1006,7 @@ public int newStateMachine( boolean[] invariantAlwaysCheck, long minConcurrency, long maxConcurrency, + long stepCount, long[] outId, long[] outConcurrency) { try (Arena arena = Arena.ofConfined()) { @@ -1032,6 +1033,7 @@ public int newStateMachine( (long) invariantNames.size(), minConcurrency, maxConcurrency, + stepCount, id, concurrency); if (code == Abi.OK) { diff --git a/hegel/src/test/java/dev/hegel/RealLibhegelTest.java b/hegel/src/test/java/dev/hegel/RealLibhegelTest.java index ee54ca8..74b31a9 100644 --- a/hegel/src/test/java/dev/hegel/RealLibhegelTest.java +++ b/hegel/src/test/java/dev/hegel/RealLibhegelTest.java @@ -142,7 +142,16 @@ void stateMachineCallsReportNullHandle() { assertEquals( Abi.E_INVALID_HANDLE, lib.newStateMachine( - 0, List.of("r"), new long[] {0}, List.of("i"), new boolean[] {true}, 1, 1, out, concurrency)); + 0, + List.of("r"), + new long[] {0}, + List.of("i"), + new boolean[] {true}, + 1, + 1, + 50, + out, + concurrency)); assertEquals(7, out[0]); assertEquals(9, concurrency[0]); assertEquals(Abi.E_INVALID_HANDLE, lib.stateMachineNextGroup(0, 0, out)); diff --git a/pom.xml b/pom.xml index 407a7e0..df6ab4f 100644 --- a/pom.xml +++ b/pom.xml @@ -56,7 +56,7 @@ - 0.37.6 + 0.42.4 false diff --git a/scripts/fetch_natives.py b/scripts/fetch_natives.py index c1a1d33..150d220 100644 --- a/scripts/fetch_natives.py +++ b/scripts/fetch_natives.py @@ -58,9 +58,19 @@ def http_get(url: str) -> bytes: return resp.read() +def release_tag(version: str) -> str: + """The git tag of the libhegel release for ``version``. + + Since libhegel 0.42.1 a release is tagged ``libhegel-v`` (the plain ``v`` + tags belong to the ``hegeltest`` crate, whose version differs). Earlier releases carry both + tags, so this one scheme resolves every version. + """ + return f"libhegel-v{version}" + + def discover_assets(repo: str, version: str) -> dict[str, str]: - """Return ``{asset_name: download_url}`` for the release tagged ``v``.""" - api = f"https://api.github.com/repos/{repo}/releases/tags/v{version}" + """Return ``{asset_name: download_url}`` for the release tagged ``libhegel-v``.""" + api = f"https://api.github.com/repos/{repo}/releases/tags/{release_tag(version)}" release = json.loads(http_get(api)) return {a["name"]: a["browser_download_url"] for a in release.get("assets", [])} @@ -77,7 +87,7 @@ def populate_cache(repo: str, version: str, cache: Path) -> None: assets = discover_assets(repo, version) libs = {name: url for name, url in assets.items() if ASSET_RE.match(name)} if not libs: - log(f"release v{version} of {repo} publishes no libhegel shared objects") + log(f"release {release_tag(version)} of {repo} publishes no libhegel shared objects") return cache.mkdir(parents=True, exist_ok=True) for name, url in sorted(libs.items()): @@ -133,7 +143,7 @@ def main() -> int: libs = cached_libs(cache) if not libs: - log(f"release v{args.version} of {args.repo} staged no libhegel shared objects") + log(f"release {release_tag(args.version)} of {args.repo} staged no libhegel shared objects") return 1 stage(libs, args.out) return 0 diff --git a/shared/src/main/java/dev/hegel/Backend.java b/shared/src/main/java/dev/hegel/Backend.java index 26619af..be9b594 100644 --- a/shared/src/main/java/dev/hegel/Backend.java +++ b/shared/src/main/java/dev/hegel/Backend.java @@ -5,13 +5,14 @@ /** * The source of randomness the engine draws from. * - *

Mirrors Hypothesis's {@code backend} setting. The default, {@link #AUTO}, selects {@link - * #URANDOM} automatically when running inside Antithesis and - * {@link #DEFAULT} otherwise; an explicit choice always wins over the automatic one. + *

Mirrors Hypothesis's {@code backend} setting. The default, {@link #AUTO}, leaves the choice to + * the engine's settings profile: the shipped {@code workload} profile, which the engine selects when + * running inside Antithesis, uses {@link #URANDOM}, and every + * other profile uses {@link #DEFAULT}. An explicit choice always wins over the profile's. */ public enum Backend { - /** Choose automatically: {@code URANDOM} under Antithesis, otherwise {@code DEFAULT}. */ - AUTO(Abi.BACKEND_AUTO), + /** Leave the choice to the engine's profile: {@code URANDOM} under Antithesis, else {@code DEFAULT}. */ + AUTO(null), /** * Expand a single seeded PRNG. Runs are reproducible from the seed and shrinking and replay * work as usual. @@ -24,9 +25,10 @@ public enum Backend { */ URANDOM(Abi.BACKEND_URANDOM); - final int code; + /** The {@code hegel_backend_t} value to send, or {@code null} to leave the profile's choice. */ + final Integer code; - Backend(int code) { + Backend(Integer code) { this.code = code; } } diff --git a/shared/src/main/java/dev/hegel/DataSource.java b/shared/src/main/java/dev/hegel/DataSource.java index fca5fe6..d5bbf0c 100644 --- a/shared/src/main/java/dev/hegel/DataSource.java +++ b/shared/src/main/java/dev/hegel/DataSource.java @@ -93,7 +93,8 @@ StringGeneratorHandle textGenerator( // Stateful testing. Machines are driven sequentially (one worker, one concurrency group). - long newStateMachine(List ruleNames, List invariantNames, boolean[] invariantAlwaysCheck); + long newStateMachine( + List ruleNames, List invariantNames, boolean[] invariantAlwaysCheck, int stepCount); /** Start the next round: its group id, or {@link Abi#STATE_MACHINE_DONE} when the machine is done. */ long stateMachineNextGroup(long stateMachineId); diff --git a/shared/src/main/java/dev/hegel/HegelTest.java b/shared/src/main/java/dev/hegel/HegelTest.java index 8fe301b..73fdbad 100644 --- a/shared/src/main/java/dev/hegel/HegelTest.java +++ b/shared/src/main/java/dev/hegel/HegelTest.java @@ -107,8 +107,8 @@ String database() default ""; /** - * The source of randomness. The default, {@link Backend#AUTO}, selects {@link Backend#URANDOM} - * automatically when running inside Antithesis and {@link Backend#DEFAULT} otherwise. + * The source of randomness. The default, {@link Backend#AUTO}, leaves the choice to the engine's + * settings profile: {@link Backend#URANDOM} inside Antithesis and {@link Backend#DEFAULT} otherwise. * * @return the randomness backend */ diff --git a/shared/src/main/java/dev/hegel/Label.java b/shared/src/main/java/dev/hegel/Label.java index 297aa22..4e82b24 100644 --- a/shared/src/main/java/dev/hegel/Label.java +++ b/shared/src/main/java/dev/hegel/Label.java @@ -1,84 +1,119 @@ package dev.hegel; -import dev.hegel.lowlevel.Abi; +import java.nio.charset.StandardCharsets; /** * Span labels for {@link TestCase#span(long, java.util.function.Supplier)} and {@link * TestCase#startSpan(long)}. * - *

A label tells the engine what kind of structure a span encloses, so shrinking can apply the - * right strategies to it. The constants here are the labels the engine reserves for its own - * structural notions; frontends building their own composite generators mint stable labels of their - * own with {@link #of(String)}. The label space is a full 64 bits and any value outside the reserved - * ones is fine, but a label must be the same on every run for the engine to recognise the - * structure. + *

A label identifies the generator that opened a span, and has no meaning beyond identity: the + * engine treats two spans with the same label as coming from the same generator — candidates for + * swapping, duplicating and reordering with each other when it shrinks and mutates test cases — and + * does nothing else with it. Any 64-bit value works as long as the same generator always uses the + * same one. Derive a label from a name with {@link #of(String)}, and give a generator built from + * other generators a label {@link #combine(long...) combined} from its own and its components', so + * that a list of integers and a list of strings get different labels while every list of integers + * gets the same one. Both match the engine's {@code hegel_label_from_name} and {@code + * hegel_label_combine}, so labels minted here agree with those of every other Hegel frontend. + * + *

The constants below are the labels Hegel's own composite generators open their spans with, + * derived from names of the form {@code dev.hegel.}. The engine labels the spans around its + * own draws from {@code hegel.} names; prefix custom names with your library's to keep clear + * of both. */ public final class Label { private Label() {} + private static final long FNV_OFFSET_BASIS = 0xcbf29ce484222325L; + private static final long FNV_PRIME = 0x100000001b3L; + /** A variable-length list. */ - public static final long LIST = Abi.LABEL_LIST; + public static final long LIST = of("dev.hegel.list"); /** One element of a list. */ - public static final long LIST_ELEMENT = Abi.LABEL_LIST_ELEMENT; + public static final long LIST_ELEMENT = of("dev.hegel.list_element"); /** A set. */ - public static final long SET = Abi.LABEL_SET; + public static final long SET = of("dev.hegel.set"); /** One element of a set. */ - public static final long SET_ELEMENT = Abi.LABEL_SET_ELEMENT; + public static final long SET_ELEMENT = of("dev.hegel.set_element"); /** A map. */ - public static final long MAP = Abi.LABEL_MAP; + public static final long MAP = of("dev.hegel.map"); /** One key/value entry of a map. */ - public static final long MAP_ENTRY = Abi.LABEL_MAP_ENTRY; + public static final long MAP_ENTRY = of("dev.hegel.map_entry"); /** A fixed-arity tuple. */ - public static final long TUPLE = Abi.LABEL_TUPLE; + public static final long TUPLE = of("dev.hegel.tuple"); /** A choice between alternative generators. */ - public static final long ONE_OF = Abi.LABEL_ONE_OF; + public static final long ONE_OF = of("dev.hegel.one_of"); /** An optional value. */ - public static final long OPTIONAL = Abi.LABEL_OPTIONAL; + public static final long OPTIONAL = of("dev.hegel.optional"); /** A record-like structure with a fixed set of named fields. */ - public static final long FIXED_DICT = Abi.LABEL_FIXED_DICT; + public static final long FIXED_DICT = of("dev.hegel.fixed_dict"); /** A draw whose generator depends on an earlier draw. */ - public static final long FLAT_MAP = Abi.LABEL_FLAT_MAP; + public static final long FLAT_MAP = of("dev.hegel.flat_map"); /** A draw filtered by a predicate. */ - public static final long FILTER = Abi.LABEL_FILTER; + public static final long FILTER = of("dev.hegel.filter"); /** A draw transformed by a function. */ - public static final long MAPPED = Abi.LABEL_MAPPED; + public static final long MAPPED = of("dev.hegel.mapped"); /** A value sampled from a fixed collection. */ - public static final long SAMPLED_FROM = Abi.LABEL_SAMPLED_FROM; + public static final long SAMPLED_FROM = of("dev.hegel.sampled_from"); /** One variant of an enum-like type. */ - public static final long ENUM_VARIANT = Abi.LABEL_ENUM_VARIANT; + public static final long ENUM_VARIANT = of("dev.hegel.enum_variant"); /** One rule invocation of a stateful test. */ - public static final long STATEFUL_RULE = Abi.LABEL_STATEFUL_RULE; + public static final long STATEFUL_RULE = of("dev.hegel.stateful_rule"); /** A value built imperatively from several draws ({@link Generators#composite}). */ public static final long COMPOSITE = of("dev.hegel.composite"); /** - * Mint a stable label from a name, hashing it with 64-bit FNV-1a. The same name always yields - * the same label, so a frontend can label its generators by fully-qualified name. + * Mint a stable label from a name: the 64-bit FNV-1a hash of its UTF-8 bytes. The same name + * always yields the same label, so a frontend can label its generators by fully-qualified name. * * @param name the label's name * @return the label */ public static long of(String name) { - long hash = 0xcbf29ce484222325L; - for (byte b : name.getBytes(java.nio.charset.StandardCharsets.UTF_8)) { + return fnv1a(FNV_OFFSET_BASIS, name.getBytes(StandardCharsets.UTF_8)); + } + + /** + * The label for a generator built from other generators: a hash of the given labels, in order. + * Pass the generator's own label (from {@link #of(String)}) first and its components' labels + * after it. Combining is order-sensitive, and combining a single label does not return it + * unchanged. + * + * @param labels the generator's own label followed by its components' + * @return the combined label + */ + public static long combine(long... labels) { + long hash = FNV_OFFSET_BASIS; + byte[] bytes = new byte[8]; + for (long label : labels) { + for (int i = 0; i < 8; i++) { + bytes[i] = (byte) (label >>> (8 * i)); + } + hash = fnv1a(hash, bytes); + } + return hash; + } + + private static long fnv1a(long hash, byte[] bytes) { + for (byte b : bytes) { hash ^= (b & 0xffL); - hash *= 0x100000001b3L; + hash *= FNV_PRIME; } return hash; } diff --git a/shared/src/main/java/dev/hegel/LiveDataSource.java b/shared/src/main/java/dev/hegel/LiveDataSource.java index a87dcc0..e6244ef 100644 --- a/shared/src/main/java/dev/hegel/LiveDataSource.java +++ b/shared/src/main/java/dev/hegel/LiveDataSource.java @@ -302,7 +302,8 @@ public long poolGenerate(long poolId, boolean consume) { } @Override - public long newStateMachine(List ruleNames, List invariantNames, boolean[] invariantAlwaysCheck) { + public long newStateMachine( + List ruleNames, List invariantNames, boolean[] invariantAlwaysCheck, int stepCount) { checkLive(); // Sequential: every rule in group 0 and exactly one worker, so the drawn concurrency level // is always 1 and every rule/rejection call below is made on behalf of worker 0. @@ -310,7 +311,8 @@ public long newStateMachine(List ruleNames, List invariantNames, long[] id = new long[1]; long[] concurrency = new long[1]; translate( - lib.newStateMachine(tc, ruleNames, groups, invariantNames, invariantAlwaysCheck, 1, 1, id, concurrency), + lib.newStateMachine( + tc, ruleNames, groups, invariantNames, invariantAlwaysCheck, 1, 1, stepCount, id, concurrency), "new_state_machine"); return id[0]; } diff --git a/shared/src/main/java/dev/hegel/Runner.java b/shared/src/main/java/dev/hegel/Runner.java index f26f6ee..c2a01bf 100644 --- a/shared/src/main/java/dev/hegel/Runner.java +++ b/shared/src/main/java/dev/hegel/Runner.java @@ -261,7 +261,9 @@ static void applySettings(Libhegel lib, long s, Settings st, Map } lib.settingsDerandomize(s, st.derandomize != null ? st.derandomize : ci); lib.settingsReportMultipleFailures(s, st.reportMultipleFailures); - lib.settingsBackend(s, st.backend.code); + if (st.backend.code != null) { + lib.settingsBackend(s, st.backend.code); + } if (st.suppressMask != 0) { lib.settingsSuppressHealthCheck(s, st.suppressMask); } diff --git a/shared/src/main/java/dev/hegel/Settings.java b/shared/src/main/java/dev/hegel/Settings.java index 975917c..c21f33a 100644 --- a/shared/src/main/java/dev/hegel/Settings.java +++ b/shared/src/main/java/dev/hegel/Settings.java @@ -185,8 +185,8 @@ public Settings verbosity(Verbosity verbosity) { } /** - * Select the source of randomness (default {@link Backend#AUTO}: {@link Backend#URANDOM} when - * running inside Antithesis, otherwise {@link Backend#DEFAULT}). + * Select the source of randomness (default {@link Backend#AUTO}: the engine's settings profile + * decides, {@link Backend#URANDOM} inside Antithesis and {@link Backend#DEFAULT} otherwise). * * @param backend the randomness backend * @return a new settings instance diff --git a/shared/src/main/java/dev/hegel/Stateful.java b/shared/src/main/java/dev/hegel/Stateful.java index 053cb92..cd7c4b6 100644 --- a/shared/src/main/java/dev/hegel/Stateful.java +++ b/shared/src/main/java/dev/hegel/Stateful.java @@ -47,7 +47,9 @@ * number of steps. A rule that fails an assumption is skipped without counting as a step. Invariants * are checked in full on the machine's initial and final state and sampled in between: after any * given rule each invariant runs with probability {@code 1 / stepCount}, so its expected cost per - * test case stays constant as the step count grows. Mark an invariant {@code @Invariant(alwaysRun = + * test case stays constant as the step count grows. The step count is the target number of rules + * per test case, {@link #DEFAULT_STEP_COUNT} unless {@link #run(Object, TestCase, int)} is given + * one. Mark an invariant {@code @Invariant(alwaysRun = * true)} to check it after every rule instead. Use a {@link Pool} to act on previously generated * values. */ @@ -55,8 +57,14 @@ public final class Stateful { private Stateful() {} /** - * Run {@code machine}'s rules and invariants under {@code tc} until the engine's step budget - * for this test case is exhausted. + * The step count {@link #run(Object, TestCase)} uses: the conventional choice across Hegel + * frontends. + */ + public static final int DEFAULT_STEP_COUNT = 50; + + /** + * Run {@code machine}'s rules and invariants under {@code tc} for up to {@link + * #DEFAULT_STEP_COUNT} steps. * *

The machine's {@code @Rule}/{@code @Invariant} methods are discovered reflectively from * its class (superclass methods are not considered) and ordered by name, so rule numbering is @@ -66,13 +74,33 @@ private Stateful() {} * @param tc the current test case */ public static void run(Object machine, TestCase tc) { + run(machine, tc, DEFAULT_STEP_COUNT); + } + + /** + * Run {@code machine}'s rules and invariants under {@code tc} for up to {@code stepCount} + * steps. + * + *

Every test case runs at least one step and at most {@code stepCount}; the engine decides + * when to stop within that budget. After each step a sampled invariant is checked with + * probability {@code 1 / stepCount}. + * + * @param machine the state machine to drive + * @param tc the current test case + * @param stepCount the target number of steps per test case, at least 1 + * @throws IllegalArgumentException if {@code stepCount} is less than 1 + */ + public static void run(Object machine, TestCase tc, int stepCount) { + if (stepCount < 1) { + throw new IllegalArgumentException("stepCount must be at least 1, got " + stepCount); + } List rules = annotated(machine, Rule.class); List invariants = annotated(machine, Invariant.class); if (rules.isEmpty()) { throw new IllegalArgumentException( machine.getClass().getName() + " has no @Rule methods; a state machine needs at least one"); } - long machineId = tc.newStateMachine(names(rules), names(invariants), alwaysRun(invariants)); + long machineId = tc.newStateMachine(names(rules), names(invariants), alwaysRun(invariants), stepCount); try { drive(machine, rules, invariants, tc, machineId); } finally { @@ -95,7 +123,7 @@ private static void drive( int step = 0; while (true) { - tc.startSpan(Abi.LABEL_STATEFUL_RULE); + tc.startSpan(Label.STATEFUL_RULE); if (tc.stateMachineNextGroup(machineId) == Abi.STATE_MACHINE_DONE) { tc.stopSpan(false); break; diff --git a/shared/src/main/java/dev/hegel/TestCase.java b/shared/src/main/java/dev/hegel/TestCase.java index 60b9401..da89e21 100644 --- a/shared/src/main/java/dev/hegel/TestCase.java +++ b/shared/src/main/java/dev/hegel/TestCase.java @@ -379,8 +379,9 @@ long poolGenerate(long poolId, boolean consume) { return source.poolGenerate(poolId, consume); } - long newStateMachine(List ruleNames, List invariantNames, boolean[] invariantAlwaysCheck) { - return source.newStateMachine(ruleNames, invariantNames, invariantAlwaysCheck); + long newStateMachine( + List ruleNames, List invariantNames, boolean[] invariantAlwaysCheck, int stepCount) { + return source.newStateMachine(ruleNames, invariantNames, invariantAlwaysCheck, stepCount); } long stateMachineNextGroup(long stateMachineId) { diff --git a/shared/src/main/java/dev/hegel/generators/FilteredGenerator.java b/shared/src/main/java/dev/hegel/generators/FilteredGenerator.java index 5ddfc8f..e89b158 100644 --- a/shared/src/main/java/dev/hegel/generators/FilteredGenerator.java +++ b/shared/src/main/java/dev/hegel/generators/FilteredGenerator.java @@ -2,8 +2,8 @@ import dev.hegel.AssumeRejected; import dev.hegel.Generator; +import dev.hegel.Label; import dev.hegel.TestCase; -import dev.hegel.lowlevel.Abi; import java.util.function.Predicate; /** @@ -28,7 +28,7 @@ public FilteredGenerator(Generator source, Predicate predicate) { @Override public T doDraw(TestCase tc) { for (int attempt = 0; attempt < FILTER_RETRIES; attempt++) { - tc.startSpan(Abi.LABEL_FILTER); + tc.startSpan(Label.FILTER); boolean discard = true; try { T value = source.doDraw(tc); diff --git a/shared/src/main/java/dev/hegel/generators/FlatMappedGenerator.java b/shared/src/main/java/dev/hegel/generators/FlatMappedGenerator.java index 7608676..df56a5c 100644 --- a/shared/src/main/java/dev/hegel/generators/FlatMappedGenerator.java +++ b/shared/src/main/java/dev/hegel/generators/FlatMappedGenerator.java @@ -1,8 +1,8 @@ package dev.hegel.generators; import dev.hegel.Generator; +import dev.hegel.Label; import dev.hegel.TestCase; -import dev.hegel.lowlevel.Abi; import java.util.function.Function; /** @@ -24,7 +24,7 @@ public FlatMappedGenerator(Generator source, Function next = f.apply(value); diff --git a/shared/src/main/java/dev/hegel/generators/IpAddressGenerator.java b/shared/src/main/java/dev/hegel/generators/IpAddressGenerator.java index 9e055fb..b65c1f9 100644 --- a/shared/src/main/java/dev/hegel/generators/IpAddressGenerator.java +++ b/shared/src/main/java/dev/hegel/generators/IpAddressGenerator.java @@ -1,8 +1,8 @@ package dev.hegel.generators; import dev.hegel.Generator; +import dev.hegel.Label; import dev.hegel.TestCase; -import dev.hegel.lowlevel.Abi; /** * Generates IP address strings. By default produces a mix of IPv4 and IPv6; restrict to one family @@ -35,7 +35,7 @@ public String doDraw(TestCase tc) { if (version != null) { return version == 4 ? formatV4(tc.generateIpv4()) : formatV6(tc.generateIpv6()); } - tc.startSpan(Abi.LABEL_ONE_OF); + tc.startSpan(Label.ONE_OF); try { return tc.generateInteger(0, 1) == 0 ? formatV4(tc.generateIpv4()) : formatV6(tc.generateIpv6()); } finally { diff --git a/shared/src/main/java/dev/hegel/generators/ListGenerator.java b/shared/src/main/java/dev/hegel/generators/ListGenerator.java index bdc3c37..7cea90b 100644 --- a/shared/src/main/java/dev/hegel/generators/ListGenerator.java +++ b/shared/src/main/java/dev/hegel/generators/ListGenerator.java @@ -1,8 +1,8 @@ package dev.hegel.generators; import dev.hegel.Generator; +import dev.hegel.Label; import dev.hegel.TestCase; -import dev.hegel.lowlevel.Abi; import java.util.ArrayList; import java.util.List; @@ -45,7 +45,7 @@ public ListGenerator maxSize(int maxSize) { /** @hidden */ @Override public List doDraw(TestCase tc) { - tc.startSpan(Abi.LABEL_LIST); + tc.startSpan(Label.LIST); try { long id = tc.newCollection(minSize, maxSize); List out = new ArrayList<>(); diff --git a/shared/src/main/java/dev/hegel/generators/MapGenerator.java b/shared/src/main/java/dev/hegel/generators/MapGenerator.java index b9cdd3e..ea8ae12 100644 --- a/shared/src/main/java/dev/hegel/generators/MapGenerator.java +++ b/shared/src/main/java/dev/hegel/generators/MapGenerator.java @@ -1,8 +1,8 @@ package dev.hegel.generators; import dev.hegel.Generator; +import dev.hegel.Label; import dev.hegel.TestCase; -import dev.hegel.lowlevel.Abi; import java.util.LinkedHashMap; import java.util.Map; @@ -46,7 +46,7 @@ public MapGenerator maxSize(int maxSize) { /** @hidden */ @Override public Map doDraw(TestCase tc) { - tc.startSpan(Abi.LABEL_MAP); + tc.startSpan(Label.MAP); try { long id = tc.newCollection(minSize, maxSize); Map out = new LinkedHashMap<>(); diff --git a/shared/src/main/java/dev/hegel/generators/MappedGenerator.java b/shared/src/main/java/dev/hegel/generators/MappedGenerator.java index 0ddf53c..eaba2fe 100644 --- a/shared/src/main/java/dev/hegel/generators/MappedGenerator.java +++ b/shared/src/main/java/dev/hegel/generators/MappedGenerator.java @@ -1,8 +1,8 @@ package dev.hegel.generators; import dev.hegel.Generator; +import dev.hegel.Label; import dev.hegel.TestCase; -import dev.hegel.lowlevel.Abi; import java.util.function.Function; /** @@ -23,7 +23,7 @@ public MappedGenerator(Generator source, Function f) @Override public U doDraw(TestCase tc) { - tc.startSpan(Abi.LABEL_MAPPED); + tc.startSpan(Label.MAPPED); try { return f.apply(source.doDraw(tc)); } finally { diff --git a/shared/src/main/java/dev/hegel/generators/OneOfGenerator.java b/shared/src/main/java/dev/hegel/generators/OneOfGenerator.java index 528833c..c67196d 100644 --- a/shared/src/main/java/dev/hegel/generators/OneOfGenerator.java +++ b/shared/src/main/java/dev/hegel/generators/OneOfGenerator.java @@ -1,8 +1,8 @@ package dev.hegel.generators; import dev.hegel.Generator; +import dev.hegel.Label; import dev.hegel.TestCase; -import dev.hegel.lowlevel.Abi; import java.util.List; /** @@ -22,7 +22,7 @@ public OneOfGenerator(List> options) { /** @hidden */ @Override public T doDraw(TestCase tc) { - tc.startSpan(Abi.LABEL_ONE_OF); + tc.startSpan(Label.ONE_OF); try { int index = (int) tc.generateInteger(0, options.size() - 1); return options.get(index).doDraw(tc); diff --git a/shared/src/main/java/dev/hegel/generators/RecordGenerator.java b/shared/src/main/java/dev/hegel/generators/RecordGenerator.java index 93f49dc..8bc0177 100644 --- a/shared/src/main/java/dev/hegel/generators/RecordGenerator.java +++ b/shared/src/main/java/dev/hegel/generators/RecordGenerator.java @@ -2,8 +2,8 @@ import dev.hegel.Generator; import dev.hegel.HegelException; +import dev.hegel.Label; import dev.hegel.TestCase; -import dev.hegel.lowlevel.Abi; import java.lang.reflect.RecordComponent; import java.util.HashMap; import java.util.Map; @@ -54,7 +54,7 @@ public T doDraw(TestCase tc) { RecordComponent[] components = type.getRecordComponents(); Object[] values = new Object[components.length]; Class[] paramTypes = new Class[components.length]; - tc.startSpan(Abi.LABEL_FIXED_DICT); + tc.startSpan(Label.FIXED_DICT); try { for (int i = 0; i < components.length; i++) { RecordComponent rc = components[i]; diff --git a/shared/src/main/java/dev/hegel/generators/SetGenerator.java b/shared/src/main/java/dev/hegel/generators/SetGenerator.java index ab0b58b..412f88d 100644 --- a/shared/src/main/java/dev/hegel/generators/SetGenerator.java +++ b/shared/src/main/java/dev/hegel/generators/SetGenerator.java @@ -1,8 +1,8 @@ package dev.hegel.generators; import dev.hegel.Generator; +import dev.hegel.Label; import dev.hegel.TestCase; -import dev.hegel.lowlevel.Abi; import java.util.LinkedHashSet; import java.util.Set; @@ -44,7 +44,7 @@ public SetGenerator maxSize(int maxSize) { /** @hidden */ @Override public Set doDraw(TestCase tc) { - tc.startSpan(Abi.LABEL_SET); + tc.startSpan(Label.SET); try { long id = tc.newCollection(minSize, maxSize); Set out = new LinkedHashSet<>(); diff --git a/shared/src/main/java/dev/hegel/generators/TupleGenerator.java b/shared/src/main/java/dev/hegel/generators/TupleGenerator.java index 75afb29..0c67b72 100644 --- a/shared/src/main/java/dev/hegel/generators/TupleGenerator.java +++ b/shared/src/main/java/dev/hegel/generators/TupleGenerator.java @@ -1,8 +1,8 @@ package dev.hegel.generators; import dev.hegel.Generator; +import dev.hegel.Label; import dev.hegel.TestCase; -import dev.hegel.lowlevel.Abi; import java.util.ArrayList; import java.util.List; import java.util.function.Function; @@ -26,7 +26,7 @@ public TupleGenerator(List> elements, Function, T> ass /** @hidden */ @Override public T doDraw(TestCase tc) { - tc.startSpan(Abi.LABEL_TUPLE); + tc.startSpan(Label.TUPLE); try { List out = new ArrayList<>(elements.size()); for (Generator g : elements) { diff --git a/shared/src/test/java/dev/hegel/BindingErrorPathsTest.java b/shared/src/test/java/dev/hegel/BindingErrorPathsTest.java index c90bc0b..52d2bdb 100644 --- a/shared/src/test/java/dev/hegel/BindingErrorPathsTest.java +++ b/shared/src/test/java/dev/hegel/BindingErrorPathsTest.java @@ -47,13 +47,13 @@ void okPathsReturnTheEngineValues() { assertEquals(7, ds.newCollection(0, 5)); assertFalse(ds.collectionMore(7)); ds.collectionReject(7, "dup"); - ds.startSpan(Abi.LABEL_LIST); + ds.startSpan(Label.LIST); ds.stopSpan(false); ds.target(1.0, "l"); assertEquals(3, ds.newPool()); assertEquals(0, ds.poolAdd(3)); assertEquals(0, ds.poolGenerate(3, true)); - assertEquals(5, ds.newStateMachine(List.of("r"), List.of("i"), new boolean[] {false})); + assertEquals(5, ds.newStateMachine(List.of("r"), List.of("i"), new boolean[] {false}, 50)); assertEquals(Abi.STATE_MACHINE_DONE, ds.stateMachineNextGroup(5)); assertEquals(Abi.STATE_MACHINE_DONE, ds.stateMachineNextRule(5)); ds.stateMachineRuleRejected(5); @@ -105,14 +105,14 @@ void stopTestUnwindsAndAbortsEverything() { assertThrows(StopTest.class, () -> ds.emailGenerator()); assertThrows(StopTest.class, () -> ds.urlGenerator()); assertThrows(StopTest.class, () -> ds.domainGenerator(10)); - assertThrows(StopTest.class, () -> ds.startSpan(Abi.LABEL_LIST)); + assertThrows(StopTest.class, () -> ds.startSpan(Label.LIST)); assertThrows(StopTest.class, () -> ds.newCollection(0, 1)); assertThrows(StopTest.class, () -> ds.collectionMore(1)); assertThrows(StopTest.class, () -> ds.collectionReject(1, "x")); assertThrows(StopTest.class, () -> ds.newPool()); assertThrows(StopTest.class, () -> ds.poolAdd(1)); assertThrows(StopTest.class, () -> ds.poolGenerate(1, false)); - assertThrows(StopTest.class, () -> ds.newStateMachine(List.of("r"), List.of(), new boolean[0])); + assertThrows(StopTest.class, () -> ds.newStateMachine(List.of("r"), List.of(), new boolean[0], 50)); assertThrows(StopTest.class, () -> ds.stateMachineNextGroup(1)); assertThrows(StopTest.class, () -> ds.stateMachineNextRule(1)); assertThrows(StopTest.class, () -> ds.stateMachineRuleRejected(1)); @@ -171,7 +171,7 @@ void backendErrorWithNullMessage() { void spanAndCollectionErrorsPropagate() { FakeLibhegel startSpan = new FakeLibhegel(); startSpan.startSpanRc = Abi.E_STOP_TEST; - assertThrows(StopTest.class, () -> source(startSpan).startSpan(Abi.LABEL_LIST)); + assertThrows(StopTest.class, () -> source(startSpan).startSpan(Label.LIST)); FakeLibhegel stopSpan = new FakeLibhegel(); stopSpan.stopSpanRc = Abi.E_INVALID_HANDLE; @@ -218,7 +218,7 @@ void poolAndStateMachineErrorsPropagate() { sm.newStateMachineRc = Abi.E_INVALID_ARG; assertThrows( IllegalArgumentException.class, - () -> source(sm).newStateMachine(List.of("r"), List.of(), new boolean[0])); + () -> source(sm).newStateMachine(List.of("r"), List.of(), new boolean[0], 50)); FakeLibhegel group = new FakeLibhegel(); group.stateMachineNextGroupRc = Abi.E_STOP_TEST; diff --git a/shared/src/test/java/dev/hegel/CoverageTest.java b/shared/src/test/java/dev/hegel/CoverageTest.java index f94dacd..8e1ebb5 100644 --- a/shared/src/test/java/dev/hegel/CoverageTest.java +++ b/shared/src/test/java/dev/hegel/CoverageTest.java @@ -248,6 +248,21 @@ void unsampled(TestCase tc) { } } + @Test + void statefulStepCountIsPassedToTheEngineAndValidated() { + FakeLibhegel fake = new FakeLibhegel(); + fake.ruleSequence = new long[] {0}; + Stateful.run(new TwoRuleMachine(), fakeTestCase(fake), 7); + assertEquals(7, fake.stateMachineStepCount); + + FakeLibhegel untouched = new FakeLibhegel(); + IllegalArgumentException e = assertThrows( + IllegalArgumentException.class, () -> Stateful.run(new TwoRuleMachine(), fakeTestCase(untouched), 0)); + assertTrue(e.getMessage().contains("stepCount"), e.getMessage()); + // Rejected before anything reached the engine. + assertEquals(-1, untouched.stateMachineStepCount); + } + @Test void statefulDriverFollowsTheEngineRuleSequence() { FakeLibhegel fake = new FakeLibhegel(); @@ -261,6 +276,7 @@ void statefulDriverFollowsTheEngineRuleSequence() { assertArrayEquals(new long[] {0, 0}, fake.stateMachineRuleGroups); assertEquals(1, fake.stateMachineMinConcurrency); assertEquals(1, fake.stateMachineMaxConcurrency); + assertEquals(Stateful.DEFAULT_STEP_COUNT, fake.stateMachineStepCount); // Invariants are ordered by name and carry their always-run flags. assertEquals(List.of("sampled", "unsampled"), fake.stateMachineInvariants); assertArrayEquals(new boolean[] {false, true}, fake.stateMachineAlwaysCheck); diff --git a/shared/src/test/java/dev/hegel/FakeLibhegel.java b/shared/src/test/java/dev/hegel/FakeLibhegel.java index f4b3766..4787e4f 100644 --- a/shared/src/test/java/dev/hegel/FakeLibhegel.java +++ b/shared/src/test/java/dev/hegel/FakeLibhegel.java @@ -129,6 +129,7 @@ final class FakeLibhegel implements Libhegel { boolean[] stateMachineAlwaysCheck; long stateMachineMinConcurrency = -1; long stateMachineMaxConcurrency = -1; + long stateMachineStepCount = -1; long stateMachineConcurrency = 1; // what new_state_machine writes to out_concurrency int stateMachineNextGroupRc = Abi.OK; long stateMachineGroupId = 0; @@ -483,6 +484,7 @@ public int newStateMachine( boolean[] invariantAlwaysCheck, long minConcurrency, long maxConcurrency, + long stepCount, long[] outId, long[] outConcurrency) { if (newStateMachineRc == Abi.OK) { @@ -492,6 +494,7 @@ public int newStateMachine( stateMachineAlwaysCheck = invariantAlwaysCheck; stateMachineMinConcurrency = minConcurrency; stateMachineMaxConcurrency = maxConcurrency; + stateMachineStepCount = stepCount; outId[0] = stateMachineId; outConcurrency[0] = stateMachineConcurrency; } diff --git a/shared/src/test/java/dev/hegel/ReporterTest.java b/shared/src/test/java/dev/hegel/ReporterTest.java index d2d59e2..b3d91f7 100644 --- a/shared/src/test/java/dev/hegel/ReporterTest.java +++ b/shared/src/test/java/dev/hegel/ReporterTest.java @@ -2,7 +2,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -import dev.hegel.lowlevel.Abi; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.nio.charset.StandardCharsets; @@ -67,22 +66,22 @@ void printingReporterHonoursVerbosity() { @Test void labelsMatchTheEngineConstants() { - assertEquals(Abi.LABEL_LIST, Label.LIST); - assertEquals(Abi.LABEL_LIST_ELEMENT, Label.LIST_ELEMENT); - assertEquals(Abi.LABEL_SET, Label.SET); - assertEquals(Abi.LABEL_SET_ELEMENT, Label.SET_ELEMENT); - assertEquals(Abi.LABEL_MAP, Label.MAP); - assertEquals(Abi.LABEL_MAP_ENTRY, Label.MAP_ENTRY); - assertEquals(Abi.LABEL_TUPLE, Label.TUPLE); - assertEquals(Abi.LABEL_ONE_OF, Label.ONE_OF); - assertEquals(Abi.LABEL_OPTIONAL, Label.OPTIONAL); - assertEquals(Abi.LABEL_FIXED_DICT, Label.FIXED_DICT); - assertEquals(Abi.LABEL_FLAT_MAP, Label.FLAT_MAP); - assertEquals(Abi.LABEL_FILTER, Label.FILTER); - assertEquals(Abi.LABEL_MAPPED, Label.MAPPED); - assertEquals(Abi.LABEL_SAMPLED_FROM, Label.SAMPLED_FROM); - assertEquals(Abi.LABEL_ENUM_VARIANT, Label.ENUM_VARIANT); - assertEquals(Abi.LABEL_STATEFUL_RULE, Label.STATEFUL_RULE); + assertEquals(Label.of("dev.hegel.list"), Label.LIST); + assertEquals(Label.of("dev.hegel.list_element"), Label.LIST_ELEMENT); + assertEquals(Label.of("dev.hegel.set"), Label.SET); + assertEquals(Label.of("dev.hegel.set_element"), Label.SET_ELEMENT); + assertEquals(Label.of("dev.hegel.map"), Label.MAP); + assertEquals(Label.of("dev.hegel.map_entry"), Label.MAP_ENTRY); + assertEquals(Label.of("dev.hegel.tuple"), Label.TUPLE); + assertEquals(Label.of("dev.hegel.one_of"), Label.ONE_OF); + assertEquals(Label.of("dev.hegel.optional"), Label.OPTIONAL); + assertEquals(Label.of("dev.hegel.fixed_dict"), Label.FIXED_DICT); + assertEquals(Label.of("dev.hegel.flat_map"), Label.FLAT_MAP); + assertEquals(Label.of("dev.hegel.filter"), Label.FILTER); + assertEquals(Label.of("dev.hegel.mapped"), Label.MAPPED); + assertEquals(Label.of("dev.hegel.sampled_from"), Label.SAMPLED_FROM); + assertEquals(Label.of("dev.hegel.enum_variant"), Label.ENUM_VARIANT); + assertEquals(Label.of("dev.hegel.stateful_rule"), Label.STATEFUL_RULE); // Minted labels are stable and match the hash the composite generator already uses. assertEquals(Label.COMPOSITE, Label.of("dev.hegel.composite")); assertEquals(Label.of("my.frontend.pair"), Label.of("my.frontend.pair")); diff --git a/shared/src/test/java/dev/hegel/RunnerTest.java b/shared/src/test/java/dev/hegel/RunnerTest.java index 78a6922..6b63082 100644 --- a/shared/src/test/java/dev/hegel/RunnerTest.java +++ b/shared/src/test/java/dev/hegel/RunnerTest.java @@ -331,6 +331,16 @@ void nondeterministicRunFailureIsAnInternalError() { assertTrue(fake.replayedBlobs.isEmpty()); } + @Test + void autoBackendLeavesTheChoiceToTheEngineProfile() { + FakeLibhegel fake = new FakeLibhegel(); + run(fake, new Settings().backend(Backend.AUTO), tc -> {}); + assertNull(fake.backendCode); + FakeLibhegel explicit = new FakeLibhegel(); + run(explicit, new Settings().backend(Backend.DEFAULT), tc -> {}); + assertEquals(Abi.BACKEND_DEFAULT, explicit.backendCode); + } + @Test void settingsBranchesAllApplied() { FakeLibhegel fake = new FakeLibhegel(); From 59f1fc0f8026bcf1d35225f962390708eaa1b570 Mon Sep 17 00:00:00 2001 From: Esau Rodriguez Date: Wed, 16 Sep 2026 15:45:05 +0100 Subject: [PATCH 3/4] Add release notes for the libhegel 0.42.4 bump --- RELEASE.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 RELEASE.md diff --git a/RELEASE.md b/RELEASE.md new file mode 100644 index 0000000..5a34532 --- /dev/null +++ b/RELEASE.md @@ -0,0 +1,38 @@ +RELEASE_TYPE: patch + +This patch upgrades the bundled libhegel engine from 0.37.6 to 0.42.4. The engine's shrinker now +reaches minimal examples in several situations where it previously stopped short (pairs of draws a +test pins together, `oneOf` alternatives that must switch to a shorter branch, failures that only +occur at multiples of a round number, bounded floats whose failing range excludes zero), a panic +during span reordering that could lose the shrunk counterexample is fixed, a memory leak in string +draws is fixed, and the limit on the number of choices a single test case may make rises from +8,192 to 1,048,576 (suppressing the `TEST_CASES_TOO_LARGE` health check removes it entirely). + +Settings defaults are now resolved by the engine from named profiles, so hegel-java reads the same +`hegel.toml` as every other Hegel frontend: a `hegel.toml` in the working directory or one of its +parents (or the file named by `HEGEL_CONFIG`) applies to every run, and `HEGEL_DEFAULT_PROFILE` +selects the profile in effect. Settings given explicitly in Java, whether through `Settings` or +`@HegelTest`, still take precedence. `Backend.AUTO` now means exactly that the engine's profile +chooses: the shipped `workload` profile, selected inside Antithesis, uses `URANDOM`, and every other +profile uses `DEFAULT`. + +Span labels no longer carry meaning beyond identity: the engine treats two spans with the same +label as coming from the same generator and does nothing else with them. The `Label` constants are +now derived from `dev.hegel.` names with the same FNV-1a hash as `Label.of`, and the new +`Label.combine(long...)` derives the label of a generator built from other generators from its own +label and its components', matching the engine's `hegel_label_combine`, so that a list of integers +and a list of strings get different labels while every list of integers gets the same one: + +```java +long myList = Label.of("mylib.list"); +long myListOfIntegers = Label.combine(myList, Label.of("mylib.integers")); +``` + +The stateful step count is now a per-machine parameter rather than an engine default. `Stateful.run` +keeps using 50 steps per test case (`Stateful.DEFAULT_STEP_COUNT`), and a new +`Stateful.run(machine, tc, stepCount)` overload sets a different budget; each sampled invariant runs +with probability `1 / stepCount` after a step. + +In `dev.hegel:hegel-lowlevel`, `Libhegel.newStateMachine` takes the step count after the +concurrency bounds, the `Abi.LABEL_*` and `Abi.BACKEND_AUTO` constants are gone with their engine +counterparts, and the `Abi.VERBOSITY_*` values follow the engine's renumbering (`NORMAL` is now 0). From 9420717be9485b697fd2f974b3a928167c094b3b Mon Sep 17 00:00:00 2001 From: Esau Rodriguez Date: Wed, 16 Sep 2026 15:45:05 +0100 Subject: [PATCH 4/4] Add release notes for the libhegel 0.42.4 bump --- RELEASE.md | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 RELEASE.md diff --git a/RELEASE.md b/RELEASE.md new file mode 100644 index 0000000..f590735 --- /dev/null +++ b/RELEASE.md @@ -0,0 +1,43 @@ +RELEASE_TYPE: minor + +This release upgrades the bundled libhegel engine from 0.37.6 to 0.42.4. The engine's shrinker now +reaches minimal examples in several situations where it previously stopped short (pairs of draws a +test pins together, `oneOf` alternatives that must switch to a shorter branch, failures that only +occur at multiples of a round number, bounded floats whose failing range excludes zero), a panic +during span reordering that could lose the shrunk counterexample is fixed, a memory leak in string +draws is fixed, and the limit on the number of choices a single test case may make rises from +8,192 to 1,048,576 (suppressing the `TEST_CASES_TOO_LARGE` health check removes it entirely). + +The only breaking changes are in `dev.hegel:hegel-lowlevel`, and only code that implements or calls +`Libhegel` directly is affected; users of `dev.hegel:hegel` and `dev.hegel:hegel-jna` need not +change anything. `Libhegel.newStateMachine` takes a new `stepCount` argument after the concurrency +bounds (the engine no longer has a default; 50 is the conventional choice), the `Abi.LABEL_*` and +`Abi.BACKEND_AUTO` constants are removed along with their engine counterparts, and +`Abi.VERBOSITY_NORMAL` and `Abi.VERBOSITY_QUIET` swap values (`NORMAL` is now 0). The verbosity +constants are inlined at compile time, so code built against 0.6.1 must be recompiled to send the +right value. + +Settings defaults are now resolved by the engine from named profiles, so hegel-java reads the same +`hegel.toml` as every other Hegel frontend: a `hegel.toml` in the working directory or one of its +parents (or the file named by `HEGEL_CONFIG`) applies to every run, and `HEGEL_DEFAULT_PROFILE` +selects the profile in effect. Settings given explicitly in Java, whether through `Settings` or +`@HegelTest`, still take precedence. `Backend.AUTO` now means exactly that the engine's profile +chooses: the shipped `workload` profile, selected inside Antithesis, uses `URANDOM`, and every other +profile uses `DEFAULT`. + +Span labels no longer carry meaning beyond identity: the engine treats two spans with the same +label as coming from the same generator and does nothing else with them. The `Label` constants are +now derived from `dev.hegel.` names with the same FNV-1a hash as `Label.of`, and the new +`Label.combine(long...)` derives the label of a generator built from other generators from its own +label and its components', matching the engine's `hegel_label_combine`, so that a list of integers +and a list of strings get different labels while every list of integers gets the same one: + +```java +long myList = Label.of("mylib.list"); +long myListOfIntegers = Label.combine(myList, Label.of("mylib.integers")); +``` + +The stateful step count is now a per-machine parameter rather than an engine default. `Stateful.run` +keeps using 50 steps per test case (`Stateful.DEFAULT_STEP_COUNT`), and a new +`Stateful.run(machine, tc, stepCount)` overload sets a different budget; each sampled invariant runs +with probability `1 / stepCount` after a step.