From 525c372fc52b188636db725aea3f7526db63205c Mon Sep 17 00:00:00 2001 From: piexel Date: Tue, 6 Oct 2026 11:37:24 +0200 Subject: [PATCH] Document WorldRandom, id ownership for feature ids and mantle keys, and accumulating ModuleHost DESIGN describes WorldRandom's algorithms per method, why it does not implement RandomGenerator, why StrictMath suffices for the gaussian and how the pinned values are produced. README and CONTRACTS name it as the generator stages and object generators see. The namespace rule now reads that feature ids and mantle keys are the module id or below it, with the new error text, and ModuleHost's methods are documented to accumulate. --- CONTRACTS.md | 18 +++++++----- DESIGN.md | 80 +++++++++++++++++++++++++++++++++++++--------------- README.md | 40 ++++++++++++++++++-------- 3 files changed, 96 insertions(+), 42 deletions(-) diff --git a/CONTRACTS.md b/CONTRACTS.md index a706ed52a..ebfe7092e 100644 --- a/CONTRACTS.md +++ b/CONTRACTS.md @@ -10,8 +10,9 @@ types that carry them and lists what the Iris adapter still owes. | Module identity and composition | `IrisModule`, `ModuleInfo` (with optional packages), `StageGraph` | | Loading and unloading a pack | host `LoadedPack`, `ModuleHost` | | Module jars in a pack | `META-INF/services/com.volmit.IrisModule`, host `PackModules` (jar copies), `DiscoveredModule`, `MissingClasses` | -| Namespace ownership | module-prefixed stage ids, `ModuleInfo.ownsStage`, `ModuleHost.reservingNamespaces` | -| Randomness independent of other modules | `StageContext.random()`, host `StageRandom`, invocation seed passed to the runners | +| Namespace ownership | module-prefixed stage, feature and mantle ids, `ModuleInfo.ownsStage`/`ownsId`, `ModuleHost.reservingNamespaces` | +| Randomness independent of other modules | `StageContext.random()`, host `StageStreams`, invocation seed passed to the runners | +| Random values identical on every JDK | `WorldRandom`, pinned by `WorldRandomTest` | | Module-owned work and ordering | `Stage`, `StageRef`, stage definitions, `requires`/`softAfter`/`softBefore` | | Activation by configuration | `when(settingsType)`, host `ConfiguredSettings` | | Typed results between modules | stage outputs, `StageContext.output` | @@ -30,11 +31,13 @@ types that carry them and lists what the Iris adapter still owes. Stage bodies hold no invocation state. Everything per placement or chunk travels through the context, outputs and accumulators. Each stage draws only from its own stream, derived from the -invocation seed and its id, so other modules never change its draws. +invocation seed and its id, so other modules never change its draws. Every value a stream produces is +defined by IrisApi (`WorldRandom`), not by the JDK, so a JDK update never changes a world. -A module owns its id (`ns:name`, no `/`) and the stage ids below it (`ns:name/...`). Feature ids and -mantle keys it registers use its namespace; a settings type in `when(...)` uses the namespace of a -module that names it. Pack modules cannot use the namespaces the host reserves. +A module owns its id (`ns:name`, no `/`) and the ids below it (`ns:name/...`). Its stage ids are +below it; feature ids and mantle keys it registers are its id or below it; a settings type in +`when(...)` is owned in that sense by a module that names it. Pack modules cannot use the +namespaces the host reserves. A pack load works on copies of the module jars, so the originals are never held open. @@ -70,7 +73,8 @@ running Iris is the pack publisher's responsibility. storage. Abort chunk publication when any stage fails. - Preserve unknown persisted mantle layers or fail explicitly. - Build one `ModuleHost` at startup with the built-in modules, exposed packages, reserved JSON - fields and `reservingNamespaces("iris")`. Load each pack with `LoadedPack.load(packRoot, host)` + fields and `reservingNamespaces("iris")`. Its methods accumulate, so the configuration can be + built in steps. Load each pack with `LoadedPack.load(packRoot, host)` and close it only after nothing built from it is in use. - Route `System.Logger` output (logger `com.volmit.host.PackModules`) to the server log, so the per-pack module summary and missing-classes warning are visible. diff --git a/DESIGN.md b/DESIGN.md index 1d7ff9049..2937574f4 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -40,7 +40,8 @@ name, version and optional packages; there is no separate descriptor. A pack kee The host loads a pack with `LoadedPack.load(packRoot, host)`. `ModuleHost` is the host's fixed configuration, built once and the same for every pack: built-in modules, exposed host packages, -reserved JSON fields and reserved namespaces. `LoadedPack` runs `PackModules.load` (below), then +reserved JSON fields and reserved namespaces. Each of its methods adds to what earlier calls +configured, and all names are checked when it is built. `LoadedPack` runs `PackModules.load` (below), then `LoadedModules.load` on the built-ins followed by the pack's modules. If the second step fails it closes the first before rethrowing, so a failed load leaves no class loader or jar copy behind. `close()` closes the pack's class loader, then deletes its jar copies. @@ -255,15 +256,17 @@ with its id and `/` (`iris:stilt/fill`), so no module can define stages under an path, and a stage id names exactly one possible owner. A `/` in a module id would let `iris:stilt/x` define stages under `iris:stilt`, so `ModuleInfo` rejects it. -Feature ids of geometry generators and mantle keys a module registers must use its namespace. -A settings type named in `when(...)` must have a feature id in the namespace of at least one module -that names it: its owner. Other modules may name it too, to run only where that setting is +Feature ids of geometry generators and mantle keys a module registers follow the same rule +(`ModuleInfo.ownsId`): they are the module id or start with it and `/`, such as `iris:snow` or +`iris:snow/depth`. Sharing the namespace is not enough, or `iris:snow` could claim `iris:stilt`'s +ids. A settings type named in `when(...)` must have a feature id that at least one module naming it +owns in this sense: its owner. Other modules may name it too, to run only where that setting is configured. Checking against the module that names it alone would forbid that; checking nothing -would let any module claim a settings id in another namespace. +would let any module claim another module's settings id. The host reserves namespaces for its own modules (`ModuleHost.reservingNamespaces("iris")`). A module from a pack jar whose id uses a reserved namespace fails the load, naming the jar and the -namespace. Its stages, settings ids and mantle keys follow its namespace, so this one check keeps +namespace. Its stage ids, settings ids and mantle keys start with its id, so this one check keeps pack modules out of reserved namespaces everywhere. Built-in modules, discovered with `DiscoveredModule.builtIn`, may use them. @@ -377,33 +380,64 @@ stream: `InvocationContext` has no `random()`, and the host passes a `long` invo shared stream, every draw of an earlier stage shifts what later stages get, so installing a module would change other modules' output for the same seed. -The stream depends only on the invocation seed and the stage id (`StageRandom`): +The stream depends only on the invocation seed and the stage id (`StageStreams`): 1. `idHash` = 64-bit FNV-1a of the UTF-8 bytes of `namespace:path`, computed once per pack load; 2. `streamSeed` = `mix(mix(invocationSeed) ^ idHash)`, `mix` being the SplitMix64 finalizer (Stafford variant 13); -3. the stream is `new java.util.Random(streamSeed)`, created when the stage first draws. +3. the stream is `new WorldRandom(streamSeed)`, created when the stage first draws. `mix` is a bijection, so a stage gets a different stream for every invocation seed, and in one invocation stages with different id hashes get different streams. The hash and the mixer are written out in IrisApi, not taken from `String.hashCode` or a JDK class whose output may change. -`java.util.Random` is the only JDK generator whose algorithm the Java SE specification fixes: -`nextInt()`, `nextInt(bound)`, `nextLong()`, `nextBoolean()`, `nextFloat()`, `nextDouble()`, -`nextGaussian()` and `nextBytes` give the same values on every JDK. `SplittableRandom` and the named -`RandomGeneratorFactory` algorithms such as `L64X128MixRandom` promise identical sequences only within -one program, so a JDK update could change worlds. The methods `Random` inherits from -`RandomGenerator`, such as `nextInt(origin, bound)` and `nextDouble(bound)`, and `nextExponential()`, -are not fixed either. `Random`'s 48-bit state and its synchronized updates are fine for world -generation, which Minecraft has done with it for years. A test pins the derivation to values -computed outside Java from this description. +A test pins the derivation to values computed outside Java from this description. + +### WorldRandom + +`com.volmit.random.WorldRandom` is the generator stages and object generators see. No JDK generator +fits: `SplittableRandom` and the `RandomGeneratorFactory` algorithms promise identical sequences only +within one program, and `java.util.Random` fixes only some methods. The ones it inherits from +`RandomGenerator`, such as `nextInt(origin, bound)`, `nextDouble(bound)` and `nextExponential()`, are +JDK defaults that a JDK update may change, and those are the ones module authors reach for. +`Random` is also a 48-bit LCG with an atomic update per draw, a cost in per-block stages. + +`WorldRandom` therefore does not implement `RandomGenerator`. Overriding every default would hold +only until a JDK adds another one; a type of its own exposes nothing IrisApi does not define. The +price is that it cannot be passed to JDK methods that take a `Random` or `RandomGenerator`, such as +`Collections.shuffle`. Its methods: + +| Method | Definition | +| --- | --- | +| `nextLong()` | SplitMix64 (Steele, Lea and Flood): `state += 0x9e3779b97f4a7c15`, return `mix(state)` | +| `nextInt()` | the high 32 bits of `nextLong()` | +| `nextBoolean()` | whether `nextLong()` is negative | +| `nextFloat()` | the high 24 bits of `nextLong()` times 2^-24, in `[0, 1)` | +| `nextDouble()` | the high 53 bits of `nextLong()` times 2^-53, in `[0, 1)` | +| `nextInt(bound)`, `nextInt(origin, bound)`, `nextLong(bound)`, `nextLong(origin, bound)` | Lemire's multiply-and-reject method, unbiased; ranges wider than `MAX_VALUE` work | +| `nextDouble(bound)`, `nextDouble(origin, bound)` | `origin + nextDouble() * (bound - origin)`, a result rounded up to `bound` replaced by the double below it; a width beyond `Double.MAX_VALUE` is halved first | +| `nextGaussian()` | Marsaglia's polar method on `StrictMath.log` and `StrictMath.sqrt`; the pair's second value is discarded | +| `chance(probability)` | `nextDouble() < probability` | + +The state is one `long`, initially the seed. Methods without rejection draw exactly one value; how +many values a method draws is part of the sequence. `StrictMath` is fixed bit for bit by the Java SE +specification (fdlibm), so the gaussian needs no math of its own. Invalid bounds (`bound <= 0`, +`origin >= bound`, NaN or infinite doubles, a probability outside `[0, 1]`) throw +`IllegalArgumentException`. The generator is not thread-safe: one stage uses its stream within one +invocation. + +`WorldRandomTest` pins every method to values computed by `src/test/reference/world_random.py`, an +independent implementation of this table with Python integers and IEEE doubles. Python's `math.log` +is the platform's, so the script carries a port of fdlibm's `log`. The build does not run it. The test +also fails if `WorldRandom` gains a public method without pinned values or implements +`RandomGenerator`. A block stage gets one stream per placement and draws from it across the blocks of its pass in block order. Fused block stages interleave per block, but each still draws only from its own stream. A stage that never draws never creates its stream. -Object generators are not stages. `ObjectGenerationContext.random()` stays, seeded from the variant -seed the host chooses. +Object generators are not stages. `ObjectGenerationContext.random()` stays, a `WorldRandom` seeded +from the variant seed the host chooses. ## Chunk invocations @@ -416,15 +450,15 @@ the host from publishing the chunk. ## Settings, geometry and storage Settings types implement `IrisFeature` and carry `@Feature` (namespaced id, display name, JSON -field, attachment targets). `when(type)` validates the annotation while the module installs. The id's -namespace must be that of a module naming the type (see Namespaces). +field, attachment targets). `when(type)` validates the annotation while the module installs. The id +must be owned by a module naming the type (see Namespaces). `LoadedModules.load` catalogs those types with geometry generator settings and rejects duplicate ids and JSON field collisions per attachment target, including reserved host fields. `OptionsMetadata` reads field metadata; it does not decode JSON. `GeneratedObject` is sparse, immutable local geometry with bounds, an anchor, sorted blocks, -optional block entity payloads and markers. `ObjectGenerator` produces it from settings and an -RNG. `ObjectTransformer` is host-owned. +optional block entity payloads and markers. `ObjectGenerator` produces it from settings and a +`WorldRandom`. `ObjectTransformer` is host-owned. The mantle contracts are unchanged: namespaced typed keys, versioned codecs, generation and persistent lifetimes, ordered sparse visits and buffered edits. Only owners register a layer. diff --git a/README.md b/README.md index 69f005279..5172b2ffb 100644 --- a/README.md +++ b/README.md @@ -44,11 +44,23 @@ public final class MossModule implements IrisModule { - `ctx.random()` is the stage's own random stream, derived from the invocation seed and the stage id. Installing or removing other modules, or how much they draw, never changes what a stage draws. A block stage has one stream per placement, drawn across the blocks of its pass. + The stream is a `WorldRandom` (see Random streams below). `IrisModule.register(FeatureRegistry)` stays for non-stage contributions: geometry generators -and mantle layers. Their feature ids and mantle keys use the module's namespace. A settings type -named in `when(...)` needs a feature id in the namespace of a module that names it, so another -module may still condition its stages on it. +and mantle layers. Their feature ids and mantle keys are the module id or start with it and `/`, +like stage ids: `iris:snow` or `iris:snow/depth` for module `iris:snow`. A settings type named in +`when(...)` needs a feature id that at least one module naming it owns in that sense, so another +module may still condition its stages on it, as moss does on `iris:snow`. + +## Random streams + +Stages and object generators draw from `com.volmit.random.WorldRandom`, an IrisApi type whose every +method is defined and pinned by tests, so the same seed gives the same world on every JDK. It is +SplitMix64 with `nextInt`, `nextLong` and `nextDouble` in plain, bounded and `origin, bound` forms, +plus `nextBoolean`, `nextFloat`, `nextGaussian` and `chance(probability)`. It does not implement +`java.util.random.RandomGenerator`, whose default methods the JDK may change. Bounded integers are +unbiased, `nextDouble` never returns its bound, and invalid bounds throw. It is not thread-safe: a +stage uses its own stream within one invocation and must not share or keep it. ## Module jars @@ -68,8 +80,8 @@ Every stage id of the module starts with the module id and `/`; the name is what and versions must fit on one line: line breaks and other control characters are rejected. The host reserves namespaces for its own modules, such as `iris`. A pack module whose id uses a -reserved namespace fails the load, and because stages, settings ids and mantle keys follow the -module's namespace, none of them can use it either: +reserved namespace fails the load, and because stage ids, settings ids and mantle keys start with the +module id, none of them can use it either: ``` Module iris:stilt from modules/stilt.jar uses reserved namespace 'iris', which only modules that ship with the host may use; give the module a namespace of its own, which its stages, settings ids and mantle keys then use too @@ -136,7 +148,8 @@ try (var pack = LoadedPack.load(packRoot, host)) { `ModuleHost` holds what the host passes to every pack load: built-in modules (their `info()` is read once, here), the host packages pack modules may use, the JSON fields the host itself uses and the -reserved namespaces. Package names and namespaces are checked when it is built. `LoadedPack.load` +reserved namespaces. Each method adds to what earlier calls configured. Package names, namespaces and +reserved field names are checked when it is built. `LoadedPack.load` loads the pack's jars, installs them after the built-ins, catalogs settings and registrations and resolves both pipelines. It returns a complete pack or throws with nothing left open. `close()` releases the pack's class loader and its jar copies. `PackModules` and `LoadedModules`, which it @@ -144,7 +157,7 @@ uses, stay available for tests and special cases. The host derives one seed per placement or chunk, from the world seed and the placement's stable identity or the chunk position, and passes it to the runner. Each stage's stream comes from that -seed and the stage id (`StageRandom` documents the derivation). Stages cannot reach a shared stream. +seed and the stage id (`StageStreams` documents the derivation). Stages cannot reach a shared stream. The load copies the pack's jars into a fresh temporary directory (`iris-pack-modules-*`) and reads, checks and loads classes only from the copies. The originals are never held open, so a module author @@ -211,13 +224,13 @@ A pack without jars logs `Pack overworld uses no modules: no jars in its modules together with geometry and mantle registrations, and resolves both pipelines. Graph errors (missing requirement, cycle, a stage outside its module's id, duplicate, cross-pipeline edge) fail here, at pack load, and so do reserved namespaces and feature ids or mantle keys outside the -module's namespace. Errors about one module name its jar or "host classpath", and so does a module +module's id. Errors about one module name its jar or "host classpath", and so does a module that fails to link a class while installing, such as a jar built against an API whose methods have since changed: ``` Stage iris:anchor/height must start with example:moss/, the id of its defining module example:moss from modules/moss.jar -Mantle layer iris:depth must use namespace 'example' of its registering module example:moss from modules/moss.jar +Mantle layer iris:depth must be example:moss or start with example:moss/, the id of its registering module example:moss from modules/moss.jar ``` `describe()` prints the order, owners, edge kinds, fused block passes and the stage pairs that have no path between them. @@ -247,10 +260,13 @@ concurrent invocations and must not keep views, streams or mutable state. ``` The tests cover graph resolution and every error path, fusion, activation cascade, outputs, -rejection and rollback, install-order determinism, stage-id prefixes and settings, object and mantle -namespaces (`StageGraphTest`, `ModuleRegistryTest`), and per-stage random streams: inserting a module +rejection and rollback, install-order determinism, id ownership of stages, settings, objects and +mantle keys (`StageGraphTest`, `ModuleRegistryTest`), and per-stage random streams: inserting a module that draws leaves the other stages' draws unchanged, block stages draw one stream across their pass, -and the derivation is pinned to values computed from its specification (`StageRandomTest`). +and the derivation is pinned to values computed from its specification (`StageStreamsTest`). +`WorldRandomTest` pins every `WorldRandom` method, bounds and edge cases to values computed by an +independent Python reference (`python3 -B src/test/reference/world_random.py`; the build does not +need Python) and fails if the type gains an unpinned method or implements `RandomGenerator`. `DiscoveredModuleTest` checks that `info()` is read once. `LoadedPackTest` loads a pack with built-ins and checks that a failed load and `close()` leave nothing open. `PackJarSnapshotTest` checks that classes come from the copies, that the original can be replaced while loaded, that copies go