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
test(java): pin the guards 1d1b656 added, and say what abandonment means
Adeshina Lasisi7900dbad63f0
diff --git a/.github/workflows/java.yml b/.github/workflows/java.ymlindex 30ed56c2..43a1d72c 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: 1000 register here, 983 run, 17 abort on a checkpoint this+ # Measured on the merged tree: 1002 register here, 985 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 984. Same suite, one more test,+ # `TestJvmVersionTest` run instead of skip, so it executes 986. Same suite, one more test,# because the flag enables it. The step below is where the number is justified.- name: Tests that need no modelworking-directory: laya-javarun: ./gradlew --no-daemon test- # Measured on the merged tree, Corretto 17, no checkpoints: 1000 registered, 17 abort,- # 983 RUN -- the floor counts what ran, so the aborted checkpoint-backed cases are not in+ # Measured on the merged tree, Corretto 17, no checkpoints: 1002 registered, 17 abort,+ # 985 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 983 --allow-aborted+ laya-java/laya-java/build/test-results/test --min-tests 985 --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.#- # 984, not the build lane's 983, and the one is the point: this lane runs one test MORE+ # 986, not the build lane's 985, 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 984 --allow-aborted+ laya-java/laya-java/build/test-results/test --min-tests 986 --allow-aborted--require-passed TestJvmVersionTest- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2if: ${{ failure() }}diff --git a/.github/workflows/release-java.yml b/.github/workflows/release-java.ymlindex 9d0e571b..deeba7e3 100644--- a/.github/workflows/release-java.yml+++ b/.github/workflows/release-java.yml@@ -67,7 +67,7 @@ jobs:# that lane already covered.- name: Model-free testsrun: ./gradlew --no-daemon test- # 983 of the 1000 registered tests run without a checkpoint. This floor has been raised+ # 985 of the 1002 registered tests run without a checkpoint. This floor has been raised# as the model-free suite grew -- 440 left 191 of slack, 625 left 60 -- because a floor# that does not bind is not a gate: under 625 the whole confidence-gate family of 26# tests could have vanished from this lane with the gate still green.@@ -83,7 +83,7 @@ jobs:- name: The suite was not vacuousrun: >python3 scripts/check_test_results.py laya-java/build/test-results/test- --min-tests 983 --allow-aborted+ --min-tests 985 --allow-aborted- name: Build the jarsrun: ./gradlew --no-daemon build -x testdiff --git a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Router.java b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Router.javaindex 7a9ef177..587672b5 100644--- a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Router.java+++ b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Router.java@@ -962,7 +962,10 @@ public final class Router implements AutoCloseable, Predictor {}}- /** {@code addSuppressed}, unless the hook rethrew {@code failure} itself, which cannot suppress itself. */+ /**+ * {@code addSuppressed}, unless the hook rethrew {@code failure} itself: a throwable cannot+ * suppress itself.+ */private static void attachHookFailure(Throwable failure, Throwable hookFailure) {if (hookFailure != failure) {failure.addSuppressed(hookFailure);@@ -1494,7 +1497,8 @@ public final class Router implements AutoCloseable, Predictor {// Before routing, as the reference does: a malformed call must not cold-load a// checkpoint, fire on_load or evict a resident one before it is refused.if (state == null) {- throw new IllegalArgumentException("state must not be null");+ throw new IllegalArgumentException(+ "state must not be null; pass a string, a map or a list");}if (questions == null) {throw new IllegalArgumentException("questions must not be null");@@ -1586,7 +1590,7 @@ public final class Router implements AutoCloseable, Predictor {} catch (RuntimeException failure) {if (first == null) {first = failure;- } else {+ } else if (failure != first) {first.addSuppressed(failure);}}diff --git a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/hooks/AsyncHook.java b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/hooks/AsyncHook.javaindex 4eb31433..05da7f99 100644--- a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/hooks/AsyncHook.java+++ b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/hooks/AsyncHook.java@@ -29,9 +29,10 @@ import java.util.function.Consumer;* returns, and a later callback of the same call on that thread is accepted; an abandonment* made by anything else on that thread stays in force. A callback that had not started when the* caller stopped waiting -- on the deadline, or on an interrupt while it waited for one -- never- * runs. Without a deadline the wait ignores interrupts, so it always ends with the callback. It still occupies the executor's- * thread until it returns, so a single-threaded executor has none left for the next callback --- * size the executor for the deadline, or leave the deadline off.+ * runs. Without a deadline the wait ignores interrupts, so it always ends with the callback. An+ * abandoned callback still occupies the executor's thread until it returns, so a single-threaded+ * executor has none left for the next callback -- size the executor for the deadline, or leave+ * the deadline off.*/public final class AsyncHook implements Hook {@@ -169,7 +170,10 @@ public final class AsyncHook implements Hook {}}- /** The call has stopped waiting: a callback not yet started never runs, a running one is cut off. */+ /**+ * The call has stopped waiting: a callback not yet started never runs, and a running one is+ * cut off.+ */private static void giveUp(Object gate, boolean[] gaveUp, boolean[] finished,boolean[] abandonedHere, AtomicReference<Thread> runner,PredictContext ctx) {diff --git a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/hooks/Hooks.java b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/hooks/Hooks.javaindex 2f37a0dd..eb76b6e6 100644--- a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/hooks/Hooks.java+++ b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/hooks/Hooks.java@@ -778,8 +778,8 @@ public final class Hooks {* A hook failure is attached to {@code problem}, never substituted.** <p>For a {@link com.convaiinnovations.laya.Predictor} that must do work before its start- * event, as {@code Router} does. {@code ctx} must be fresh: one that already carries results- * or an error belongs to a call that has been reported, and is refused.+ * event, as {@code Router} does. Pass a context no hook has seen: one that already carries+ * results or an error is refused, though a context mid-call without either is not detected.*/public static void failedBeforeStart(List<? extends Hook> hooks, PredictContext ctx,Policy policy, Throwable problem, long startedAtNanos) {diff --git a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/hooks/PredictContext.java b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/hooks/PredictContext.javaindex e4e2c4d2..d891755b 100644--- a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/hooks/PredictContext.java+++ b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/hooks/PredictContext.java@@ -61,10 +61,11 @@ public final class PredictContext {private volatile Throwable error;/**- * Threads whose deadline expired, and whose writes are therefore refused.+ * Threads the call stopped waiting for -- on a deadline or an interrupt -- whose writes are+ * therefore refused.*- * <p>Empty for every call that does not use {@link Hooks.Policy#timeout}, and at most one- * entry per overrun. A set rather than a flag because a chain can overrun more than once.+ * <p>Empty unless the call stopped waiting for some hook, and at most one entry each time it+ * did. A set rather than a flag because that can happen more than once in one chain.*/private final Set<Thread> abandoned = ConcurrentHashMap.newKeySet();@@ -158,7 +159,7 @@ public final class PredictContext {* store", which is a narrowing, not a proof — and the guard is cheap enough to be worth the* narrowing on its own.*- * @throws IllegalStateException when the calling thread's deadline has already expired+ * @throws IllegalStateException when the call has already stopped waiting for this thread*/public void states(List<?> replacement) {refuseIfAbandoned("states");@@ -173,7 +174,7 @@ public final class PredictContext {/*** Replaces the questions, keeping the given iteration order — a choice's options are positional.*- * @throws IllegalStateException when the calling thread's deadline has already expired; see+ * @throws IllegalStateException when the call has already stopped waiting for this thread; see* {@link #states(List)}*/public void questions(Map<String, Question> replacement) {@@ -196,7 +197,7 @@ public final class PredictContext {/*** Replaces the results. From an end hook this is what the caller receives.*- * @throws IllegalStateException when the calling thread's deadline has already expired; see+ * @throws IllegalStateException when the call has already stopped waiting for this thread; see* {@link #states(List)}*/public void results(List<Prediction> replacement) {@@ -212,7 +213,7 @@ public final class PredictContext {/*** Overrides the token budget for this call.*- * @throws IllegalStateException when the calling thread's deadline has already expired; see+ * @throws IllegalStateException when the call has already stopped waiting for this thread; see* {@link #states(List)}*/public void maxLen(Integer replacement) {@@ -228,7 +229,7 @@ public final class PredictContext {/*** Overrides the head budget for this call.*- * @throws IllegalStateException when the calling thread's deadline has already expired; see+ * @throws IllegalStateException when the call has already stopped waiting for this thread; see* {@link #states(List)}*/public void headMaxLen(Integer replacement) {@@ -289,7 +290,7 @@ public final class PredictContext {* "nothing", and an empty call has nothing to answer.** @throws IllegalArgumentException when the count is neither 1 nor one per state- * @throws IllegalStateException when the calling thread's deadline has already expired; see+ * @throws IllegalStateException when the call has already stopped waiting for this thread; see* {@link #states(List)}*/public void skip(List<Prediction> results) {@@ -305,8 +306,8 @@ public final class PredictContext {/*** Cuts {@code runner} off from this call: nothing it writes from here on is accepted.*- * <p>Called by {@link Hooks} the moment a hook's deadline expires, which is the moment the- * call stops being able to wait for it. See {@link #states(List)}.+ * <p>Called the moment the call stops waiting for a hook, on its deadline or on an interrupt.+ * See {@link #states(List)}.*/void abandon(Thread runner) {abandoned.add(runner);@@ -324,8 +325,8 @@ public final class PredictContext {private void refuseIfAbandoned(String what) {if (!abandoned.isEmpty() && abandoned.contains(Thread.currentThread())) {throw new IllegalStateException(String.format(- "laya: hook thread %s exceeded its deadline and was abandoned; this call has "- + "moved on, so ctx.%s() is refused rather than applied to it",+ "laya: hook thread %s was abandoned when the call stopped waiting for it; "+ + "this call has moved on, so ctx.%s() is refused rather than applied to it",Thread.currentThread().getName(), what));}}diff --git a/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/AsyncHookTest.java b/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/AsyncHookTest.javaindex 030e94e6..d8edbc20 100644--- a/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/AsyncHookTest.java+++ b/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/AsyncHookTest.java@@ -241,17 +241,14 @@ class AsyncHookTest {void anInterruptedWaitAbandonsTheCallback() throws Exception {ExecutorService pool = Executors.newSingleThreadExecutor();try {+ CountDownLatch release = new CountDownLatch(1);CountDownLatch started = new CountDownLatch(1);CountDownLatch done = new CountDownLatch(1);Hook async = AsyncHook.of(new Hook() {@Overridepublic void onPredictStart(PredictContext c) {started.countDown();- try {- Thread.sleep(300);- } catch (InterruptedException e) {- Thread.currentThread().interrupt();- }+ awaitQuietly(release);try {c.states(List.of("late"));} catch (RuntimeException refused) {@@ -261,7 +258,8 @@ class AsyncHookTest {}}}, pool, Duration.ofSeconds(5)); // timed: an untimed join() ignores interrupts- assertLateWriteRefusedAfterInterrupt(ctx -> async.onPredictStart(ctx), started, done);+ assertLateWriteRefusedAfterInterrupt(ctx -> async.onPredictStart(ctx), started, release,+ done);} finally {pool.shutdownNow();}@@ -270,17 +268,14 @@ class AsyncHookTest {@Test@DisplayName("an interrupted wait on a Hooks timeout thread cuts that hook off too")void anInterruptedHooksWaitAbandonsTheHook() throws Exception {+ CountDownLatch release = new CountDownLatch(1);CountDownLatch started = new CountDownLatch(1);CountDownLatch done = new CountDownLatch(1);Hook slow = new Hook() {@Overridepublic void onPredictStart(PredictContext c) {started.countDown();- try {- Thread.sleep(300);- } catch (InterruptedException e) {- Thread.currentThread().interrupt();- }+ awaitQuietly(release);try {c.states(List.of("late"));} catch (RuntimeException refused) {@@ -292,12 +287,12 @@ class AsyncHookTest {};assertLateWriteRefusedAfterInterrupt(ctx -> Hooks.dispatch(List.of(slow),Hooks.Event.PREDICT_START, ctx, Hooks.Policy.raising().timeout(Duration.ofSeconds(5))),- started, done);+ started, release, done);}private static void assertLateWriteRefusedAfterInterrupt(java.util.function.Consumer<PredictContext> waitOn, CountDownLatch started,- CountDownLatch done) throws Exception {+ CountDownLatch release, CountDownLatch done) throws Exception {PredictContext ctx = ctx();AtomicReference<Throwable> caught = new AtomicReference<>();Thread caller = new Thread(() -> {@@ -312,10 +307,20 @@ class AsyncHookTest {caller.interrupt();caller.join(3_000);assertTrue(caught.get() instanceof IllegalStateException, String.valueOf(caught.get()));+ release.countDown(); // only now may the hook try its late writeassertTrue(done.await(3, TimeUnit.SECONDS));assertEquals(List.of("state"), ctx.states(), "a write after the caller moved on landed");}+ /** Waits for the test's go-ahead; a latch, so no sleep decides the order of events. */+ private static void awaitQuietly(CountDownLatch release) {+ try {+ release.await(5, TimeUnit.SECONDS);+ } catch (InterruptedException e) {+ Thread.currentThread().interrupt();+ }+ }+@Test@DisplayName("a non-positive timeout is refused rather than meaning 'never wait'")void refusesANonPositiveTimeout() {diff --git a/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/RouterHooksTest.java b/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/RouterHooksTest.javaindex 5d8fe542..468b30be 100644--- a/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/RouterHooksTest.java+++ b/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/RouterHooksTest.java@@ -8,6 +8,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;import com.convaiinnovations.laya.Router.Checkpoint;import com.convaiinnovations.laya.hooks.Hook;+import com.convaiinnovations.laya.hooks.Hooks;import com.convaiinnovations.laya.hooks.PredictContext;import java.io.IOException;import java.nio.file.Path;@@ -383,6 +384,70 @@ class RouterHooksTest {}}+ @Test+ @DisplayName("a hook rethrowing ctx.error() from any error or end event leaves the real failure")+ void rethrowingCtxErrorNeverReplacesTheFailure(@TempDir Path root) throws IOException {+ TinyCheckpoint.write(root, 64, 32);+ // A start hook failing goes through Hooks.around; a load failing through failedBeforeStart.+ for (String rethrowIn : List.of("error", "end")) {+ Hook rethrow = new Hook() {+ @Override+ public void onPredictStart(PredictContext ctx) {+ throw new IllegalStateException("start refused");+ }++ @Override+ public void onError(PredictContext ctx) {+ if (rethrowIn.equals("error")) {+ throw (RuntimeException) ctx.error();+ }+ }++ @Override+ public void onPredictEnd(PredictContext ctx) {+ if (rethrowIn.equals("end")) {+ throw (RuntimeException) ctx.error();+ }+ }+ };+ try (Router router = Router.builder().agents(new StubAgents(root)).build()) {+ router.hooks().addHook(rethrow);+ IllegalStateException thrown = assertThrows(IllegalStateException.class,+ () -> router.predict("hello", questions()));+ assertEquals("start refused", thrown.getMessage(), "rethrown in " + rethrowIn);+ }+ }+ Router.AgentFactory broken = checkpoint -> {+ throw new IllegalStateException("no weights here");+ };+ try (Router router = Router.builder().agents(broken).build()) {+ router.hooks().addHook(new Hook() {+ @Override+ public void onPredictEnd(PredictContext ctx) {+ throw (RuntimeException) ctx.error();+ }+ });+ IllegalStateException thrown = assertThrows(IllegalStateException.class,+ () -> router.predict("hello", questions()));+ assertEquals("no weights here", thrown.getMessage());+ }+ }++ @Test+ @DisplayName("failedBeforeStart refuses a null failure and a context that already has an outcome")+ void failedBeforeStartRefusesMisuse() {+ PredictContext fresh = new PredictContext(List.of("s"), Map.of(), "english", null);+ assertThrows(NullPointerException.class, () -> Hooks.failedBeforeStart(List.of(), fresh,+ Hooks.Policy.raising(), null, System.nanoTime()));+ PredictContext reported = new PredictContext(List.of("s"), Map.of(), "english", null);+ Hooks.failedBeforeStart(List.of(), reported, Hooks.Policy.raising(),+ new IllegalStateException("first"), System.nanoTime());+ assertThrows(IllegalStateException.class, () -> Hooks.failedBeforeStart(List.of(),+ reported, Hooks.Policy.raising(), new IllegalStateException("second"),+ System.nanoTime()));+ assertEquals("first", reported.error().getMessage());+ }+/** A factory that keeps every session it hands out, so a test can see which were closed. */private static Router.AgentFactory recording(Path root, List<TinyCheckpoint.RecordingSession> out) {return checkpoint -> {@@ -444,16 +509,22 @@ class RouterHooksTest {}@Test- @DisplayName("null questions are refused before routing: nothing is built, loaded or evicted")- void nullQuestionsAreRefusedBeforeAnyLoad(@TempDir Path root) throws IOException {+ @DisplayName("null state or questions are refused before routing, with or without hooks")+ void nullsAreRefusedBeforeAnyLoad(@TempDir Path root) throws IOException {TinyCheckpoint.write(root, 64, 32);- StubAgents agents = new StubAgents(root);- Trace trace = new Trace();- try (Router router = Router.builder().agents(agents).build()) {- router.hooks().addHook(trace);- assertThrows(IllegalArgumentException.class, () -> router.predict("hello", null));- assertEquals(0, agents.builds);- assertEquals(List.of(), trace.events);+ for (boolean hooked : new boolean[] {true, false}) {+ StubAgents agents = new StubAgents(root);+ Trace trace = new Trace();+ try (Router router = Router.builder().agents(agents).build()) {+ if (hooked) {+ router.hooks().addHook(trace);+ }+ assertThrows(IllegalArgumentException.class, () -> router.predict("hello", null));+ assertThrows(IllegalArgumentException.class,+ () -> router.predict(null, questions()));+ assertEquals(0, agents.builds, "hooked=" + hooked + ": a refused call loaded");+ assertEquals(List.of(), trace.events);+ }}}@@ -479,9 +550,12 @@ class RouterHooksTest {seen.set(ctx.error());}});+ long before = System.nanoTime();assertSame(failure, assertThrows(IllegalStateException.class,() -> router.predict("hello", questions())));+ double wall = (System.nanoTime() - before) / 1_000_000.0;assertTrue(elapsed.get() >= 50.0, "elapsedMs " + elapsed.get() + " missed the load");+ assertTrue(elapsed.get() <= wall, "elapsedMs " + elapsed.get() + " exceeds the call");assertSame(failure, seen.get());}}