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): what the review of the merged runtime found
Adeshina Lasisie5cba4aa142a
diff --git a/.github/workflows/release-java.yml b/.github/workflows/release-java.ymlindex 4e3b0d05..d2cf118f 100644--- a/.github/workflows/release-java.yml+++ b/.github/workflows/release-java.yml@@ -58,7 +58,7 @@ jobs:- name: The suite was not vacuousrun: >python3 scripts/check_test_results.py laya-java/build/test-results/test- --min-tests 80 --allow-aborted+ --min-tests 440 --allow-aborted- name: Build the jarsrun: ./gradlew --no-daemon build -x test@@ -94,6 +94,17 @@ jobs:distribution: temurinjava-version: "17"cache: gradle+ - name: Refuse to publish anything but a tag+ run: |+ # Central is append-only: a version published once can never be replaced. On a+ # workflow_dispatch from a branch, GITHUB_REF_NAME is the BRANCH, so the version became+ # the branch name -- `:main` -- and the snapshot guard did not match it. The build job's+ # version check is guarded by this same condition and was simply skipped, so the two+ # jobs disagreed about what was being released.+ case "$GITHUB_REF" in+ refs/tags/laya-java-v*) : ;;+ *) echo "::error::refusing to publish from $GITHUB_REF; tag the release as laya-java-vX.Y.Z and run this on the tag" ; exit 1 ;;+ esac- name: Version from the tagrun: |tag="${GITHUB_REF_NAME#laya-java-v}"@@ -111,6 +122,11 @@ jobs:MAVEN_CENTRAL_USERNAME: ${{ secrets.MAVEN_CENTRAL_USERNAME }}MAVEN_CENTRAL_PASSWORD: ${{ secrets.MAVEN_CENTRAL_PASSWORD }}run: |+ # All four, not two. Without the passphrase the build calls useInMemoryPgpKeys(key,+ # null) and uploads UNSIGNED artifacts, which Central rejects only after the upload --+ # a much worse place to find out.test -n "$LAYA_SIGNING_KEY" || { echo "::error::LAYA_SIGNING_KEY is unset; see the header"; exit 1; }+ test -n "$LAYA_SIGNING_PASSWORD" || { echo "::error::LAYA_SIGNING_PASSWORD is unset; an unsigned bundle is rejected by Central after upload"; exit 1; }test -n "$MAVEN_CENTRAL_USERNAME" || { echo "::error::MAVEN_CENTRAL_USERNAME is unset; see the header"; exit 1; }+ test -n "$MAVEN_CENTRAL_PASSWORD" || { echo "::error::MAVEN_CENTRAL_PASSWORD is unset; see the header"; exit 1; }./gradlew --no-daemon publishAllPublicationsToCentralRepositorydiff --git a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/config/AgentConfig.java b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/config/AgentConfig.javaindex 5bd050a4..14d58c55 100644--- a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/config/AgentConfig.java+++ b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/config/AgentConfig.java@@ -88,6 +88,16 @@ public final class AgentConfig {private static AgentConfig of(Map<String, Object> document) {double[] temperature = {1.0, 1.0, 1.0};Object raw = document.get("temperature");+ if (raw != null && !(raw instanceof List)) {+ // Refused, not defaulted. The reference raises "Incompatible model" for a+ // non-list temperature; defaulting it to 1.0 let a corrupt or hand-edited checkpoint+ // load and then answer with an UNCALIBRATED temperature while reporting confidence as+ // though it were fitted. The wrong length was already refused here; only the wrong+ // type slipped through.+ throw new Json.JsonException(+ "temperature must be a list of three values, one per question type, got "+ + raw.getClass().getSimpleName());+ }if (raw instanceof List) {List<?> values = (List<?>) raw;if (values.size() != 3) {@@ -101,6 +111,12 @@ public final class AgentConfig {}Map<String, Double> byOptions = new LinkedHashMap<>();Object tbo = document.get("temperature_by_options");+ if (tbo != null && !(tbo instanceof Map)) {+ // The reference calls .items() on it, so a non-mapping raises there too.+ throw new Json.JsonException(+ "temperature_by_options must be a mapping, got "+ + tbo.getClass().getSimpleName());+ }if (tbo instanceof Map) {for (Map.Entry<?, ?> entry : ((Map<?, ?>) tbo).entrySet()) {byOptions.put(String.valueOf(entry.getKey()), clampTemperature(entry.getValue()));@@ -181,10 +197,25 @@ public final class AgentConfig {* Python and read as fitted and sharpening temperatures, so both are refused here.*/public static double clampTemperature(Object value) {- if (value instanceof Boolean || !(value instanceof Number)) {+ if (value instanceof Boolean) {+ return 1.0;+ }+ double t;+ if (value instanceof Number) {+ t = ((Number) value).doubleValue();+ } else if (value instanceof String) {+ // The reference does `float(t)` in a try/except, so "2.5" IS 2.5. Discarding it+ // silently defaulted the temperature to 1.0 and overstated the published+ // answer_confidence -- by 19 points on the case that found this -- while the class's+ // whole stated purpose is that these are never defaulted.+ try {+ t = Double.parseDouble(((String) value).trim());+ } catch (NumberFormatException notANumber) {+ return 1.0;+ }+ } else {return 1.0;}- double t = ((Number) value).doubleValue();if (Double.isNaN(t) || Double.isInfinite(t)) {return 1.0;}diff --git a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/decode/Decoder.java b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/decode/Decoder.javaindex 1d5e3ae2..63fe2692 100644--- a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/decode/Decoder.java+++ b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/decode/Decoder.java@@ -124,7 +124,16 @@ public final class Decoder {* plausible for a well-trained head, which is what makes it the wrong kind of bug.*/public static float[] actionProbabilities(float[] actLogits) {- int width = Math.max(2, actLogits.length);+ if (actLogits.length == 0) {+ // Published as NaN before this. The reference indexes act[r, 0] and raises; an+ // escalation signal that reads NaN is worse than one that refuses.+ throw new IllegalArgumentException(+ "the graph produced no action logits, so there is no action probability");+ }+ // The width the graph actually produced. Padding a single column up to two turned a+ // probability of 1.0 into sigmoid(logit) -- a plausible-looking number for a value the+ // model never produced, in the field callers gate escalation on.+ int width = actLogits.length;double max = Double.NEGATIVE_INFINITY;for (int i = 0; i < width; i++) {double value = i < actLogits.length ? actLogits[i] : 0.0;diff --git a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/decode/Rounding.java b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/decode/Rounding.javaindex 884e8397..334c02c7 100644--- a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/decode/Rounding.java+++ b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/decode/Rounding.java@@ -35,7 +35,14 @@ public final class Rounding {// Python returns NaN / inf unchanged rather than raising.return value;}- return new BigDecimal(value).setScale(4, RoundingMode.HALF_EVEN).doubleValue();+ double rounded = new BigDecimal(value).setScale(4, RoundingMode.HALF_EVEN).doubleValue();+ // BigDecimal has no signed zero, so -1e-5 came back as +0.0 where CPython's round gives+ // -0.0. The class claims exact parity, and a record's equals() distinguishes the two, so+ // a serialized-answer comparison would disagree. Restored from the operand's sign.+ if (rounded == 0.0 && (Double.doubleToRawLongBits(value) & Long.MIN_VALUE) != 0) {+ return -0.0;+ }+ return rounded;}/**diff --git a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/json/Json.java b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/json/Json.javaindex 39ccd233..00109fc8 100644--- a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/json/Json.java+++ b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/json/Json.java@@ -153,6 +153,43 @@ public final class Json {// ------------------------------------------------------------------ the reader itself+ /**+ * How deep a document may nest.+ *+ * <p>Without a limit, deeply nested input threw {@link StackOverflowError} -- an+ * {@link Error}, which a server's {@code catch (Exception)} does not contain and which can+ * leave a thread's invariants broken. CPython raises a catchable {@code RecursionError}.+ * Both {@code tokenizer.json} and {@code rl_agent_config.json} arrive from a remote download+ * and are parsed by this reader, so the input is not trusted; the limit sits far above+ * anything a real checkpoint nests and far below the stack.+ */+ private static final int MAX_DEPTH = 512;++ /** CPython's integer-string limit, which also bounds a quadratic BigDecimal conversion. */+ private static final int MAX_INTEGER_DIGITS = 4300;++ private int depth;++ /**+ * A hex digit, ASCII only.+ *+ * <p>{@code Character.digit} is Unicode-aware, so fullwidth and Arabic-Indic digits were+ * accepted as hex: two byte-different documents parsed to the same value, and input CPython+ * rejects was accepted.+ */+ private static int asciiHex(int c) {+ if (c >= '0' && c <= '9') {+ return c - '0';+ }+ if (c >= 'a' && c <= 'f') {+ return c - 'a' + 10;+ }+ if (c >= 'A' && c <= 'F') {+ return c - 'A' + 10;+ }+ return -1;+ }+private Object readValue() throws IOException {int c = peek();switch (c) {@@ -179,6 +216,17 @@ public final class Json {}private Map<String, Object> readObject() throws IOException {+ if (++depth > MAX_DEPTH) {+ throw new JsonException("JSON nested deeper than " + MAX_DEPTH + " levels");+ }+ try {+ return readObjectBody();+ } finally {+ depth--;+ }+ }++ private Map<String, Object> readObjectBody() throws IOException {read(); // '{'Map<String, Object> out = new LinkedHashMap<>();skipWhitespace();@@ -212,6 +260,17 @@ public final class Json {}private List<Object> readArray() throws IOException {+ if (++depth > MAX_DEPTH) {+ throw new JsonException("JSON nested deeper than " + MAX_DEPTH + " levels");+ }+ try {+ return readArrayBody();+ } finally {+ depth--;+ }+ }++ private List<Object> readArrayBody() throws IOException {read(); // '['List<Object> out = new ArrayList<>();skipWhitespace();@@ -278,7 +337,7 @@ public final class Json {int value = 0;for (int i = 0; i < 4; i++) {int c = read();- int digit = Character.digit(c, 16);+ int digit = asciiHex(c);if (c == -1 || digit < 0) {throw fail("a \\u escape needs four hex digits");}@@ -310,6 +369,14 @@ public final class Json {try {return Long.valueOf(literal);} catch (NumberFormatException tooWide) {+ if (literal.length() > MAX_INTEGER_DIGITS) {+ // CPython refuses past 4,300 digits (the CVE-2020-10735 mitigation), and+ // this conversion is QUADRATIC: a 1 MB literal cost 21 seconds of a+ // request thread, from one field of an untrusted tokenizer.json.+ throw new JsonException("integer literal of " + literal.length()+ + " digits exceeds the " + MAX_INTEGER_DIGITS+ + " this reader allows");+ }return new BigDecimal(literal).toBigIntegerExact();}}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 f3b82fa8..608e1f61 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@@ -48,7 +48,26 @@ public final class PythonJson {return out.toString();}+ /**+ * How deep {@code dumps} will descend.+ *+ * <p>The identity set closed the CYCLIC case; a deep ACYCLIC caller-supplied state still+ * threw {@link StackOverflowError} -- the same {@link Error} the cycle fix exists to stop.+ */+ private static final int MAX_DEPTH = 512;+private static void write(StringBuilder out, Object value, Set<Object> open) {+ write(out, value, open, 0);+ }++ private static void write(StringBuilder out, Object value, Set<Object> open, int depth) {+ if (depth > MAX_DEPTH) {+ throw new Json.JsonException("value nested deeper than " + MAX_DEPTH + " levels");+ }+ writeValue(out, value, open, depth);+ }++ private static void writeValue(StringBuilder out, Object value, Set<Object> open, int depth) {if (value == null) {out.append("null");} else if (value instanceof String) {@@ -65,11 +84,11 @@ public final class PythonJson {out.append(value.toString());} else if (value instanceof Map) {enter(open, value);- writeObject(out, (Map<?, ?>) value, open);+ writeObject(out, (Map<?, ?>) value, open, depth);open.remove(value);} else if (value instanceof List) {enter(open, value);- writeArray(out, (List<?>) value, open);+ writeArray(out, (List<?>) value, open, depth);open.remove(value);} else {throw new Json.JsonException(@@ -86,7 +105,8 @@ public final class PythonJson {}}- private static void writeObject(StringBuilder out, Map<?, ?> map, Set<Object> open) {+ private static void writeObject(StringBuilder out, Map<?, ?> map, Set<Object> open,+ int depth) {out.append('{');boolean first = true;for (Map.Entry<?, ?> entry : map.entrySet()) {@@ -96,7 +116,7 @@ public final class PythonJson {first = false;writeString(out, key(entry.getKey()));out.append(": ");- write(out, entry.getValue(), open);+ write(out, entry.getValue(), open, depth + 1);}out.append('}');}@@ -134,13 +154,14 @@ public final class PythonJson {+ key.getClass().getName());}- private static void writeArray(StringBuilder out, List<?> list, Set<Object> open) {+ private static void writeArray(StringBuilder out, List<?> list, Set<Object> open,+ int depth) {out.append('[');for (int i = 0; i < list.size(); i++) {if (i > 0) {out.append(", ");}- write(out, list.get(i), open);+ write(out, list.get(i), open, depth + 1);}out.append(']');}diff --git a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/onnx/LayaSession.java b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/onnx/LayaSession.javaindex bfa65242..b0c08365 100644--- a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/onnx/LayaSession.java+++ b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/onnx/LayaSession.java@@ -110,6 +110,13 @@ public final class LayaSession implements InferenceSession {/** Runs one collated batch. */@Overridepublic Output run(Collator.Batch batch) {+ if (closed) {+ // The guard isClosed()'s javadoc promises ("so a caller gets an API error, not a+ // backend one") and which did not exist: a use-after-close surfaced ORT's own+ // IllegalStateException, outside both the OrtException catch below and+ // InferenceException -- precisely the one error class this API says it never emits.+ throw new InferenceException("this laya session is closed");+ }List<OnnxTensor> owned = new ArrayList<>();try {OnnxTensor inputIds = track(owned, OnnxTensor.createTensor(environment, batch.inputIds()));@@ -138,7 +145,20 @@ public final class LayaSession implements InferenceSession {headInputs.put("hidden_states", hidden);headInputs.put("marker_pos", markerPos);headInputs.put("marker_mask", markerMask);- headInputs.put("qtype", qtype);+ // [B, 1], not [B]. The exporter declares the head's qtype input with a+ // trailing dimension of 1 (it squeezes it back off internally), so a rank-1+ // tensor is refused outright: "Invalid rank for input: qtype Got: 1 Expected: 2".+ // Every split export the only exporter produces therefore failed, and the error+ // arrived as a per-batch backend failure that reads like a bad model rather than+ // a bad client. laya-ts passes [n, 1] here for the same reason.+ long[] flat = batch.qtype();+ long[][] column = new long[flat.length][1];+ for (int row = 0; row < flat.length; row++) {+ column[row][0] = flat[row];+ }+ OnnxTensor qtypeColumn = track(owned,+ OnnxTensor.createTensor(environment, column));+ headInputs.put("qtype", qtypeColumn);headInputs.put("attention_mask", attention);try (OrtSession.Result result = head.run(headInputs, REQUESTED)) {return new Output(floats(result, "logits"), floats(result, "act_logits"));@@ -184,7 +204,24 @@ public final class LayaSession implements InferenceSession {"the laya graph's " + name + " output is not a tensor but a "+ value.getClass().getSimpleName());}- return (float[][]) ((OnnxTensor) value).getValue();+ Object raw;+ try {+ raw = ((OnnxTensor) value).getValue();+ } catch (OrtException failure) {+ throw new InferenceException(+ "the laya graph's " + name + " output could not be read", failure);+ }+ if (!(raw instanceof float[][])) {+ // Checked rather than cast. An fp16/fp64 export, or a re-export that changes the rank,+ // produced a ClassCastException naming neither the graph nor the output -- through a+ // method whose javadoc promises the backend never leaks, so a caller's+ // catch (InferenceException) did not catch it.+ throw new InferenceException(String.format(+ "the laya graph's %s output must be a 2-D float tensor, got %s;"+ + " re-export the graph with float32 logits of shape [batch, markers]",+ name, raw == null ? "null" : raw.getClass().getSimpleName()));+ }+ return (float[][]) raw;}/** Whether this session has been closed, so a caller gets an API error, not a backend one. */diff --git a/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/config/AgentConfigTest.java b/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/config/AgentConfigTest.javaindex 9324deb3..96210af4 100644--- a/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/config/AgentConfigTest.java+++ b/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/config/AgentConfigTest.java@@ -33,16 +33,53 @@ final class AgentConfigTest {}@Test- @DisplayName("a non-number, a boolean, NaN and an infinity all fall back to 1.0")+ @DisplayName("clampTemperature falls back exactly where the reference falls back")void nonNumbersFallBack() {// A bool used to float to 1.0/0.0 and read as a fitted or sharpening temperature.assertEquals(1.0, AgentConfig.clampTemperature(Boolean.TRUE), 0.0);assertEquals(1.0, AgentConfig.clampTemperature(Boolean.FALSE), 0.0);- assertEquals(1.0, AgentConfig.clampTemperature("2.0"), 0.0);assertEquals(1.0, AgentConfig.clampTemperature(null), 0.0);assertEquals(1.0, AgentConfig.clampTemperature(Double.NaN), 0.0);assertEquals(1.0, AgentConfig.clampTemperature(Double.POSITIVE_INFINITY), 0.0);assertEquals(AgentConfig.TEMP_MAX, AgentConfig.clampTemperature(99.0), 0.0);++ // A NUMERIC STRING IS PARSED. The reference does `float(t)` in a try/except, so "2.0"+ // is 2.0 -- this assertion previously demanded 1.0, which is what the port did and the+ // reference does not. Discarding it defaulted the temperature silently and overstated+ // the published answer_confidence while the class's stated purpose is that these are+ // never defaulted.+ assertEquals(2.0, AgentConfig.clampTemperature("2.0"), 0.0);+ assertEquals(2.5, AgentConfig.clampTemperature("2.5"), 0.0);+ assertEquals(2.5, AgentConfig.clampTemperature(" 2.5 "), 0.0);+ // and a string that is not a number still falls back, as the except branch does+ assertEquals(1.0, AgentConfig.clampTemperature("abc"), 0.0);+ assertEquals(1.0, AgentConfig.clampTemperature(""), 0.0);+ // the clamp still applies to a parsed string+ assertEquals(AgentConfig.TEMP_MIN, AgentConfig.clampTemperature("0.1"), 0.0);+ assertEquals(AgentConfig.TEMP_MAX, AgentConfig.clampTemperature("99.0"), 0.0);+ }++ @Test+ @DisplayName("a malformed temperature refuses the checkpoint instead of defaulting it")+ void malformedTemperatureIsRefused() {+ // The reference raises "Incompatible model" for a non-list temperature. Defaulting it to+ // 1.0 let a corrupt or hand-edited checkpoint load and then answer with an uncalibrated+ // temperature while reporting confidence as though it were fitted.+ for (String document : new String[] {+ "{\"temperature\": 2.0}",+ "{\"temperature\": \"2.0\"}",+ "{\"temperature\": {\"0\": 2.0, \"1\": 2.0, \"2\": 2.0}}",+ }) {+ org.junit.jupiter.api.Assertions.assertThrows(+ com.convaiinnovations.laya.json.Json.JsonException.class,+ () -> AgentConfig.fromReader(new java.io.StringReader(document)),+ "a non-list temperature must refuse the checkpoint: " + document);+ }+ org.junit.jupiter.api.Assertions.assertThrows(+ com.convaiinnovations.laya.json.Json.JsonException.class,+ () -> AgentConfig.fromReader(new java.io.StringReader(+ "{\"temperature\": [2.0, 2.0, 2.0], \"temperature_by_options\": [1, 2]}")),+ "a non-mapping temperature_by_options must refuse the checkpoint too");}@Testdiff --git a/laya-java/scripts/check_test_results.py b/laya-java/scripts/check_test_results.pyindex 82608017..9fd8dce5 100755--- a/laya-java/scripts/check_test_results.py+++ b/laya-java/scripts/check_test_results.py@@ -91,8 +91,14 @@ def main(argv=None):print(" %d test(s) aborted, which this lane allows:" % len(aborted))for name in aborted[:5]:print(" " + name)- if tests < args.min_tests:- problems.append("only %d tests ran, expected at least %d" % (tests, args.min_tests))+ # Executed, not merely registered. The JUnit `tests` attribute COUNTS SKIPPED tests, so a+ # suite in which every single test aborted satisfied this floor and both --allow-aborted+ # lanes -- the build gate and the Maven Central release gate -- went green with nothing+ # actually run. That is the exact failure this script's docstring says it exists to catch.+ executed = tests - skipped+ if executed < args.min_tests:+ problems.append("only %d of %d tests actually ran (%d skipped), expected at least %d"+ % (executed, tests, skipped, args.min_tests))if problems:for line in problems:diff --git a/laya-java/settings.gradle.kts b/laya-java/settings.gradle.ktsindex a21457a6..294e3fa5 100644--- a/laya-java/settings.gradle.kts+++ b/laya-java/settings.gradle.kts@@ -3,4 +3,9 @@// published artifact to measure against.rootProject.name = "laya-java-root"-include("laya-java", "laya-java-client")+// `laya-java-client` is deliberately NOT included. The directory holds no source -- the README+// lists the HTTP client as not implemented -- and including it gave it a full MavenPublication+// that `publishAllPublicationsToCentralRepository` would have pushed to Central as a zero-class+// jar whose POM promises an HTTP client. A version burned on Central cannot be replaced, so the+// module joins the build when it has code.+include("laya-java")