Skip to content

armgit / laya

Public Imported from GitHub
0

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.

fix(java): a per-bucket threshold key must name a bucket an answer can produce

Adeshina Lasisi99794146ccd3
diff --git a/.github/workflows/java.yml b/.github/workflows/java.yml
index ad89b336..8b9e509f 100644
--- a/.github/workflows/java.yml
+++ b/.github/workflows/java.yml
@@ -154,17 +154,17 @@ jobs:
# Without LAYA_CHECKPOINTS the checkpoint-backed factories abort by assumption and the rest
# run. `--min-tests` is what stops an aborted suite from looking like a pass.
#
- # Measured on the merged tree: 934 register here, 917 run, 17 abort on a checkpoint this
+ # Measured on the merged tree: 950 register here, 933 run, 17 abort on a checkpoint this
# lane does not have. Exact, not padded -- the floor stood at 680 long enough for the real
# figure to pass it by, which is how a floor stops being one. ONE below the JDK-matrix
# lane's floor on purpose: that lane passes `-PtestJavaVersion`, which is what makes
- # `TestJvmVersionTest` run instead of skip, so it executes 918. Same suite, one more test,
+ # `TestJvmVersionTest` run instead of skip, so it executes 934. Same suite, one more test,
# because the flag enables it. The step below is where the number is justified.
- name: Tests that need no model
working-directory: laya-java
run: ./gradlew --no-daemon test
- # Measured on the merged tree, Corretto 17, no checkpoints: 934 registered, 17 abort,
- # 917 RUN -- the floor counts what ran, so the aborted checkpoint-backed cases are not in
+ # Measured on the merged tree, Corretto 17, no checkpoints: 950 registered, 17 abort,
+ # 933 RUN -- the floor counts what ran, so the aborted checkpoint-backed cases are not in
# it. The floor is EXACT, with no margin, which is a change from the margin-of-4 this
# replaced. A margin only binds classes larger than itself, and three classes in this lane
# run exactly ONE test (`DecoderTest`, `TypedDecisionsParityTest`, `WindowPlanTest`), so no
@@ -179,7 +179,7 @@ jobs:
if: ${{ !cancelled() }}
run: >
python laya-java/scripts/check_test_results.py
- laya-java/laya-java/build/test-results/test --min-tests 917 --allow-aborted
+ laya-java/laya-java/build/test-results/test --min-tests 933 --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
@@ -335,7 +335,7 @@ jobs:
# dropped, Gradle exits 0 and the floor alone exits 0 on 488 tests, having run JDK 17 twice
# and reported it as two JDKs. With `--require-passed` it exits 1.
#
- # 918, not the build lane's 917, and the one is the point: this lane runs one test MORE
+ # 934, not the build lane's 933, and the one is the point: this lane runs one test MORE
# than that one, because `TestJvmVersionTest` is `@EnabledIfSystemProperty` and only
# enabled here. Both are EXACT, with no margin, for the reason the build lane gives --
# three classes here run exactly one test, so no positive margin can bind them.
@@ -356,7 +356,7 @@ jobs:
if: ${{ !cancelled() }}
run: >
python laya-java/scripts/check_test_results.py
- laya-java/laya-java/build/test-results/test --min-tests 918 --allow-aborted
+ laya-java/laya-java/build/test-results/test --min-tests 934 --allow-aborted
--require-passed TestJvmVersionTest
- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
if: ${{ failure() }}
diff --git a/laya-java/fixtures/confidence_gate.json b/laya-java/fixtures/confidence_gate.json
index 89f0be8f..a1db52e4 100644
--- a/laya-java/fixtures/confidence_gate.json
+++ b/laya-java/fixtures/confidence_gate.json
@@ -823,7 +823,7 @@
},
{
"input": "{1: 0.5}",
- "error": "min_confidence map keys must be strings like 'choice:3-5', got 1"
+ "error": "min_confidence map keys must be a bucket like 'choice:3-5' or 'default', got 1"
}
]
}
diff --git a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/ConfidenceGate.java b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/ConfidenceGate.java
index 635657a3..4fc382ab 100644
--- a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/ConfidenceGate.java
+++ b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/ConfidenceGate.java
@@ -1,5 +1,6 @@
package com.convaiinnovations.laya;
+import com.convaiinnovations.laya.json.PythonJson;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
@@ -147,6 +148,31 @@ public final class ConfidenceGate {
return thresholds.getOrDefault("default", 0.0);
}
+ /**
+ * Every bucket key an answer can actually produce, plus {@code "default"}.
+ *
+ * <p>Derived from the same spelling {@link #optionBucket} emits, rather than written out, so
+ * the two cannot drift: three question types crossed with the four size bands.
+ *
+ * <p>This set is the whole point of the check below. The reference validated only that a key
+ * was a string until {@code cb85656}; it now requires the key to name a bucket an answer can
+ * produce, because a threshold under {@code "choice:99"} is silently never applied -- the
+ * caller believes they gated something and nothing is gated. A port that accepts the key the
+ * reference refuses turns a loud refusal into exactly that silence.
+ */
+ private static final java.util.Set<String> ALLOWED_BUCKETS;
+
+ static {
+ java.util.Set<String> allowed = new java.util.LinkedHashSet<>();
+ for (String type : new String[]{"choice", "score", "noul"}) {
+ for (String size : new String[]{"2", "3-5", "6-10", "11+"}) {
+ allowed.add(type + ":" + size);
+ }
+ }
+ allowed.add("default");
+ ALLOWED_BUCKETS = java.util.Set.copyOf(allowed);
+ }
+
/** Validates a scalar threshold, returning it. */
public static double checkMinConfidence(double value) {
if (!Double.isFinite(value) || value < 0.0 || value > 1.0) {
@@ -164,9 +190,13 @@ public final class ConfidenceGate {
}
Map<String, Double> out = new LinkedHashMap<>();
for (Map.Entry<String, ? extends Number> entry : map.entrySet()) {
- if (entry.getKey() == null) {
- throw new IllegalArgumentException(
- "min_confidence map keys must be strings like 'choice:3-5', got null");
+ if (entry.getKey() == null || !ALLOWED_BUCKETS.contains(entry.getKey())) {
+ // Message matched to `laya/confidence.py` character for character, including the
+ // `%r` spelling of the offending key, because a caller greps for this line.
+ throw new IllegalArgumentException(String.format(
+ "min_confidence map keys must be a bucket like 'choice:3-5' or "
+ + "'default', got %s",
+ entry.getKey() == null ? "None" : PythonJson.repr(entry.getKey())));
}
out.put(entry.getKey(), checkMinConfidence(entry.getValue().doubleValue()));
}
diff --git a/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/ConfidenceGateBucketKeyTest.java b/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/ConfidenceGateBucketKeyTest.java
new file mode 100644
index 00000000..93bd0831
--- /dev/null
+++ b/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/ConfidenceGateBucketKeyTest.java
@@ -0,0 +1,74 @@
+package com.convaiinnovations.laya;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+
+import java.util.Map;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+/**
+ * A per-bucket threshold key must name a bucket an answer can produce.
+ *
+ * <p>This exists because {@code fixtures/confidence_gate.json} cannot hold it. The reference's
+ * recorded refusal uses the key {@code 1} — an int — and this port's signature is
+ * {@code Map<String, ? extends Number>}, so that case is unrepresentable here and the fixture
+ * comparison skips it. A BAD STRING key is representable, the reference refuses it since
+ * {@code cb85656}, and nothing in the recorded set reaches that path.
+ *
+ * <p>Why it matters more than a message: a threshold filed under {@code "choice:99"} is never
+ * applied to anything, because no answer ever resolves to that bucket. The caller believes they
+ * gated a decision and nothing is gated. Accepting the key the reference refuses converts a loud
+ * error into exactly that silence, which is the failure this gate exists to prevent.
+ */
+final class ConfidenceGateBucketKeyTest {
+
+ @Test
+ @DisplayName("a key that names no reachable bucket is refused, with the reference's message")
+ void unreachableBucketKeyIsRefused() {
+ for (String key : new String[]{"choice:99", "nonsense", "Choice:3-5", "choice:3_5", ""}) {
+ IllegalArgumentException refused = assertThrows(IllegalArgumentException.class,
+ () -> ConfidenceGate.checkMinConfidenceMap(Map.of(key, 0.5)),
+ "expected " + key + " to be refused");
+ assertEquals("min_confidence map keys must be a bucket like 'choice:3-5' or "
+ + "'default', got '" + key + "'", refused.getMessage(), "message for " + key);
+ }
+ }
+
+ @Test
+ @DisplayName("every bucket an answer can produce is accepted, and so is default")
+ void everyReachableBucketIsAccepted() {
+ for (String type : new String[]{"choice", "score", "noul"}) {
+ for (String size : new String[]{"2", "3-5", "6-10", "11+"}) {
+ String key = type + ":" + size;
+ assertDoesNotThrow(() -> ConfidenceGate.checkMinConfidenceMap(Map.of(key, 0.5)),
+ key + " is a bucket optionBucket emits, so it must be accepted");
+ }
+ }
+ assertDoesNotThrow(() -> ConfidenceGate.checkMinConfidenceMap(Map.of("default", 0.5)));
+ }
+
+ /**
+ * The allowed set and {@link ConfidenceGate#optionBucket} must not drift apart.
+ *
+ * <p>Asserted through the public API rather than by reading the private set: every bucket the
+ * bucketing function can emit is fed back in as a key, so a change to one that is not mirrored
+ * in the other fails here rather than in a caller's config.
+ */
+ @Test
+ @DisplayName("optionBucket cannot emit a bucket checkMinConfidenceMap would reject")
+ void bucketingAndValidationAgree() {
+ for (int options = 0; options <= 40; options++) {
+ Map<String, Double> probabilities = new java.util.LinkedHashMap<>();
+ for (int i = 0; i < options; i++) {
+ probabilities.put("option" + i, 1.0 / Math.max(1, options));
+ }
+ Answer.Choice choice =
+ new Answer.Choice("option0", probabilities, 0.5, 0.5, 0.5);
+ ConfidenceGate.optionBucket(choice).ifPresent(bucket ->
+ assertDoesNotThrow(() -> ConfidenceGate.checkMinConfidenceMap(Map.of(bucket, 0.5)),
+ "optionBucket emitted " + bucket + " but the key check rejects it"));
+ }
+ }
+}