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 list state was truncated from the wrong end
Adeshina Lasisiace0bae85532
diff --git a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Agent.java b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Agent.javaindex 2c013107..5424c55d 100644--- a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Agent.java+++ b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Agent.java@@ -90,10 +90,14 @@ public final class Agent implements AutoCloseable, Predictor {*/public static Agent using(Tokenizer tokenizer, AgentConfig config, InferenceSession session) {// Padding is masked out of attention but still embedded, so it has to be a real id.- int padId = tokenizer.padId().orElseGet(- () -> tokenizer.sepId().orElseThrow(() -> new IllegalStateException(- "this checkpoint names neither a pad_token nor a sep_token, so a batch "- + "of more than one row cannot be padded")));+ // No silent fallback to the SEP id. The reference hands `tok.pad_token_id` straight to+ // the collator, so a checkpoint naming no pad_token fails the request outright -- and a+ // mis-exported checkpoint that fails loudly on one runtime must not answer quietly on the+ // other. (The graph masks padding, so the substituted id did not itself move the numbers;+ // the divergence was that one runtime accepted a configuration the other rejects.)+ int padId = tokenizer.padId().orElseThrow(() -> new IllegalStateException(+ "this checkpoint names no pad_token, so a batch cannot be padded; the reference "+ + "refuses the same checkpoint"));return new Agent(tokenizer, config, session, padId);}@@ -118,8 +122,12 @@ public final class Agent implements AutoCloseable, Predictor {* @param language a tag whose prefix before {@code -} may select a temperature override, or null*/public Prediction predict(Object state, Map<String, Question> questions, String language) {- // `singletonList`, not `List.of`: a null state is legitimate -- Python serialises `None`- // to the text "null" -- and `List.of` rejects it with a bare NullPointerException.+ // `singletonList`, not `List.of`, so that a null state reaches predictBatch's own check+ // and is refused there with a message rather than by a bare NullPointerException from+ // the list factory. The comment here used to claim a null state was legitimate because+ // "Python serialises None to the text null" -- which is backwards: the reference refuses+ // it for exactly that reason, since answering it would be a confident decision about the+ // four characters "null", byte-identical to passing the string.return predictBatch(Collections.singletonList(state), questions, language, 0, false).get(0);}@@ -146,16 +154,35 @@ public final class Agent implements AutoCloseable, Predictor {*/public List<Prediction> predictBatch(List<?> states, Map<String, Question> questions,String language, int batchSize, boolean sortByLength) {- if (questions.isEmpty()) {- throw new IllegalArgumentException("no questions to ask");- }if (closed) {throw new IllegalStateException("this agent is closed; open a new one rather than reusing it");}+ for (int i = 0; i < states.size(); i++) {+ // Refused, as both Python backends refuse it: `serialize_state(None)` is+ // `json.dumps(None)`, so a missing state would otherwise be answered as a decision+ // about the literal text "null" -- byte-identical to passing the string, and at full+ // confidence. A caller whose state field is absent should hear about it.+ if (states.get(i) == null) {+ throw new IllegalArgumentException(+ "state at index " + i + " is null; pass a string, a map or a list");+ }+ }if (states.isEmpty()) {return List.of();}+ if (questions.isEmpty()) {+ // Empty answers, zero usage, no tokenization and no forward pass -- which is what+ // the reference returns. Throwing here meant the two runtimes could not be swapped+ // under a question set derived from a filter: an empty schema or a disabled rule set+ // is a well-formed request with a well-formed empty answer.+ List<Prediction> empty = new ArrayList<>(states.size());+ for (int i = 0; i < states.size(); i++) {+ empty.add(new Prediction(Prediction.MODEL, Map.of(),+ new Usage(0, 0, 0, 0, false, List.of(), Map.of())));+ }+ return List.copyOf(empty);+ }// Snapshotted once, at entry. The map is the caller's, and the sequences are built before// the graph runs while the answers are labelled after it: reading it twice let a mutation// in between attach one question's labels to another question's logits, with no exception@@ -227,8 +254,16 @@ public final class Agent implements AutoCloseable, Predictor {List<SequenceBuilder.Sequence> built = new ArrayList<>(questionIds.size());for (String id : questionIds) {Question question = questions.get(id);+ // truncateLeft for a LIST state, which is the reference's rule+ // (`truncate_left = isinstance(state, list)`). A conversation is serialised+ // newest-last, so cutting from the right throws away the current turn and answers+ // about the opening of the conversation instead. Measured on a 120-turn history: the+ // kept window started at message 0 where the reference started mid-message-95, and+ // the answers moved -- one option's probability by 3.8x -- while `usage` stayed+ // byte-identical, so nothing a caller can read revealed it.+ boolean truncateLeft = state instanceof List;SequenceBuilder.Sequence sequence = SequenceBuilder.build(tokenizer, state, question,- config.maxLen(), config.headMaxLen(), null, false, stateIds);+ config.maxLen(), config.headMaxLen(), null, truncateLeft, stateIds);int defined = question.renderOptions().size();if (sequence.markers().length != defined) {// Markers sit at absolute positions and the sequence is then cut to `max_len`, sodiff --git a/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/AgentBatchingTest.java b/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/AgentBatchingTest.javaindex cc14599d..51ab744d 100644--- a/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/AgentBatchingTest.java+++ b/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/AgentBatchingTest.java@@ -165,15 +165,73 @@ final class AgentBatchingTest {}@Test- @DisplayName("no questions is refused, and no states is an empty result rather than a call")+ @DisplayName("no questions is an empty answer per state, and no states is an empty list")void degenerateInputs(@TempDir Path root) throws IOException {+ // This asserted that no questions is REFUSED. The reference returns empty answers and+ // zero usage with no tokenization and no forward pass -- so throwing meant the two+ // runtimes could not be swapped under a question set derived from a filter, where an+ // empty schema or a fully disabled rule set is a well-formed request.TinyCheckpoint.write(root, 128, 48);try (TinyCheckpoint.RecordingSession session = new TinyCheckpoint.RecordingSession();Agent agent = TinyCheckpoint.agent(root, session)) {- assertThrows(IllegalArgumentException.class,- () -> agent.predict("s", new LinkedHashMap<>()));+ Prediction one = agent.predict("s", new LinkedHashMap<>());+ assertTrue(one.answers().isEmpty(), "no questions, no answers");+ assertEquals(0, one.usage().inputTokens());+ assertEquals(0, one.usage().outputTokens());++ List<Prediction> many = agent.predictBatch(List.of("a", "b"), new LinkedHashMap<>());+ assertEquals(2, many.size(), "one empty result per state");+ for (Prediction prediction : many) {+ assertTrue(prediction.answers().isEmpty());+ assertEquals(0, prediction.usage().inputTokens());+ }+assertTrue(agent.predictBatch(List.of(), twoQuestions()).isEmpty());- assertTrue(session.batches.isEmpty(), "an empty batch must not reach the graph");+ assertTrue(agent.predictBatch(List.of(), new LinkedHashMap<>()).isEmpty());+ assertTrue(session.batches.isEmpty(),+ "no questions means no tokenization and no forward pass");+ }+ }++ @Test+ @DisplayName("a LIST state truncates from the left, so the newest turns survive")+ void listStateTruncatesLeft(@TempDir Path root) throws IOException {+ // The reference's rule is `truncate_left = isinstance(state, list)`, and a conversation is+ // serialised newest-last. Cutting from the right threw away the current turn and answered+ // about the opening of the conversation -- measured on a 120-turn history as one option's+ // probability moving by 3.8x, while `usage` stayed byte-identical so nothing a caller+ // could read revealed it.+ TinyCheckpoint.write(root, 96, 32);+ try (TinyCheckpoint.RecordingSession session = new TinyCheckpoint.RecordingSession();+ Agent agent = TinyCheckpoint.agent(root, session)) {+ List<Object> history = new java.util.ArrayList<>();+ for (int turn = 0; turn < 40; turn++) {+ Map<String, Object> message = new LinkedHashMap<>();+ message.put("role", turn % 2 == 0 ? "user" : "agent");+ message.put("text", "turn " + turn);+ history.add(message);+ }+ Map<String, Question> one = new LinkedHashMap<>();+ one.put("urgent", Question.noul("Is this urgent?"));++ Prediction listed = agent.predict(history, one);+ assertTrue(listed.usage().stateTokensDropped() > 0,+ "the fixture must actually truncate, or this proves nothing");++ // The surviving window is the TAIL. The tiny checkpoint is byte-level, so the kept+ // ids decode back to text: the last turn must be present and the first must not.+ long[] kept = session.batches.get(0).inputIds()[0];+ StringBuilder decoded = new StringBuilder();+ for (long id : kept) {+ if (id < 243) {+ decoded.append((char) id);+ }+ }+ String window = decoded.toString();+ assertTrue(window.contains("turn 39"),+ "the newest turn must survive, got: " + window);+ assertFalse(window.contains("turn 0\""),+ "the oldest turn must be the one dropped, got: " + window);}}@@ -252,15 +310,28 @@ final class AgentBatchingTest {}@Test- @DisplayName("a null state is answered, as Python answers it")- void nullStateIsAnswered(@TempDir Path root) throws IOException {- // Python's serialize_state hands None to json.dumps, which writes "null", and the model is- // asked about that. `List.of` used to reject it with a bare NullPointerException.+ @DisplayName("a null state is REFUSED, as Python refuses it")+ void nullStateIsRefused(@TempDir Path root) throws IOException {+ // This test asserted the opposite, with a comment claiming Python answers a null state+ // because serialize_state writes "null". Both Python backends raise+ // `TypeError("state must not be None; pass a string, dict, or list")` -- and the+ // reference's own comment gives that very serialisation as the REASON: answering it would+ // be a confident decision about the four characters "null", byte-identical to passing the+ // string. A caller whose state field is absent must hear about it.TinyCheckpoint.write(root, 128, 48);try (TinyCheckpoint.RecordingSession session = new TinyCheckpoint.RecordingSession();Agent agent = TinyCheckpoint.agent(root, session)) {- Prediction got = agent.predict(null, twoQuestions());- assertEquals(4, got.usage().stateTokens(), "the four characters of \"null\"");+ assertThrows(IllegalArgumentException.class,+ () -> agent.predict(null, twoQuestions()));+ assertThrows(IllegalArgumentException.class,+ () -> agent.predictBatch(java.util.Collections.singletonList(null),+ twoQuestions()));+ // and one null among several is refused by index, not silently answered+ IllegalArgumentException failure = assertThrows(IllegalArgumentException.class,+ () -> agent.predictBatch(java.util.Arrays.asList("a", null),+ twoQuestions()));+ assertTrue(failure.getMessage().contains("index 1"), failure.getMessage());+ assertTrue(session.batches.isEmpty(), "nothing may reach the graph");}}