Modern Apps git

← Modern-Apps.git

openassistant: JSON extraction skips an invalid object for a later valid one

Commit
bc102027c62cd6cdf67ee2d035eb9fba92bbd919
Author
Vayun Mathur <[email protected]>
When
2026-12-01 14:40 UTC (4 days ago)
Parents
7c3c97a
largestObject anchored on the first '{'; when that balanced
candidate failed validation it kept scanning from the same
start, so every later candidate included the invalid prefix
and failed too: noise {"bad":} more {"good":1} returned
null. This runs on every streaming callback and the final
reply in intent inference, so a valid object never triggered
the early-halt send and the turn ended as a schema mismatch.
(The existing test for this case was permissive enough to
mask it; left untouched.)

Each brace now tries as its own candidate start; the first
syntactically valid balanced object wins, string/escape
matching and unclosed behaviour unchanged. Four regression
tests (2 failed pre-fix). Baseline bumped for the new test
lines per the ratchet rule. Verified:
:openassistant:testDevUnitTest 206 tests and lint BUILD
SUCCESSFUL.

Diff

openassistant/src/main/java/com/vayunmathur/openassistant/domain/JsonSchemaValidator.kt
diff --git a/openassistant/src/main/java/com/vayunmathur/openassistant/domain/JsonSchemaValidator.kt b/openassistant/src/main/java/com/vayunmathur/openassistant/domain/JsonSchemaValidator.ktindex 858c4797f0..e3bfab98b1 100644--- a/openassistant/src/main/java/com/vayunmathur/openassistant/domain/JsonSchemaValidator.kt+++ b/openassistant/src/main/java/com/vayunmathur/openassistant/domain/JsonSchemaValidator.kt@@ -221,9 +221,22 @@ object JsonSchemaValidator { /** Extracts the largest balanced `{...}` JSON object from LLM streaming output. */ internal object JsonExtractor {     fun largestObject(text: String): String? {-        val start = text.indexOf('{')-        if (start == -1) return null+        var start = text.indexOf('{')+        while (start >= 0) {+            balancedObjectAt(text, start)?.let { return it }+            start = text.indexOf('{', start + 1)+        }+        return null+    } +    /**+     * The balanced object starting at [start] when it is valid JSON, or null.+     *+     * A balanced but invalid candidate (e.g. `{"bad":}`) must not anchor the+     * search: the caller retries from the next `{`, so a valid object later+     * in the same streamed reply is still found instead of being dropped.+     */+    private fun balancedObjectAt(text: String, start: Int): String? {         var depth = 0         var inString = false         var escaped = false@@ -244,7 +257,7 @@ internal object JsonExtractor {                     depth--                     if (depth == 0) {                         val candidate = text.substring(start, i + 1)-                        if (isValidJson(candidate)) return candidate+                        return candidate.takeIf { isValidJson(it) }                     }                 }             }
openassistant/src/test/java/com/vayunmathur/openassistant/util/JsonExtractorSkipInvalidRegressionTest.kt
diff --git a/openassistant/src/test/java/com/vayunmathur/openassistant/util/JsonExtractorSkipInvalidRegressionTest.kt b/openassistant/src/test/java/com/vayunmathur/openassistant/util/JsonExtractorSkipInvalidRegressionTest.ktnew file mode 100644index 0000000000..bc97772294--- /dev/null+++ b/openassistant/src/test/java/com/vayunmathur/openassistant/util/JsonExtractorSkipInvalidRegressionTest.kt@@ -0,0 +1,43 @@+package com.vayunmathur.openassistant.util++import com.vayunmathur.openassistant.domain.JsonExtractor+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNull++/**+ * Regression: a balanced-but-invalid first brace group must not poison+ * extraction of a valid object later in the same streamed reply.+ * Pre-fix, `largestObject` anchored on the first `{` forever, so the+ * mixed input below returned null and intent inference reported a+ * schema mismatch despite the model having produced valid JSON.+ */[email protected](org.robolectric.RobolectricTestRunner::class)+class JsonExtractorSkipInvalidRegressionTest {++    @Test fun invalidFirstObjectDoesNotDropLaterValidObject() {+        assertEquals(+            """{"good":1}""",+            JsonExtractor.largestObject("""noise {"bad":} more {"good":1}"""),+        )+    }++    @Test fun invalidNestedObjectDoesNotDropLaterValidObject() {+        assertEquals(+            """{"good":1}""",+            JsonExtractor.largestObject("""{"outer":{"bad":}} {"good":1}"""),+        )+    }++    @Test fun onlyInvalidObjectsStillYieldNull() {+        assertNull(JsonExtractor.largestObject("""{"a":}"""))+        assertNull(JsonExtractor.largestObject("""{"a":} {"b":}"""))+    }++    @Test fun firstValidObjectStillWins() {+        assertEquals(+            """{"a":1}""",+            JsonExtractor.largestObject("""prefix {"a":1} {"b":2}"""),+        )+    }+}