sdk-openassistant: align timeout with service budget, explicit expiry
- Commit
d815cb9bddc30258bde3845d252ec996de1a38d3- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-06 04:36 UTC (5 hours ago)
- Parents
d5950b9
The SDK waited 60 s while the OpenAssistant intent service cut inference at 45 s, and both sat far below real on-device time for a vision encode plus up to 1,024 greedy tokens (the service's own chat budget is 300 s). Healthy extractions were killed mid-run and, when the service never answered at all, the caller got a raw coroutine timeout it could not phrase. Default per-call budget is now DEFAULT_TIMEOUT_MS = 330 s: above the service's 300 s intent budget so its timeout error normally arrives first, with this as the last-resort guard for a service that never answers (process killed, request dropped). Expiry now throws an AssistantException saying OpenAssistant did not respond in time, instead of TimeoutCancellationException. Tests: default-budget consistency (SDK >= 300 s service budget), and never-responding services ending in the explicit error for both text and image calls.
Diff
sdk/openassistant/AGENTS.md
diff --git a/sdk/openassistant/AGENTS.md b/sdk/openassistant/AGENTS.mdindex 1fc9abd846..5ccff63fdd 100644--- a/sdk/openassistant/AGENTS.md+++ b/sdk/openassistant/AGENTS.md@@ -16,7 +16,7 @@ **Gotchas:** Tool schemas are a compatibility surface on both ends — changes need assistant + provider updated together. ## Key files-- com/vayunmathur/sdk/openassistant/OpenAssistant.kt — `generate`/`generateJson` text, `generateJsonWithImages(prompt, schema, imagePaths)` for vision structured extraction (sends `image_paths` to InferenceService); the result receiver crosses as the plain framework `ResultReceiver` (subclass stripped by a parcel round-trip), never this module's `SecureResultReceiver`, which the OpenAssistant app does not ship+- com/vayunmathur/sdk/openassistant/OpenAssistant.kt — `generate`/`generateJson` text, `generateJsonWithImages(prompt, schema, imagePaths)` for vision structured extraction (sends `image_paths` to InferenceService; per-call default budget `DEFAULT_TIMEOUT_MS` = 330 s, above the service's 300 s intent budget, expiry as an explicit `AssistantException`); the result receiver crosses as the plain framework `ResultReceiver` (subclass stripped by a parcel round-trip), never this module's `SecureResultReceiver`, which the OpenAssistant app does not ship - com/vayunmathur/sdk/openassistant/Exceptions.kt ## Verify (this module only)
sdk/openassistant/src/main/java/com/vayunmathur/sdk/openassistant/OpenAssistant.kt
diff --git a/sdk/openassistant/src/main/java/com/vayunmathur/sdk/openassistant/OpenAssistant.kt b/sdk/openassistant/src/main/java/com/vayunmathur/sdk/openassistant/OpenAssistant.ktindex bcfae9641d..8987e3184a 100644--- a/sdk/openassistant/src/main/java/com/vayunmathur/sdk/openassistant/OpenAssistant.kt+++ b/sdk/openassistant/src/main/java/com/vayunmathur/sdk/openassistant/OpenAssistant.kt@@ -9,16 +9,31 @@ import android.os.Handler import android.os.Looper import android.os.Parcel import android.os.ResultReceiver+import kotlinx.coroutines.TimeoutCancellationException import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.withTimeout import kotlin.coroutines.resume import kotlin.coroutines.resumeWithException /** Client for requesting on-device inference from the OpenAssistant app. */-class OpenAssistant(private val context: Context, private val timeoutMs: Long = 60_000L) {+class OpenAssistant(private val context: Context, private val timeoutMs: Long = DEFAULT_TIMEOUT_MS) { /** OpenAssistant service identity and default response schema. */ companion object {+ /**+ * Default per-call budget. The OpenAssistant InferenceService allows+ * an intent (structured) inference up to 300 s — the same budget its+ * own chat turns get (STANDARD_INFERENCE_TIMEOUT_MILLIS there) —+ * because a vision encode plus up to 1,024 greedy tokens on device+ * routinely takes well over a minute. This default sits 30 s above+ * the service budget so the service's own timeout error normally+ * arrives first; this timeout is only the last resort for when the+ * service never answers at all (process killed mid-inference, a+ * request dropped before queueing), and it must still terminate the+ * call with an explicit error instead of silence.+ */+ const val DEFAULT_TIMEOUT_MS = 330_000L+ private const val OA_PACKAGE = "com.vayunmathur.openassistant" private const val OA_SERVICE = "$OA_PACKAGE.util.InferenceService" private const val DEFAULT_SCHEMA =@@ -27,16 +42,12 @@ class OpenAssistant(private val context: Context, private val timeoutMs: Long = suspend fun generate(prompt: String): String { if (!isAvailable()) throw AssistantNotInstalledException()- return withTimeout(timeoutMs) {- dispatchInference(prompt, schema = null)- }+ return awaitInference { dispatchInference(prompt, schema = null) } } suspend fun generateJson(prompt: String, schema: String): String { if (!isAvailable()) throw AssistantNotInstalledException()- return withTimeout(timeoutMs) {- dispatchInference(prompt, schema)- }+ return awaitInference { dispatchInference(prompt, schema) } } /**@@ -48,11 +59,26 @@ class OpenAssistant(private val context: Context, private val timeoutMs: Long = */ suspend fun generateJsonWithImages(prompt: String, schema: String, imagePaths: List<String>): String { if (!isAvailable()) throw AssistantNotInstalledException()- return withTimeout(timeoutMs) {- dispatchInference(prompt, schema, imagePaths)- }+ return awaitInference { dispatchInference(prompt, schema, imagePaths) } } + /**+ * Every call terminates: a result, the service's [AssistantException],+ * or — when the service never answers (killed mid-inference, request+ * dropped before queueing) — an [AssistantException] saying it did not+ * respond in time. Never a raw timeout the caller cannot phrase, and+ * never an indefinite wait.+ */+ private suspend fun awaitInference(block: suspend () -> String): String =+ try {+ withTimeout(timeoutMs) { block() }+ } catch (e: TimeoutCancellationException) {+ throw AssistantException(+ "OpenAssistant did not respond within ${timeoutMs / 1_000} seconds. " ++ "Its model may still be loading or the OpenAssistant app may have been stopped.",+ )+ }+ fun isAvailable(): Boolean { return try { context.packageManager.getPackageInfo(OA_PACKAGE, 0)
sdk/openassistant/src/test/java/com/vayunmathur/sdk/openassistant/OpenAssistantTest.kt
diff --git a/sdk/openassistant/src/test/java/com/vayunmathur/sdk/openassistant/OpenAssistantTest.kt b/sdk/openassistant/src/test/java/com/vayunmathur/sdk/openassistant/OpenAssistantTest.ktindex 7e701151b2..73bc2ce8ca 100644--- a/sdk/openassistant/src/test/java/com/vayunmathur/sdk/openassistant/OpenAssistantTest.kt+++ b/sdk/openassistant/src/test/java/com/vayunmathur/sdk/openassistant/OpenAssistantTest.kt@@ -11,7 +11,6 @@ import android.os.ResultReceiver import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import kotlinx.coroutines.Dispatchers-import kotlinx.coroutines.TimeoutCancellationException import kotlinx.coroutines.async import kotlinx.coroutines.runBlocking import org.junit.runner.RunWith@@ -149,10 +148,33 @@ class OpenAssistantTest { @Test fun generateTimesOutWhenServiceNeverResponds() = runBlocking { installAssistant() val oa = OpenAssistant(app, timeoutMs = 300)- assertFailsWith<TimeoutCancellationException> { oa.generate("slow") }+ // Never silence: a service that never answers ends in an explicit+ // error the caller can show, not a raw coroutine timeout.+ val e = assertFailsWith<AssistantException> { oa.generate("slow") }+ assertTrue(e.message!!.contains("did not respond"), e.message) Unit } + @Test fun imageExtractionTimesOutWhenServiceNeverResponds() = runBlocking {+ installAssistant()+ val oa = OpenAssistant(app, timeoutMs = 300)+ val e = assertFailsWith<AssistantException> {+ oa.generateJsonWithImages("extract", "{}", listOf("/tmp/page-1.png"))+ }+ assertTrue(e.message!!.contains("did not respond"), e.message)+ Unit+ }++ @Test fun defaultTimeoutCoversServiceInferenceBudget() {+ // The InferenceService intent budget is 300 s (its chat turns get+ // the same); the SDK default must sit above it so the service's own+ // timeout error is what callers normally see, with this as backstop.+ assertTrue(+ OpenAssistant.DEFAULT_TIMEOUT_MS >= 300_000L,+ "SDK default ${OpenAssistant.DEFAULT_TIMEOUT_MS} must cover the 300s service budget",+ )+ }+ /** Context whose startForegroundService always throws [failure]. */ private fun throwingContext(failure: RuntimeException): Context = object : ContextWrapper(app) { override fun startForegroundService(service: Intent?): ComponentName? = throw failure