Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions .claude/CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<libhegel.version>` release publishes — no runtime download. The fetch is **strict** —
shared objects the pinned `<libhegel.version>` release (tagged `libhegel-v<version>` in hegel-rust;
the plain `v<version>` 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
Expand Down Expand Up @@ -104,8 +105,10 @@ public `Generator`/`TestCase`/`Generators`/`Hegel`/`Stateful` surface stays in `
output callback (`hegel_output_callback_t`) to a `Consumer<String>` — 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.<kind>` 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
Expand All @@ -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.
Expand All @@ -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
Expand Down
43 changes: 43 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -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.<kind>` 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.
3 changes: 3 additions & 0 deletions hegel-jna/src/main/java/dev/hegel/JnaLibhegel.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ int hegel_new_state_machine(
long invariantNamesLen,
long minConcurrency,
long maxConcurrency,
long stepCount,
LongByReference outStateMachine,
LongByReference outConcurrency);

Expand Down Expand Up @@ -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));
Expand All @@ -831,6 +833,7 @@ public int newStateMachine(
invariantNames.size(),
minConcurrency,
maxConcurrency,
stepCount,
id,
concurrency);
if (code == Abi.OK) {
Expand Down
11 changes: 10 additions & 1 deletion hegel-jna/src/test/java/dev/hegel/JnaLibhegelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
32 changes: 7 additions & 25 deletions hegel-lowlevel/src/main/java/dev/hegel/lowlevel/Abi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -237,6 +238,7 @@ int newStateMachine(
boolean[] invariantAlwaysCheck,
long minConcurrency,
long maxConcurrency,
long stepCount,
long[] outId,
long[] outConcurrency);

Expand Down
4 changes: 3 additions & 1 deletion hegel/src/main/java/dev/hegel/RealLibhegel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1006,6 +1006,7 @@ public int newStateMachine(
boolean[] invariantAlwaysCheck,
long minConcurrency,
long maxConcurrency,
long stepCount,
long[] outId,
long[] outConcurrency) {
try (Arena arena = Arena.ofConfined()) {
Expand All @@ -1032,6 +1033,7 @@ public int newStateMachine(
(long) invariantNames.size(),
minConcurrency,
maxConcurrency,
stepCount,
id,
concurrency);
if (code == Abi.OK) {
Expand Down
11 changes: 10 additions & 1 deletion hegel/src/test/java/dev/hegel/RealLibhegelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<!-- The libhegel/hegel-rust release whose native libraries are bundled into the jars.
Bump this and rebuild to ship a new engine; scripts/fetch_natives.py fetches the
matching shared objects at build time. -->
<libhegel.version>0.37.6</libhegel.version>
<libhegel.version>0.42.4</libhegel.version>
<!-- Set -Dhegel.natives.skip=true to skip the build-time native fetch (e.g. offline
local dev against a sibling hegel-rust checkout). -->
<hegel.natives.skip>false</hegel.natives.skip>
Expand Down
18 changes: 14 additions & 4 deletions scripts/fetch_natives.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<version>`` (the plain ``v<version>``
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<version>``."""
api = f"https://api.github.com/repos/{repo}/releases/tags/v{version}"
"""Return ``{asset_name: download_url}`` for the release tagged ``libhegel-v<version>``."""
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", [])}

Expand All @@ -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()):
Expand Down Expand Up @@ -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
Expand Down
16 changes: 9 additions & 7 deletions shared/src/main/java/dev/hegel/Backend.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
/**
* The source of randomness the engine draws from.
*
* <p>Mirrors Hypothesis's {@code backend} setting. The default, {@link #AUTO}, selects {@link
* #URANDOM} automatically when running inside <a href="https://antithesis.com/">Antithesis</a> and
* {@link #DEFAULT} otherwise; an explicit choice always wins over the automatic one.
* <p>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 <a href="https://antithesis.com/">Antithesis</a>, 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.
Expand All @@ -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;
}
}
3 changes: 2 additions & 1 deletion shared/src/main/java/dev/hegel/DataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ StringGeneratorHandle textGenerator(

// Stateful testing. Machines are driven sequentially (one worker, one concurrency group).

long newStateMachine(List<String> ruleNames, List<String> invariantNames, boolean[] invariantAlwaysCheck);
long newStateMachine(
List<String> ruleNames, List<String> 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);
Expand Down
4 changes: 2 additions & 2 deletions shared/src/main/java/dev/hegel/HegelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Loading