Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.
Python calibrationclassificationdecision-modelhuggingfacejevmodernbertmultilingualnlppythonpytorchroutingtyped-decisionszero-shot
fix(java): a non-finite score, CPython's float spelling, and anyOf's truthiness
Adeshina Lasisib02761d99af7
diff --git a/.github/workflows/java.yml b/.github/workflows/java.ymlindex c439af38..5a30cc56 100644--- a/.github/workflows/java.yml+++ b/.github/workflows/java.yml@@ -156,16 +156,16 @@ jobs:- name: Tests that need no modelworking-directory: laya-javarun: ./gradlew --no-daemon test- # Measured locally on JDK 17 with no checkpoints: 790 registered, 14 abort, 776 run. The+ # Measured locally on JDK 17 with no checkpoints: 798 registered, 14 abort, 784 run. The# floor keeps the 5-test margin the 685/680 pair had, which is what makes it bind the- # class this bump is for: DecisionsTest is 91 of those 776, so dropping it leaves 685 and- # the floor exits 1. Measured both ways -- 776 passes at 771, and with DecisionsTest.java+ # class this bump is for: DecisionsTest is 99 of those 784, so dropping it leaves 685 and+ # the floor exits 1. Measured both ways -- 784 passes at 779, and with DecisionsTest.java# moved out of the tree the suite is still green and the floor still exits 1.- name: Model-free suite was not vacuousif: ${{ !cancelled() }}run: >python laya-java/scripts/check_test_results.py- laya-java/laya-java/build/test-results/test --min-tests 771 --allow-aborted+ laya-java/laya-java/build/test-results/test --min-tests 779 --allow-aborted# The samples are single-file source programs, deliberately NOT a Gradle module: a# `samples` subproject would inherit the root build's MavenPublication and@@ -324,7 +324,7 @@ jobs:if: ${{ !cancelled() }}run: >python laya-java/scripts/check_test_results.py- laya-java/laya-java/build/test-results/test --min-tests 771 --allow-aborted+ laya-java/laya-java/build/test-results/test --min-tests 779 --allow-aborted--require-passed TestJvmVersionTest- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2if: ${{ failure() }}diff --git a/laya-java/CHANGELOG.md b/laya-java/CHANGELOG.mdindex bc4f81ee..8290e73e 100644--- a/laya-java/CHANGELOG.md+++ b/laya-java/CHANGELOG.md@@ -82,9 +82,12 @@ see [Installing](README.md#installing).### Testing-- 1,190 tests, 776 of which need no model at all (measured on JDK 17 with both shipped- checkpoints present; the rest need one, and the end-to-end golden additionally needs a traced- graph).+- 1,195 tests **run** with both shipped checkpoints present (1,198 registered, 3 aborted), and+ 784 **run** with no checkpoints at all (798 registered, 14 aborted). Both counts are+ tests that actually executed, not tests that were registered: a `@TestFactory` that aborts by+ assumption still appears in the report, so the two kinds of number are not comparable and+ mixing them overstates whichever one is the smaller. Measured on JDK 17; the aborted ones need+ a checkpoint, and the end-to-end golden additionally needs a traced graph.- Every expectation is **generated from the Python package**, never written by hand:`laya-java/fixtures/*.json`, regenerated by `scripts/gen_fixtures.py` and gated by `--check` inCI so a change to `laya/` that moves a recorded contract shows up here rather than as drift.diff --git a/laya-java/build.gradle.kts b/laya-java/build.gradle.ktsindex e0e7697d..cf9946a5 100644--- a/laya-java/build.gradle.kts+++ b/laya-java/build.gradle.kts@@ -93,8 +93,18 @@ subprojects {// are supposed to abort by assumption. Treating blank as unset is what makes "this cell// has no graph" and "this machine has no graph" the same case, which is what the tests// are written against.+ //+ // Declared as an INPUT as well as set. `environment(...)` alone is not a task input, so+ // toggling one of these left `test` UP-TO-DATE and handed back the PREVIOUS lane's+ // results -- a with-checkpoints run reporting the model-free run's counts, which is a+ // measurement of the wrong thing that looks exactly like a measurement of the right one.+ // CI never saw it (fresh runners), and every local count taken without `cleanTest` could+ // silently have been the other lane's. The input makes the lane part of the task's+ // identity, so the toggle re-runs the suite on its own.listOf("LAYA_CHECKPOINTS", "LAYA_ONNX_GRAPH", "LAYA_PREDICT_GOLDEN").forEach { name ->- System.getenv(name)?.takeIf { it.isNotBlank() }?.let { environment(name, it) }+ val value = System.getenv(name)?.takeIf { it.isNotBlank() }+ inputs.property("env.$name", value).optional(true)+ value?.let { environment(name, it) }}}diff --git a/laya-java/fixtures/structured.json b/laya-java/fixtures/structured.jsonindex 806ed936..4a0384da 100644--- a/laya-java/fixtures/structured.json+++ b/laya-java/fixtures/structured.json@@ -422,6 +422,83 @@}]},+ {+ "case": "an-empty-description-generates-the-instruction",+ "schema": {+ "type": "object",+ "properties": {+ "dept": {+ "enum": [+ "billing",+ "support"+ ],+ "description": ""+ },+ "ok": {+ "type": "boolean",+ "description": ""+ },+ "n": {+ "type": "integer",+ "minimum": 1,+ "maximum": 3,+ "description": ""+ }+ }+ },+ "questions": {+ "dept": {+ "type": "choice",+ "instructions": "What is `dept`?",+ "criteria": {+ "billing": null,+ "support": null+ }+ },+ "ok": {+ "type": "noul",+ "instructions": "Is `ok` true?"+ },+ "n": {+ "type": "score",+ "instructions": "Score `n` from 1 to 3",+ "criteria": [+ "1",+ "2",+ "3"+ ]+ }+ },+ "plan": [+ {+ "name": "dept",+ "kind": "choice",+ "options": [+ [+ "billing",+ "billing"+ ],+ [+ "support",+ "support"+ ]+ ],+ "minimum": null+ },+ {+ "name": "ok",+ "kind": "noul",+ "options": [],+ "minimum": null+ },+ {+ "name": "n",+ "kind": "score",+ "options": [],+ "minimum": 1+ }+ ]+ },{"case": "description-replaces-the-generated-instruction","schema": {@@ -588,6 +665,167 @@}]},+ {+ "case": "falsy-anyof-falls-through-to-oneof",+ "schema": {+ "type": "object",+ "properties": {+ "a": {+ "anyOf": [],+ "oneOf": [+ {+ "type": "boolean"+ },+ {+ "type": "null"+ }+ ]+ },+ "b": {+ "anyOf": {},+ "oneOf": [+ {+ "enum": [+ "x",+ "y"+ ]+ },+ {+ "type": "null"+ }+ ]+ },+ "c": {+ "anyOf": "",+ "oneOf": [+ {+ "type": "integer",+ "minimum": 2,+ "maximum": 4+ },+ {+ "type": "null"+ }+ ]+ },+ "d": {+ "anyOf": 0,+ "oneOf": [+ {+ "type": "boolean"+ },+ {+ "type": "null"+ }+ ]+ },+ "e": {+ "anyOf": false,+ "oneOf": [+ {+ "type": "boolean"+ },+ {+ "type": "null"+ }+ ]+ },+ "f": {+ "anyOf": null,+ "oneOf": [+ {+ "type": "boolean"+ },+ {+ "type": "null"+ }+ ]+ }+ }+ },+ "questions": {+ "a": {+ "type": "noul",+ "instructions": "Is `a` true?"+ },+ "b": {+ "type": "choice",+ "instructions": "What is `b`?",+ "criteria": {+ "x": null,+ "y": null+ }+ },+ "c": {+ "type": "score",+ "instructions": "Score `c` from 2 to 4",+ "criteria": [+ "2",+ "3",+ "4"+ ]+ },+ "d": {+ "type": "noul",+ "instructions": "Is `d` true?"+ },+ "e": {+ "type": "noul",+ "instructions": "Is `e` true?"+ },+ "f": {+ "type": "noul",+ "instructions": "Is `f` true?"+ }+ },+ "plan": [+ {+ "name": "a",+ "kind": "noul",+ "options": [],+ "minimum": null+ },+ {+ "name": "b",+ "kind": "choice",+ "options": [+ [+ "x",+ "x"+ ],+ [+ "y",+ "y"+ ]+ ],+ "minimum": null+ },+ {+ "name": "c",+ "kind": "score",+ "options": [],+ "minimum": 2+ },+ {+ "name": "d",+ "kind": "noul",+ "options": [],+ "minimum": null+ },+ {+ "name": "e",+ "kind": "noul",+ "options": [],+ "minimum": null+ },+ {+ "name": "f",+ "kind": "noul",+ "options": [],+ "minimum": null+ }+ ]+ },{"case": "anyof-branch-keeps-its-own-description","schema": {@@ -2955,6 +3193,45 @@}},"error": "properties.c: $ref '#/$defs/Node' is recursive; flatten the schema"+ },+ {+ "case": "truthy-non-array-anyof-is-not-a-fallthrough",+ "schema": {+ "type": "object",+ "properties": {+ "x": {+ "anyOf": "abc"+ }+ }+ },+ "error": "properties.x: only 'Optional[...]' unions (one non-null branch) are supported, got 0"+ },+ {+ "case": "truthy-non-array-anyof-with-a-oneof-to-shadow",+ "schema": {+ "type": "object",+ "properties": {+ "x": {+ "anyOf": {+ "a": 1+ },+ "oneOf": [+ {+ "type": "boolean"+ },+ {+ "type": "null"+ }+ ]+ }+ }+ },+ "error": "properties.x: only 'Optional[...]' unions (one non-null branch) are supported, got 0"+ },+ {+ "case": "top-level-is-null",+ "schema": null,+ "error": "expected a JSON schema object, got NoneType"}],"arguments": [diff --git a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Decisions.java b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Decisions.javaindex a8b7eac9..d9f98f29 100644--- a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Decisions.java+++ b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Decisions.java@@ -67,6 +67,40 @@ import java.util.Set;* the one the reference's own {@code decide_batch} always takes.</li>* <li>{@code routing} is not reported. {@link Prediction} carries no routing block, so there is* nothing to pass through; {@link Router} reports its decision at the point it makes it.</li>+ * <li>A <b>batched</b> decision cannot route per state. The reference's {@code decide_batch}+ * special-cases a {@code route_batch}-capable runner and sends one request per state "so+ * states may route to different checkpoints"; {@link Router} here implements+ * {@link Predictor} and not {@link BatchPredictor}, so {@link #decideBatch} will not take+ * one at all. Batching a router means deciding what a shared forward pass over two+ * checkpoints is, which is a design question and not a port.</li>+ * <li>An {@code enum} must be an <b>array</b>. The reference measures and iterates whatever+ * is there, so {@code "enum": "abc"} becomes a three-option choice over {@code "a"},+ * {@code "b"}, {@code "c"} — a schema nobody wrote, built from a typo — and+ * {@code "enum": 5} is a {@code TypeError} from inside {@code len()} rather than a+ * refusal naming the property.</li>+ * <li>A score's {@code minimum} and {@code maximum} must be integers, and a <b>boolean</b> is+ * not one. Python's {@code bool} subclasses {@code int}, so the reference accepts+ * {@code {"minimum": false, "maximum": true}} and asks "Score {@code x} from 0 to 1".</li>+ * <li>A {@code description} must be a <b>string</b>. The reference puts whatever is there into+ * the question's {@code instructions} — {@code 5}, or {@code ['a']} — which is not text a+ * model can be asked.</li>+ * <li>A {@code score} answer whose {@code score} is not finite is refused. CPython's+ * {@code int()} refuses it too — {@code ValueError} for a NaN, {@code OverflowError} for+ * an infinity — so the verdict matches and only the wording differs: this one names the+ * field, as the rest of the answer-shape refusals do.</li>+ * <li>An {@code anyOf}/{@code oneOf} that is neither falsy nor iterable — a number, or+ * {@code true} — is a {@link SchemaException} naming the key, where the reference raises+ * {@code TypeError: 'int' object is not iterable} from inside its own comprehension. The+ * truthiness itself is reproduced: a falsy {@code anyOf} falls through to {@code oneOf}+ * and a truthy non-array reaches the same "got 0" refusal the reference raises. This is+ * the <b>one</b> exception to the rule that a refusal message matches character for+ * character.</li>+ * <li>A top level that is not an object is a <b>compile error</b>, not a refusal:+ * {@link #plan} takes a {@link Map}, where the reference raises+ * {@code expected a JSON schema object, got list}. A caller who reaches it through an+ * unchecked cast — {@code (Map<String, Object>) Json.parse(text)}, which is the README's+ * own idiom — gets a {@link ClassCastException} from the cast instead. The one case the+ * static type does not cover, a null schema, is refused with the reference's message.</li>* </ul>*/public final class Decisions {@@ -182,6 +216,18 @@ public final class Decisions {if (!score.probabilities().isEmpty()) {level = argmax(score.probabilities());} else {+ // Refused rather than narrowed. `(long)` on a non-finite double is silent+ // -- NaN becomes 0 and +Infinity becomes Long.MAX_VALUE -- so+ // `minimum + level` would project 1 for a NaN and+ // -9223372036854775808 for an infinity onto a field the schema declared+ // as 1..3, with nothing in the Decision saying so. CPython's `int()`+ // refuses both (ValueError for a NaN, OverflowError for an infinity),+ // and this is the one arithmetic path `expect` does not cover.+ if (!Double.isFinite(score.score())) {+ throw new IllegalArgumentException(String.format(+ "%s is a score field, so its score must be finite and is %s",+ field.name(), pythonStr(score.score())));+ }// `score` is already the 0-based LEVEL INDEX, so it is rounded on its own// with no `minimum` subtracted first. Math.rint is Python's round: half to// even. Math.round is half up and gives 3 for 2.5.@@ -283,6 +329,13 @@ public final class Decisions {/** Validates a JSON schema and returns one planned field per property. */public static Plan plan(Map<String, Object> schema) {+ if (schema == null) {+ // The reference's `isinstance(schema, dict)` check has one reachable case here and+ // this is it: `Map` is the guard for a list, a string or a number, and null is+ // assignable to it. Without this the refusal is an NPE from `schema.get`, which+ // names neither the argument nor what was wrong with it.+ throw new SchemaException("expected a JSON schema object, got NoneType");+ }Object type = schema.get("type");if (!(type == null || "object".equals(type)) || !schema.containsKey("properties")) {throw new SchemaException("the top level must be an object with 'properties'");@@ -380,10 +433,15 @@ public final class Decisions {// handles below. The single non-null branch is unwrapped so Optional[Literal[...]]// and friends map; a union of two real types is genuinely ambiguous and refused.//- // An EMPTY `anyOf` falls through to `oneOf`, because the reference spells this- // `prop.get("anyOf") or prop.get("oneOf")` and an empty list is falsy there.+ // A FALSY `anyOf` falls through to `oneOf`, because the reference spells this+ // `prop.get("anyOf") or prop.get("oneOf")`. Falsy is Python's, not "not a list":+ // an empty list, an empty object, an empty string, zero and false are all falsy+ // there and all fall through, while a NON-EMPTY string or object is truthy and does+ // NOT -- it is iterated, yields no mapping branch, and lands on the `got 0` refusal+ // below. Reproducing only the empty-list case sent a truthy `{"anyOf": "abc"}`+ // through to `oneOf` and, with no `oneOf` to find, out to "unsupported schema".Object union = prop.get("anyOf");- if (!(union instanceof List) || ((List<?>) union).isEmpty()) {+ if (!truthy(union)) {union = prop.get("oneOf");}if (union != null) {@@ -395,6 +453,15 @@ public final class Decisions {branches.add((Map<String, Object>) candidate);}}+ } else if (!(union instanceof String) && !(union instanceof Map)) {+ // A string iterates its characters and an object its keys, so both reach the+ // refusal below with zero branches, which is what the reference raises. A+ // number or a boolean is not iterable at all: the reference raises TypeError+ // there, and this is the one recorded exception to the+ // message-for-message rule -- see the class note.+ throw new SchemaException(path + ": '"+ + (truthy(prop.get("anyOf")) ? "anyOf" : "oneOf")+ + "' must be an array of branches, got " + pythonType(union));}if (branches.size() != 1) {throw new SchemaException(path + ": only 'Optional[...]' unions (one non-null"@@ -540,6 +607,40 @@ public final class Decisions {return new Field(name, Question.Type.SCORE, question, List.of(), low);}+ /**+ * Python's truth value of a parsed-JSON value, which is what {@code a or b} tests.+ *+ * <p>Empty is false: {@code []}, <code>{}</code>, {@code ""}, {@code 0}, {@code 0.0} and+ * {@code false} are all falsy, which is why a schema may spell any of them and still mean+ * "look at the other key". A Java truth test on the same value would be a null check and+ * would differ on every one of them.+ */+ private static boolean truthy(Object value) {+ if (value == null) {+ return false;+ }+ if (value instanceof Boolean) {+ return (Boolean) value;+ }+ if (value instanceof String) {+ return !((String) value).isEmpty();+ }+ if (value instanceof List) {+ return !((List<?>) value).isEmpty();+ }+ if (value instanceof Map) {+ return !((Map<?, ?>) value).isEmpty();+ }+ if (value instanceof BigInteger) {+ return ((BigInteger) value).signum() != 0;+ }+ if (value instanceof Number) {+ // `0.0`, `-0.0` and `0` are all falsy; a NaN is truthy, as every non-zero float is.+ return ((Number) value).doubleValue() != 0.0;+ }+ return true;+ }+/*** Whether a bound counts as an integer.*@@ -599,7 +700,25 @@ public final class Decisions {/** Decides one state against a schema, ungated. */public static Decision decide(Predictor runner, Object state, Map<String, Object> schema) {- return decide(runner, state, schema, null, (Double) null);+ return decide(runner, state, schema, null);+ }++ /**+ * Decides one state, ungated: pass exactly <b>one</b> of {@code schema} or+ * {@code questions}.+ *+ * <p>The ungated spelling of the five-argument form, and the reason it exists is that the+ * five-argument form is <b>ambiguous</b> on a bare {@code null} -- both the {@link Double}+ * and the {@code Map} threshold overload match, so "no gate" could only be written+ * {@code (Double) null}. A cast is not an argument, and an explicit-questions caller who+ * wants no gate should not have to pick one of two threshold types to say so.+ *+ * @throws IllegalArgumentException if both or neither of {@code schema} and+ * {@code questions} is given+ */+ public static Decision decide(Predictor runner, Object state, Map<String, Object> schema,+ Map<String, Question> questions) {+ return decide(runner, state, schema, questions, (Double) null);}/**@@ -631,7 +750,23 @@ public final class Decisions {/** Decides many states against one schema in one batched call, ungated. */public static List<Decision> decideBatch(BatchPredictor runner, List<?> states,Map<String, Object> schema) {- return decideBatch(runner, states, schema, null, (Double) null);+ return decideBatch(runner, states, schema, null);+ }++ /**+ * Decides many states in one batched call, ungated: pass exactly <b>one</b> of+ * {@code schema} or {@code questions}.+ *+ * <p>The batched counterpart of {@link #decide(Predictor, Object, Map, Map)}, and there for+ * the same reason: the five-argument form cannot express "no gate" without a cast.+ *+ * @throws IllegalArgumentException if both or neither of {@code schema} and+ * {@code questions} is given+ */+ public static List<Decision> decideBatch(BatchPredictor runner, List<?> states,+ Map<String, Object> schema,+ Map<String, Question> questions) {+ return decideBatch(runner, states, schema, questions, (Double) null);}/**@@ -767,7 +902,22 @@ public final class Decisions {return PythonJson.repr((String) value);}if (value instanceof Double || value instanceof Float) {- return PythonJson.repr(((Number) value).doubleValue());+ double number = ((Number) value).doubleValue();+ // CPython's `repr` spells the non-finite floats in LOWER CASE -- `nan`, `inf`,+ // `-inf` -- where `json.dumps` writes `NaN`, `Infinity`, `-Infinity`.+ // PythonJson.repr(double) reproduces the `dumps` spelling, which is right for+ // `dumps` and wrong here: this is `%r` and `str`, so a refusal message quotes the+ // fragment the way CPython would, and a choice LABEL -- what the model is shown and+ // what the answer comes back keyed by -- is the same string on both sides. Not+ // reachable through Json.parse, which rejects a bare NaN, and fully reachable from+ // the hand-built Map the public `questions(Map)` signature takes.+ if (Double.isNaN(number)) {+ return "nan";+ }+ if (Double.isInfinite(number)) {+ return number > 0 ? "inf" : "-inf";+ }+ return PythonJson.repr(number);}if (value instanceof Number) {return value.toString();diff --git a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/json/PythonJson.java b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/json/PythonJson.javaindex 608e1f61..add106a0 100644--- a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/json/PythonJson.java+++ b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/json/PythonJson.java@@ -284,15 +284,29 @@ public final class PythonJson {return "nan";}if (Double.isInfinite(scaled)) {- // CPython's %-formatting spells these in lower case, which is NOT how `repr` spells- // them: `'%.0f' % float('nan')` is "nan" where `repr` gives "NaN". Latent today --- // both call sites are finite by construction -- and wrong the moment one is not.+ // CPython's %-formatting spells these in lower case, and so does `repr`:+ // `'%.0f' % float('nan')` and `repr(float('nan'))` are both "nan". It is+ // `json.dumps` that writes "NaN"/"Infinity", which is the spelling `repr(double)`+ // below reproduces because that is the method `dumps` is built on -- so the two+ // disagree here on purpose. Latent today -- both call sites are finite by+ // construction -- and wrong the moment one is not.return scaled > 0 ? "inf" : "-inf";}// Math.rint is IEEE ties-to-even, which is the rule; a long keeps a share above 2^31.return Long.toString((long) Math.rint(scaled));}+ /**+ * A double the way CPython's {@code json.dumps} writes it.+ *+ * <p>For every finite value that is also {@code repr}: {@code dumps} uses {@code repr} under+ * the hood. The non-finite three are where the two part company -- {@code dumps} writes+ * {@code NaN}, {@code Infinity} and {@code -Infinity} (which is not legal JSON, and is what+ * CPython emits anyway), where {@code repr} writes {@code nan}, {@code inf} and {@code -inf}.+ * This method is the {@code dumps} spelling, because {@code dumps} is what it serves. A+ * caller interpolating a float into a {@code %r} or {@code str} message needs the other+ * spelling and has to say so.+ */public static String repr(double value) {if (Double.isNaN(value)) {return "NaN";diff --git a/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/DecisionsTest.java b/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/DecisionsTest.javaindex e2c47f7f..fb7278f8 100644--- a/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/DecisionsTest.java+++ b/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/DecisionsTest.java@@ -210,15 +210,16 @@ final class DecisionsTest {BatchPredictor neverBatch = (states, asked) -> {throw new AssertionError("the runner must not be reached");};+ // The ungated FOUR-argument form. Spelled with the five-argument one these four read+ // `(Double) null`, because a bare null there matches both the `Double` and the `Map`+ // threshold overload and does not compile -- and a cast is not an argument.Map<String, Runnable> calls = new LinkedHashMap<>();- calls.put("decide-with-both", () -> Decisions.decide(never, "s", schema, questions,- (Double) null));- calls.put("decide-with-neither", () -> Decisions.decide(never, "s", null, null,- (Double) null));+ calls.put("decide-with-both", () -> Decisions.decide(never, "s", schema, questions));+ calls.put("decide-with-neither", () -> Decisions.decide(never, "s", null, null));calls.put("decide-batch-with-both", () -> Decisions.decideBatch(neverBatch, List.of("s"),- schema, questions, (Double) null));+ schema, questions));calls.put("decide-batch-with-neither", () -> Decisions.decideBatch(neverBatch,- List.of("s"), null, null, (Double) null));+ List.of("s"), null, null));List<DynamicTest> tests = new ArrayList<>();int matched = 0;@@ -360,8 +361,9 @@ final class DecisionsTest {return new Prediction(Prediction.MODEL, Map.of("ok", answer),new Usage(1, 0, 1, 0, false, List.of(), Map.of()));};- Decisions.Decision decision = Decisions.decide(runner, "s", null, questions,- (Double) null);+ // Ungated explicit questions, with no threshold type to pick: the entry point that did+ // not exist, and the reason the four calls above had to be cast.+ Decisions.Decision decision = Decisions.decide(runner, "s", null, questions);// No schema, so nothing is projected: the reference returns `dict(answers)` and so does// this. The values are the answers themselves, not a schema-shaped map.assertEquals(Map.of("ok", answer), decision.values());@@ -401,6 +403,134 @@ final class DecisionsTest {assertFalse(refused instanceof SchemaException, "the schema is fine; the answer is not");}+ @Test+ @DisplayName("the reference forwards min_confidence from decide only, and nothing is "+ + "forwarded here")+ void forwardingIsPinned() {+ // `forwarded` is the ONLY recorded datum for the documented forwarding divergence: the+ // reference passes `min_confidence` down to `runner.predict` and falls back to gating+ // the result itself when the runner does not take the keyword, while `decide_batch`+ // always gates itself and forwards nothing. Here there is no such keyword on+ // `Predictor`, so the fallback is the only path -- and that choice is only defensible+ // while the reference's own behaviour is what the class note says it is. Asserted+ // rather than merely recorded, because recorded-and-unasserted data reads as coverage+ // and is not.+ int gated = 0;+ int batched = 0;+ for (Object raw : section("calls")) {+ Map<String, Object> one = map(raw);+ String id = (String) one.get("case");+ boolean batch = Boolean.TRUE.equals(one.get("batch"));+ Object recorded = one.get("forwarded");+ assertTrue(recorded instanceof List, id + " records what the reference forwarded");+ List<?> forwarded = (List<?>) recorded;+ if (!batch && one.get("min_confidence") != null) {+ gated++;+ assertEquals(List.of("min_confidence"), forwarded,+ id + ": the reference forwards the threshold from decide");+ } else {+ if (batch) {+ batched++;+ }+ assertEquals(List.of(), forwarded,+ id + ": nothing to forward, so nothing is forwarded on either side");+ }+ }+ final int forwards = gated;+ final int batches = batched;+ assertTrue(forwards >= 3,+ () -> "only " + forwards + " gated decide case(s) record a forward");+ assertTrue(batches >= 2, () -> "only " + batches + " batch case(s) record no forward");++ // And the structural half: the keyword the reference forwards has nowhere to go here.+ // A `Predictor` takes the state and the questions and nothing else, so "forward+ // nothing" is not a choice made at the call site that a later edit could quietly+ // reverse -- it is the only thing the interface can express.+ long shaped = java.util.Arrays.stream(Predictor.class.getMethods())+ .filter(method -> "predict".equals(method.getName()))+ .filter(method -> method.getParameterCount() == 2)+ .count();+ assertEquals(1, shaped,+ "Predictor.predict(state, questions) is the whole interface; a third parameter "+ + "would be somewhere for min_confidence to go and the class note would "+ + "be out of date");+ }++ @Test+ @DisplayName("a score answer that is not finite is refused, not narrowed into the range")+ void nonFiniteScoreIsRefused() {+ Map<String, Object> schema = Map.of("type", "object", "properties",+ new LinkedHashMap<>(Map.of("n",+ Map.of("type", "integer", "minimum", 1L, "maximum", 3L))));+ // `(long)` on a non-finite double is a silent narrowing that -Werror -Xlint:all cannot+ // see: NaN becomes 0 and +Infinity becomes Long.MAX_VALUE, so `minimum + level`+ // projected 1 for a NaN and -9223372036854775808 for an infinity onto a field declared+ // 1..3 -- measured, and with no exception and nothing in the Decision saying so.+ // CPython refuses both: `ValueError: cannot convert float NaN to integer` and+ // `OverflowError: cannot convert float infinity to integer`. Reached only when+ // `probabilities` is empty; otherwise the argmax path runs and the score is unused.+ for (Map.Entry<String, Double> bad : new LinkedHashMap<>(Map.of(+ "nan", Double.NaN,+ "inf", Double.POSITIVE_INFINITY,+ "-inf", Double.NEGATIVE_INFINITY)).entrySet()) {+ Map<String, Answer> answers = Map.of("n",+ new Answer.Score(bad.getValue(), Map.of(), Map.of(), 0.9, 0.9, 0.2));+ IllegalArgumentException refused = assertThrows(IllegalArgumentException.class,+ () -> Decisions.answersToJson(answers, schema), bad.getKey());+ assertEquals("n is a score field, so its score must be finite and is " + bad.getKey(),+ refused.getMessage());+ assertFalse(refused instanceof SchemaException,+ "the schema is fine; the answer is not");+ }+ // The finite path is untouched, including Python's half-to-even at 2.5 -> level 2.+ assertEquals(Map.of("n", 3L), Decisions.answersToJson(+ Map.of("n", new Answer.Score(2.5, Map.of(), Map.of(), 0.9, 0.9, 0.2)), schema));+ // And a non-finite score is ignored outright when probabilities decide the level.+ assertEquals(Map.of("n", 2L), Decisions.answersToJson(+ Map.of("n", new Answer.Score(Double.NaN, Map.of(),+ new LinkedHashMap<>(Map.of("0", 0.1, "1", 0.9)), 0.9, 0.9, 0.2)), schema));+ }++ @Test+ @DisplayName("a non-finite float is spelled CPython's way, in a label and in a refusal")+ void nonFiniteFloatsUseCPythonSpelling() {+ // Not reachable through `Json.parse`, which rejects a bare NaN, and fully reachable from+ // the hand-built Map the public `Decisions.questions(Map)` signature takes -- so it is+ // not in `fixtures/structured.json`, whose schemas are JSON. The expectations below are+ // CPython's, measured on the reference:+ //+ // repr(float("nan")) == "nan"; repr(float("inf")) == "inf"+ // questions_from_json_schema({"type": "object",+ // "properties": {"x": {"enum": [float("nan"), 1]}}})+ // -> {"x": {..., "criteria": {"nan": None, "1": None}}}+ // ... {"properties": {"x": {"a": float("inf")}}}+ // -> SchemaError("properties.x: unsupported schema {'a': inf}")+ //+ // `PythonJson.repr(double)` gives "NaN"/"Infinity" on purpose: that is `json.dumps`'s+ // spelling, and `dumps` is what it serves. This is the `%r` and `str` path.+ Map<String, Object> labelled = Map.of("type", "object", "properties",+ new LinkedHashMap<>(Map.of("x",+ Map.of("enum", List.of(Double.NaN, Double.POSITIVE_INFINITY,+ Double.NEGATIVE_INFINITY, 1L)))));+ Map<String, Object> spec = Decisions.questions(labelled).get("x").spec();+ // The LABEL is what the model is shown and what the answer comes back keyed by, so a+ // port spelling it "NaN" hands a cross-language caller a different answer key.+ assertEquals(List.of("nan", "inf", "-inf", "1"),+ new ArrayList<>(map(spec.get("criteria")).keySet()));++ for (Map.Entry<String, Double> bad : new LinkedHashMap<>(Map.of(+ "nan", Double.NaN,+ "inf", Double.POSITIVE_INFINITY,+ "-inf", Double.NEGATIVE_INFINITY)).entrySet()) {+ Map<String, Object> schema = Map.of("type", "object", "properties",+ new LinkedHashMap<>(Map.of("x", Map.of("a", bad.getValue()))));+ SchemaException refused = assertThrows(SchemaException.class,+ () -> Decisions.questions(schema), bad.getKey());+ assertEquals("properties.x: unsupported schema {'a': " + bad.getKey() + "}",+ refused.getMessage());+ }+ }+// ------------------------------------------------------------------ fixture decoding/** Every recorded answer as the typed record. */diff --git a/laya-java/scripts/gen_fixtures.py b/laya-java/scripts/gen_fixtures.pyindex 9654e056..740c7613 100644--- a/laya-java/scripts/gen_fixtures.py+++ b/laya-java/scripts/gen_fixtures.py@@ -3142,6 +3142,15 @@ STRUCTURED_SCHEMAS = [{"type": "object", "properties": {"n": {"type": "integer", "minimum": -2, "maximum": 2}}}),("score-of-one-level",{"type": "object", "properties": {"n": {"type": "integer", "minimum": 7, "maximum": 7}}}),+ # An EMPTY description means "generate one", matching the reference's `description or ...`.+ # A port returning it unchanged asks a question with no instructions at all -- and a port+ # that returned the generated text for a NON-empty one would be caught by the case below,+ # not by this one. Measured: a mutant dropping the emptiness test survived.+ ("an-empty-description-generates-the-instruction",+ {"type": "object", "properties": {+ "dept": _sd_enum(["billing", "support"], description=""),+ "ok": {"type": "boolean", "description": ""},+ "n": {"type": "integer", "minimum": 1, "maximum": 3, "description": ""}}}),("description-replaces-the-generated-instruction",{"type": "object", "properties": {"dept": _sd_enum(["billing", "support"], description="Which desk owns this?"),@@ -3156,6 +3165,21 @@ STRUCTURED_SCHEMAS = [("oneof-optional-is-unwrapped",{"type": "object", "properties": {"n": {"oneOf": [{"type": "integer", "minimum": 1, "maximum": 4}, {"type": "null"}]}}}),+ # `anyOf or oneOf`, so a FALSY `anyOf` falls through to `oneOf` -- and in Python falsy is+ # not "absent": an empty list, an empty object, an empty string and a zero are all falsy and+ # all fall through. A port spelling the test as a null check, or as "not a list", sends the+ # empty-list case to `oneOf` by accident and the empty-OBJECT case there too while refusing+ # a truthy non-array that should have reached `got 0`. Measured: a mutant that dropped the+ # emptiness test survived the whole suite, because nothing asked.+ ("falsy-anyof-falls-through-to-oneof",+ {"type": "object", "properties": {+ "a": {"anyOf": [], "oneOf": [{"type": "boolean"}, {"type": "null"}]},+ "b": {"anyOf": {}, "oneOf": [_sd_enum(["x", "y"]), {"type": "null"}]},+ "c": {"anyOf": "", "oneOf": [{"type": "integer", "minimum": 2, "maximum": 4},+ {"type": "null"}]},+ "d": {"anyOf": 0, "oneOf": [{"type": "boolean"}, {"type": "null"}]},+ "e": {"anyOf": False, "oneOf": [{"type": "boolean"}, {"type": "null"}]},+ "f": {"anyOf": None, "oneOf": [{"type": "boolean"}, {"type": "null"}]}}}),# The outer description is carried onto the branch only when the branch has none.("anyof-branch-keeps-its-own-description",{"type": "object", "properties": {@@ -3370,6 +3394,21 @@ STRUCTURED_REFUSALS = [("recursive-ref",{"type": "object", "properties": {"c": {"$ref": "#/$defs/Node"}},"$defs": {"Node": {"$ref": "#/$defs/Node"}}}),+ # A TRUTHY non-array `anyOf` does NOT fall through to `oneOf`: it is iterated, yields no+ # mapping branch, and lands here. The other side of `falsy-anyof-falls-through-to-oneof`,+ # and the case a port testing "is it a list" gets wrong in the opposite direction -- it+ # falls through and, with nothing to fall through to, refuses with "unsupported schema"+ # instead of this.+ ("truthy-non-array-anyof-is-not-a-fallthrough",+ {"type": "object", "properties": {"x": {"anyOf": "abc"}}}),+ ("truthy-non-array-anyof-with-a-oneof-to-shadow",+ {"type": "object", "properties": {"x": {"anyOf": {"a": 1},+ "oneOf": [{"type": "boolean"},+ {"type": "null"}]}}}),+ # The one case of the reference's top-level `isinstance(schema, dict)` check that is+ # reachable on a side where the argument is typed: a list, a string or a number cannot be+ # passed at all, and null can.+ ("top-level-is-null", None),]@@ -3593,6 +3632,7 @@ def structured():"nested objects are not supported","only 'Optional[...]' unions (one non-null branch) are supported","'type' has multiple non-null types",+ "expected a JSON schema object","the top level must be an object with 'properties'","'properties' must be a non-empty object","exceeds MAX_PROPERTIES=%d" % STRUCTURED_MAX_PROPERTIES,