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): negative max_chars, and an extra that shadows a parameter
Adeshina Lasisid732a78d4e4b
diff --git a/laya-java/fixtures/email.json b/laya-java/fixtures/email.jsonindex c2cd66de..06bb3ef5 100644--- a/laya-java/fixtures/email.json+++ b/laya-java/fixtures/email.json@@ -507,6 +507,30 @@"max_chars": 0,"result": ""},+ {+ "name": "budget-negative-small",+ "body": "I cannot log in and need a password reset.",+ "max_chars": -5,+ "result": "I cannot log in a"+ },+ {+ "name": "budget-negative-one",+ "body": "I cannot log in and need a password reset.",+ "max_chars": -1,+ "result": "I cannot log in and need a password r"+ },+ {+ "name": "budget-negative-astral",+ "body": "𝐀𝐀𝐀𝐀𝐀𝐀𝐀𝐀𝐀𝐀𝐀𝐀𝐀𝐀𝐀𝐀𝐀𝐀𝐀𝐀",+ "max_chars": -2,+ "result": "𝐀𝐀𝐀𝐀𝐀𝐀𝐀𝐀𝐀𝐀"+ },+ {+ "name": "budget-negative-empties",+ "body": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",+ "max_chars": -12,+ "result": ""+ },{"name": "budget-splits-a-surrogate-pair","body": "ab𝐀cd𝐀ef",diff --git a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/LayaEmail.java b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/LayaEmail.javaindex 03243417..e7957242 100644--- a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/LayaEmail.java+++ b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/LayaEmail.java@@ -680,13 +680,11 @@ public final class LayaEmail {// In CODE POINTS, because Python slices a str by code point. Cutting by char index splits// a surrogate pair, and the half character that comes out is not what the reference// returns -- 4,641 of 20,000 astral bodies in a one-off differential sweep.- if (maxChars > 0) {- text = cutToCodePoints(text, (long) maxChars * 4);- } else {- // Python slices with max_chars * 4, which is <= 0 here: `text[:0]` is empty, and so is- // the result. Kept explicit because a negative length is an exception in Java.- text = "";- }+ // Unconditional, because the helper is a no-op when the bound exceeds the length and+ // because a NEGATIVE bound has a meaning of its own: Python drops the last |n| code+ // points. The `len(text) > max_chars * 4` guard the reference writes is redundant once+ // the slice is faithful -- it is always true for a negative bound anyway.+ text = cutToCodePoints(text, (long) maxChars * 4);String[] source = text.split("\n", -1);List<String> lines = new ArrayList<>(source.length);for (int i = 0; i < source.length; i++) {@@ -754,12 +752,21 @@ public final class LayaEmail {out.append(cleaned);}String result = Patterns.SPACES_AND_TABS.matcher(out.toString()).replaceAll(" ");- return cutToCodePoints(result, Math.max(maxChars, 0));+ return cutToCodePoints(result, maxChars);}/**- * The first {@code limit} CODE POINTS of {@code text}, which is what Python's {@code [:n]}- * takes.+ * Python's {@code text[:n]} in code points, for any {@code n} -- including a negative one,+ * which drops the LAST {@code |n|} code points rather than returning nothing.+ *+ * <p>That sign is not a curiosity. {@code clean_email_body} slices twice, once at+ * {@code max_chars * 4} and once at {@code max_chars}, and a negative budget runs through+ * both: the reference answers {@code "I cannot log in a"} for a budget of -5 where this+ * returned the empty string. 17 of 171 body-and-budget combinations differed, every one of+ * them negative, and the test that was here asserted the empty string as correct.+ *+ * <p>The sibling port has the same semantics for free -- JavaScript's {@code slice} reads a+ * negative end the way Python does -- which is why only this port needed the helper.** <p>Never cuts between a high and a low surrogate: a half pair is not a character the* reference can return. Cutting by char index returned one, and a one-off differential sweep@@ -769,17 +776,18 @@ public final class LayaEmail {* {@code fixtures/email.json}, each of which fails if this reverts to a char index.*/private static String cutToCodePoints(String text, long limit) {- if (limit <= 0) {- return "";- }- if (text.length() <= limit) {+ if (limit >= 0 && text.length() <= limit) {return text; // cannot hold more code points than chars}- int capped = (int) Math.min(limit, text.length());- if (text.codePointCount(0, text.length()) <= capped) {+ int codePoints = text.codePointCount(0, text.length());+ long end = limit < 0 ? codePoints + limit : Math.min(limit, codePoints);+ if (end <= 0) {+ return "";+ }+ if (end >= codePoints) {return text;}- return text.substring(0, text.offsetByCodePoints(0, capped));+ return text.substring(0, text.offsetByCodePoints(0, (int) end));}private static boolean matchesAny(List<Pattern> patterns, String line) {@@ -843,15 +851,18 @@ public final class LayaEmail {* drops it.** @return a mutable, insertion-ordered map: {@code subject}, {@code body}, then- * {@code from} if a sender was given, then the extras. An extra named- * {@code subject}, {@code body} or {@code from} REPLACES that value and keeps the- * original position, as Python's {@code dict.update} does; a null key is permitted,- * again as the reference permits it. Both are input mutations rather than errors,- * which is the reference's choice and is why neither is validated.+ * {@code from} if a sender was given, then the extras. An extra named {@code from}+ * replaces that value and keeps its position, as Python's {@code dict.update} does,+ * and a null key is permitted, again as the reference permits it -- both are input+ * mutations rather than errors, which is the reference's choice. An extra naming a+ * PARAMETER of this method is refused; see {@link #refuseReservedKeys}.+ * @throws IllegalArgumentException if {@code extra} names {@code subject}, {@code body},+ * {@code sender}, {@code clean} or {@code max_chars}*/public static Map<String, Object> emailState(String subject, String body, String sender,boolean clean, int maxChars,Map<String, Object> extra) {+ refuseReservedKeys(extra);Map<String, Object> state = new LinkedHashMap<>();state.put("subject", UnicodeTables.strip(subject == null ? "" : subject));state.put("body", clean ? cleanEmailBody(body, maxChars) : (body == null ? "" : body));@@ -870,6 +881,35 @@ public final class LayaEmail {return state;}+ /**+ * Refuse an {@code extra} that names one of this method's own parameters.+ *+ * <p>In the reference these cannot be state fields at all: {@code subject}, {@code body} and+ * {@code max_chars} raise a {@code TypeError}, and {@code sender} and {@code clean} bind the+ * parameter instead. A Java {@code Map} has no such protection, so every one of them was+ * being {@code put} into the state -- and {@code body} is the one that bites, because the+ * state is model input and the value lands there UNCLEANED: no quoted history removed, no+ * signature cut, no budget applied. A caller translating {@code email_state(**payload)} got+ * silence where the reference gives an error.+ *+ * <p>Refusing all five rather than emulating the two that bind: a Java caller has explicit+ * parameters for those, so an entry naming one is ambiguous rather than meaningful, and+ * erroring is the direction that cannot ship the wrong thing to a model.+ */+ private static void refuseReservedKeys(Map<String, Object> extra) {+ if (extra == null) {+ return;+ }+ for (String reserved : List.of("subject", "body", "sender", "clean", "max_chars")) {+ if (extra.containsKey(reserved)) {+ throw new IllegalArgumentException(+ "extra may not contain \"" + reserved + "\": it names a parameter of "+ + "emailState, and the reference cannot put it in a state either. Pass it "+ + "as the argument instead.");+ }+ }+ }+/*** The email question set, re-exported because {@code from laya.email import email_questions}* is a path callers already have. One definition, in {@link Presets}; this is the alias.diff --git a/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/EmailTest.java b/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/EmailTest.javaindex e7d48583..30234a66 100644--- a/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/EmailTest.java+++ b/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/EmailTest.java@@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse;import static org.junit.jupiter.api.Assertions.assertNotEquals;import static org.junit.jupiter.api.Assertions.assertNotSame;import static org.junit.jupiter.api.Assertions.assertSame;+import static org.junit.jupiter.api.Assertions.assertThrows;import static org.junit.jupiter.api.Assertions.assertTrue;import static org.junit.jupiter.api.DynamicTest.dynamicTest;@@ -65,7 +66,10 @@ final class EmailTest {private static final List<String> REQUIRED_BUDGETS = List.of("budget-four-x-bound", "budget-splits-a-surrogate-pair","budget-ends-on-a-surrogate-pair", "budget-all-astral",- "budget-four-x-bound-astral");+ "budget-four-x-bound-astral",+ // a negative budget is not a zero budget+ "budget-negative-small", "budget-negative-one", "budget-negative-astral",+ "budget-negative-empties");@SuppressWarnings("unchecked")private static List<Object> rows(String key) {@@ -95,7 +99,7 @@ final class EmailTest {() -> "the corpus lost the budget case " + required);}assertEquals(95, cleaned.size(), "cases were added or removed; update the count knowingly");- assertEquals(9, budgets.size(), "budget cases were added or removed");+ assertEquals(13, budgets.size(), "budget cases were added or removed");assertEquals(7, names("states").size(), "state cases were added or removed");}@@ -121,7 +125,7 @@ final class EmailTest {@DisplayName("every budget cuts where the reference cuts")List<DynamicTest> budgetsMatch() {List<Object> cases = rows("budgets");- assertEquals(9, cases.size(), "the budget cases changed size");+ assertEquals(13, cases.size(), "the budget cases changed size");List<DynamicTest> tests = new ArrayList<>();for (Object entry : cases) {Map<String, Object> row = asMap(entry);@@ -447,13 +451,54 @@ final class EmailTest {}@Test- @DisplayName("a non-positive budget returns nothing rather than throwing")- void nonPositiveBudget() {- // Python slices with max_chars * 4 and then max_chars, and a slice to a non-positive- // bound is empty. In Java a negative length is an exception, so the case is explicit.- assertEquals("", LayaEmail.cleanEmailBody("a message", 0));- assertEquals("", LayaEmail.cleanEmailBody("a message", -5));+ @DisplayName("a negative budget drops the tail, as Python's slice does")+ void negativeBudget() {+ // This test used to assert the empty string for -5, which is what the port returned and+ // is NOT what the reference returns. Python slices twice -- at `max_chars * 4` and at+ // `max_chars` -- and a negative index drops the LAST |n| code points.+ //+ // Both slices apply, which is why a short body still empties: "a message" is 9 code+ // points, `[: -12]` already takes everything. A body only survives a budget of -n if it+ // is longer than 5n code points. Every expectation below was read off the reference, not+ // reasoned about -- the first version of this test guessed "a mess" and was wrong.+ assertEquals("", LayaEmail.cleanEmailBody("a message", 0), "zero really is empty");assertEquals("a", LayaEmail.cleanEmailBody("a message", 1));+ assertEquals("", LayaEmail.cleanEmailBody("a message", -3),+ "9 code points less 12 is nothing, before the second slice is reached");+ assertEquals("", LayaEmail.cleanEmailBody("a message", -99));+ assertEquals("I cannot log in a", LayaEmail.cleanEmailBody(+ "I cannot log in and need a password reset.", -5),+ "41 code points: the first slice keeps 21, the second drops 5 of those");+ // code points, not chars, on the negative side too+ assertEquals("\uD835\uDC00".repeat(10), LayaEmail.cleanEmailBody(+ "\uD835\uDC00".repeat(20), -2),+ "20 astral characters: the first slice keeps 12, the second drops 2");+ assertEquals("", LayaEmail.cleanEmailBody("\uD835\uDC00".repeat(3), -1),+ "and a short astral body empties the same way a short ASCII one does");+ }++ @Test+ @DisplayName("an extra may not name a parameter of emailState")+ void extraMayNotShadowAParameter() {+ // `body` is the one that bites: the state is model input, so a silently accepted `body`+ // extra ships an UNCLEANED body -- no quoted history removed, no signature cut, no+ // budget. The reference cannot do this at all; `email_state(**{"body": x})` raises.+ for (String reserved : List.of("subject", "body", "sender", "clean", "max_chars")) {+ Map<String, Object> extra = new LinkedHashMap<>();+ extra.put(reserved, "shadowed");+ IllegalArgumentException refused = assertThrows(IllegalArgumentException.class,+ () -> LayaEmail.emailState("S", "b", null, true, 3000, extra),+ () -> "an extra named " + reserved + " was accepted into the state");+ assertTrue(refused.getMessage().contains(reserved),+ () -> "the refusal does not name the key: " + refused.getMessage());+ }+ // `from` is NOT reserved -- the reference lets it through, and it replaces the sender in+ // place rather than being appended.+ Map<String, Object> from = new LinkedHashMap<>();+ from.put("from", "[email protected]");+ Map<String, Object> state = LayaEmail.emailState("S", "b", "[email protected]", true, 3000, from);+ assertEquals(List.of("subject", "body", "from"), new ArrayList<>(state.keySet()));+ assertEquals("[email protected]", state.get("from"));}@Testdiff --git a/laya-java/scripts/gen_fixtures.py b/laya-java/scripts/gen_fixtures.pyindex 54a03c2d..5bdc15e0 100644--- a/laya-java/scripts/gen_fixtures.py+++ b/laya-java/scripts/gen_fixtures.py@@ -1963,6 +1963,15 @@ def email_clean():("budget-four-x-bound", ("word " * 400) + "This email is confidential and intended solely ""for the addressee.", 100),("budget-zero", q, 0),+ # A NEGATIVE budget is not a zero budget. Python slices twice, at `max_chars * 4` and at+ # `max_chars`, and a negative index drops the LAST |n| code points rather than everything+ # -- so the reference answers with a tail-trimmed body where this port used to answer+ # with nothing. Every one of the 17 differing body-and-budget combinations a review found+ # was negative, and the Java test asserted the empty string as correct.+ ("budget-negative-small", q, -5),+ ("budget-negative-one", q, -1),+ ("budget-negative-astral", "\U0001D400" * 20, -2),+ ("budget-negative-empties", "x" * 60, -12),# A budget that lands between a high and a low surrogate. Python slices by code point and# returns the whole character; a char-index cut returns half of one, which is not a# character the reference can produce.